使用GPT大模型调用工具链

本文特指openai使用sdk的方式调用工具链。

安装openai

python 复制代码
pip install openai
export OPENAI_API_KEY="YOUR OPENAI KEY"

定义工具函数

python 复制代码
from openai import OpenAI
import json

client = OpenAI()
#工具函数
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"})

#工具函数的说明,传给sdk,让大模型理解工具的功能和调用方式
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"],
                },
            },
        }
]

大模型调用工具链

注意尽量保证调工具(传递了参数tools=tools)的prompt(messages中)不要有其它指令,否则大模型可能会出现幻觉,调用不存在的函数或者不回答其它指令(下面结果ChatCompletionMessage的content=None,就是没有回答问题,纯粹调链)。

tool_choice参数指定了调用工具的模式

  • 强制模型调用特定的函数,可以通过使用特定的函数名称设置tool_choice来实现。
  • 设置tool_choice:"none"来强制模型生成面向用户的消息。
  • 请注意,默认行为(tool_choice:"auto")是由模型自己决定是否调用函数

下面案例是一个指令中调用了多次工具,所以返回的结果也调用了多次。

python 复制代码
messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
def run_conversation():
    # Step 1: send the conversation and available functions to the model
    
    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
    print(response_message )
    return response_message 

run_conversation()
'''
执行结果
ChatCompletionMessage(
    content=None, 
    role='assistant', 
    function_call=None, 
    tool_calls=[
        ChatCompletionMessageToolCall(id='call_hoCM3KJJRQhkupaZR7gguZjr', function=Function(arguments='{"location": "San Francisco", "unit": "celsius"}', name='get_current_weather'), type='function'), 
        ChatCompletionMessageToolCall(id='call_OTiRmgF7F7AHx92RHnfa8pSl', function=Function(arguments='{"location": "Tokyo", "unit": "celsius"}', name='get_current_weather'), type='function'), 
        ChatCompletionMessageToolCall(id='call_zpIGNP5BuxpVVq2EUdSg8f1x', function=Function(arguments='{"location": "Paris", "unit": "celsius"}', name='get_current_weather'), 
        type='function')
    ]
)
'''

执行调用并汇总结果

注意下面汇总结果到messages时,注意这里的角色不是user也不是assistant,而是tool。

将上下文所有的消息全都汇总到message中,即可实现,模型调用工具拿到结果后,再汇总结果输出给用户。

python 复制代码
def main(response_message):
    tool_calls = response_message.tool_calls
    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)
            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", #注意这里的角色不是user也不是assistant,而是tool
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        #汇总并输出结果
        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
        return second_response
相关推荐
鸿芯微控科技6 小时前
MFC关断后还有流量怎么办?零流量、阀门泄漏、压差与Python分析
c++·python·mfc·质量流量控制器·关断泄漏·零流量测试
一个王同学6 小时前
从零到一 | CV转多模态大模型 | week22 | 实战项目-DocuMind-VL:基于 OCR 与 Qwen-VL 的文档多模态问答系统(二)
人工智能·深度学习·机器学习·计算机视觉·ocr
Canace6 小时前
笔记本都合上了,Claude 为什么还能在手机上执行电脑上装的技能?
前端·人工智能·ai编程
u0103055276 小时前
昇腾+AtomGit打造AI甘特图应用生产线
人工智能
美狐美颜SDK开放平台6 小时前
第三方美颜SDK接入教程:直播APP实现实时美颜、滤镜与美型效果的方法
人工智能·深度学习·音视频·sdk·美颜sdk·视频美颜sdk·直播app开发
乱世刀疤6 小时前
AI Weekly 8.3-8.9
人工智能
AI导出鸭6 小时前
怎么让豆包做表格?AI导出鸭苹果版将豆包输出的管道表格智能解析为二维结构,一键导出为Excel或Word标准表格。
人工智能·chatgpt·word·excel·ai导出鸭
霸道流氓气质8 小时前
普通办公电脑基于 Ollama 的本地 AI 能力技术文档
人工智能·电脑
点云-激光雷达-Slam-三维牙齿8 小时前
速度起飞 笔记本电脑6G显卡llama运行Qwen3.6 35BA3B MTP 大模型
人工智能·python·电脑·llama
言乐69 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame