

# 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 経由ではまだ利用できません。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 を呼び出すときにカスタムプロンプトを指定する必要があります。これを行うには、次に示すように、[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)」を参照してください。

Amazon Nova モデルは、カスタムテキスト RAG システム内の LLM として使用できます。Amazon Nova で独自の RAG システムを構築するには、ナレッジベースを直接クエリするように RAG システムを設定するか、ナレッジベースをエージェントに関連付けることができます (詳細については、「[Amazon Nova を使用した AI エージェントの構築](agents.md)」を参照してください)。

RAG システム内で Amazon Nova を使用する場合、2 つの一般的なアプローチがあります。
+ **リトリーバーをツールとして使用する** (推奨): converse API または Invokemodel API の ToolParameter で、リトリーバーをツールとして使用するように定義できます。たとえば、Bedrock [Retrieve API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html) またはその他のリトリーバーを「ツール」として定義できます。
+ **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 のパラメータとして渡すことができます。

**レスポンス要素を解釈する方法**

モデルからのレスポンスは、コンテンツタイプが「toolUse」である、アシスタント「ロール」のもとでの JSON として、または、モデルがリトリーバーツールを使用しないことを選択した場合は「テキスト」のコンテキストタイプとして受け取ります。モデルがリトリーバーツールを使用することを選択した場合、レスポンスはツール (tool\$1name) を識別します。リクエストされたツールが使用される方法に関する情報は、モデルが `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) には、「テキスト」、「JSON」、「イメージ」を使用できる「コンテンツ」を含めることができます (使用するモデルによって異なります)。存在しない引数や間違った引数の要求など、ツールでエラーが発生した場合は、`toolResult` フィールドでモデルにエラー情報を送信することができます。エラーを表示するには、`status` フィールドに `error` を指定します。