

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

# 調用工具
<a name="tool-use-invocation"></a>

如果 Amazon Nova 決定呼叫工具，則會在助理訊息中傳回工具使用區塊，且停止原因將為「tool\$1use」。該工具區塊將包含工具的名稱及其輸入。

**注意**  
為了提高工具呼叫的準確性，Amazon Nova 模型的預設行為是將思維鏈推理用於工具呼叫。思考過程將在助理訊息中提供給您，並包含在 <thinking> 標籤中。回應中可能會有多個工具呼叫和思考區塊，因此您的應用程式應該將此納入考量。  
如果工具選擇設定為 `any` 或 `tool`，這會覆寫思維鏈的行為，且回應只會包含必要的工具呼叫。

```
{
   "toolUse": 
    {
        "toolUseId": "tooluse_20Z9zl0BQWSXjFuLKdTJcA", 
        "name": "top_song", 
        "input": {
            "sign": "WZPZ"
        }
    }
}
```

若要實際呼叫工具，可以從訊息中擷取工具名稱和引數，然後應用程式就可以叫用它。

以下是如何處理工具呼叫的範例。

```
def get_top_song(sign):
    print(f"Getting the top song at {sign}")
    return ("Espresso", "Sabrina Carpenter")

stop_reason = response["stopReason"]

tool, song, artist = None, None, None
if stop_reason == "tool_use":
    thought_process = next(
        block["text"]
        for block in response["output"]["message"]["content"]
        if "text" in block
    )

    print(thought_process)

    tool = next(
        block["toolUse"]
        for block in response["output"]["message"]["content"]
        if "toolUse" in block
    )

    if tool["name"] == "top_song":
        song, artist = get_top_song(tool["input"]["sign"])
```

當您定義和調用工具時，務必謹記安全性。像 Amazon Nova 這樣的 LLMs 無法存取工作階段詳細資訊，因此在必要時應在叫用工具之前驗證許可。依賴工作階段中的使用者詳細資訊，而不是增強提示詞並允許 Amazon Nova 將其注入工具呼叫。