

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 配置和自定义查询与响应生成
<a name="kb-test-config"></a>

您可以配置和自定义检索和响应生成，从而进一步提高响应的相关性。例如，您可以对文档元数据应用筛选器 fields/attributes ，以使用最近更新的文档或修改时间最近的文档。

**注意**  
除**编排和生成**外，以下所有配置仅适用于非结构化数据来源。

要详细了解控制台或 API 中的这些配置，请从以下主题中进行选择。

## 来源分块的数量
<a name="kb-test-config-number"></a>

当您查询知识库时，默认情况下，Amazon Bedrock 会在响应中返回最多五个结果。每个结果都对应一个来源块。

**注意**  
响应中的实际结果数可能小于指定的 `numberOfResults` 值，因为此参数设置了要返回的最大结果数。如果您为分块策略配置了分层分块，`numberOfResults` 参数会映射到知识库将检索的子分块的数量。由于共享同一父块的子块在最终响应中会被父块替换，因此返回的结果数可能少于请求的数量。

要修改返回的最大结果数，请选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。在**配置**窗格中，展开**来源分块**部分，然后输入要返回的最大来源分块数。

------
#### [ API ]

[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html)发出[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html)或请求时，请添加一个映射到[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象的`retrievalConfiguration`字段。要查看此字段的位置，请参阅 API 参考中的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) 和 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求正文。

以下 JSON 对象显示了[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象中设置要返回的最大结果数所需的最小字段：

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

指定要在`numberOfResults`字段中[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)返回的最大检索结果数（有关可接受值的范围，请参阅中的`numberOfResults`字段）。

------

## 搜索类型
<a name="kb-test-config-search"></a>

搜索类型定义了知识库中的数据来源的查询方式。可能的搜索类型如下：

**注意**  
只有包含可筛选文本字段的 Amazon RDS、Amazon OpenSearch Serverless 和 MongoDB 矢量存储才支持混合搜索。如果您使用其他向量存储，或者您的向量存储不包含可筛选的文本字段，则查询将使用语义搜索。
+ **默认**：Amazon Bedrock 为您决定搜索策略。
+ **混合**：将搜索向量嵌入（语义搜索）与搜索原始文本相结合。
+ **语义**：仅搜索向量嵌入。

要了解如何定义搜索类型，请选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。打开**配置**窗格后，展开**搜索类型**部分，打开**覆盖默认搜索**，然后选择一个选项。

------
#### [ API ]

[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html)发出[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html)或请求时，请添加一个映射到[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象的`retrievalConfiguration`字段。要查看此字段的位置，请参阅 API 参考中的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) 和 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求正文。

以下 JSON 对象显示了该[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象中设置搜索类型配置所需的最少字段：

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

在 `overrideSearchType` 字段中指定搜索类型。您有以下选项：
+ 如果您没有指定值，Amazon Bedrock 会决定哪种搜索策略最适合您的向量存储配置。
+ **混合**：Amazon Bedrock 同时使用向量嵌入和原始文本来查询知识库。
+ **语义**：Amazon Bedrock 使用其向量嵌入来查询知识库。

------

## 流式传输
<a name="kb-test-config-stream"></a>

------
#### [ Console ]

按照 [根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md) 中的控制台步骤进行操作。打开**配置**窗格后，展开**流式传输首选项**部分，然后打开**流式处理响应**。

------
#### [ API ]

要流式处理响应，请使用 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateStream.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateStream.html) API。有关填写字段的更多详细信息，请参阅[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的 **API** 选项卡。

------

## 手动元数据筛选
<a name="kb-test-config-filters"></a>

您可以对文档应用过滤器 fields/attributes ，以帮助您进一步提高回复的相关性。您的数据源可以包含 attributes/fields 要筛选的文档元数据，也可以指定要在嵌入中包含哪些字段。

例如，“epoch\$1modification\$1time”表示文档自 1970 年 1 月 1 日（UTC）最后一次更新之后经历的秒数。您可以通过将“epoch\$1modification\$1time”设置为*大于*某个数值，筛选出最新的数据。这些最新的文档可用于查询。

要在查询知识库时使用筛选条件，请检查您的知识库是否满足以下要求：
+ 配置数据来源连接器时，大多数连接器都会抓取文档的主元数据字段。如果使用 Amazon S3 存储桶作为数据来源，则针对与其关联的文件或文档，该存储桶必须至少包含一个 `fileName.extension.metadata.json`。有关配置元数据文件的更多信息，请参阅[连接配置](s3-data-source-connector.md#configuration-s3-connector)中的**文档元数据字段**。
+ 如果知识库的向量索引位于 Amazon OpenSearch Serverless 矢量存储中，请检查该`faiss`引擎是否配置了矢量索引。如果向量索引是使用 `nmslib` 引擎配置，则您必须执行以下操作之一：
  + 在控制台中@@ [创建新的知识库](knowledge-base-create.md)，让 Amazon Bedrock 在 Amazon OpenSearch Serverless 中自动为你创建矢量索引。
  + 在向量存储中[创建另一个向量索引](knowledge-base-setup.md)，然后选择 `faiss` 作为**引擎**。然后[创建新的知识库](knowledge-base-create.md)并指定新的向量索引。
+ 如果您的知识库使用 S3 向量存储桶中的向量索引，则无法使用 `startsWith` 和 `stringContains` 筛选条件。
+ 如果您要向 Amazon Aurora 数据库集群中的现有向量索引添加元数据，建议您提供自定义元数据列的字段名称，以便将所有元数据存储在一个列中。在[数据摄取](kb-data-source-sync-ingest.md)期间，此列将用来填入数据来源中的元数据文件包含的所有信息。如果您选择提供该字段，则必须在此列上创建索引。
  + 当您在控制台中[创建新的知识库](knowledge-base-create.md)，并让 Amazon Bedrock 配置您的 Amazon Aurora 数据库时，它将自动创建一个列，并用您的元数据文件中的信息填充该列。
  + 当您选择在向量存储中[创建另一个向量索引](knowledge-base-setup.md)时，必须提供自定义元数据字段名称，以存储元数据文件中的信息。如果未提供此字段名称，则必须为文件中的每个元数据属性创建一个列，并指定数据类型（文本、数字或布尔值）。例如，如果数据来源中存在属性 `genre`，您应添加名为 `genre` 的列，并指定 `text` 作为数据类型。在摄取期间，会在这些单独的列中填入相应的属性值。

*如果您的数据源中有 PDF 文档，并且使用 Amazon OpenSearch Serverless 作为矢量存储：Amazon Bedrock 知识库将生成文档页码并将其存储在 field/attribute 名为 x-amz-bedrock-kb-的元数据中。document-page-number*请注意，如果您为文档选择不分块，则不支持在元数据字段中存储的页码。

您可以使用以下筛选运算符在查询时筛选结果：


**筛选运算符**  

| 运算符 | 控制台 | API 筛选条件名称 | 支持的属性数据类型 | 筛选结果 | 
| --- | --- | --- | --- | --- | 
| Equals | = | [equals](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-equals) | 字符串、数字、布尔值 | 属性与您提供的值相匹配 | 
| Not equals | \$1= | [noteQuals](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-notEquals) | 字符串、数字、布尔值 | 属性与您提供的值不匹配 | 
| Greater than | > | [greaterThan](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-greaterThan) | 数字 | 属性大于您提供的值 | 
| Greater than or equals | >= | [greaterThanOr等于](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-greaterThanOrEquals) | 数字 | 属性大于或等于您提供的值 | 
| Less than | < | [lessThan](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-lessThan) | 数字 | 属性小于您提供的值 | 
| Less than or equals | <= | [lessThanOr等于](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-lessThanOrEquals) | 数字 | 属性小于或等于您提供的值 | 
| In | : | [in](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-in) | 字符串列表 | 属性在你提供的列表中（目前亚马逊 OpenSearch 无服务器和 Neptune Analytics GraphRag 矢量存储最受支持） | 
| Not in | \$1: | [notIn](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-notIn) | 字符串列表 | 属性不在你提供的列表中（目前亚马逊 OpenSearch 无服务器和 Neptune Analytics GraphRag 矢量存储最受支持） | 
| String contains | 不可用 | [stringContains](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-stringContains) | 字符串 | 属性必须是字符串。属性名称与密钥相匹配，其值是一个包含您作为子字符串提供的值的字符串，或者一个包含您作为子字符串提供的值的成员列表（目前最支持的是 Amazon OpenSearch Serverless 矢量存储）。 Neptune Analytics GraphRag 矢量存储支持字符串变体，但不支持此过滤器的列表变体）。 | 
| List contains | 不可用 | [listContains](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-listContains) | 字符串 | 属性必须采用字符串列表形式。属性名称与密钥匹配，其值是一个列表，其中包含您作为其成员之一提供的值（目前最支持的是 Amazon OpenSearch Serverless 矢量存储）。 | 

要组合筛选运算符，可以使用以下逻辑运算符：


**逻辑运算符**  

| 运算符 | 控制台 | API 筛选条件字段名称 | 筛选结果 | 
| --- | --- | --- | --- | 
| And | 和 | [andAll](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-andAll) | 结果满足组中的所有筛选表达式 | 
| Or | 或者 | [orAll](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html#bedrock-Type-agent-runtime_RetrievalFilter-orAll) | 结果满足组中至少一个筛选表达式 | 

要了解如何使用元数据筛选结果，请选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。当你打开**配置**窗格时，你会看到**筛选条件**部分。以下过程介绍了不同的应用场景：
+ 要添加筛选条件，请在框中输入元数据属性、筛选运算符和值来创建筛选表达式。用空格分隔表达式的每个部分。按 **Enter** 来添加筛选条件。

  有关可接受的筛选运算符的列表，请参阅上面的**筛选运算符**表。在元数据属性后面添加空格时，您也可以看到筛选运算符列表。
**注意**  
您必须用引号将字符串括起来。

  例如，您可以通过添加以下筛选条件从来源文档中筛选出包含值为 `"entertainment"` 的 `genre` 元数据属性的结果：**genre = "entertainment"**。  
![\[添加一个筛选条件。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-one.png)
+ 要添加其他筛选条件，请在框中输入另一个筛选表达式，然后按 **Enter**。您最多可以在组中添加 5 个筛选条件。  
![\[添加其他筛选条件。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-more.png)
+ 默认情况下，查询将返回满足您提供的所有筛选表达式的结果。要返回至少满足其中一个筛选表达式的结果，请在任意两个筛选操作之间选择**和**下拉菜单，然后选择**或**。  
![\[更改筛选条件之间的逻辑操作。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-logical.png)
+ 要组合不同的逻辑运算符，请选择 **\$1 添加组**以添加筛选条件组。在新组中输入筛选表达式。您可以添加最多 5 个组。  
![\[添加筛选条件组以组合不同的逻辑运算符。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-group.png)
+ 要更改所有筛选组之间使用的逻辑运算符，请在任意两个筛选条件组之间选择 **AND** 下拉菜单，然后选择 **OR**。  
![\[更改筛选条件组之间的逻辑操作。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-group-logical.png)
+ 要编辑筛选条件，请将其选中，修改筛选操作，然后选择**应用**。  
![\[编辑筛选条件。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-edit.png)
+ 要移除筛选条件组，请选择相应组旁边的垃圾桶图标 (![\[Trapezoid-shaped diagram showing data flow from source to destination through AWS Transfer Family.\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/trash.png))。要移除筛选条件，请选择相应筛选条件旁边的删除图标 (![\[Close or cancel icon represented by an "X" symbol.\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/close.png))。  
![\[删除筛选条件或筛选条件组。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-delete.png)

下图显示了一个筛选条件配置示例，除了类型为 **"cooking"** 或 **"sports"** 且作者名字以 **"C"** 开头的文档外，该配置还返回了所有 **2018** 之后编写的类型为 **"entertainment"** 的文档。

![\[示例筛选条件配置。\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/kb/filter-example.png)


------
#### [ API ]

[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html)发出[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html)或请求时，请添加一个映射到[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象的`retrievalConfiguration`字段。要查看此字段的位置，请参阅 API 参考中的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) 和 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求正文。

以下 JSON 对象显示了[KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html)对象中为不同用例设置过滤器所需的最少字段：

1. 使用一个筛选运算符（请参阅上面的**筛选运算符**表）。

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

1. 使用逻辑运算符（请参阅上面的**逻辑运算符**表）最多可组合 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", ...]
                   },
                   ...
               ]
           }
       }
   }
   ```

1. 使用逻辑运算符将最多 5 个筛选运算符组合到一个筛选条件组中，使用第二个逻辑运算符将该筛选条件组与其他筛选运算符组合。

   ```
   "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", ...]
                   }
               ]
           }
       }
   }
   ```

1. 将最多 5 个筛选条件组嵌入到另一个逻辑运算符中，从而将它们组合。您可以创建一个嵌入级别。

   ```
   "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", ...]
                       },
                       ...
                   ]
               ]
           }
       }
   }
   ```

下表描述了您可以使用的筛选条件类型：


****  

| 字段 | 支持的值数据类型 | 筛选结果 | 
| --- | --- | --- | 
| equals | 字符串、数字、布尔值 | 属性与您提供的值相匹配 | 
| notEquals | 字符串、数字、布尔值 | 属性与您提供的值不匹配  | 
| greaterThan | 数字 | 属性大于您提供的值 | 
| greaterThanOrEquals | 数字 | 属性大于或等于您提供的值 | 
| lessThan | 数字 | 属性小于您提供的值  | 
| lessThanOrEquals | 数字 | 属性小于或等于您提供的值 | 
| in | 字符串列表 | 属性在您提供的列表中 | 
| notIn | 字符串列表 | 属性不在您提供的列表中 | 
| startsWith | 字符串 | 属性以您提供的字符串开头（仅支持 Amazon OpenSearch Serverless 矢量存储） | 

要组合筛选条件类型，您可以使用以下逻辑运算符之一：


****  

| 字段 | 映射到 | 筛选结果 | 
| --- | --- | --- | 
| andAll | 最多 5 种筛选条件类型的列表 | 结果满足组中的所有筛选表达式 | 
| orAll | 最多 5 种筛选条件类型的列表 | 结果满足组中至少一个筛选表达式 | 

有关示例，请参阅[发送查询并包含筛选条件（检索）](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html#API_agent-runtime_Retrieve_Example_2)和[发送查询并包含筛选条件（RetrieveAndGenerate）](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html#API_agent-runtime_RetrieveAndGenerate_Example_2)。

------

## 隐式元数据筛选
<a name="kb-test-config-implicit"></a>

Amazon Bedrock 知识库根据用户查询和元数据架构生成并应用检索筛选器。

**注意**  
该功能目前仅适用于 Anthropic Claude 3.5 Sonnet。

`implicitFilterConfiguration` 在 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) 请求正文的 `vectorSearchConfiguration` 中指定。包括以下字段：
+ `metadataAttributes` – 在此数组中，提供用于描述模型将为其生成筛选条件的元数据属性的架构。
+ `modelArn` – 要使用的模型的 ARN。

以下是您可以添加到 `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"
    }
]
```

## 护栏
<a name="kb-test-config-guardrails"></a>

您可以基于自己的应用场景和负责任的人工智能政策为知识库实施防护机制。您可以创建针对不同应用场景量身定制的多个防护机制，并跨多个请求和响应条件应用它们，从而在整个知识库中提供一致的用户体验并标准化安全控制。您可以将拒绝的主题配置为禁止不受欢迎的主题，将内容筛选条件配置为屏蔽模型输入和响应中的有害内容。有关更多信息，请参阅[使用 Amazon Bedrock 护栏检测和筛选掉有害内容](guardrails.md)。

**注意**  
Claude 3 Sonnet 和 Haiku 目前不支持针对知识库应用具有上下文基础的防护机制。

有关提示工程的一般性准则，请参阅[提示工程概念](prompt-engineering-guidelines.md)。

选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。在测试窗口中，打开**生成响应**。然后，在**配置**窗格中，展开**防护机制**部分。

1. 在**防护机制**部分，选择防护机制的**名称**和**版本**。如果您想查看所选防护机制和版本的详细信息，请选择**查看**。

   或者，您可以通过选择**防护机制**链接来创建一个新的机制。

1. 编辑完成后，选择**保存更改**。要退出而不保存更改，请选择**放弃更改**。

------
#### [ API ]

在提出 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求时，请在 `generationConfiguration` 中添加 `guardrailConfiguration` 字段，以将防护机制用于该请求。要查看此字段的位置，请参阅 API 参考中的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求正文。

以下 JSON 对象显示了在中设置所需的最少字段`guardrailConfiguration`：[GenerationConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_GenerationConfiguration.html)

```
"generationConfiguration": {
    "guardrailConfiguration": {
        "guardrailId": "string",
        "guardrailVersion": "string"
    }
}
```

指定所选防护机制的 `guardrailVersion` 和 `guardrailId`。

------

## 重排
<a name="kb-test-config-rerank"></a>

您可以使用重排器模型对知识库查询的结果进行重排。按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。打开**配置**窗格后，展开**重排**部分。选择重排模型，必要时更新权限，并修改任何其他选项。完成重排后，输入提示，然后选择**运行**以测试结果。

## 查询分解
<a name="kb-test-query-modifications"></a>

查询分解是一种用于将复杂查询分解为更小、更易于管理的子查询的技术。这种方法可以帮助检索更准确、更相关的信息，尤其是在初始查询涉及多个方面或过于宽泛的情况下。启用此选项可能会导致对您的知识库执行多个查询，这可能有助于获得更准确的最终响应。

例如，对于像*“谁在 2022 年 FIFA 世界杯上得分更高，阿根廷还是法国？”*这样的问题，在生成最终答案之前，Amazon Bedrock 知识库可能会先生成以下子查询：

1. *阿根廷在 2022 年 FIFA 世界杯决赛中打进了多少球？*

1. *法国在 2022 年 FIFA 世界杯决赛中打进了多少球？*

------
#### [ Console ]

1. 创建和同步数据来源或使用现有知识库。

1. 进入测试窗口并打开配置面板。

1. 启用查询分解。

------
#### [ 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
           }
         },
...}
}
```

------

## 推理参数
<a name="kb-test-model-params"></a>

在基于信息检索生成响应时，您可以使用[推理参数](inference-parameters.md)来更好地控制模型在推理期间的行为并影响模型的输出。

要了解如何修改推理参数，请选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

**要在查询知识库时修改推理参数**，请按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤操作。在打开**配置**窗格时，您会看到**推理参数**部分。根据需要修改参数。

**要在与您的文档交互时修改推理参数**，请按照 [无需配置知识库即可使用您的文档聊天](knowledge-base-chatdoc.md) 中的步骤操作。在**配置**窗格中，展开**推理参数**部分，并根据需要修改参数。

------
#### [ API ]

您在调用 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 时提供模型参数。您可以通过在 `inferenceConfig` 字段中或 `knowledgeBaseConfiguration` 中（如果您查询知识库）或 `externalSourcesConfiguration` 中（如果您[与文档交互](knowledge-base-chatdoc.md)）提供推理参数来自定义模型。

`inferenceConfig` 字段中有一个包含以下参数的 `textInferenceConfig` 字段，您可以执行以下操作：
+ temperature
+ topP
+ maxTokenCount
+ stopSequences

您可以通过在 `externalSourcesConfiguration` 和 `knowledgeBaseConfiguration` 的`inferenceConfig` 字段中使用以下参数来自定义模型：
+ temperature
+ topP
+ maxTokenCount
+ stopSequences

有关每个参数的功能的详细说明，请参阅 [利用推理参数影响响应生成](inference-parameters.md)。

此外，您可以通过 `additionalModelRequestFields` 映射提供 `textInferenceConfig` 不支持的自定义参数。您可以使用此参数提供特定模型所特有的参数，有关唯一参数的信息，请参阅 [基础模型的推理请求参数和响应字段](model-parameters.md)。

如果 `textInferenceConfig` 中省略了参数，则将使用默认值。`textInferneceConfig` 中未识别的所有参数都将被忽略，而 `AdditionalModelRequestFields` 中未识别的所有参数都将导致异常。

如果 `additionalModelRequestFields` 和 `TextInferenceConfig` 中有相同的参数，则会引发验证异常。

**在中使用模型参数 RetrieveAndGenerate**

 以下是 `RetrieveAndGenerate` 请求正文中 `generationConfiguration` 下的 `inferenceConfig` 和 `additionalModelRequestFields` 结构示例：

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

 上面的示例将 `temperature` 设为 0.5，`top_p` 设为 0.5，`maxTokens` 设为 2048，如果在生成的响应中遇到字符串“\$1nObservation”，则停止生成，并传递自定义 `top_k` 值 50。

------

## 知识库提示模板：编排和生成
<a name="kb-test-config-prompt-template"></a>

当您查询知识库并请求生成响应时，Amazon Bedrock 使用将指令和上下文与用户查询相结合的提示模板，来构造发送到模型以生成响应的生成提示。您还可以自定义编排提示（用于将用户的提示转换为搜索查询）。您可以使用以下工具设计提示模板：
+ **提示占位符**：Amazon Bedrock 知识库中的预定义变量，这些变量在知识库查询期间在运行时动态填充。在系统提示中，您将看到这些占位符两边都有 `$` 符号。以下列表介绍了可以使用的占位符：
**注意**  
`$output_format_instructions$` 占位符是在响应中显示引文的必填字段。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/kb-test-config.html)
+ **XML 标签**：Anthropic 模型支持使用 XML 标签来构造和描述您的提示。请使用描述性标签名称以获得最佳结果。例如，在默认的系统提示中，您将看到用于描述先前询问过的相关数据库的 `<database>` 标签）。有关更多信息，请参阅[《Anthropic 用户指南》](https://docs.anthropic.com/en/docs/welcome)中的[使用 XML 标签](https://docs.anthropic.com/claude/docs/use-xml-tags)。

有关提示工程的一般性准则，请参阅[提示工程概念](prompt-engineering-guidelines.md)。

选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Console ]

按照[查询知识库并检索数据](kb-test-retrieve.md)或[根据检索到的数据查询知识库并生成响应](kb-test-retrieve-generate.md)中的控制台步骤进行操作。在测试窗口中，打开**生成响应**。然后，在**配置**窗格中，展开**知识库提示模板**部分。

1. 选择**编辑**。

1. 根据需要，在文本编辑器中编辑系统提示，包括提示占位符和 XML 标签。要恢复到默认提示模板，请选择**重置为默认值**。

1. 编辑完成后，选择**保存更改**。要在不保存系统提示的情况下退出，请选择**放弃更改**。

------
#### [ API ]

当您提出[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html)请求时，请添加一个映射到[GenerationConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_GenerationConfiguration.html)对象的`generationConfiguration`字段。要查看此字段的位置，请参阅 API 参考中的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 请求正文。

以下 JSON 对象显示了[GenerationConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_GenerationConfiguration.html)对象中设置要返回的最大检索结果数所需的最小字段：

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

在 `textPromptTemplate` 字段中输入您的自定义提示模板，根据需要添加提示占位符和 XML 标签。有关系统提示符中允许的最大字符数，请参阅中的`textPromptTemplate`字段[GenerationConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_GenerationConfiguration.html)。

------