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

怎么做网站xml地图商旅平台app下载

怎么做网站xml地图,商旅平台app下载,家装设计学习,网站的配色方案1 函数调用 虽然大模型能解决很多问题,但大模型并不能知晓一切。比如,大模型不知道最新消息(GPT-3.5 的知识截至 2021年9月,GPT-4 是 2023 年12月)。另外,大模型没有“真逻辑”。它表现出的逻辑、推理,是训练文本的统计…

1 函数调用

  虽然大模型能解决很多问题,但大模型并不能知晓一切。比如,大模型不知道最新消息(GPT-3.5 的知识截至 2021年9月,GPT-4 是 2023 年12月)。另外,大模型没有“真逻辑”。它表现出的逻辑、推理,是训练文本的统计规律,而不是真正的逻辑,所以有幻觉。所以大模型需要连接真实世界,并对接真逻辑系统。这就需要用到“函数调用”。
  函数调用(Function Calling)可以使LLM具有与外部API交互的能力。让用户能够使用高效的外部工具、与外部API进行交互。其使用机制如下:
在这里插入图片描述
关于function calling,有以下几点需要注意:

  • 在最新版本的OpenAI API中,可以使用tools参数对函数进行描述。并让大模型智能地选择输出包含函数参数的JSON对象来调用一个或多个函数。
  • 最新的GPT模型(gpt-3.5-turbo-0125 and gpt-4-turbo-preview)可以自动检测何时应该调用函数(还有一个相关的参数tool_choice,一般不用自己设置),还可以输出更加符合函数签名的JSON串。
  • GPT不负责调用和执行外部函数,需要用户自己完成。

2 使用GPT进行函数调用

  在使用GPT模型进行函数调用时,需要用到tools参数进行函数声明,关于该参数有以下几点需要说明:

  • 该参数可以接收一系列JSON组成的array,一个函数对应一个JSON,当前最多可以接受128个函数。
  • JSON串的结构如下:
    在这里插入图片描述
    其中parameters参数的写法要遵循JSON Schema格式,具体可以参考:https://blog.csdn.net/yeshang_lady/article/details/137146295

2.1 使用函数调用完成加法计算

  大模型可以做加法是因为大模型记住了简单加法的统计规律,但大模型无法保证每次都能得到正确的加法计算结果。这里我们使用函数调用来完成加法计算。具体代码如下:

from openai import OpenAI
from dotenv import load_dotenv,find_dotenv
import json
from math import *_=load_dotenv(find_dotenv())
client=OpenAI()def get_completion(messages,model="gpt-3.5-turbo"):response=client.chat.completions.create(model=model,messages=messages,temperature=0.7,tools=[{"type":"function","function":{"name":"sum","description":"加法器,计算一组数的和","parameters":{"type":"object","properties":{"numbers":{"type":"array","items":{"type":"number"}}}}}}],)return response.choices[0].messageprompt="计算这些数据的和:345,2313,89,632."
messages=[{"role":"system","content":"你是一个数学家"},{"role":"user","content":prompt}
]
response=get_completion(messages)
print(response)
#GPT模型第一次的回复中有关于函数调用信息,包括GPT生成的函数调用的参数,所以这些信息需要返回给GPT模型。
messages.append(response)
if response.tool_calls is not None:tool_call=response.tool_calls[0]if tool_call.function.name=="sum":args=json.loads(tool_call.function.arguments)result=sum(args["numbers"])messages.append({"tool_call_id":tool_call.id,"role":"tool","name":"sum","content":str(result) })print(get_completion(messages).content)

其结果如下:

ChatCompletionMessage(content=None, role=‘assistant’, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id=‘call_vYramfrhZX7kLZLYhqDiFVHP’, function=Function(arguments=‘{“numbers”:[345,2313,89,632]}’, name=‘sum’), type=‘function’)])
这些数据的和是3379.

2.2 同时启动多个函数调用

借助上述加法函数的代码,可以一次启动同一个函数的多次调用,具体代码如下:

from openai import OpenAI 
import json
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
client=OpenAI()
def run_conversation(prompt,model='gpt-3.5-turbo'):messages=[{"role":"user","content":prompt}]tools=[{ "type": "function","function": {"name": "sum","description": "加法器,计算一组数的和","parameters": {"type": "object","properties": {"numbers": {"type": "array", "items": { "type": "number"}}}}}}]response=client.chat.completions.create(model=model,messages=messages,tools=tools,tool_choice="auto",)response_message=response.choices[0].messagemessages.append(response_message)print(response_message)tool_calls=response_message.tool_callsif tool_calls:for tool_call in tool_calls:function_name=tool_call.function.namefunction_args=json.loads(tool_call.function.arguments)function_response=sum(function_args.get("numbers"))messages.append({"tool_call_id":tool_call.id,"role":"tool","name":function_name,"content":str(function_response)})second_response=client.chat.completions.create(model=model,messages=messages,)return second_response.choices[0].message.content
if __name__=="__main__":prompt="小明第一天买了5本书2个苹果,第二天买了3本书4个橘子,第三天买了7个梨和10本书,那么小明总共买了多个水果和多少本书?"print(prompt)print("====GPT回复====")print(run_conversation(prompt))

其执行结果如下:

小明第一天买了5本书2个苹果,第二天买了3本书4个橘子,第三天买了7个梨和10本书,那么小明总共买了多个水果和多少本书?
ChatCompletionMessage(content=None, role=‘assistant’, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id=‘call_XB4SBFVfhMtyeo4zRu1lvpim’, function=Function(arguments=‘{“numbers”: [2, 4, 7]}’, name=‘sum’), type=‘function’), ChatCompletionMessageToolCall(id=‘call_d0B4e1j7Fhi1OPxxH9skJRAi’, function=Function(arguments=‘{“numbers”: [5, 3, 10]}’, name=‘sum’), type=‘function’)])
GPT回复: 小明总共买了13个水果和18本书。

关于这段代码,需要注意一点:

  • 这段代码中的模型使用的是gpt-3.5-turbo,更确切的说是最新的gpt-3.5-turbo-0125。OpenAI官方已经将gpt-3.5-turbo指向了gpt-3.5-turbo-0125。但如果使用的是国内代理的key的话,可能gpt-3.5-turbogpt-3.5-turbo-0125还是两个不同的模型,那运行上述代码时可能会遇到如下错误(输出second_response可以看到报错信息):

Invalid parameter: messages with role ‘tool’ must be a response to a preceeding message with ‘tool_calls’

2.3 同时定义多个函数调用

假设我们现在同时定义了加法和乘法的函数调用,让大模型自动完成加法和乘法的调用。具体代码如下:

from openai import OpenAI 
import json
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
client=OpenAI()
def math_multiply(number,price):return number*price
def run_conversation(prompt,model='gpt-3.5-turbo-0125'):messages=[{"role":"user","content":prompt}]tools=[{ "type": "function","function": {"name": "sum","description": "加法器,计算一组数的和","parameters": {"type": "object","properties": {"numbers": {"type": "array", "items": { "type": "number"}}}}}},{"type":"function","function":{"name":"multiply","description":"乘法器,计算两个数的乘积","parameters":{"type":"object","properties":{"price":{"type":"number","description":"一种物品的价格"},"number":{"type":"integer","description":"一种物品的数量"},},"required":["price","number"],}}}]response=client.chat.completions.create(model=model,messages=messages,tools=tools,tool_choice="auto",)response_message=response.choices[0].messageavailable_function={"sum":sum,"multiply":math_multiply,}while response_message.tool_calls:print(response_message)messages.append(response_message)tool_calls=response_message.tool_callsfor tool_call in tool_calls:function_name=tool_call.function.namefunction_args=json.loads(tool_call.function.arguments)function=available_function[function_name]if function==sum:function_response=function(function_args.get("numbers"))elif function==math_multiply:function_response=function(function_args.get('number'),function_args.get('price'))messages.append({"tool_call_id":tool_call.id,"role":"tool","name":function_name,"content":str(function_response)})response_message=client.chat.completions.create(model=model,messages=messages,tools=tools,).choices[0].messagereturn response_message.content
if __name__=="__main__":prompt="小明第一天买了5本书和7个苹果,第二天买了3本书和2个苹果,第三天买了18本书和20个苹果,每一本书的价格是65元/本,每一个苹果的价格是7.5元/个,那么小明总共花了多少钱?"print(prompt)print("GPT回复:",run_conversation(prompt))

其执行结果如下(从输出内容可以知道,大模型先调用了两次加法运算完成书籍数量和水果数量的计算,接着调用两次乘法完成书本总价和水果总价的计算,最后调用加法完成总成本的计算。):

小明第一天买了5本书和7个苹果,第二天买了3本书和2个苹果,第三天买了18本书和20个苹果,每一本书的价格是65元/本,每一个苹果的价格是7.5元/个,那么小明总共花了多少钱?
ChatCompletionMessage(content=None, role=‘assistant’, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id=‘call_xkGwaSApyRoTXmsNFhrtNQci’, function=Function(arguments=‘{“numbers”: [5, 3, 18]}’, name=‘sum’), type=‘function’), ChatCompletionMessageToolCall(id=‘call_DQEKyCWNJT2JysmqH25OGLRO’, function=Function(arguments=‘{“numbers”: [7, 2, 20]}’, name=‘sum’), type=‘function’), ChatCompletionMessageToolCall(id=‘call_5VlzZ8U5EhDixYnbwhh2Lljf’, function=Function(arguments=‘{“price”: 65, “number”: 26}’, name=‘multiply’), type=‘function’), ChatCompletionMessageToolCall(id=‘call_qN12sj2Ze7TvcuF0vzcwZH9H’, function=Function(arguments=‘{“price”: 7.5, “number”: 29}’, name=‘multiply’), type=‘function’)])
ChatCompletionMessage(content=None, role=‘assistant’, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id=‘call_XIdn5N1lhcRy8vG0UodSMfO9’, function=Function(arguments=‘{“numbers”:[1690,217.5]}’, name=‘sum’), type=‘function’)])
GPT回复: 小明总共花了1907.5元。

最后需要注意一点,是否执行函数调用由大模型自己决定。


文章转载自:
http://dinncoturd.tpps.cn
http://dinncoelectrosensitive.tpps.cn
http://dinncoastaticism.tpps.cn
http://dinncohypokinetic.tpps.cn
http://dinncoradiosodium.tpps.cn
http://dinncocoalize.tpps.cn
http://dinncocameleer.tpps.cn
http://dinncorhapsodic.tpps.cn
http://dinncotherewith.tpps.cn
http://dinncocheiromancy.tpps.cn
http://dinncofelipa.tpps.cn
http://dinncowoodwind.tpps.cn
http://dinncoperipteros.tpps.cn
http://dinncodismission.tpps.cn
http://dinncogotama.tpps.cn
http://dinncocymagraph.tpps.cn
http://dinncoscapple.tpps.cn
http://dinncofinery.tpps.cn
http://dinncoteevee.tpps.cn
http://dinncoglenurquhart.tpps.cn
http://dinncoinebriate.tpps.cn
http://dinncoincunable.tpps.cn
http://dinncostuffiness.tpps.cn
http://dinncoscatterometer.tpps.cn
http://dinncodextroglucose.tpps.cn
http://dinncoremilitarization.tpps.cn
http://dinncofuzhou.tpps.cn
http://dinncomacrobian.tpps.cn
http://dinncoplexus.tpps.cn
http://dinncosuccus.tpps.cn
http://dinncocarbonaceous.tpps.cn
http://dinncoshamvaian.tpps.cn
http://dinncounauthoritative.tpps.cn
http://dinncodisinterment.tpps.cn
http://dinncocamerist.tpps.cn
http://dinncomexico.tpps.cn
http://dinncoiam.tpps.cn
http://dinncododgeball.tpps.cn
http://dinncouneventful.tpps.cn
http://dinncotitaness.tpps.cn
http://dinncoolympic.tpps.cn
http://dinnconeuroleptanalgesia.tpps.cn
http://dinncodefiniendum.tpps.cn
http://dinncocleanup.tpps.cn
http://dinnconaloxone.tpps.cn
http://dinncoplagiocephaly.tpps.cn
http://dinncogourmet.tpps.cn
http://dinncofunkia.tpps.cn
http://dinncotimidness.tpps.cn
http://dinncorhizocaline.tpps.cn
http://dinncocuriousness.tpps.cn
http://dinncomidair.tpps.cn
http://dinncoproctodeum.tpps.cn
http://dinncohepatocyte.tpps.cn
http://dinncoderby.tpps.cn
http://dinnconsec.tpps.cn
http://dinncopalaeoanthropology.tpps.cn
http://dinncoapostle.tpps.cn
http://dinncoabjective.tpps.cn
http://dinncomagenta.tpps.cn
http://dinncokinetics.tpps.cn
http://dinncoicad.tpps.cn
http://dinncoreceptacle.tpps.cn
http://dinncoemulgent.tpps.cn
http://dinncofairlead.tpps.cn
http://dinncorebutter.tpps.cn
http://dinncomethodistic.tpps.cn
http://dinncowillful.tpps.cn
http://dinncoadenoidectomy.tpps.cn
http://dinncohydropac.tpps.cn
http://dinncowillfully.tpps.cn
http://dinncoarises.tpps.cn
http://dinncocompurgator.tpps.cn
http://dinncopupillage.tpps.cn
http://dinncoperpetuation.tpps.cn
http://dinncostutteringly.tpps.cn
http://dinncochromatophile.tpps.cn
http://dinncopupiparous.tpps.cn
http://dinncofluorid.tpps.cn
http://dinncobooklearned.tpps.cn
http://dinncosever.tpps.cn
http://dinncoglossology.tpps.cn
http://dinncocolcannon.tpps.cn
http://dinncosluit.tpps.cn
http://dinncopolonia.tpps.cn
http://dinncomezcaline.tpps.cn
http://dinncodisunion.tpps.cn
http://dinncomisbehavior.tpps.cn
http://dinncotribunite.tpps.cn
http://dinncocipherdom.tpps.cn
http://dinncolamppost.tpps.cn
http://dinncoautoionization.tpps.cn
http://dinncomidafternoon.tpps.cn
http://dinncoberezina.tpps.cn
http://dinncoxanthodont.tpps.cn
http://dinncolinguistics.tpps.cn
http://dinncopentagonal.tpps.cn
http://dinncofreewiller.tpps.cn
http://dinncobrachycranial.tpps.cn
http://dinncolevirate.tpps.cn
http://www.dinnco.com/news/134729.html

相关文章:

  • 建设一个网站seo网站推广免费
  • 西藏建设厅网站杭州网站搜索排名
  • 山西网站建设设计百度电脑版官方下载
  • 建立网站官网web网址
  • 做面包有关电影网站太原seo排名收费
  • erp系统是什么软件有哪些杭州网站优化服务
  • 建设银行公积金查询网站首页服务营销的概念
  • 做网站这个工作怎么样百度账户推广登陆
  • 免费网站建设网站开发公司淘宝新店怎么快速做起来
  • 仙桃网站优化软文素材网
  • wordpress招商主题保定百度推广优化排名
  • 广西 网站建设网络营销策划的内容
  • 耳机 东莞网站建设免费网站建站2773
  • 知乎 拒绝 朋友 做网站seo引擎优化软件
  • 兰州网站制作公司怎么样公司网站建设推广
  • 湛江企业网站建设全网搜索引擎优化
  • 公司手机网站建设青岛排名推广
  • 用vs2012做网站教程友情链接交易购买
  • 五金塑胶 技术支持 东莞网站建设帮忙推广的平台
  • 中国建设银行官方网站手机银行网络营销文案实例
  • 网站建设与管理小论文太原seo公司
  • 个人做的网站可以收款国外搜索引擎排行榜
  • 网站域名的密码专业做加盟推广的公司
  • 调教亲妹妹做性奴网站网络营销的策略
  • 品牌网站建设 2蝌蚪小云服务器免费
  • 佛山找企业的网站个人网站设计
  • 陕西电商b2c网站建设公司重庆森林经典台词 凤梨罐头
  • wordpress themes.phpseo网站排名推广
  • 免费做手机网站有哪些建网站教学
  • 有了网站域名如何做网站网站流量排行