
類(lèi)型:人工智能
簡(jiǎn)介:一款基于深度學(xué)習(xí)和自然語(yǔ)言處理技術(shù)的產(chǎn)品,人氣趕超ChatGPT。
DeepSeek Function Calling功能允許模型調(diào)用外部工具,從而增強(qiáng)其能力,執(zhí)行一些超出模型本身的任務(wù)。
提示:當(dāng)前版本的 Deepseek-Chat 模型的 Function Calling 功能效果不穩(wěn)定,可能會(huì)出現(xiàn)循環(huán)調(diào)用或空回復(fù)等問(wèn)題。我們正在積極進(jìn)行修復(fù),預(yù)計(jì)下一個(gè)版本將解決這些問(wèn)題。
以下是一個(gè)示例代碼,展示如何使用 Function Calling 獲取用戶(hù)當(dāng)前位置的天氣信息。
一、Function Calling示例代碼
假設(shè)我們想獲取用戶(hù)詢(xún)問(wèn)的某個(gè)地點(diǎn)的天氣情況,以下是使用 Function Calling 的完整 Python 代碼:
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 should 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}")
二、Function Calling執(zhí)行流程
1、用戶(hù)提問(wèn)當(dāng)前天氣情況,例如:“Hangzhou 的天氣怎么樣?”
2、模型返回調(diào)用函數(shù) get_weather({location: ‘Hangzhou’}) 的請(qǐng)求。
3、用戶(hù)調(diào)用 get_weather 函數(shù),提供必要的參數(shù)(例如,返回 Hangzhou 的天氣數(shù)據(jù)),并傳回模型。
4、模型最終返回自然語(yǔ)言的響應(yīng),例如:“Hangzhou 當(dāng)前的溫度是 24°C?!?/p>
說(shuō)明:
- 在上述代碼中,get_weather 函數(shù)是用戶(hù)提供的功能,模型本身并不執(zhí)行該函數(shù);
- 這段代碼使用了 Function Calling 功能,通過(guò)外部工具提供的天氣信息來(lái)增強(qiáng)模型的回答能力。