iT邦幫忙

2025 iThome 鐵人賽

DAY 14
1
生成式 AI

agent-brain: 從 0 開始打造一個 python package系列 第 14

Day 14: agent-brain 接上 tools

  • 分享至 

  • xImage
  •  

今天則補上最後一塊拼圖:Tools。讓整個 agent-brain 真正動起來。


BaseTool 抽象介面

我先定義了一個最小的 BaseTool 介面,包含三個東東: (反正就按照 MCP server 的概念,之後應該也要能 support mcp server)

  1. name:工具的唯一名稱,會被 Reasoning state 引用。

  2. description:文字描述,能直接塞進 prompt,讓 LLM 知道這個工具用途。

  3. input_schema:輸入格式,用類似 JSON Schema 的結構來定義。

    • 這樣能保證 prompt 時清楚列出參數格式。
    • 之後也能搭配驗證器(Pydantic or jsonschema)做更嚴謹的檢查。

程式骨架如下:

from abc import ABC, abstractmethod

class BaseTool(ABC):
    def __init__(self, name: str, description: str, input_schema: dict):
        self.name = name
        self.description = description
        self.input_schema = input_schema

    def to_dict(self) -> dict:
        return {
            self.name: {
                "description": self.description,
                "input_schema": self.input_schema,
            }
        }

    @abstractmethod
    async def execute(self, **kwargs) -> str: ...
  • to_dict():序列化成統一格式,方便 Memory 拿去丟進 system prompt。
  • execute():真正執行工具邏輯,並回傳結果字串。

最小範例:加法工具

接著我寫了一個最小的工具 AddTwoNumbersTool,用來示範整個流程。

class AddTwoNumbersTool(BaseTool):
    def __init__(self) -> None:
        super().__init__(
            name="add_two_numbers",
            description="Add two numbers.",
            input_schema={
                "type": "object",
                "properties": {
                    "a": {"type": "number", "description": "The first number."},
                    "b": {"type": "number", "description": "The second number."},
                },
                "required": ["a", "b"],
            },
        )

    async def execute(self, **kwargs) -> str:
        a = kwargs.get("a", 0)
        b = kwargs.get("b", 0)
        return f"tool result: {a} + {b} = {a + b}"

Demo

model: gpt-4.1-mini

import asyncio

from agent_brain import BaseTool, Brain


class AddTwoNumbersTool(BaseTool):
    def __init__(self) -> None:
        super().__init__(
            name="add_two_numbers",
            description="Add two numbers.",
            input_schema={
                "type": "object",
                "properties": {
                    "a": {"type": "number", "description": "The first number."},
                    "b": {"type": "number", "description": "The second number."},
                },
                "required": ["a", "b"],
            },
        )

    async def execute(self, **kwargs) -> str:
        a = kwargs.get("a", 0)
        b = kwargs.get("b", 0)
        return f"tool result: {a} + {b} = {a + b}"


brain = Brain(net="ReAct", memory="Messages", tools=[AddTwoNumbersTool()])
task = "What is the value of 12345 + 67890 + 999999?"


async def main() -> None:
    async for chunk in brain.answer(task):
        print(chunk, end="", flush=True)


asyncio.run(main())

來看看會長怎樣吧
Yes

@@

to-do:

要可以 support mcp server,e.g., user 只要提供 url


上一篇
Day 13: agent-brain 的 Brain
下一篇
Day 15: 用 Streamlit 打造 chat UI
系列文
agent-brain: 從 0 開始打造一個 python package17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言