> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Bedrock（知识库）集成

> 使用 LangChain Python 与 Bedrock（知识库）检索器进行集成。

本指南将帮助您开始使用 AWS 知识库 [检索器](/oss/python/langchain/retrieval)。

[Knowledge Bases for Amazon Bedrock](https://aws.amazon.com/bedrock/knowledge-bases/) 是亚马逊网络服务 (AWS) 的一项服务，它允许您利用私有数据定制 FM 响应，从而快速构建 RAG 应用程序。

实施 `RAG` 需要组织执行多个繁琐的步骤，将数据转换为嵌入（向量），将嵌入存储到专用的向量数据库中，并构建自定义集成以搜索和检索与用户查询相关的文本。这既耗时又低效。

使用 `Knowledge Bases for Amazon Bedrock`，只需指向您在 `Amazon S3` 中的数据位置，`Knowledge Bases for Amazon Bedrock` 就会处理整个数据摄入工作流到您的向量数据库。如果您没有现有的向量数据库，Amazon Bedrock 会为您创建一个 Amazon OpenSearch Serverless 向量存储。对于检索，请通过 Retrieve API 使用 LangChain - Amazon Bedrock 集成，从知识库中检索与用户查询相关的相关结果。

### 集成详情

<ItemTable category="document_retrievers" item="AmazonKnowledgeBasesRetriever" />

## 设置

知识库可以通过 [AWS 控制台](https://aws.amazon.com/console/) 配置，也可以使用 [AWS SDKs](https://aws.amazon.com/developer/tools/) 配置。我们需要 `knowledge_base_id` 来实例化检索器。

如果您希望从单个查询中获得自动追踪，也可以通过取消注释以下内容来设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

此检索器位于 `langchain-aws` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-aws
```

## 实例化

现在我们可以实例化我们的检索器：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws.retrievers import AmazonKnowledgeBasesRetriever

retriever = AmazonKnowledgeBasesRetriever(
    knowledge_base_id="PUIJP4EQUA",
    retrieval_config={"vectorSearchConfiguration": {"numberOfResults": 4}},
)
```

## 使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query = "What did the president say about Ketanji Brown?"

retriever.invoke(query)
```

## 在链中使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from botocore.client import Config
from langchain_classic.chains import RetrievalQA
from langchain_aws import Bedrock

model_kwargs_claude = {"temperature": 0, "top_k": 10, "max_tokens_to_sample": 3000}

llm = Bedrock(model_id="anthropic.claude-v2", model_kwargs=model_kwargs_claude)

qa = RetrievalQA.from_chain_type(
    llm=llm, retriever=retriever, return_source_documents=True
)

qa(query)
```

***

## API 参考

有关所有 `AmazonKnowledgeBasesRetriever` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/python/langchain-aws/retrievers/bedrock/AmazonKnowledgeBasesRetriever)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\retrievers\bedrock.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
