定制代码工具 - Hello World

此示例代码演示了如何使用辅助工具测试定制代码工具。

Hello World 示例是最简单的自定义代码工具。它定义一个工具类,它接受一个 name 参数并返回一个 greeting。使用它作为您自己的工具的起点。

tool_implementation.py

from aidputils.agents.tools.custom_tools.base import CustomToolBase
 

@BaseTool.register
 class HelloTool(CustomToolBase):
     """A simple greeting tool."""
 
    @classmethod
     def _execute_tool(cls, conf, runtime_params, **context_vars):
         name = runtime_params.get("name", "World")
         return {"greeting": f"Hello, {name}!"}

tool_config.json

{
   "displayName": "Hello Tool",
   "description": "A simple hello world tool",
   "tools": [
     {
       "toolClassName": "HelloTool",
       "displayName": "Hello Tool",
       "description": "Returns a hello world greeting",
       "version": "1.0.0",
       "schema": [
         {
           "name": "name",
           "type": "string",
           "description": "Name to greet"
         }
       ],
       "conf": {}
     }
   ]
 }

要求 .txt

# no deps

将这三个文件打包到 ZIP 归档文件的根目录,然后通过 Package 选项卡上载 ZIP。上载后,切换到参数选项卡,如果要覆盖默认值,请填写“说明”,然后切换到测试选项卡以调用该工具。使用 name="Alice" 时,该工具将返回:

{"greeting": "Hello, Alice!"}