今天則補上最後一塊拼圖:Tools。讓整個 agent-brain 真正動起來。
我先定義了一個最小的 BaseTool
介面,包含三個東東: (反正就按照 MCP server 的概念,之後應該也要能 support mcp server)
name:工具的唯一名稱,會被 Reasoning state 引用。
description:文字描述,能直接塞進 prompt,讓 LLM 知道這個工具用途。
input_schema:輸入格式,用類似 JSON Schema 的結構來定義。
程式骨架如下:
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}"
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())
@@
要可以 support mcp server,e.g., user 只要提供 url