Overseas access: www.kdjingpai.com
Bookmark Us
Current Position:fig. beginning " AI Answers

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

2025-08-21 225

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

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

Steps:

  1. 定义工具类:继承 BaseTool 并实现 run Methods.
  2. Registration Tools: By 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 等企业级工具。

Recommended

Can't find AI tools? Try here!

Just type in the keyword Accessibility Bing SearchYou can quickly find all the AI tools on this site.

Top

en_USEnglish