W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Function Calling 讓模型能夠調(diào)用外部工具,來增強(qiáng)自身能力。
當(dāng)前版本 deepseek-chat 模型 Function Calling 功能效果不穩(wěn)定,會(huì)出現(xiàn)循環(huán)調(diào)用、空回復(fù)的情況。我們正在積極修復(fù)中,預(yù)計(jì)將在下一個(gè)版本中得到修復(fù)。
這里以獲取用戶當(dāng)前位置的天氣信息為例,展示了使用 Function Calling 的完整 Python 代碼。
Function Calling 的具體 API 格式請(qǐng)參考對(duì)話補(bǔ)全文檔。
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}")
這個(gè)例子的執(zhí)行流程如下:
注:上述代碼中 get_weather 函數(shù)功能需由用戶提供,模型本身不執(zhí)行具體函數(shù)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: