我们知道在很多的应用场景中,我们需要定位服务,比如附近最近的餐馆或者酒店。当我们使用大模型进行 agent 任务执行的时候,我们需要位置服务。确切地说,我们需要根据位置来得到相应的经纬度来对数据进行搜索。而大模型有时不具备这种能力。我们在 AI Agents 的设计中,可以通过 workflow 的方法来创建这种工具。
创建 geocoing workflow
我们按照如下的方式来创建一个叫做 geocoding 的 workflow:
`
1. version: "1"
2. name: geocoding
3. description: "Geocoding for a location"
4. enabled: true
6. consts:
7. geocoding_api_url: https://maps.googleapis.com/maps/api/geocode/json?key=<Your API key>
9. inputs:
10. - name: location
11. type: string
12. required: true
13. description: "location (e.g., Washington)"
15. triggers:
16. - type: manual
19. steps:
20. - name: get_coordinate
21. type: http
22. with:
23. url: "{{ consts.geocoding_api_url }}address={{ inputs.location }}"
24. method: GET
27. - name: print_location
28. type: console
29. with:
30. message: "{{ inputs.location }} coordinate is {\"lat\": {{ steps.get_coordinate.output.data.results[0].geometry.location.lat }}, \"lon\": {{ steps.get_coordinate.output.data.results[0].geometry.location.lng }} }."
`AI写代码
在上面,我们需要填入自己的谷歌位置开发者 API key。
创建 get_coordinate_by_location 工具
我们接下来创建一个叫做 get_coordinate_by_location 的工具:
创建 agent
我们仿照之前的文章 “Elastic AI agent builder 介绍(一)” 来创建一个叫做 Find parents 的 agent。 它包含创建一个叫做 people 及 parents 的索引。
位置查询示例
我们进行如下的查询:
`哪个离湖北广水最近?`AI写代码
我们可以看到相应的 get_coordinate_by_location 被调用。