本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
如果 Amazon Nova 決定呼叫工具,工具使用區塊將作為助理訊息的一部分傳回,且停止原因將為「tool_use」。工具區塊將包含工具的名稱及其輸入。
注意
為了提高工具呼叫的準確性,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 將其插入工具呼叫。