海外访问:www.kdjingpai.com
Ctrl + D 收藏本站
当前位置:首页 » AI答疑

如何为 Cogency 代理添加自定义工具?能否举例说明?

2025-08-21 229

自定义工具的添加方法与示例

Cogency 支持开发者扩展工具库,以下是具体步骤和示例:

步骤:

  1. 定义工具类:继承 BaseTool 并实现 run 方法。
  2. 注册工具:通过 Agent 初始化时的 tools 参数添加工具。

示例(创建时间查询工具):

class TimezoneTool(BaseTool):
    def __init__(self):
        super().__init__("timezone", "Get time in any city")
    async def run(self, city: str):
        return {"time": f"Current time in {city}: 14:30 PST"}
    def get_schema(self):
        return "timezone(city='string')"

注册工具并调用:

agent = Agent("time_assistant", tools=[TimezoneTool()])
async for chunk in agent.stream("What's the time in London?"):
    print(chunk, end="", flush=True)

输出结果将显示 Current time in London: 14:30 PST。开发者可基于此模式集成数据库、API 等企业级工具。

相关推荐

找不到AI工具?在这试试!

输入关键词,即可 无障碍访问 必应 搜索,快速找到本站所有 AI 工具。

回顶部

zh_CN简体中文