DeepSeek Function Calling

2025-02-20 69
DeepSeek

类型:人工智能

简介:一款基于深度学习和自然语言处理技术的产品,人气赶超ChatGPT。

DeepSeek Function Calling功能允许模型调用外部工具,从而增强其能力,执行一些超出模型本身的任务。

提示:当前版本的 Deepseek-Chat 模型的 Function Calling 功能效果不稳定,可能会出现循环调用或空回复等问题。我们正在积极进行修复,预计下一个版本将解决这些问题。

以下是一个示例代码,展示如何使用 Function Calling 获取用户当前位置的天气信息。

一、Function Calling示例代码

假设我们想获取用户询问的某个地点的天气情况,以下是使用 Function Calling 的完整 Python 代码:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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}")
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}")
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执行流程

1、用户提问当前天气情况,例如:“Hangzhou 的天气怎么样?”

2、模型返回调用函数 get_weather({location: ‘Hangzhou’}) 的请求。

3、用户调用 get_weather 函数,提供必要的参数(例如,返回 Hangzhou 的天气数据),并传回模型。

4、模型最终返回自然语言的响应,例如:“Hangzhou 当前的温度是 24°C。”

说明:

  • 在上述代码中,get_weather 函数是用户提供的功能,模型本身并不执行该函数;
  • 这段代码使用了 Function Calling 功能,通过外部工具提供的天气信息来增强模型的回答能力。
  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。