Configure and customize queries and response generation - Amazon Bedrock

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:

    Console

    Follow the console steps at Query a knowledge base and generate AI responses. When you open the Configurations pane, you'll see the following options for Search type:

    • Default – Amazon Bedrock decides which search strategy is best-suited for your vector store configuration.

    • Hybrid – Amazon Bedrock queries the knowledge base using both the vector embeddings and the raw text. This option is only available if you're using an Amazon OpenSearch Serverless vector store configured with a filterable text field.

    • Semantic – Amazon Bedrock queries the knowledge base using its vector embeddings.

    API

    When you make a Retrieve or RetrieveAndGenerate request, include a retrievalConfiguration field, mapped to a KnowledgeBaseRetrievalConfiguration object. To see the location of this field, refer to the Retrieve and RetrieveAndGenerate request bodies in the API reference.

    The following JSON object shows the minimal fields required in the KnowledgeBaseRetrievalConfiguration object to set search type configurations:

    "retrievalConfiguration": { "vectorSearchConfiguration": { "overrideSearchType": "HYBRID | SEMANTIC" } }

    Specify the search type in the overrideSearchType field. You have the following options:

    • If you don't specify a value, Amazon Bedrock decides which search strategy is best-suited for your vector store configuration.

    • HYBRID – Amazon Bedrock queries the knowledge base using both the vector embeddings and the raw text. This option is only available if you're using an Amazon OpenSearch Serverless vector store configured with a filterable text field.

    • SEMANTIC – Amazon Bedrock queries the knowledge base using its vector embeddings.

    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:

    1. How many goals did Argentina score in the 2022 FIFA World Cup final?

    2. How many goals did France score in the 2022 FIFA World Cup final?

    Console
    1. Create and sync a data source or use an existing knowledge base.

    2. Go to the test window and open the configuration panel.

    3. Enable query reformulation.

    API
    POST /retrieveAndGenerate HTTP/1.1 Content-type: application/json { "input": { "text": "string" }, "retrieveAndGenerateConfiguration": { "knowledgeBaseConfiguration": { "orchestrationConfiguration": { // Query decomposition "queryTransformationConfiguration": { "type": "string" // enum of QUERY_DECOMPOSITION } }, ...} }

    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:

    Console

    To modify inference parameters when querying a knowledge base – Follow the console steps at Query a knowledge base and generate AI responses. When you open the Configurations pane, you'll see an Inference parameters section. Modify the parameters as necessary.

    To modify inference parameters when chatting with your document – Follow the steps at Chat with your document without a knowledge base configured. In the Configurations pane, expand the Inference parameters section and modify the parameters as necessary.

    API

    You provide the model parameters in the call to the RetrieveAndGenerate API. You can customize the model by providing inference parameters in the inferenceConfig field of either the knowledgeBaseConfiguration (if you query a knowledge base) or the externalSourcesConfiguration (if you chat with your document).

    Within the inferenceConfig field is a textInferenceConfig field that contains the following parameters that you can:

    • temperature

    • topP

    • maxTokenCount

    • stopSequences

    You can customize the model by using the following parameters in the inferenceConfig field of both externalSourcesConfiguration and knowledgeBaseConfiguration:

    • temperature

    • topP

    • maxTokenCount

    • stopSequences

    For a detailed explanation of the function of each of these parameters, see Influence response generation with inference parameters.

    Additionally, you can provide custom parameters not supported by textInferenceConfig via the additionalModelRequestFields map. You can provide parameters unique to specific models with this argument, for the unique parameters see Inference request parameters and response fields for foundation models.

    If a parameter is omitted from textInferenceConfig, a default value will be used. Any parameters not recognized in textInferneceConfig will be ignored, while any parameters not recognized in AdditionalModelRequestFields will cause an exception.

    A validation exception is thrown if there is the same parameter in both additionalModelRequestFields and TextInferenceConfig.

    Using model parameters in RetrieveAndGenerate

    The following is an example of the structure for inferenceConfig and additionalModelRequestFields under the generationConfiguration in the RetrieveAndGenerate request body:

    "inferenceConfig": { "textInferenceConfig": { "temperature": 0.5, "topP": 0.5, "maxTokens": 2048, "stopSequences": ["\nObservation"] } }, "additionalModelRequestFields": { "top_k": 50 }

    The proceeding example sets a temperature of 0.5, top_p of 0.5, maxTokens of 2048, stops generation if it encounters the string "\nObservation" in the generated response, and passes a custom top_k value of 50.

    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:

    Console

    Follow the console steps at Query a knowledge base and generate AI responses. In the Configurations pane, expand the Maximum number of retrieved results.

    API

    When you make a Retrieve or RetrieveAndGenerate request, include a retrievalConfiguration field, mapped to a KnowledgeBaseRetrievalConfiguration object. To see the location of this field, refer to the Retrieve and RetrieveAndGenerate request bodies in the API reference.

    The following JSON object shows the minimal fields required in the KnowledgeBaseRetrievalConfiguration object to set the maximum number of results to return:

    "retrievalConfiguration": { "vectorSearchConfiguration": { "numberOfResults": number } }

    Specify the maximum number of retrieved results (see the numberOfResults field in KnowledgeBaseRetrievalConfiguration for the range of accepted values) to return in the numberOfResults field.

    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 fields 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. See Amazon S3 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 the nmslib engine, you'll have to do one of the following:

    • 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:

    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 (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:

    Logical operators
    Operator Console API filter field name Filtered results
    And and andAll Results fulfill all of the filtering expressions in the group
    Or or orAll Results fulfill at least one of the filtering expressions in the group

    To learn how to filter results using metadata, select the tab corresponding to your method of choice and follow the steps:

    Console

    Follow the console steps at Query a knowledge base and generate AI responses. When you open the Configurations pane, you'll see a Filters section. The following procedures describe different use cases:

    • To add a filter, create a filtering expression by entering a metadata attribute, filtering operator, and value in the box. Separate each part of the expression with a whitespace. Press Enter to add the filter.

      For a list of accepted filtering operators, see the Filtering operators table above. You can also see a list of filtering operators when you add a whitespace after the metadata attribute.

      Note

      You must surround strings with quotation marks.

      For example, you can filter for results from source documents that contain a genre metadata attribute whose value is "entertainment" by adding the following filter: genre = "entertainment".

      Add one filter.
    • To add another filter, enter another filtering expression in the box and press Enter. You can add up to 5 filters in the group.

      Add another filter.
    • By default, the query will return results that fulfill all the filtering expressions you provide. To return results that fulfill at least one of the filtering expressions, choose the and dropdown menu between any two filtering operations and select or.

      Change the logical operation between filters.
    • To combine different logical operators, select + Add Group to add a filter group. Enter filtering expressions in the new group. You can add up to 5 filter groups.

      Add a filter group to combine different logical operators.
    • To change the logical operator used between all the filtering groups, choose the AND dropdown menu between any two filter groups and select OR.

      Change the logical operation between filter groups.
    • To edit a filter, select it, modify the filtering operation, and choose Apply.

      Edit a filter.
    • To remove a filter group, choose the trash can icon ( Trapezoid-shaped diagram showing data flow from source to destination through AWS Transfer Family. ) next to the group. To remove a filter, choose the delete icon ( Close or cancel icon represented by an "X" symbol. ) next to the filter.

      Delete a filter or filter group.

    The following image shows an example filter configuration that returns all documents written after 2018 whose genre is "entertainment", in addition to documents whose genre is "cooking" or "sports" and whose author starts with "C".

    Example filter configuration.
    API

    When you make a Retrieve or RetrieveAndGenerate request, include a retrievalConfiguration field, mapped to a KnowledgeBaseRetrievalConfiguration object. To see the location of this field, refer to the Retrieve and RetrieveAndGenerate request bodies in the API reference.

    The following JSON objects show the minimal fields required in the KnowledgeBaseRetrievalConfiguration object to set filters for different use cases:

    1. Use one filtering operator (see the Filtering operators table above).

      "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] } } } }
    2. Use a logical operator (see the Logical operators table above) to combine up to 5.

      "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ] } } }
    3. Use a logical operator to combine up to 5 filtering operators into a filter group, and a second logical operator to combine that filter group with another filtering operator.

      "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { "andAll | orAll": [ "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ], "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] } ] } } }
    4. Combine up to 5 filter groups by embedding them within another logical operator. You can create one level of embedding.

      "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { "andAll | orAll": [ "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ], "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ] ] } } }

    The following table describes the filter types that you can use:

    Field Supported value data types Filtered results
    equals string, number, boolean Attribute matches the value you provide
    notEquals string, number, boolean Attribute doesn't match the value you provide
    greaterThan number Attribute is greater than the value you provide
    greaterThanOrEquals number Attribute is greater than or equal to the value you provide
    lessThan number Attribute is less than the value you provide
    lessThanOrEquals number Attribute is less than or equal to the value you provide
    in list of strings Attribute is in the list you provide
    notIn list of strings Attribute isn't in the list you provide
    startsWith string Attribute starts with the string you provide (only supported for Amazon OpenSearch Serverless vector stores)

    To combine filter types, you can use one of the following logical operators:

    Field Maps to Filtered results
    andAll List of up to 5 filter types Results fulfill all of the filtering expressions in the group
    orAll List of up to 5 filter types Results fulfill at least one of the filtering expressions in the group

    For examples, see Send a query and include filters (Retrieve) and Send a query and include filters (RetrieveAndGenerate).

    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 tags in the Anthropic user guide.

    For general prompt engineering guidelines, see Prompt engineering concepts.

    Select the tab corresponding to your method of choice and follow the steps:

    Console

    Follow the console steps at Query a knowledge base and generate AI responses. In the test window, turn on Generate responses. Then, in the Configurations pane, expand the Knowledge base prompt template section.

    1. Choose Edit.

    2. Edit the system prompt in the text editor, including prompt placeholders and XML tags as necessary. To revert to the default prompt template, choose Reset to default.

    3. When you're finished editing, choose Save changes. To exit without saving the system prompt, choose Discard changes.

    API

    When you make a RetrieveAndGenerate request, include a generationConfiguration field, mapped to a GenerationConfiguration object. To see the location of this field, refer to the RetrieveAndGenerate request body in the API reference.

    The following JSON object shows the minimal fields required in the GenerationConfiguration object to set the maximum number of retrieved results to return:

    "generationConfiguration": { "promptTemplate": { "textPromptTemplate": "string" } }

    Enter your custom prompt template in the textPromptTemplate field, including prompt placeholders and XML tags as necessary. For the maximum number of characters allowed in the system prompt, see the textPromptTemplate field in GenerationConfiguration.

    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.

    Select the tab corresponding to your method of choice and follow the steps:

    Console

    Follow the console steps at Query a knowledge base and generate AI responses. In the test window, turn on Generate responses. Then, in the Configurations pane, expand the Guardrails section.

    1. In the Guardrails section, choose the Name and the Version of your guardrail. If you would like to see the details for your chosen guardrail and version, choose View.

      Alternatively, you can create a new one by choosing the Guardrail link.

    2. When you're finished editing, choose Save changes. To exit without saving choose Discard changes.

    API

    When you make a RetrieveAndGenerate request, include the guardrailsConfiguration field within the generationConfiguration to use your guardrail with the request. To see the location of this field, refer to the RetrieveAndGenerate request body in the API reference.

    The following JSON object shows the minimal fields required in the GenerationConfiguration to set the guardrailsConfiguration:

    "generationConfiguration": { "guardrailsConfiguration": { "guardrailsId": "string", "guardrailsVersion": "string" } }

    Specify the guardrailsId and guardrailsVersion of your chosen guardrails.