LangChain的函数,工具和代理(一):OpenAI的函数调用

一、什么是函数调用功能

几个月前OpenAI官方发布了其API的函数调用功能(Function calling), 在 API 调用中,您可以描述函数,并让模型智能地选择输出包含调用一个或多个函数的参数的 JSON 对象。API函数“ChatCompletion” 虽然不会实际调用该函数;但是模型会生成这些基于函数参数的JSON对象,您可以使用它来调用代码中的实际函数。

也就是说当用户和ChatGPT对话的过程中需要调用某些外部的函数或者API时,我们可以让ChatGPT生成调用外部函数所需的参数,然后我们再使用这些参数再去实际的调用外部函数,目前OpenAl 对 gpt-3.5-turbo-0613 和 gpt-4-0613 模型进行了微调,使它们具备了以下函数调用功能:

1. 接受额外的参数,用户可以通过这些参数传入函数的描述。
2. 如果相关,则返回要使用的函数的名称,以及带有适当输入参数的 JSON 对象。

二,如何实现OpenAI的函数调用功能

在实现OpenAI的函数调用功能之前,我们先定义一个外部函数,当用户和ChatGPT对话时,ChatGPT会自动判断是否需要调用外部函数,当需要调用外部函数时ChatGPT会返回调用函数的json对象给用户:

import json

# 查询天气的模拟函数示例
# 在生产中,这可能是您的后端 API 或外部 API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    weather_info = {
        "location": location, #城市
        "temperature": "72", # 温度
        "unit": unit, #温度单位
        "forecast": ["sunny", "windy"], #天气情况
    }
    return json.dumps(weather_info)

这里我们定义一个外部函数get_current_weather,他用来查询特定城市的天气情况,并返回一个jons对象作为查询结果。接下来我们需要定义一个该函数的描述对象,该描述对象后面会作为参数传递给ChatGPT:

#函数描述对象
functions = [
    {
        "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"],
        },
    }
]

下面我们来说明一下函数描述对象的主要成员:

  • name: 外部函数名称如get_current_weather
  • description:外部函数功能的描述
  • parameters:外部函数的参数集
  • parameters-type:外部函数的参数集的类型
  • properties:外部函数的具体参数集
  • location:具体的外部函数的参数
  • location-type:外部函数的参数的类型
  • location-description:外部函数的参数的描述
  • unit:具体的外部函数的参数
  • unit-type:外部函数的参数的类型
  • enum:外部函数的参数的枚举值
  • required:必填的参数

这里我们生成了一个外部函数的描述对象,该描述对象会告诉ChatGPT该外部函数的作用,以及我们需要在恰当的时候来调用该函数,至于什么时候才是“恰当的时候”这需要由ChatGPT根据用户对话的上下文来判断。接下来我们向ChatGPT询问一个关于天气的问题:

import openai
openai.api_key = "XXXXXXXXX"

messages = [
    {
        "role": "user",
        "content": "上海的天气怎么样?"
    }
]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions
)

print(response)

这里我们向ChatGPT提出了关于天气的问题:“上海的天气怎么样?”, 从ChatGPT的返回结果中我们看到"function_call",这告诉我们接下来我们该调用外部函数了,同时ChatGPT还返回了调用外部函数的参数location和unit,以及所需调用的外部函数名:get_current_weather,有意思的是这里返回的unit为“celsius”即摄氏度而非美国使用的"fahrenheit(华氏度)", 这似乎说明ChatGPT知道中国使用摄氏度作为温度的单位,下面我们询问一下美国城市的天气:

messages = [
    {
        "role": "user",
        "content": "What's the weather like in Boston?" #波士顿 的天气怎么样?
    }
]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions
)

print(response)

这里我们用英语询问了美国城市波士顿的天气情况,从ChatGPT的返回结果中我们看到arguments中只包含了location,而没有包含unit, 而在我们的外部函数get_current_weather中unit为非必填参数,它有一个默认值为:unit="fahrenheit",因此在实际调用外部函数时我们只需将chatgpt返回结果中的arguments中取出对应的参数然后传递给外部函数即可,接下来我们从ChatGPT的返回结果中获取参数来实际调用外部函数get_current_weather:

args = json.loads(response_message["function_call"]["arguments"])
result=get_current_weather(args)
print(result)

 接下来我们来测试一下ChatGPT能否准确识别何时该调用外部函数,下面我们会对ChatGPT发送一个简单的问候语:hi,  当Chatgpt收到该问候语时不应该触发函数调用功能:

messages = [
    {
        "role": "user",
        "content": "hi!",
    }
]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
)

print(response)

从上面的chatgpt的返回结果中我们看到不存在先前的“function_call"内容即没有生成外部函数的调用参数,这说明此时我们不需要调用外部函数。

三、设置OPAI API的默认参数

openai的API函数ChatCompletion.create中存在一个function_call的参数,该参数的默认值为“auto”即让模型自己来选择是否需要调用外部函数:

messages = [
    {
        "role": "user",
        "content": "hi!",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
    function_call="auto",
)
print(response)

上面我们在openai.ChatCompletion.create的方法中加入了function_call="auto",意思是让模型根据上下文来确定是否调用外部函数,我们看到当我们向ChatGPT打招呼时,如输入“hi”时 ,chatgpt的返回结果中没有“function_call”的内容。这说明ChatGPT知道此时不应该调用外部函数。

messages = [
    {
        "role": "user",
        "content": "hi!",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
    function_call="none",#禁止调用外部函数
)
print(response)

上面当我们将function_cal设置为"none"时(即禁止chatGPT调用外部函数),chatGPT的返回结果中也不会出现“function_call”的内容。

messages = [
    {
        "role": "user",
        "content": "What's the weather in Boston?",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
    function_call="none", #禁止调用外部函数
)
print(response)

在上面的代码中我们向ChatGPT询问了波士顿的天气,但是我们设置了function_call="none",也就是说虽然我们询问了波士顿的天气情况,但我们却禁止chatgpt调用外部函数,从chatgpt的返回结果中我们看到仍然没有“function_cal”的相关内容。

下面我们设置chatgpt强制调用外部函数,看看会发生上面情况:

messages = [
    {
        "role": "user",
        "content": "hi!",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
    function_call={"name": "get_current_weather"},#强制调用外部函数
)
print(response)

在上面的代码中我们在 ChatCompletion.create中设置了function_call={"name": "get_current_weather"}意思是让chatgpt强制生成调用get_current_weather函数的参数,但是我们向chatgpt发送的用户消息却是:hi!, 这时会让chatgpt产生困惑,因为用户消息中没有有关询问天气的内容,但是却要强制chatgpt去生成外部函数的调用参数,所以在chatgpt的返回结果中function_call中的arguments中给出了一个随机的location:San Francisco,CA。

下面我们向chatgpt询问波士顿的天气,并且让chatgpt强制调用get_current_weather,看看会发生什么情况:

messages = [
    {
        "role": "user",
        "content": "What's the weather like in Boston!",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    functions=functions,
    function_call={"name": "get_current_weather"}, #强制调用外部函数
)
print(response)

从上面的chatgpt的返回结果中我们看到了“function_call”中的内容。这说明只要我们设置了chatgpt强制指定了外部调用函数时,它总会生成相应的函数参数。

四、外部函数的调用结果的应用

上面我们让chatgpt来判断是否应该调用外部函数,并且让chatgpt返回了调用外部函数的参数,接下来我们要做的是用chatgpt提供的参数去实际调用外部函数,并将外部函数的返回结果再喂给chatgpt,这样做的目的是让chatgpt来汇总所有的信息并产生最终对用户友好的返回信息。

#整合chatgpt的返回结果
messages.append(response["choices"][0]["message"])
#从chatgpt的返回结果中获取外部函数的调用参数
args = json.loads(response["choices"][0]["message"]['function_call']['arguments'])
#调用外部函数
observation = get_current_weather(args)

messages.append(
        {
            "role": "function",
            "name": "get_current_weather",
            "content": observation, #外部函数的返回结果
        }
)

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=messages,
)
print(response)

 这里我们看到ChatGPT最终返回了一个非常友好的回复,该回复是在外部函数调用结果的基础上经过整理后得到的。

五、关于token统计

我们知道chatgpt的API是通过token来收费的,这里我们在使用chatgpt的函数调用功能时我们创建了一个函数描述对象functions,因此functions也会作为上下文的一部分被统计token数,下面我们去掉ChatCompletion.create中的functions和function_call这两个参数看看最后chatgpt返回的总的token数是多少:

messages = [
    {
        "role": "user",
        "content": "What's the weather like in Boston!",
    }
]
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-1106",
    messages=messages
)
print(response)

从上面的返回结果中我们看到当我们去掉了ChatCompletion.create中的functions和function_call这两个参数时,总token数为48,而先前的总token数为99,这说明外部函数描述对象functions被统计了token数。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-派神-

感谢您慷慨解囊,我会更加努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值