chatGPT的Function calling示例

python 复制代码
import json
import os
from openai import OpenAI

client = OpenAI(
    api_key = os.getenv("OPENAI_API_KEY"),
)

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})
python 复制代码
# Step 1: send the conversation and available functions to the model
messages = [{"role": "user", 
             "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        },
    }
]       

response = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    tools=tools,
    tool_choice="auto",  # auto is default, but we'll be explicit
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
python 复制代码
# Step 2: check if the model wanted to call a function
global second_response 
if tool_calls:
    
    # Step 3: call the function
    # Note: the JSON response may not always be valid; be sure to handle errors
    available_functions = {
        "get_current_weather": get_current_weather,
    }  # only one function in this example, but you can have multiple
    messages.append(response_message)  # extend conversation with assistant's reply
    
    # Step 4: send the info for each function call and function response to the model
    for tool_call in tool_calls:
        function_name = tool_call.function.name
        function_to_call = available_functions[function_name] # 确定要调用的函数名
        function_args = json.loads(tool_call.function.arguments) # 从llm的返回对象中,获取调用函数的参数
        function_response = function_to_call(
            location=function_args.get("location"),
            unit=function_args.get("unit"),
        )
        messages.append(
            {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": function_response,
            }
        )  # extend conversation with function response

    #再次调用模型,将message对象给大模型
    second_response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=messages,
    )  # get a new response from the model where it can see the function response

查看执行结果:

second_response

复制代码
ChatCompletion(id='chatcmpl-8aJh1gWOluIGMlaGqYkCrcRqCpuM9', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content="Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C.", role='assistant', function_call=None, tool_calls=None), logprobs=None)], created=1703666199, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint='fp_772e8125bb', usage=CompletionUsage(completion_tokens=29, prompt_tokens=169, total_tokens=198))

返回了一堆

second_response.choices0.message

复制代码
ChatCompletionMessage(content="Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C.", role='assistant', function_call=None, tool_calls=None)

second_response.choices0.message.role

复制代码
'assistant'

second_response.choices0.message.content

复制代码
"Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C."

可看到大模型又重新组织了语言,输出给用户

chatGPT的Function calling功能允许用户通过消息和模型进行交互,并根据用户提供的函数调用来获取所需的数据或执行特定的操作。下面是一个完整的例子:

  1. 用户发送一条消息给模型,包含问题和请求的函数调用,例如:"我需要计算两个数字的和。"
  2. 自己的程序接收到消息后,解析函数调用,找到对应的函数,并执行它。在这个例子中,程序会调用一个名为 calculate_sum 的函数,计算两个数字的和。
  3. 程序获取到计算结果后,将结果返回给模型。
  4. 模型接收到结果后,使用语言模型重新组织语言,生成回复消息,并将消息返回给用户。

整个过程至少需要两次交互:

  • 第一次交互:用户向模型提问,并告知需要调用的函数,例如:"我需要计算两个数字的和。"
  • 第二次交互:模型返回给自己的代码调用的函数列表,自己的代码根据函数列表,执行对应的函数,并将结果返回给模型。

在第二次交互中,自己的代码可以根据模型返回的函数列表,选择合适的函数,并将函数所需的参数传递给它。然后再将函数执行的结果返回给模型进行处理。

总结而言,Function calling功能通过将用户的函数调用传递给模型来实现更复杂的交互和操作。自己的代码根据模型返回的函数调用列表,调用相应的函数,并将结果传递给模型,模型根据结果生成回复消息,实现了更灵活和动态的对话交互。

相关推荐
霸道流氓气质几秒前
Spring AI 多租户隔离方案
java·人工智能·spring
ACP广源盛139246256732 分钟前
M6/M5 Pro Mac mini 端侧 AI 爆发@ACP#YLB3118 存储扩展芯片在本地 AI 服务中的机会与落地场景
大数据·网络·数据库·人工智能·嵌入式硬件·macos
Rain5097 分钟前
谁动了我的 URL?——记一次微前端“灵异 Bug“的排查实录
前端·vue.js·人工智能·前端框架·bug·ai编程
外域速览10 分钟前
OpenAI Astra 跨过「高危红线」、李飞飞世界模型 Atlas 落地:AI 行业进入「安全与落地」双拐点
人工智能·安全
Mr数据杨12 分钟前
莫斯科公寓价格预测实战 从 Kaggle 房价回归到可落地估值流程
人工智能·数据分析·kaggle竞赛
故七月14 分钟前
产业观察|从 9 月行业数据看西南市场 GEO 落地现状与发展路径
大数据·人工智能
丶浅行DE时光17 分钟前
管道式电磁流量计选型指南 介质腐蚀与工况适配方案推荐
大数据·网络·人工智能·科技·推荐算法
电子科技圈17 分钟前
芯科科技在IOTE 2026上以全栈无线连接与边缘AI展现其在智能网联领域的卓越领导力
人工智能·科技
Ro Jace18 分钟前
基于深度学习的图像处理方法
图像处理·人工智能·深度学习
Y_Mathison19 分钟前
Agent安全护栏设计:权限控制、对抗鲁棒性与人工确认环
人工智能·ai