AI 网关零代码解决 AI 幻觉问题
阿里妹导读
一、前言
什么是 AI Agent
什么是 AI Gateway
将传统的 QPS 限流扩展到 Token 限流。
将传统的负载均衡/重试/fallback 能力延伸,支持对接多个大模型厂商 API,提高整体稳定性。
扩展可观测能力,支持不同模型之间效果对比的 A/B Test,以及对话上下文链路 Tracing 等。
二、AI Agent 插件使用
apiKey 申请
Higress服务配置
插件参数配置
dashscope: #通义千问大模型客户端配置 apiKey: sk-xxxxxxxxxxxxxxxxxxxxxxx domain: dashscope.aliyuncs.com serviceName: dashscope servicePort: 443promptTemplate: language: CHapis:- apiProvider: domain: restapi.amap.com serviceName: geo servicePort: 80 apiKey: in: query name: key value: fcxxxxxxxxxxxxxxxxxx api: | openapi: 3.1.0 info: title: 高德地图 description: 获取 POI 的相关信息 version: v1.0.0 servers: - url: https://restapi.amap.com paths: /v5/place/text: get: description: 根据POI名称,获得POI的经纬度坐标 operationId: get_location_coordinate parameters: - name: keywords in: query description: POI名称,必须是中文 required: true schema: type: string - name: region in: query description: POI所在的区域名,必须是中文 required: true schema: type: string deprecated: false /v5/place/around: get: description: 搜索给定坐标附近的POI operationId: search_nearby_pois parameters: - name: keywords in: query description: 目标POI的关键字 required: true schema: type: string - name: location in: query description: 中心点的经度和纬度,用逗号隔开 required: true schema: type: string deprecated: false components: schemas: {}- apiProvider: domain: api.seniverse.com serviceName: seniverse servicePort: 80 apiKey: in: query name: key value: SMxxxxxxxxxxxxxx api: | openapi: 3.1.0 info: title: 心知天气 description: 获取 天气预办相关信息 version: v1.0.0 servers: - url: https://api.seniverse.com paths: /v3/weather/now.json: get: description: 获取指定城市的天气实况 operationId: get_weather_now parameters: - name: location in: query description: 所查询的城市 required: true schema: type: string - name: language in: query description: 返回天气查询结果所使用的语言 required: true schema: type: string default: zh-Hans enum: - zh-Hans - en - ja - name: unit in: query description: 表示温度的的单位,有摄氏度和华氏度两种 required: true schema: type: string default: c enum: - c - f deprecated: false components: 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. luckin coffee 瑞幸咖啡(鑫盛大厦店),位于新泺大街1299号鑫盛大厦2号楼大堂;\n2. 三庆齐盛广场挪瓦咖啡(三庆·齐盛广场店),位于新泺大街与颖秀路交叉口西南60米;\n3. luckin coffee 瑞幸咖啡(三庆·齐盛广场店),位于颖秀路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 原理
插件实现逻辑
1. 参数配置
2. prompt 模板
尽你所能回答以下问题。你可以使用以下工具:{tools}请使用以下格式,其中Action字段后必须跟着Action Input字段,并且不要将Action Input替换成Input或者tool等字段,不能出现格式以外的字段名,每个字段在每个轮次只出现一次:Question: 你需要回答的输入问题Thought: 你应该总是思考该做什么Action: 要采取的动作,动作只能是{tools_name}中的一个 ,一定不要加入其它内容Action Input: 行动的输入,必须出现在Action后。Observation: 行动的结果...(这个Thought/Action/Action Input/Observation可以重复N次)Thought: 我现在知道最终答案Final Answer: 对原始输入问题的最终答案再次重申,不要修改以上模板的字段名称,开始吧!Question: {input}
role: usermsg: {prompt模板}
3. 推理过程(工具调用)
role: assistantmsg: {大模型的回复}Thought: 为了提供咖啡店的推荐,我首先需要获取五道口这一地点的经纬度坐标。Action: get_location_coordinateAction Input: {"keywords": "五道口", "region": "北京市"}GET https://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_poisAction Input: {"keywords": "咖啡", "longitude": "116.352978", "latitude": "39.982849"}Thought: 我现在知道最终答案Final Answer: 在北京市五道口附近有几家咖啡店可以选择,包括:- 星巴克五道口店- Costa Coffee五道口店- 漫咖啡五道口店您可以根据个人喜好选择前往。
这一次,程序通过可以检测到回复中包含了 Final Answer,这说明大模型已经得到了最终答案,无需再次调用工具了。因此检测到 Final Answer 就是结束递归调用的条件,此时就可以将 Final Answer 的答案通过:
函数替换掉 response body 返回给用户了。
四、总结
本文主要介绍了 AI Agent 的背景,概念,探讨了 AI Agent 网关插件的使用方法,效果以及实现原理。希望对你有帮助!
[1] Higress
https://github.com/alibaba/higress
[2] 入门指南-Web 服务 API丨高德地图 API(amap.com)
https://lbs.amap.com/api/webservice/gettingstarted
[3] 查看/修改你的 API 密钥(yuque.com)
快速部署定制化文生图应用
PAI Stable Diffusion WebUI 解决方案为企业提供云上快速部署定制化的文生图应用。提供了方便、高效的模型部署产品,并支持根据实际需求,配置不同的服务版本及服务参数。具有分钟级部署上线,方便快捷、开箱即用,多版本部署方案,参数可定制化调整的优势。
点击阅读原文查看详情。