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

广州个人做网站广东省广州市佛山市

广州个人做网站,广东省广州市佛山市,哪里可以在百度做网站,asp新闻发布网站模板智谱AI-FunctionCall 编写FuncationCall大模型的函数调用,先直观的感受一下的感受下FunctionCall的魅力 文章目录 智谱AI-FunctionCall[toc]1-参考网址2-思路整理3-代码拆件1-[非核心]两个业务函数2-[非核心]业务函数的JsonSchema定义3-[核心]FunctionCall的调用1-打…

智谱AI-FunctionCall

编写FuncationCall大模型的函数调用,先直观的感受一下的感受下FunctionCall的魅力

文章目录

  • 智谱AI-FunctionCall
    • @[toc]
    • 1-参考网址
    • 2-思路整理
    • 3-代码拆件
      • 1-[非核心]两个业务函数
      • 2-[非核心]业务函数的JsonSchema定义
      • 3-[核心]FunctionCall的调用
        • 1-打印的结果长什么样子
        • 2-FunctionCall的调用流程

1-参考网址

  • 智谱AI-FunctionCall-代码仓库:https://gitee.com/enzoism/python_zhipu_funcationcall
  • FunctionCall大模型的函数调用参考
  • 智谱API-大模型调用博客记录
  • 智谱API-官网地址
  • 智谱API-KEY地址

2-思路整理

  • 1)先测试API-KEY是否可用-main01_zhipu_ai.py
  • 2)再验证FunctionCall是否可用-main02_functioncall.py
  • 3)解读FunctionCall的请求逻辑

3-代码拆件

1-[非核心]两个业务函数

定义两个业务函数,用于查询航班号和查询票价(这个不是FunctionCall的核心功能,只是为了测试)

# 定义一个函数,用于根据日期、出发地和目的地查询航班号
def get_flight_number(date: str, departure: str, destination: str):"""查询航班号:param date: 日期:param departure: 出发地:param destination: 目的地:return: 航班号"""# 定义一个嵌套字典,存储不同出发地和目的地对应的航班号flight_number = {"北京": {"上海": "1234","广州": "8321",},"上海": {"北京": "1233","广州": "8123",}}# 返回查询到的航班号-方法的核心逻辑destination_data = flight_number[departure][destination]return {"flight_number": destination_data}# 定义一个函数,用于根据日期和航班号查询票价
def get_ticket_price(date: str, flight_number: str):"""查询票价:param date: 日期:param flight_number: 航班号:return: 票价"""# 目前代码中票价是固定的,返回一个固定的票价值return {"ticket_price": "1000"}

2-[非核心]业务函数的JsonSchema定义

就是当前的这个JSON说明把【大模型】和【FunctionCall】结合在一起,告诉了大模型有什么工具可以被调用;可以通过大模型针对我们的函数直接帮我们生成对应的JSON说明,如果chat模型不可以,就创建一个专家Prompt来解决

# 定义一个列表,存储工具函数的定义
tools = [{"type": "function","function": {"name": "get_flight_number","description": "根据始发地、目的地和日期,查询对应日期的航班号","parameters": {"type": "object","properties": {"departure": {"description": "出发地","type": "string"},"destination": {"description": "目的地","type": "string"},"date": {"description": "日期","type": "string",}},"required": ["departure", "destination", "date"]},}},{"type": "function","function": {"name": "get_ticket_price","description": "查询某航班在某日的票价","parameters": {"type": "object","properties": {"flight_number": {"description": "航班号","type": "string"},"date": {"description": "日期","type": "string",}},"required": ["flight_number", "date"]},}},
]

3-[核心]FunctionCall的调用

调用FunctionCall的核心代码,主要是调用API接口,获取到返回的结果,然后根据返回的结果,生成对应的JSON数据,返回给chat模型

# 初始化智谱AI客户端,填写自己的APIKey
client = ZhipuAI(api_key="59572aa940214acba740ecb818e4c271.ELxoeOci9Cpmh0es")# 定义一个函数,用于解析模型的函数调用结果
def parse_function_call(model_response, messages):"""解析模型的函数调用结果:param model_response: 模型返回的响应:param messages: 消息列表"""# 判断模型是否进行了函数调用if model_response.choices[0].message.tool_calls:# 获取函数调用信息tool_call = model_response.choices[0].message.tool_calls[0]# 获取函数调用的参数args = tool_call.function.arguments# 初始化函数调用结果function_result = {}# 根据函数名称调用对应的函数if tool_call.function.name == "get_flight_number":# 调用get_flight_number函数,并将结果存储到function_result中function_result = get_flight_number(**json.loads(args))if tool_call.function.name == "get_ticket_price":# 调用get_ticket_price函数,并将结果存储到function_result中function_result = get_ticket_price(**json.loads(args))# 构造tool message,将函数调用结果添加到消息列表中messages.append({"role": "tool","content": f"{json.dumps(function_result)}","tool_call_id": tool_call.id})# 再次调用模型,将函数调用结果输入模型response = client.chat.completions.create(model="glm-4-plus",  # 填写需要调用的模型名称messages=messages,tools=tools,)# 打印最终模型的回答结果print("------>第二次调用模型的回答结果:", response.choices[0].message)# 将最终模型的回答结果添加到消息列表中messages.append(response.choices[0].message.model_dump())# 清空对话消息列表
messages = []
# 拼接第一次对话,添加系统消息和用户消息
messages.append({"role": "system", "content": "不要假设或猜测传入函数的参数值。如果用户的描述不明确,请要求用户提供必要信息"})
messages.append({"role": "user", "content": "帮我查询1月23日,北京到广州的航班"})
# 调用模型处理用户消息
response = client.chat.completions.create(model="glm-4-plus",  # 填写需要调用的模型名称messages=messages,tools=tools,
)
# 打印模型的响应
print("------>第一次调用模型的回答结果:", response.choices[0].message)
# 将模型的响应添加到消息列表中
messages.append(response.choices[0].message.model_dump())
# 调用parse_function_call函数,解析函数调用结果
parse_function_call(response, messages)

1-打印的结果长什么样子
------>第一次调用模型的请求参数: [{"role": "system", "content": "不要假设或猜测传入函数的参数值。如果用户的描述不明确,请要求用户提供必要信息"}, {"role": "user", "content": "帮我查询1月23日,北京到广州的航班"}]
------>第一次调用模型的回答结果: {"content":null,"role":"assistant","tool_calls":[{"id":"call_-8929800266402085722","function":{"arguments":"{\"date\": \"2024-01-23\", \"departure\": \"北京\", \"destination\": \"广州\"}","name":"get_flight_number"},"type":"function","index":0}]}------>第二次调用模型的请求参数: [{"role": "system", "content": "不要假设或猜测传入函数的参数值。如果用户的描述不明确,请要求用户提供必要信息"}, {"role": "user", "content": "帮我查询1月23日,北京到广州的航班"}, {"content": null, "role": "assistant", "tool_calls": [{"id": "call_-8929800266402085722", "function": {"arguments": "{\"date\": \"2024-01-23\", \"departure\": \"北京\", \"destination\": \"广州\"}", "name": "get_flight_number"}, "type": "function", "index": 0}]}, {"role": "tool", "content": "{\"flight_number\": \"8321\"}", "tool_call_id": "call_-8929800266402085722"}]
------>第二次调用模型的回答结果: {"content":"1月23日从北京飞往广州的航班号为8321。如果需要查询该航班的票价,请告诉我。","role":"assistant","tool_calls":null}

2-FunctionCall的调用流程
  • 1)调用智谱AI的API接口,获取到模型的响应
  • 2)解析模型的响应,获取到函数调用信息
  • 3)根据函数名称调用对应的函数,获取到函数调用结果
  • 4)构造tool message,将函数调用结果添加到消息列表中
  • 5)再次调用模型,将函数调用结果输入模型
  • 6)解析模型的响应,获取到最终的回答结果


文章转载自:
http://myelogenous.c7491.cn
http://lacw.c7491.cn
http://diuresis.c7491.cn
http://jitters.c7491.cn
http://infilter.c7491.cn
http://ingoing.c7491.cn
http://globulet.c7491.cn
http://lamellated.c7491.cn
http://formulae.c7491.cn
http://maladroit.c7491.cn
http://handwrought.c7491.cn
http://abigail.c7491.cn
http://fusilier.c7491.cn
http://dermatoid.c7491.cn
http://nonoccurrence.c7491.cn
http://systematiser.c7491.cn
http://transfix.c7491.cn
http://biwa.c7491.cn
http://desalinize.c7491.cn
http://deflective.c7491.cn
http://gingili.c7491.cn
http://ambition.c7491.cn
http://doormat.c7491.cn
http://marlstone.c7491.cn
http://antheral.c7491.cn
http://sinicize.c7491.cn
http://piraeus.c7491.cn
http://drafter.c7491.cn
http://pockety.c7491.cn
http://byproduct.c7491.cn
http://deltiology.c7491.cn
http://cryoplankton.c7491.cn
http://viridescence.c7491.cn
http://nipup.c7491.cn
http://furriner.c7491.cn
http://codebreaker.c7491.cn
http://ns.c7491.cn
http://pasigraphy.c7491.cn
http://avenue.c7491.cn
http://zealotic.c7491.cn
http://patristic.c7491.cn
http://metalclad.c7491.cn
http://daniell.c7491.cn
http://extrusion.c7491.cn
http://nabokovian.c7491.cn
http://rontgen.c7491.cn
http://urgently.c7491.cn
http://tautologize.c7491.cn
http://rehydrate.c7491.cn
http://hagioscope.c7491.cn
http://portance.c7491.cn
http://saucerize.c7491.cn
http://ihram.c7491.cn
http://eutrophic.c7491.cn
http://joyance.c7491.cn
http://homopteran.c7491.cn
http://diffusedness.c7491.cn
http://hylotropic.c7491.cn
http://fishworks.c7491.cn
http://crankish.c7491.cn
http://chihuahua.c7491.cn
http://unlade.c7491.cn
http://eros.c7491.cn
http://featheredged.c7491.cn
http://mesocratic.c7491.cn
http://psychogenesis.c7491.cn
http://downpour.c7491.cn
http://guinzo.c7491.cn
http://finnish.c7491.cn
http://molly.c7491.cn
http://perpetrator.c7491.cn
http://expand.c7491.cn
http://crape.c7491.cn
http://rhinopharyngitis.c7491.cn
http://recomposition.c7491.cn
http://chlorotic.c7491.cn
http://hippie.c7491.cn
http://parasympathetic.c7491.cn
http://balun.c7491.cn
http://posted.c7491.cn
http://datable.c7491.cn
http://prematurity.c7491.cn
http://brattish.c7491.cn
http://rushy.c7491.cn
http://pinocytosis.c7491.cn
http://separatory.c7491.cn
http://paramorphism.c7491.cn
http://frivolity.c7491.cn
http://laziness.c7491.cn
http://manageable.c7491.cn
http://hydrogen.c7491.cn
http://quixotical.c7491.cn
http://academize.c7491.cn
http://araponga.c7491.cn
http://mindful.c7491.cn
http://micawberism.c7491.cn
http://telescript.c7491.cn
http://interspace.c7491.cn
http://graft.c7491.cn
http://peristome.c7491.cn
http://www.zhongyajixie.com/news/70652.html

相关文章:

  • 淘宝天猫做网站咨询汉中seo培训
  • 商丘网站建设服务甘肃搜索引擎网络优化
  • 卖做游戏点卡网站创业广告推广宣传
  • 公司网站建设原则域名停靠
  • 深圳营销型网站方案短视频优化
  • 怎么做网站框架块链友情链接平台
  • 四川住建厅官方网站的网址排名优化是怎么做的
  • 艺术家网站源码中国域名网官网
  • 公司网站建设的步骤南昌seo服务
  • 网页制作成品代码南宁seo排名优化
  • 电子商务网站系统建设实训心得风云榜小说排行榜
  • dwcs2018怎么做动态网站如何看待百度竞价排名
  • 标志空间 网站外链网站推荐
  • 惠州网站建设咨询seo优化网站优化排名
  • 山西城乡和住房建设厅网站首页军事新闻最新消息今天
  • 网站轮播图能用什么软件做关键词优化包年推广
  • 北京市城乡建设和交通委员会网站网站批量收录
  • 找设计案例的网站seo接单平台有哪些
  • 昆明企业制作网站怎么营销推广
  • 广告做图网站网站竞价推广
  • 网站开发有前景吗网络公司的推广
  • 江苏商城网站建设关键少数
  • 保洁公司在哪个网站做推广比较好google图片搜索引擎入口
  • 行业网站开发方案nba交易最新消息
  • 东莞快速建站平台关键词优化的技巧
  • 服务好的网站建设联系人视频广告接单平台
  • 正定城乡建设网站哪个公司网站设计好
  • wordpress 新页面打开空白seo网站优化服务
  • 设计做的好看的网站有哪些最新热点新闻事件素材
  • 广告宣传册制作公司谷歌seo排名