本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
工具是向 Amazon Nova 提供外部功能的方法,例如 API 呼叫或程式碼函數。本節將介紹如何在使用 Amazon Nova 模型時定義和整合工具。
工具使用涉及三個高階步驟:
-
使用者查詢 - 您可以透過提供描述每個工具的功能和輸入要求的 JSON 結構描述來定義 Amazon Nova 可以使用的工具。
-
工具選擇 - 當使用者傳送訊息時,Amazon Nova 會對其進行分析,以判斷是否需要工具來產生回應。這稱為
Auto
工具選擇。如需詳細資訊,請參閱選擇工具。如果 Amazon Nova 識別合適的工具,它會「呼叫工具」並傳回工具的名稱和要使用的參數。身為開發人員,您負責根據模型的請求執行工具。這表示您需要撰寫程式碼來叫用工具的功能,並處理模型提供的輸入參數。
注意
如同所有 LLM 回應,Amazon Nova 可能會幻覺工具呼叫。開發人員必須負責驗證工具是否存在、輸入格式是否正確,且已具備適當的許可。
-
傳回結果 - 執行工具後,您必須以結構化格式將結果傳回 Amazon Nova。有效格式包括 JSON 或文字和影像的組合。這可讓 Amazon Nova 將工具的輸出納入對使用者的最終回應中。
如果在工具執行期間有任何錯誤,您可以在對 Amazon Nova 的工具回應中表示,允許 Amazon Nova 相應地調整其回應。
考慮一個簡單的計算工具範例:
工具呼叫工作流程的第一步是使用者查詢 Amazon Nova 以取得數學方程式的結果 - 10 倍 5。此查詢會與代表計算器的工具規格一起做為提示傳送至 Amazon Nova。
user_query = "10*5"
messages = [{
"role": "user",
"content": [{"text": user_query}]
}]
tool_config = {
"tools": [
{
"toolSpec": {
"name": "calculator", # Name of the tool
"description": "A calculator tool that can execute a math equation", # Concise description of the tool
"inputSchema": {
"json": {
"type": "object",
"properties": {
"equation": { # The name of the parameter
"type": "string", # parameter type: string/int/etc
"description": "The full equation to evaluate" # Helpful description of the parameter
}
},
"required": [ # List of all required parameters
"equation"
]
}
}
}
}
]
}
Amazon Nova 允許在調用和 Converse API 中使用工具,但對於完整功能廣度,我們建議使用 Converse API,並將使用此 API 的範例。