

# 选择工具
<a name="tool-choice"></a>

Amazon Nova 模型支持*工具选择*功能。身为开发人员，您可以利用“工具选择”来控制工具调用方式。有三个支持的“工具选择”参数选项：`tool`、`any` 和 `auto`。
+ **工具** – 将调用指定工具一次。
+ **任意** – 将调用提供的某一工具至少一次。
+ **自动** – 模型将决定是否需要调用工具，如果需要，则决定要调用多个工具。

------
#### [ Tool ]

使用 `tool` 作为“工具选择”可以控制模型调用的特定工具。以下示例通过结构化输出应用场景强调了这一点，其中要求以一致的方式格式化回复。

```
tool_config = {
    "toolChoice": {
        "tool": { "name" : "extract_recipe"}
    },
    "tools": [
        {
            "toolSpec": {
                "name": "extract_recipe",
                "description": "Extract recipe for cooking instructions",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string",
                                "description": "Name of the recipe"
                            },
                            "description": {
                                "type": "string",
                                "description": "Brief description of the dish"
                            },
                            "ingredients": {
                                "type": "array",
                                "items": {
                                    "type": "string",
                                    "description": "Name of ingredient"
                                }
                            }
                        },
                        "required": ["name", "description", "ingredients"]
                    }
                }
            }
        }
    ]
}
```

------
#### [ Any ]

使用 `any` 作为“工具选择”可以确保每次至少调用一个工具。虽然具体调用哪个工具的决定权由模型决定，但总会返回一个工具。以下示例重点介绍了在 API 选择端点应用场景中使用 any 作为“工具选择”的情况。在要求模型返回特定工具时，本例会有所帮助。

```
tool_config = {
    "toolChoice": {
        "any": {}
    },
    "tools": [
         {
            "toolSpec": {
                "name": "get_all_products",
                "description": "API to retrieve multiple products with filtering and pagination options",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "sort_by": {
                                "type": "string",
                                "description": "Field to sort results by. One of: price, name, created_date, popularity",
                                "default": "created_date"
                            },
                            "sort_order": {
                                "type": "string",
                                "description": "Order of sorting (ascending or descending). One of: asc, desc",
                                "default": "desc"
                            },
                        },
                        "required": []
                    }
                }
            }
        },
        {
            "toolSpec": {
                "name": "get_products_by_id",
                "description": "API to retrieve retail products based on search criteria",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "product_id": {
                                "type": "string",
                                "description": "Unique identifier of the product"
                            },
                        },
                        "required": ["product_id"]
                    }
                }
            }
        }
    ]
}
```

------
#### [ Auto ]

使用 `auto` 作为“工具选择”是工具支持的默认功能，允许模型决定何时调用工具以及调用多少工具。如果未在请求中包含“工具选择”，就会发生这样的行为。

**注意**  
Amazon Nova 工具调用的默认行为是运用思维链进行工具选择。使用默认行为或使用 `auto` 作为“工具选择”时，<thinking> 标签中还会输出思维过程。

以下示例重点介绍了一个聊天机器人应用场景。在该场景中，您可能希望让模型在互联网上搜索最新信息，或者直接回复用户。这种“工具选择”功能提供了灵活性，将推理留给了模型。

```
tool_config = {
    "toolChoice": {
        "auto": {}
    },
    "tools": [
         {
            "toolSpec": {
                "name": "search",
                "description": "API that provides access to the internet",
                "inputSchema": {
                    "json": {
                        "type": "object",
                        "properties": {
                            "query": {
                                "type": "string",
                                "description": "Query to search by",
                            },
                        },
                        "required": ["query"]
                    }
                }
            }
        }
    ]
}
```

------

**注意**  
设置工具选择参数时，您可能仍会看到模型输出文本或在原始工具选择之后执行顺序工具调用。建议您在此处设置停止序列以将输出限制为仅工具：  

```
“stopSequences”: [“</tool>”]
```
有关更多信息，请参阅 Amazon Bedrock API 指南中的 [InferenceConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_InferenceConfiguration.html)。