当前位置: 首页 > news >正文

西安高端网站制作公司商业网站模板制作与开发

西安高端网站制作公司,商业网站模板制作与开发,支付宝网站开发,奢做品二手 哪个网站好ChatGPT 基于海量的训练数据生成答案,所以它无法回答训练数据中没有的信息或搜索信息 。人们希望 ChatGPT 具有对话以外的各种功能,例如“我想管理我的待办事项列表”。 函数调用是对此类请求的响应。 通过使用函数调用,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_key="sk-xxxxxx",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)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(model="gpt-3.5-turbo-0613",messages = message,temperature=0,functions= functions,function_call="auto",)message = response.choices[0].message.contentprint(message)completion = client.chat.completions.create(model="gpt-3.5-turbo",messages=messages,functions = functions,function_call = {"name": functions[0]["name"]}
)message=completion.choices[0].message
if(message.function_call):function_name = message.function_call.namearguments = json.loads(message.function_call.arguments)    if (function_name == "weather"):weatherNow=weather_function(location=arguments.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_name="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1")tools = [Tool(name = "Weather",func=weather_function,description="输入你希望了解天气的位置,例如 无锡",)]agent = initialize_agent(tools,llm,agent="zero-shot-react-description",agent_kwargs=dict(suffix='Answer should be in chinese.' + prompt.SUFFIX), verbose=True,return_intermediate_steps=True)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(content=text)]llm = ChatOpenAI(model_name="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1", temperature=0)message = llm.predict_messages(messages, functions=functions)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(location=arguments.get("location"),)function_message = FunctionMessage(name=function_name, content=function_response)messages.append(function_message)second_response = llm.predict_messages(messages=messages, functions=functions)return "AI的回答: " + second_response.contentelse:return "AI的回答: " + message.content
print(lang_chain_with_function_calling("无锡的天气怎么样?"))

结束语

这里介绍了三种大模型函数调用的方法。还可以调用多个函数,比如如果要使用大模型实现“如果天黑了,就关上灯” ,我觉得要调用两个函数

CheckDarkness 函数

判断是否天黑。

LightControl 函数

控制灯光。

下一次来研究怎么实现吧!

http://www.yayakq.cn/news/685804/

相关文章:

  • 网站域名区别重庆网站建设公司魁网
  • 外贸网站优化舟山手机网站建设
  • 电子商务网站建设实验指导有没有类似一起做网店的网站
  • dreamwear做网站个人网站名可以和别人一样吗
  • 自己做的网站如何放到微信网站建设有掏钱么
  • 游戏类网站怎么做影院网站模板
  • 公司两个网站如何都备案国内好的seo
  • 网站项目进度赣县区建设局网站
  • 淘宝运营模式南宁有名的seo费用
  • 微信聊天记录删除了怎么恢复广州seo全网营销
  • 网站规划开发前景建网站要多少钱建一个网络平台需要多少钱
  • 网站中弹出广告怎么做简述电子商务网站建设的主要步骤
  • 镇江网站建设教程昆明网站运营
  • 网站的二级目录怎么做域名抢注网站是怎么
  • 备案 个人网站名称计算机专业主要学什么软件
  • 网站备案相机中山住房和建设局网站
  • 北京P2P公司网站建设做公司网站的模板
  • 邯郸网站建设最新报价深圳 学习网站
  • 台州网站建设公司江西建筑培训网
  • 企业网站运营python做软件界面
  • 株洲做网站建设网站收银系统建设
  • 投资担保网站建设wordpress 橘子皮模板
  • 企业做网站etp和源程序注册安全工程师报名入口官网
  • 1核2g 做网站西安企业网站设计制作
  • 广东水利建设与管理信息网站android sdk
  • vps里面设置了一下读取和写入网站无法显示了哈尔滨网站优化流程
  • 类似建设通的网站朋友圈广告推广文字
  • 公司网站在国外打开很慢使用cdn好还是国外租用服务器好做o2o网站需要多少钱
  • 做婚礼效果图的网站有哪些百度小程序关键词优化
  • 做内贸的网站wordpress盒子