

# 定义工具
<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) 和[Use a tool to complete an Amazon Bedrock model response](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"
                        ]
                    }
                }
            }
        }
    ],
}
```

名称、描述和输入架构必须明确，且要准确体现该工具的确切功能。确保在工具配置中体现出何时使用该工具的关键区别特征。

**注意**  
当在 Converse API 中用于定义 [ToolInputSchema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolInputSchema.html) 时，Amazon Nova 理解模型目前仅支持 JsonSchema 功能的子集。  
顶层架构的类型必须为 [Object](https://json-schema.org/understanding-json-schema/reference/object)。
顶层 Object 仅支持三个字段：type（必须设置为“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)。。

对于工具调用，我们建议将 temperature 设置为 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)
```