

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 定義工具
<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 理解模型目前僅支援 JsonSchema 功能的子集 (當用於在 Converse API 中定義 [ToolInputSchema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolInputSchema.html) 時)。  
最上層結構描述必須是 [Object](https://json-schema.org/understanding-json-schema/reference/object) 類型。
最上層物件中僅支援三個欄位 - 類型 (必須設定為「物件」)、[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)。

對於工具呼叫，我們建議將溫度設定為 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)
```