什么是Function Calling
function calling 让模型能够调用外部工具,来增强自身的能力
样例代码
- 安装依赖
-
pip install openai
-
- demo
-
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="sk-fa13e5a983e548d9a2a7c8885ef50935", base_url="https://api.deepseek.com" ) # get_weather 函数需要自己实现 tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get weather of a 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, Zhejiang?"}] message = send_messages(messages) print(f"User>\t {messages[0]['content']}") tool = message.tool_calls[0] messages.append(message) # 模拟get_weather 函数结果加入messages送入模型 messages.append({"role":"tool","tool_call_id":tool.id, "content":"24"}) message = send_messages(messages) print(f"Model>\t {message.content}") # 执行流程 # 用户:询问现在的天气 # 模型:返回 function get_weather({location: 'Hangzhou'}) # 用户:调用 function get_weather({location: 'Hangzhou'}),并传给模型。 # 模型:返回自然语言,"The current temperature in Hangzhou is 24°C."
-

1248

被折叠的 条评论
为什么被折叠?



