

# 使用 Amazon Nova 构建自定义 RAG 系统
<a name="rag-building"></a>

**注意**  
Amazon Nova Premier 尚无法通过 [RetrieveAndGenerate](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 提供。要将 [RetrieveAndGenerate](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 与 Amazon Nova Premier 一起使用，您需要在调用 [RetrieveAndGenerate](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 时提供自定义提示。这是通过在 [RetrieveAndGenerate](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 调用中的 `generationConfiguration` 参数中提供 `promptTemplate` 来完成的，如下所示：  

```
'generationConfiguration': {
                        'promptTemplate': {
                            'textPromptTemplate': promptTemplate
                        }
                    }
```
要构建自定义提示模板，请参阅 [RAG 的提示指导](https://docs.aws.amazon.com/nova/latest/userguide/prompting-tools-rag.html)。

您可以在自定义文本 RAG 系统中使用 Amazon Nova 模型作为 LLM。要使用 Amazon Nova 构建自己的 RAG 系统，您可以将 RAG 系统配置为直接查询知识库，也可以将知识库与座席关联（有关更多信息，请参阅[使用 Amazon Nova 构建人工智能代理](agents.md)）

在任何 RAG 系统中使用 Amazon Nova 时，有两种一般方法
+ **使用检索器作为工具**（推荐）：您可以在 Converse API 或 Invokemodel API 的 ToolParameter 中定义检索器，以便用作工具。例如，您可以将 Bedrock [Retrieve API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) 或任何其他检索器定义为“tool”。
+ **为 RAG 系统使用自定义指令**：您可以定义自己的自定义指令来构建自定义 RAG 系统。

**使用检索器作为工具**

定义一个允许模型调用检索器的工具。工具的定义是您通过 `toolConfig`（[ToolConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolConfiguration.html)）请求参数传递给 `Converse` 操作的 JSON 架构。

```
{
    "tools": [
        {
            "toolSpec": {
                "name": "Retrieve information tool",
                "description": "This tool retrieves information from a custom database",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "query": {
                                "type": "string",
                                "description": "This is the description of the query parameter"
                            }
                        },
                        "required": [
                            "query"
                        ]
                    }
                }
            }
        }
    ]
}
```

定义工具后，您可以在 Converse API 中将工具配置作为参数传递。

**如何解读回复元素**

您将在助手“role”下以 JSON 形式收到来自模型的回复，内容类型为“toolUse”，或者如果模型选择不使用检索器工具，则作为上下文类型为“text”。如果模型选择使用检索器工具，则回复将识别该工具 (tool\_name)。有关如何使用所请求工具的信息包含在模型于 `output` ([ConverseOutput](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseOutput.html)) 字段中返回的消息内。具体来说，是 `toolUse`（[ToolUseBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolUseBlock.html)）字段。在后续的调用中，您可以通过 `toolUseId` 字段来识别工具请求。

```
{
    "output": {
        "message": {
            "role": "assistant",
            "content": [
                {
                    "toolUse": {
                        "toolUseId": "tooluse_1234567",
                        "name": "Retrieve information tool",
                        "input": {
                            "query": "Reformatted user query" #various arguments needed by the chosen tool
                        }
                    }
                }
            ]
        }
    },
    "stopReason": "tool_use"
}
```

在模型回复的 `toolUse` 字段中，可以使用 `name` 字段来识别工具的名称。然后调用工具实现并传递 `input` 字段中的输入参数。

**如何将检索到的内容重新输入 Converse API**

要将检索到的结果重新返回 Amazon Nova，您现在可以构造一条在用户角色中包含 `toolResult` ([ToolResultBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolResultBlock.html)) 内容块的工具块消息。在内容块中，您需要包含工具的回复以及您在上一步中获得的工具请求 ID。

```
{
    "role": "user",
    "content": [
        {
            "toolResult": {
                "toolUseId": "tooluse_1234567",
                "content": [
                    {
                        "json": {
                            "Text chunk 1": "retrieved information chunk 1",
                            "Text chunk 2": "retrieved information chunk 2"
                        }
                    }
                ],
                "status": "success | error"
            }
        }
    ]
}
```

[toolResult](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolResultBlock.html) 可以有“content”而“content”中可以有“text”“JSON”和“image”（取决于所使用的模型）。如果工具出现错误，例如请求不存在或错误的参数，您可以通过 `toolResult` 字段将错误信息发送给模型。要指示出现的错误，您可以在 `status` 字段中指定 `error`。