選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

搭配 Amazon Nova 使用工具 (函數呼叫)

焦點模式
搭配 Amazon Nova 使用工具 (函數呼叫) - Amazon Nova

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

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

工具是向 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 相應地調整其回應。

考慮一個簡單的計算工具範例:

User query

工具呼叫工作流程的第一步是使用者查詢 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" ] } } } } ] }
Tool selection

Amazon Nova 使用工具的內容以及使用者提示,來判斷要使用的必要工具和必要的組態。這會在 API 回應中傳回。

{ "toolUse": { "toolUseId": "tooluse_u7XTryCSReawd9lXwljzHQ", "name": "calculator", "input": { "equation": "10*5" } } }

應用程式負責執行工具和存放結果。

def calculator(equation: str): return eval(equation) tool_result = calculator("10*5")
Return results

若要將工具的結果傳回 Amazon Nova,工具結果會包含在新的 API 請求中。請注意,工具使用 ID 與上一個回應中從 Amazon Nova 傳回的工具 ID 一致。

{ "toolResult": { "toolUseId": "tooluse_u7XTryCSReawd9lXwljzHQ", "content": [ { "json": { "result": "50" } } ], "status": "success" } }
  • Amazon Nova 將使用訊息的完整內容,包括初始使用者查詢、工具和工具使用結果,來判斷對使用者的最終回應。在此情況下,Amazon Nova 會回應使用者「10 倍 5 為 50」。

工具呼叫工作流程的第一步是使用者查詢 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 的範例。

其他參考

下一個主題:

定義工具

上一個主題:

自訂提示範例

在本頁面

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。