GPT重磅升级,强悍的大脑长出了强健的胳膊和腿
2023年6月,OpenAI模型重磅升级,支持调用本地API了,强悍的大脑不但有嘴,还要长出手和脚。看到这个升级我有2个疑问:
chatgpt中文版 http://chatgpt.guige.xyz
OpenAI怎样在服务端调用本地的API。
原来LangChain就已支持了Tool调用,这次升级和LangChain的Agent的action有甚么区分。
带着这2个问题,我看了官方文档后实践了一把,完全弄清楚了以上疑问,先说结论。
OpenAI不会在服务端调用本地API,只会在适合的时机告知你需要调用API,并给你调用参数,需要在本地履行调用。你需要把调用结果再发给OpenAI,它会结合你的返回结果来解决问题。
LangChain的工具声明都是通过Prompt实现,OpenAI的API声明提供了专用的Functions参数,鲁棒性会更强。
先做一个小实验,我们用高德API包装一个本地查询当地天气的API,让GPT来调用。
import requests
def get_current_weather(para):
#获得当天的天气预报
city = para["city"]
url = f'https://restapi.amap.com/v3/weather/weatherInfo?parameters'
params = {
'key':get_gaode_key(),#填入自己的高德API
'city': city,
'extensions': 'base',
'output': 'JSON',
'extensions':'all'
}
response = requests.get(url, params=params)
data = response.json()
returndata['forecasts'][0]['casts'][0]
调用返回当天的天气预报情况,返回示例以下:
print(get_current_weather({"city":"北京"}))
{'date':'2023-06⑴7','week':'6','dayweather':'多云','nightweather':'晴','daytemp':'38','nighttemp':'22','daywind':'东南','nightwind':'东南','daypower':'≤3','nightpower':'≤3','daytemp_float':'38.0','nighttemp_float':'22.0'}
依照OpenAI的说明,定义一下这个API
functions=[{"name":"get_current_weather","description":"获得一个城市确当前天气","parameters":{"type":"object","properties":{"city":{"type":"string","description":"城市名称"}},"required":["city"]}},]
调用最新升级的gpt⑶.5-turbo-0613模型,增加functions参数
import openai
import json
defget_completion_from_messages(messages,functions,model="gpt⑶.5-turbo-0613",temperature=0):
print("-----------------------------------------\n输入messages:"+str(json.dumps(messages, indent=1, ensure_ascii=False)))
print("输入functions:"+str(json.dumps(functions, indent=1, ensure_ascii=False)))
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
functions=functions
)
print("输出:"+json.dumps(response, indent=1, ensure_ascii=False))
return response.choices[0]
调用GPT,当调用返回缘由为function_call时,根据调用唆使,调用本地天气查询API,并将查询结果返回GPT再次调用
#向GPT发问
def ask_gpt(prompt,functions):
#拼接消息
messages = [{"role": "user", "content": prompt}]
#第一轮调用GPT
response = get_completion_from_messages(messages,functions)
#如果返回终止缘由是API调用,本地履行
if response['finish_reason'] == "function_call":
arguments = response["message"]["function_call"]["arguments"]
function = response["message"]["function_call"]["name"]
result = eval(f"{function}({arguments})")#根据API名称和参数动态调用本地接口
#把GPT的调用指令和API返回结果再次发送给GPT
messages.append(response["message"])
messages.append( {"role": "function", "name": "get_current_weather", "content":str(result)})
response = get_completion_from_messages(messages,functions)
print(response["message"]["content"])
问GPT“我在成都,今天出门需要带伞吗”
get_completion("我在成都,今天出门要带伞吗",functions)
输入参数:
输入messages:[{"role":"user","content":"我在成都,今天出门要带伞吗"}]输入functions:[{"name":"get_current_weather","description":"获得一个城市确当前天气","parameters":{"type":"object","properties":{"city":{"type":"string","description":"城市名称"}},"required":["city"]}}]
输出返回提示我们要调用get_current_weather,输入参数是成都。
输出:{"id":"chatcmpl-xxx","object":"chat.completion","created":1687006874,"model":"gpt⑶.5-turbo-0613","choices":[{"index":0,"message":{"role":"assistant","content":null,"function_call":{"name":"get_current_weather","arguments":"{\n\"city\":\"成都\"\n}"}},"finish_reason":"function_call"}]}}
以下几句代码,履行调用天气查询接口。
ifresponse['finish_reason']=="function_call":arguments=response["message"]["function_call"]["arguments"]function=response["message"]["function_call"]["name"]#根据API名称和参数动态调用本地接口result=eval(f"{function}({arguments})")
再次要求GPT,入参数以下
print(get_current_weather({"city":"北京"}))0
终究输出结果
print(get_current_weather({"city":"北京"}))1
我们在增加一个发送邮件的API,看调用结果
print(get_current_weather({"city":"北京"}))2
print(get_current_weather({"city":"北京"}))3
print(get_current_weather({"city":"北京"}))4
print(get_current_weather({"city":"北京"}))5
print(get_current_weather({"city":"北京"}))6
完全代码以下:
print(get_current_weather({"city":"北京"}))7
桂.哥.网.络www.guIgege.cn
本文来源于chatgptplus账号购买平台,转载请注明出处:https://chatgpt.guigege.cn/jiaocheng/29134.html 咨询请加VX:muhuanidc