一、前言
什么是 AI Agent
什么是 AI Gateway
将传统的 QPS 限流扩展到 Token 限流。
将传统的负载均衡/重试/fallback 能力延伸,支持对接多个大模型厂商 API,提高整体稳定性。
二、AI Agent 插件使用ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;color: rgb(62, 62, 62);font-size: 14px;text-align: left;letter-spacing: 0.034em;">
apiKey 申请
Higress服务配置
插件参数配置
dashscope:#通义千问大模型客户端配置apiKey:sk-xxxxxxxxxxxxxxxxxxxxxxxdomain:dashscope.aliyuncs.comserviceName:dashscopeservicePort:443promptTemplate:language:CHapis:-apiProvider:domain:restapi.amap.comserviceName:geoservicePort:80apiKey:in:queryname:keyvalue:fcxxxxxxxxxxxxxxxxxxapi:|openapi:3.1.0info:title:高德地图description:获取POI的相关信息version:v1.0.0servers:-url:https://restapi.amap.compaths:/v5/place/text:get:description:根据POI名称,获得POI的经纬度坐标operationId:get_location_coordinateparameters:-name:keywordsin:querydescriptionOI名称,必须是中文required:trueschema:type:string-name:regionin:querydescription
OI所在的区域名,必须是中文required:trueschema:type:stringdeprecated:false/v5/place/around:get:description:搜索给定坐标附近的POIoperationId:search_nearby_poisparameters:-name:keywordsin:querydescription:目标POI的关键字required:trueschema:type:string-name:locationin:querydescription:中心点的经度和纬度,用逗号隔开required:trueschema:type:stringdeprecated:falsecomponents:schemas:{}-apiProvider:domain:api.seniverse.comserviceName:seniverseservicePort:80apiKey:in:queryname:keyvalue:SMxxxxxxxxxxxxxxapi:|openapi:3.1.0info:title:心知天气description:获取天气预办相关信息version:v1.0.0servers:-url:https://api.seniverse.compaths:/v3/weather/now.json:get:description:获取指定城市的天气实况operationId:get_weather_nowparameters:-name:locationin:querydescription:所查询的城市required:trueschema:type:string-name:languagein:querydescription:返回天气查询结果所使用的语言required:trueschema:type:stringdefault:zh-Hansenum:-zh-Hans-en-ja-name:unitin:querydescription:表示温度的的单位,有摄氏度和华氏度两种required:trueschema:type:stringdefault:cenum:-c-fdeprecated:falsecomponents:schemas:{}
插件效果
curl'http://<这里换成网关公网IP>/api/openai/v1/chat/completions'\-H'Accept:application/json,text/event-stream'\-H'Content-Type:application/json'\--data-raw'{"messages":[{"role":"user","content":"我想在济南市鑫盛大厦附近喝咖啡,给我推荐几个"}],"model":"qwen","stream":false}'{...,"content":"在济南市鑫盛大厦附近,您可以选择以下咖啡店:\n1.luckincoffee瑞幸咖啡(鑫盛大厦店),位于新泺大街1299号鑫盛大厦2号楼大堂;\n2.三庆齐盛广场挪瓦咖啡(三庆·齐盛广场店),位于新泺大街与颖秀路交叉口西南60米;\n3.luckincoffee瑞幸咖啡(三庆·齐盛广场店),位于颖秀路1267号;\n4.库迪咖啡(齐鲁软件园店),位于新泺大街三庆齐盛广场4号楼底商;\n5.库迪咖啡(美莲广场店),位于高新区新泺大街1166号美莲广场L117号;以及其他一些选项。希望这些建议对您有所帮助!"...}curl'http://<这里换成网关公网IP>/api/openai/v1/chat/completions'\-H'Accept:application/json,text/event-stream'\-H'Content-Type:application/json'\--data-raw'{"messages":[{"role":"user","content":"济南市现在的天气情况如何?"}],"model":"qwen","stream":false}'{..."content":"济南市现在的天气状况为阴天,温度为31℃。此信息最后更新于2024年8月9日15时12分(北京时间)。"...}curl'http://<这里换成网关公网IP>/api/openai/v1/chat/completions'\-H'Accept:application/json,text/event-stream'\-H'Content-Type:application/json'\--data-raw'{"messages":[{"role":"user","content":"济南市现在的天气情况如何?用华氏度表示,用日语回答"}],"model":"qwen","stream":false}'{..."content":"济南市の現在の天気は雨曇りで、気温は88°Fです。この情報は2024年8月9日15時12分(東京時間)に更新されました。"...}三、AI Agent 实现原理
ReAct 原理
插件实现逻辑
尽你所能回答以下问题。你可以使用以下工具:{tools}请使用以下格式,其中Action字段后必须跟着ActionInput字段,并且不要将ActionInput替换成Input或者tool等字段,不能出现格式以外的字段名,每个字段在每个轮次只出现一次:Question:你需要回答的输入问题Thought:你应该总是思考该做什么Action:要采取的动作,动作只能是{tools_name}中的一个,一定不要加入其它内容ActionInput:行动的输入,必须出现在Action后。Observation:行动的结果...(这个Thought/Action/ActionInput/Observation可以重复N次)Thought:我现在知道最终答案FinalAnswer:对原始输入问题的最终答案再次重申,不要修改以上模板的字段名称,开始吧!Question:{input}role:usermsg:{prompt模板}role:assistantmsg:{大模型的回复}Thought:为了提供咖啡店的推荐,我首先需要获取五道口这一地点的经纬度坐标。Action:get_location_coordinateActionInput:{"keywords":"五道口","region":"北京市"}GEThttps://restapi.amap.com/v5/place/text?key=xxxxxx&keywords=五道口®ion=北京市
{"status":0,"message":"成功","result":{"location":{"lng":116.352978,"lat":39.982849},"precise":1,"confidence":100,"comprehension":100}}Thought:现在我得到了五道口的经纬度坐标,接下来我可以使用这些坐标来搜索附近的咖啡店。Action:search_nearby_poisActionInput:{"keywords":"咖啡","longitude":"116.352978","latitude":"39.982849"}Thought:我现在知道最终答案FinalAnswer:在北京市五道口附近有几家咖啡店可以选择,包括:-星巴克五道口店-CostaCoffee五道口店-漫咖啡五道口店您可以根据个人喜好选择前往。
这一次,程序通过可以检测到回复中包含了 Final Answer,这说明大模型已经得到了最终答案,无需再次调用工具了。因此检测到 Final Answer 就是结束递归调用的条件,此时就可以将 Final Answer 的答案通过:
函数替换掉 response body 返回给用户了。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |