

# ツールの定義
<a name="tool-use-definition"></a>

ツール呼び出しワークフローの重要なステップは、ツールを定義することです。ツール定義には、ツールを呼び出すのが適切なタイミングでモデルをガイドするために必要なコンテキストがすべて含まれている必要があります。

ツールを定義するには、ツール設定を作成し、ユーザーメッセージとともに API に渡します。[ツール設定](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolConfiguration.html)スキーマには、ツールの配列と、オプションでツール選択パラメータが必要です。

**注記**  
Amazon Nova は、`toolChoice` の `auto`、`any`、`tool` オプションをサポートしています。詳細については、「Amazon Bedrock API ドキュメント」の「[ToolChoice](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolChoice.html)」および「[ツールを使用して Amazon Bedrock モデルレスポンスを完成させる](https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use.html)」を参照してください。

ツールを定義する方法の例を次に示します。

```
tool_config = {
    "tools": [
        {
            "toolSpec": {
                "name": "top_song",
                "description": "Get the most popular song played on a radio station.",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "sign": {
                                "type": "string",
                                "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ, and WKRP."
                            }
                        },
                        "required": [
                            "sign"
                        ]
                    }
                }
            }
        }
    ],
}
```

名前、説明、入力スキーマは、ツールの正確な機能を明示的に示す必要があります。ツールを使用するときの主要な差別化要因がツール設定に反映されていることを確認します。

**注記**  
Amazon Nova の理解モデルは、現在、Converse API で [ToolInputSchema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolInputSchema.html) を定義する際に使用される、JsonSchema 機能のサブセットのみをサポートしています。  
最上位スキーマは [Object](https://json-schema.org/understanding-json-schema/reference/object) 型である必要があります。
最上位のオブジェクトでは、type (「オブジェクト」に設定する必要がある)、[https://json-schema.org/understanding-json-schema/reference/object#properties](https://json-schema.org/understanding-json-schema/reference/object#properties)、および [https://json-schema.org/understanding-json-schema/reference/object#required](https://json-schema.org/understanding-json-schema/reference/object#required) の 3 つのフィールドのみがサポートされています。

ツール呼び出しでは、Greedy デコードを有効にするために温度を 0 に設定することをお勧めします。

Converse API を使用してツールを呼び出す例を次に示します。

```
import json
import boto3

client = boto3.client("bedrock-runtime", region_name="us-east-1")

input_text = "What is the most popular song on WZPZ?"

messages = [{
    "role": "user",
    "content": [{"text": input_text}]
}]

inf_params = {"maxTokens": 1000, "temperature": 0}

response = client.converse(
    modelId="us.amazon.nova-lite-v1:0",
    messages=messages,
    toolConfig=tool_config,
    inferenceConfig=inf_params
)

messages.append(response["output"]["message"])

# Pretty print the response JSON.
print("[Full Response]")
print(json.dumps(response, indent=2))

# Print the tool content for easy readability.
tool = next(
    block["toolUse"]
    for block in response["output"]["message"]["content"]
    if "toolUse" in block
)
print("\n[Tool Response]")
print(tool)
```