跳转至

Tool Agent

Tool Agent 支持调用外部工具,扩展智能体的能力边界。

特点

  • 支持多工具并行调用
  • 自动选择合适工具
  • 工具调用结果反馈给 LLM

使用方法

from tongagents.agents import ToolAgent
from tongagents.tools import SearchTool, CalculatorTool

agent = ToolAgent(
    name="assistant",
    llm="gpt-4",
    tools=[SearchTool(), CalculatorTool()]
)

response = agent.chat("北京的人口是多少?乘以2是多少?")

工作流程

用户输入 → LLM 分析意图 → 选择工具 → 执行工具 → 结果返回 → LLM 整合输出

工具选择策略

from tongagents import Agent

agent = ToolAgent(
    tools=[search, calculator, file_tool],
    tool_selection="auto"  # 自动选择
    # 或手动指定
    # tool_selection="manual"
)

工具定义

from tongagents import Tool

class CustomTool(Tool):
    name = "custom_tool"
    description = "自定义工具描述"

    parameters = {
        "type": "object",
        "properties": {
            "param1": {"type": "string"}
        },
        "required": ["param1"]
    }

    def run(self, param1):
        return {"result": f"处理: {param1}"}

适用场景

  • 需要调用外部 API
  • 动态信息查询
  • 计算、数据处理
  • 文件操作