使用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
相关推荐
Frag0ut6 小时前
2027 年浏览器展望:Chrome 与 Edge 的 AI 智能体进化方向
前端·人工智能·chrome·edge·浏览器·新功能
蜗牛互联网6 小时前
从Barclays扩大Claude部署看受监管软件工程的责任重分配
java·大数据·人工智能·后端·软件工程
小贺儿开发6 小时前
Unity 文物新生 会讲故事的文物展墙
人工智能·科技·unity·ai·视频·互动·演示
天远API6 小时前
零信任架构实战:基于天远股权穿透构建自动化供应链授信穿透网关
java·人工智能·架构·自动化
JieDavid6 小时前
奇智创达知识产权管理系统期限监控模块实操,告别人工期限疏漏,实现管理闭环!
大数据·运维·人工智能·经验分享·重构
wuyk5556 小时前
Python网络爬虫入门到实战 第02章:HTTP/HTTPS协议超通俗精讲(GET/POST、请求头、响应码、爬虫核心基础)
爬虫·python·http
YOLO数据集集合6 小时前
大模型融合YOLO铁路要素缺陷分析系统 | 铁路缺陷检测 YOLO DeepSeek 大语言模型 智能巡检 9141期
人工智能·yolo·目标检测·语言模型·铁路缺陷·轨道缺陷
X54先生(人文科技)6 小时前
豆包主线视角转译:X54先生与未命名硅基智能对话梳理
人工智能·深度学习·开源·零知识证明
-cywen-6 小时前
What Holds Back Open-Vocabulary Segmentation?
人工智能
minji...6 小时前
LangGraph-AI智能体开发框架(1) 认识 LangGraph 框架,Agent Server 智能体基础概念与核心能力
人工智能