做网站团队,北京做网站最牛的公司,房产信息网显示已签约,京东云 安装wordpressChatGPT 基于海量的训练数据生成答案#xff0c;所以它无法回答训练数据中没有的信息或搜索信息
。人们希望 ChatGPT 具有对话以外的各种功能#xff0c;例如“我想管理我的待办事项列表”。 函数调用是对此类请求的响应。 通过使用函数调用#xff0c;ChatGPT 现在可以在生…
ChatGPT 基于海量的训练数据生成答案所以它无法回答训练数据中没有的信息或搜索信息
。人们希望 ChatGPT 具有对话以外的各种功能例如“我想管理我的待办事项列表”。 函数调用是对此类请求的响应。 通过使用函数调用ChatGPT 现在可以在生成答案时使用用户提供的函数
例如如果要添加一个查看天气的函数可以定义一个确定天气预报 API 的函数。下面是示意图 函数
我们定义了一个获取天气函数 。这是一个常规的python 函数。
def weather_function(location):match location:case 无锡 | wuxi:weather 晴天case 苏州| suzhou:weather 多云case 常州 | changzhou:weather 雨case _ :weather 不清楚weather_answer [{天气: weather}]return json.dumps(weather_answer)
例-1--openAI function calling
from openai import OpenAI
import json
client OpenAI(api_keysk-xxxxxx,base_urlhttps://api.chatanywhere.tech/v1
)
def weather_function(location):match location:case 无锡 | wuxi:weather 晴天case 苏州| suzhou:weather 多云case 常州 | changzhou:weather 雨case _ :weather 不清楚weather_answer [{天气: weather}]return json.dumps(weather_answer)functions [{name: weather,description: 了解天气,parameters: {type: object,properties: {location: {type: string,description: 输入您想要了解天气的位置。 示例东京,},},required: [location],},}]
messages [{role: system,content: You are a useful assistant.},{role: user,content: 无锡天气怎么样?},]
print(messages[1][content])
def role_function_conversation(message):response client.chat.completions.create(modelgpt-3.5-turbo-0613,messages message,temperature0,functions functions,function_callauto,)message response.choices[0].message.contentprint(message)completion client.chat.completions.create(modelgpt-3.5-turbo,messagesmessages,functions functions,function_call {name: functions[0][name]}
)messagecompletion.choices[0].message
if(message.function_call):function_name message.function_call.namearguments json.loads(message.function_call.arguments) if (function_name weather):weatherNowweather_function(locationarguments.get(location))messages.append(message)messages.append({role: function, name: weather, content: weatherNow})#print(messages)role_function_conversation(messages)
从上面的程序看功能调用被分成两段分别访问两次大模型第一次根据functions 模板获取函数的参数location第二次真正调用 weather_function函数。然后将调用的结果交给大模型生成输出。
例-2 langchain Agent方式
这个程序使用Langchain Agent 方式调用函数简约了许多。
import json
import os
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents.mrkl import prompt
os.environ[OPENAI_API_KEY] sk-xxxxx
os.environ[OPENAI_BASE_URL] https://api.chatanywhere.tech/v1
def weather_function(location):match location:case 无锡 | wuxi:weather 晴天case 苏州| suzhou:weather 多云case 常州 | changzhou:weather 雨case _ :weather 不清楚weather_answer [{天气: weather}]return json.dumps(weather_answer)
def lang_chain_agent(text):llm ChatOpenAI(model_namegpt-3.5-turbo,base_urlhttps://api.chatanywhere.tech/v1)tools [Tool(name Weather,funcweather_function,description输入你希望了解天气的位置例如 无锡,)]agent initialize_agent(tools,llm,agentzero-shot-react-description,agent_kwargsdict(suffixAnswer should be in chinese. prompt.SUFFIX), verboseTrue,return_intermediate_stepsTrue)response agent({input: text})return response
lang_chain_agent(常州天气如何) 例-3 langchain-functioncall方式
这个程序利用langchain 实现函数调用。
import os
import json
from langchain.schema import (HumanMessage,FunctionMessage
)
from langchain_openai import ChatOpenAI
os.environ[OPENAI_API_KEY] sk-xxxxxxxx
os.environ[OPENAI_BASE_URL] https://api.chatanywhere.tech/v1
def weather_function(location):match location:case 无锡 | wuxi:weather 晴天case 苏州| suzhou:weather 多云case 常州 | changzhou:weather 雨case _ :weather 不清楚weather_answer [{天气: weather}]return json.dumps(weather_answer)
def lang_chain_with_function_calling(text):functions [{name: weather,description: 了解天气,parameters: {type: object,properties: {location: {type: string,description: 输入您想要了解天气的位置。 示例东京,},},required: [location],},}]messages[HumanMessage(contenttext)]llm ChatOpenAI(model_namegpt-3.5-turbo,base_urlhttps://api.chatanywhere.tech/v1, temperature0)message llm.predict_messages(messages, functionsfunctions)if message.additional_kwargs:function_name message.additional_kwargs[function_call][name]arguments json.loads(message.additional_kwargs[function_call][arguments])function_response weather_function(locationarguments.get(location),)function_message FunctionMessage(namefunction_name, contentfunction_response)messages.append(function_message)second_response llm.predict_messages(messagesmessages, functionsfunctions)return AI的回答: second_response.contentelse:return AI的回答: message.content
print(lang_chain_with_function_calling(无锡的天气怎么样))
结束语
这里介绍了三种大模型函数调用的方法。还可以调用多个函数比如如果要使用大模型实现“如果天黑了就关上灯” 我觉得要调用两个函数
CheckDarkness 函数
判断是否天黑。
LightControl 函数
控制灯光。
下一次来研究怎么实现吧 文章转载自: http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn