自定义工具的添加方法与示例
Cogency 支持开发者扩展工具库,以下是具体步骤和示例:
步骤:
- 定义工具类:继承
BaseTool
并实现run
方法。 - 注册工具:通过
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 等企业级工具。
本答案来源于文章《Cogency:构建智能AI代理的认知架构工具》