本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
選擇工具
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 工具呼叫的預設行為是使用chain-of-thought進行工具選擇。使用預設行為或工具選擇 時
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。
叫用工具
傳回工具結果