読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第3節 段階ごとに道具を絞る

3.1 使える道具を名前で絞る

LimitedToolbox は、もとの道具箱を包み、名前の一覧にある道具だけを見せます。

# 種類ごとに使える道具。店の案内の段階は、道具を使わず資料から答える
ROUTE_TOOLS = {
    "product": ["find_products"],
    "reservation": ["find_products", "draft_reservation"],
}


class LimitedToolbox:
    def __init__(self, toolbox: Toolbox, names: list[str]):
        self.toolbox = toolbox
        self.names = names

    def specs(self) -> list[dict]:
        return [spec for spec in self.toolbox.specs() if spec["function"]["name"] in self.names]

    def call(self, name: str, arguments: dict | str) -> tuple[dict, bool]:
        if name not in self.names:
            return {"error": f"この段階では {name} は使えません。使える道具:{', '.join(self.names)}"}, False
        return self.toolbox.call(name, arguments)

specs() で一覧から外すだけでなく、call() でも止めます。LLMは、一覧にない道具の名前を返すことがあるからです。product の段階では、LLMが何を返しても、draft_reservation は実行されません。

LimitedToolbox も Toolbox の形をしているため、run_agent はそのまま使えます。手元の道具箱でも、MCPの道具箱でも、同じように絞れます。

11 / 22