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

相关推荐
深蓝AI1 小时前
Postman 静默改装你的编码 Agent:一份 SEVERE issue 里的六条证据链
人工智能·程序员
QY_Research_5 小时前
2026房地产NFT平台创业机遇与落地指南分析报告
大数据·人工智能·物联网·机器人·制造
机器人零零壹6 小时前
技术白皮书:iRobotCAM如何解决机器人激光切割应用中的核心难点,从CAD离线编程到双目视觉定位与轨迹自适应提升复杂空间曲线切割的精度、效率与柔性化水平
人工智能·算法
电子元件小说家7 小时前
2026年四季度电感市场观察:AI算力与汽车电子驱动下的选型与验证建议智能硬件
人工智能·汽车
网易CodeWave-小码哥7 小时前
支持多模块 Java 微服务导入|CodeWave SDD V4.5.10 上线
人工智能
AI你一生一世8 小时前
不止于时序存储:DolphinDB 如何成为 AI 时代的工业智能底座
java·人工智能·struts·时序数据库·工业智能·dolphindb·ai基础设施
belldeep8 小时前
python:genesis-world, Genesis-Embodied-AI
人工智能·python·ai·genesis
怕浪猫8 小时前
小程序成长计划上新:10 亿 Token 现在能调 DeepSeek-v4.1-flash了
人工智能·aigc·ai编程
ss2738 小时前
为什么PostgreSQL适合AI场景?
数据库·人工智能·postgresql
七牛云行业应用9 小时前
DeepSeek Super-agent 与 Grok Bot:开源 Agent 架构和聊天助手有何区别
人工智能·ai·架构·ai编程