

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

# 選擇工具
<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 選擇端點的使用案例使用「任意」工具選擇。這是要求模型傳回特定工具時很有幫助的範例之一。

```
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)。