Query a knowledge base and retrieve data
Important
Guardrails are applied only to the input and the generated response from the LLM. They are not applied to the references retrieved from Knowledge Bases at runtime.
After your knowledge base is set up, you can query it and retrieve chunks from your source data that is relevant to the query by using the Retrieve API operation. You can also use a reranking model instead of the default Amazon Bedrock Knowledge Bases ranker to rank source chunks for relevance during retrieval.
Managed knowledge base considerations
When querying a managed knowledge base, the following considerations apply:
-
Use
managedSearchConfigurationin theretrievalConfigurationinstead ofvectorSearchConfiguration. ThevectorSearchConfigurationfield is used for custom knowledge bases only. -
Retrieval always uses hybrid search, which combines keyword and semantic search. Semantic-only search is not available for fully managed knowledge bases.
-
Reranking is enabled by default using a service-managed reranking model. You can control reranking behavior with the
rerankingModelTypefield inmanagedSearchConfiguration:-
MANAGED(default) – A service-managed reranking model is used automatically. No configuration required. -
CUSTOM– Use your own Bedrock reranking model. You must provide arerankingConfigurationwith the model ARN. -
NONE– Reranking is disabled.
-
-
Managed reranking is available only for knowledge bases that use managed embedding. If your knowledge base uses a custom embedding model, you can use custom reranking or disable reranking.
-
The
startsWithandstringContainsmetadata filters are not supported for managed knowledge bases. For details, see Manual metadata filtering.
The following example shows a minimal Retrieve request for a fully
managed knowledge base with managed reranking (default):
{ "retrievalQuery": { "text": "your query text" } }
To disable reranking:
{ "retrievalQuery": { "text": "your query text" }, "retrievalConfiguration": { "managedSearchConfiguration": { "rerankingModelType": "NONE" } } }
To use a custom Bedrock reranking model:
{ "retrievalQuery": { "text": "your query text" }, "retrievalConfiguration": { "managedSearchConfiguration": { "rerankingModelType": "CUSTOM", "rerankingConfiguration": { "type": "BEDROCK_RERANKING_MODEL", "bedrockRerankingConfiguration": { "numberOfRerankedResults": 5, "modelConfiguration": { "modelArn": "arn:aws:bedrock:us-west-2::foundation-model/cohere.rerank-v3-5:0" } } } } } }
To learn how to query your knowledge base, choose the tab for your preferred method, and then follow the steps:
Note
If you receive an error that the prompt exceeds the character limit while generating responses, you can shorten the prompt in the following ways:
-
Reduce the maximum number of retrieved results (this shortens what is filled in for the $search_results$ placeholder in the Knowledge base prompt templates: orchestration & generation).
-
Recreate the data source with a chunking strategy that uses smaller chunks (this shortens what is filled in for the $search_results$ placeholder in the Knowledge base prompt templates: orchestration & generation).
-
Shorten the prompt template.
-
Shorten the user query (this shortens what is filled in for the $query$ placeholder in the Knowledge base prompt templates: orchestration & generation).