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.choices[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)

second_response.choices[0].message.role

复制代码
'assistant'

second_response.choices[0].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功能通过将用户的函数调用传递给模型来实现更复杂的交互和操作。自己的代码根据模型返回的函数调用列表,调用相应的函数,并将结果传递给模型,模型根据结果生成回复消息,实现了更灵活和动态的对话交互。

相关推荐
澳鹏Appen37 分钟前
数据集月度精选 | 高质量具身智能数据集:打开机器人“感知-决策-动作”闭环的钥匙
人工智能·机器人·具身智能
q***71012 小时前
开源模型应用落地-工具使用篇-Spring AI-Function Call(八)
人工智能·spring·开源
极限实验室2 小时前
Coco AI 参选 Gitee 2025 最受欢迎开源软件!您的每一票,都是对中国开源的硬核支持
人工智能·开源
secondyoung2 小时前
Mermaid流程图高效转换为图片方案
c语言·人工智能·windows·vscode·python·docker·流程图
iFlow_AI2 小时前
iFlow CLI Hooks 「从入门到实战」应用指南
开发语言·前端·javascript·人工智能·ai·iflow·iflow cli
Shang180989357263 小时前
THC63LVD1027D一款10位双链路LVDS信号中继器芯片,支持WUXGA分辨率视频数据传输THC63LVD1027支持30位数据通道方案
人工智能·考研·信息与通信·信号处理·thc63lvd1027d·thc63lvd1027
飞哥数智坊3 小时前
项目太大,AI无法理解?试试这3种思路
人工智能·ai编程
桜吹雪3 小时前
手搓一个简易Agent
前端·人工智能·后端
数字时代全景窗3 小时前
从App时代到智能体时代,如何打破“三堵墙”
人工智能·软件工程
weixin_469163693 小时前
金融科技项目管理方式在AI加持下发展方向之,需求分析精准化减少业务与技术偏差
人工智能·科技·金融·项目管理·需求管理