W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Function Calling 讓模型能夠調(diào)用外部工具,來增強自身能力。
當前版本 deepseek-chat 模型 Function Calling 功能效果不穩(wěn)定,會出現(xiàn)循環(huán)調(diào)用、空回復的情況。我們正在積極修復中,預計將在下一個版本中得到修復。
這里以獲取用戶當前位置的天氣信息為例,展示了使用 Function Calling 的完整 Python 代碼。
Function Calling 的具體 API 格式請參考對話補全文檔。
from openai import OpenAI
def send_messages(messages):
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
tools=tools
)
return response.choices[0].message
client = OpenAI(
api_key="<your api key>",
base_url="https://api.deepseek.com",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"]
},
}
},
]
messages = [{"role": "user", "content": "How's the weather in Hangzhou?"}]
message = send_messages(messages)
print(f"User>\t {messages[0]['content']}")
tool = message.tool_calls[0]
messages.append(message)
messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"})
message = send_messages(messages)
print(f"Model>\t {message.content}")
這個例子的執(zhí)行流程如下:
注:上述代碼中 get_weather 函數(shù)功能需由用戶提供,模型本身不執(zhí)行具體函數(shù)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: