开发自定义工具
工具(Tools)定义
工具(Tools)可以扩展 Monkeys 平台的能力,一些常见的 Tools 如下:
- LLM: 提供大语言模型对话的能力
- 知识库: 为 LLM 提供知识库问答的能力
- 自定义代码: 提供运行自定义代码的能力
- 翻译: 提供翻译的能力
- 语音识别: 提供语音识别的能力
- 图像生成: 提供图像生成的能力
- 小红书、Twitter、Instagram、微博等社交平台的自动化操作
等等等等,任何能够通过 API 调用的服务都可以作为一个 Tool,集成到 Monkeys 平台中。
如何开发自定义 Tool
如上文所说,Tool 就是一个可被调用的、满足 Swagger 标准的 API,除了编写 HTTP 接口之外,你不需要任何前置技能。
也正因为此,你可以使用任何语言开发你的 Tool.
整体来说,开发一个 Tool 需要以下几个步骤:
- 使用 JSON 定义你的 Tool.
- 编写 HTTP 接口,实现你的 Tool 逻辑.
- 部署你的 Tool.
- 在 Monkeys 平台中添加你的 Tool.
示例:使用 Python 开发一个查询天气的 Tool
初始化 Python 虚拟环境
创建 venv
环境,并激活:
安装依赖
这里我们使用 flask
框架,并用 flask_restx
构建标准 Swagger API。当然,你也可以使用任何其他你喜欢的框架。
添加 /manifest.json 接口
manifest.json
接口描述了你的 Tool 的基本信息:
- schema_version: 当前版本号,目前只支持
v1
- display_name: 在 Monkeys 平台中显示的名称
- namespace: 命名空间,用于区分不同的 Tool,建议使用
monkey_tools_
前缀,如monkey_tools_my_awesome_weather_tool
。此命名空间必须全局唯一。 - auth:
- type: 认证方式,目前支持
none
和service_http
两种方式
- type: 认证方式,目前支持
- api:
- type: API 类型,目前只支持
openapi
- url: Swagger API 的地址,可以是相对地址,如
/swagger.json
- type: API 类型,目前只支持
- contact_email: 联系邮箱
示例如下:
在 Falsk 中,只需要在 main.py
中添加如下代码即可:
在命令中运行:
之后,你可以在浏览器中访问 http://localhost:5000
,可以看到 Swagger API 文档已经自动构建好了,只不过里面还没有接口,下面我们开始编写实际的天气查询接口。
添加天气查询接口
这里我们使用 https://open-meteo.com/ 来作为天气查询的 API。
注意以下代码中的:
@weather_ns.vendor
注解,这是 Monkeys 平台的扩展,用于描述 Tool 的详细信息,如输入、输出、估计耗时等。@weather_ns.expect
注解,用于描述输入参数@weather_ns.response
注解,用于描述输出参数
重启服务,你可以在 Swagger API 文档中看到你的接口已经添加成功了。
导入 Monkeys 平台
将 Tools 导入到 Monkeys 平台,你只需要填入 Manifest JSON 的地址即可,这个示例中,我们的地址是 http://localhost:5000/manifest.json
。
导入成功之后,你应该可以在 Monkeys 平台中看到你的 Tool 了。
执行工作流,可以看到你的 Tool 已经被成功调用了。