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.
Note
All of the following configurations, except for Orchestration and generation, are only applicable to unstructured data sources.
To learn more about these configurations in the console or the API, select from the following topics.
Topics
Retrieve from an Amazon Kendra GenAI index
You can query a knowledge base that uses an Amazon Kendra GenAI index, and return only relevant text from data sources. For this query, send a Retrieve request with an Agents for Amazon Bedrock runtime endpoint, like with a standard knowledge base.
The structure of a response returned from a knowledge base with an Amazon Kendra GenAI index is the same as a standard KnowledgeBaseRetrievalResult. However, the response also includes a few additional fields from Amazon Kendra.
The following table describes the fields from Amazon Kendra that you might see in a returned response. Amazon Bedrock gets these fields from the Amazon Kendra response. If that response doesn't contain these fields, then the returned query result from Amazon Bedrock won't have these fields either.
Field | Description |
---|---|
x-amz-kendra-document-title |
The title of the returned document. |
x-amz-kendra-score-confidence |
A relative ranking of how relevant the response is to the query. Possible values are VERY_HIGH, HIGH, MEDIUM, LOW, and NOT_AVAILABLE. |
x-amz-kendra-passage-id |
The ID of the returned passage. |
x-amz-kendra-document-id |
The ID of the returned document. |
DocumentAttributes |
Document attributes or metadata fields from Amazon Kendra. The returned query result from the knowledge base stores these as metadata key-value pairs. You can filter the results with metadata filtering from Amazon Bedrock. For more information, see DocumentAttribute. |
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, choose the tab for your preferred method, and then 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, choose the tab for your preferred method, and then 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, choose the tab for your preferred method, and then 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 and can specify which fields to include in the embeddings. 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 fields of your documents. If you're 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. See Document metadata fields in Connection configuration for more information about configuring the metadata file. -
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.
If you have PDF documents in your data source and use Amazon OpenSearch Serverless for your vector store: Amazon Bedrock knowledge bases will generate document page numbers and store them in a metadata field/attribute called x-amz-bedrock-kb-document-page-number. Note that page numbers stored in a metadata field is not supported if you choose no chunking for your documents.
You can use the following filtering operators to filter results when you query:
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 (currently best supported with Amazon OpenSearch Serverless vector stores) |
Not in | !: | notIn | string list | Attribute isn’t in the list you provide (currently best supported with Amazon OpenSearch Serverless vector stores) |
Starts with | ^ | startsWith | string | Attribute starts with the string you provide (currently best supported with Amazon OpenSearch Serverless vector stores) |
To combine filtering operators, you can use the following logical operators:
To learn how to filter results using metadata, choose the tab for your preferred method, and then follow the steps:
Amazon Bedrock Knowledge Base generates and applies a retrieval filter based on the user query and a metadata schema.
Note
The feature currently only works with Anthropic Claude 3.5 Sonnet.
The implicitFilterConfiguration
is specified in the vectorSearchConfiguration
of the Retrieve request body. Include the following fields:
-
metadataAttributes
– In this array, provide schemas describing metadata attributes that the model will generate a filter for. -
modelArn
– The ARN of the model to use.
The following shows an example of metadata schemas that you can add to the array in metadataAttributes
.
[ { "key": "company", "type": "STRING", "description": "The full name of the company. E.g. `Amazon.com, Inc.`, `Alphabet Inc.`, etc" }, { "key": "ticker", "type": "STRING", "description": "The ticker name of a company in the stock market, e.g. AMZN, AAPL" }, { "key": "pe_ratio", "type": "NUMBER", "description": "The price to earning ratio of the company. This is a measure of valuation of a company. The lower the pe ratio, the company stock is considered chearper." }, { "key": "is_us_company", "type": "BOOLEAN", "description": "Indicates whether the company is a US company." }, { "key": "tags", "type": "STRING_LIST", "description": "Tags of the company, indicating its main business. E.g. `E-commerce`, `Search engine`, `Artificial intelligence`, `Cloud computing`, etc" } ]
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 generation prompt that's sent to the model for response generation. You can also customize the orchestration prompt, which turns the user's prompt into a search query. You can engineer the prompt templates 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 Prompt template Replaced by Model Required? $query$ Orchestration, generation 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$ Generation The retrieved results for the user query. All Yes $output_format_instructions$ Orchestration 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$ Orchestration, generation 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 Prompt engineering concepts.
Choose the tab for your preferred method, and then 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 models 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 Prompt engineering concepts.
Choose the tab for your preferred method, and then follow the steps: