Skip to main content

Bedrock (knowledge bases) integration

还没翻译译自英文(docs.langchain.com)本页 29 段都还没译

This guide will help you get started with the AWS Knowledge Bases retriever.

Knowledge Bases for Amazon Bedrock is an Amazon Web Services (AWS) offering which lets you quickly build RAG applications by using your private data to customize FM response.

Implementing RAG requires organizations to perform several cumbersome steps to convert data into embeddings (vectors), store the embeddings in a specialized vector database, and build custom integrations into the database to search and retrieve text relevant to the user's query. This can be time-consuming and inefficient.

With Knowledge Bases for Amazon Bedrock, simply point to the location of your data in Amazon S3, and Knowledge Bases for Amazon Bedrock takes care of the entire ingestion workflow into your vector database. If you do not have an existing vector database, Amazon Bedrock creates an Amazon OpenSearch Serverless vector store for you. For retrievals, use the LangChain - Amazon Bedrock integration via the Retrieve API to retrieve relevant results for a user query from knowledge bases.

Amazon Bedrock now also offers Managed Knowledge Bases, which handle embedding, storage, and retrieval automatically—no external vector store needed. See the Managed Knowledge Base section below.

Integration details

Setup

Knowledge Bases can be configured through AWS Console or by using AWS SDKs. We will need the knowledge_base_id to instantiate the retriever.

If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:

os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"

Installation

This retriever lives in the langchain-aws package:

pip install -qU "langchain-aws>=1.6.3"

SDK requirement: Managed search and agentic retrieval require langchain-aws>=1.6.3, which installs boto3>=1.43.32.

Instantiation

Vector Knowledge Base

For traditional vector-based knowledge bases (with OpenSearch Serverless, Pinecone, etc.):

from langchain_aws.retrievers import AmazonKnowledgeBasesRetriever

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

Managed Knowledge Base

For Managed Knowledge Bases (recommended—no vector store needed):

from langchain_aws.retrievers import AmazonKnowledgeBasesRetriever

retriever = AmazonKnowledgeBasesRetriever(
    knowledge_base_id="YOUR_MANAGED_KB_ID",
    retrieval_config={"managedSearchConfiguration": {"numberOfResults": 4}},
)

Managed knowledge bases handle embedding, chunking, storage, and retrieval automatically. They also support managed reranking for improved result quality.

Agentic Retrieval

For complex queries that benefit from query decomposition and managed reranking, use the standalone agentic_retrieve helper:

from langchain_aws.retrievers import agentic_retrieve

result = agentic_retrieve(
    knowledge_base_id="YOUR_MANAGED_KB_ID",
    query="What are the differences between S3 storage classes?",
    region_name="us-west-2",
)

for doc in result["results"]:
    print(doc["content"]["text"])

Agentic retrieval uses AgenticRetrieveStream which performs intelligent query decomposition and managed reranking. It requires langchain-aws>=1.6.3 and only works with managed knowledge bases.

Usage

query = "What did the president say about Ketanji Brown?"

retriever.invoke(query)

Use within a chain

from langchain_aws import ChatBedrock
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

llm = ChatBedrock(model_id="anthropic.claude-sonnet-4-20250514-v1:0")

prompt = ChatPromptTemplate.from_template(
    "Answer the question based on the context:\n\nContext: {context}\n\nQuestion: {question}"
)

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

chain.invoke("What are the key features?")

Required IAM Permissions

{
  "Effect": "Allow",
  "Action": [
    "bedrock:Retrieve",
    "bedrock:AgenticRetrieveStream"
  ],
  "Resource": "arn:aws:bedrock:<region>:<account-id>:knowledge-base/<kb-id>"
}

Resources


API reference

For detailed documentation of all AmazonKnowledgeBasesRetriever features and configurations head to the API reference.