许多高级用例都需要您编写自定义的 Toolkits。以下是通用流程:

  1. 创建一个继承 agno.tools.Toolkit 类的类。
  2. 将您的函数添加到该类中。
  3. 重要提示:Toolkit 构造函数的 tools 参数中包含所有函数。

现在您的 Toolkit 已准备好与 Agent 一起使用。例如:

shell_toolkit.py
from typing import List

from agno.agent import Agent
from agno.tools import Toolkit
from agno.utils.log import logger

class ShellTools(Toolkit):
    def __init__(self, **kwargs):
        super().__init__(name="shell_tools", tools=[self.run_shell_command], **kwargs)

    def run_shell_command(self, args: List[str], tail: int = 100) -> str:
        """
        运行一个 shell 命令并返回输出或错误。

        Args:
            args (List[str]): 要运行的命令,以字符串列表形式表示。
            tail (int): 要从输出中返回的行数。
        Returns:
            str: 命令的输出。
        """
        import subprocess

        logger.info(f"Running shell command: {args}")
        try:
            logger.info(f"Running shell command: {args}")
            result = subprocess.run(args, capture_output=True, text=True)
            logger.debug(f"Result: {result}")
            logger.debug(f"Return code: {result.returncode}")
            if result.returncode != 0:
                return f"Error: {result.stderr}"
            # return only the last n lines of the output
            return "\n".join(result.stdout.split("\n")[-tail:])
        except Exception as e:
            logger.warning(f"Failed to run shell command: {e}")
            return f"Error: {e}"

agent = Agent(tools=[ShellTools()], show_tool_calls=True, markdown=True)
agent.print_response("List all the files in my home directory.")