Configure and customize queries and response generation
You can configure and customize retrieval and response generation, further improving the relevancy of responses. For example, you can apply filters to document metadata fields/attributes to use the most recently updated documents or documents with recent modification times.
To learn more about these configurations in the console or the API, select from the following topics.
Topics
The search type defines how data sources in the knowledge base are queried. The following search types are possible:
-
Default – Amazon Bedrock decides the search strategy for you.
-
Hybrid – Combines searching vector embeddings (semantic search) with searching through the raw text. Hybrid search is currently only supported for Amazon OpenSearch Serverless vector stores that contain a filterable text field. If you use a different vector store or your Amazon OpenSearch Serverless vector store doesn't contain a filterable text field, the query uses semantic search.
-
Semantic – Only searches vector embeddings.
To learn how to define the search type, select the tab corresponding to your method of choice and follow the steps.
Query decomposition is a technique used to break down a complex queries into smaller, more manageable sub-queries. This approach can help in retrieving more accurate and relevant information, especially when the initial query is multifaceted or too broad. Enabling this option may result in multiple queries being executed against your Knowledge Base, which may aid in a more accurate final response.
For example, for a question like “Who scored higher in the 2022 FIFA World Cup, Argentina or France?”, Amazon Bedrock knowledge bases may first generate the following sub-queries, before generating a final answer:
-
How many goals did Argentina score in the 2022 FIFA World Cup final?
-
How many goals did France score in the 2022 FIFA World Cup final?
When generating responses based off retrieval of information, you can use inference parameters to gain more control over the model’s behavior during inference and influence the model’s outputs.
To learn how to modify the inference parameters, select the tab corresponding to your method of choice and follow the steps.
When you query a knowledge base, Amazon Bedrock returns up to five results in the response by default. Each result corresponds to a source chunk.
To modify the maximum number of results to return, select the tab corresponding to your method of choice and follow the steps.
You can apply filters to document fields/attributes to help you further improve the relevancy of responses. Your data sources can include document metadata attributes/fields to filter on. For example, document "epoch_modification_time" or the number of seconds that's passed January 1 1970 for when the document was last updated. You can filter on the most recent data, where "epoch_modification_time" is greater than a certain number. These most recent documents can be used for the query.
To use filters when querying a knowledge base, check that your knowledge base fulfills the following requirements:
-
When configuring your data source connector, most connectors crawl the main metadata feilds of your documents. If using an Amazon S3 bucket as your data source, the bucket must include at least one
fileName.extension.metadata.json
for the file or document it's associated with. -
If your knowledge base's vector index is in an Amazon OpenSearch Serverless vector store, check that the vector index is configured with the
faiss
engine. If the vector index is configured with thenmslib
engine, you'll have to do one of the following:-
Create a new knowledge base in the console and let Amazon Bedrock automatically create a vector index in Amazon OpenSearch Serverless for you.
-
Create another vector index in the vector store and select
faiss
as the Engine. Then Create a new knowledge base and specify the new vector index.
-
-
If you're adding metadata to an existing vector index in an Amazon Aurora database cluster, you must add a column to the table for each metadata attribute in your metadata files before starting ingestion. The metadata attribute values will be written to these columns.
You can use the following filtering operators to filter results when you query:
Filtering operators | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Operator | Console | API filter name | Supported attribute data types | Filtered results | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Equals | = | equals | string, number, boolean | Attribute matches the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Not equals | != | notEquals | string, number, boolean | Attribute doesn’t match the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Greater than | > | greaterThan | number | Attribute is greater than the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Greater than or equals | >= | greaterThanOrEquals | number | Attribute is greater than or equal to the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Less than | < | lessThan | number | Attribute is less than the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Less than or equals | <= | lessThanOrEquals | number | Attribute is less than or equal to the value you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In | : | in | string list | Attribute is in the list you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Not in | !: | notIn | string list | Attribute isn’t in the list you provide | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Starts with | ^ | startsWith | string | Attribute starts with the string you provide (only supported for Amazon OpenSearch Serverless vector stores) |
To combine filtering operators, you can use the following logical operators:
To learn how to filter results using metadata, select the tab corresponding to your method of choice and follow the steps.
When you query a knowledge base and request response generation, Amazon Bedrock uses a prompt template that combines instructions and context with the user query to construct the prompt that's sent to the model for response generation. You can engineer the prompt template with the following tools:
-
Prompt placeholders – Pre-defined variables in Amazon Bedrock Knowledge Bases that are dynamically filled in at runtime during knowledge base query. In the system prompt, you'll see these placeholders surrounded by the
$
symbol. The following list describes the placeholders you can use:Variable Replaced by Model Required? $query$ The user query sent to the knowledge base. Anthropic Claude Instant, Anthropic Claude v2.x Yes Anthropic Claude 3 Sonnet No (automatically included in model input) $search_results$ The retrieved results for the user query. All Yes $output_format_instructions$ Underlying instructions for formatting the response generation and citations. Differs by model. If you define your own formatting instructions, we suggest that you remove this placeholder. Without this placeholder, the response won't contain citations. All No $current_time$ The current time. All No -
XML tags – Anthropic models support the use of XML tags to structure and delineate your prompts. Use descriptive tag names for optimal results. For example, in the default system prompt, you'll see the
<database>
tag used to delineate a database of previously asked questions). For more information, see Use XML tagsin the Anthropic user guide .
For general prompt engineering guidelines, see Optimize prompts with prompt engineering.
Select the tab corresponding to your method of choice and follow the steps.
You can implement safeguards for your knowledge base for your use cases and responsible AI policies. You can create multiple guardrails tailored to different use cases and apply them across multiple request and response conditions, providing a consistent user experience and standardizing safety controls across your knowledge base. You can configure denied topics to disallow undesirable topics and content filters to block harmful content in model inputs and responses. For more information, see Stop harmful content in LLMs using Amazon Bedrock Guardrails.
Note
Using guardrails with contextual grounding for knowledge bases is currently not supported on Claude 3 Sonnet and Haiku.
For general prompt engineering guidelines, see Optimize prompts with prompt engineering.
Select the tab corresponding to your method of choice and follow the steps.