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

相关推荐
G皮T3 小时前
【人工智能】ChatGPT、DeepSeek-R1、DeepSeek-V3 辨析
人工智能·chatgpt·llm·大语言模型·deepseek·deepseek-v3·deepseek-r1
九年义务漏网鲨鱼3 小时前
【大模型学习 | MINIGPT-4原理】
人工智能·深度学习·学习·语言模型·多模态
元宇宙时间3 小时前
Playfun即将开启大型Web3线上活动,打造沉浸式GameFi体验生态
人工智能·去中心化·区块链
开发者工具分享3 小时前
文本音频违规识别工具排行榜(12选)
人工智能·音视频
产品经理独孤虾4 小时前
人工智能大模型如何助力电商产品经理打造高效的商品工业属性画像
人工智能·机器学习·ai·大模型·产品经理·商品画像·商品工业属性
老任与码4 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
蹦蹦跳跳真可爱5894 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
雷羿 LexChien4 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
两棵雪松5 小时前
如何通过向量化技术比较两段文本是否相似?
人工智能
heart000_15 小时前
128K 长文本处理实战:腾讯混元 + 云函数 SCF 构建 PDF 摘要生成器
人工智能·自然语言处理·pdf