taolib.harness.agents.graph_agent#
LangGraph Agent 基类 - 定义基于 StateGraph 的可注册、可组合 Agent 抽象。
本模块提供两个核心抽象:
AgentConfig—— 通过 Pydantic v2 定义的 Agent 元配置(名称、版本、 描述、工具集、LLM 模型参数、最大迭代次数等);GraphAgent—— LangGraph Agent 的抽象基类,子类通过实现build_graph()声明节点与边,基类负责图编译、检查点接入、状态回调 广播以及与上层注册表/桥接层的协同。
并附带预置模板 ReActAgent,按 reason → act → observe → reason 循环
组织节点,支持工具调用与条件终止。
装饰器风格示例:
from taolib.harness.core import register_agent
@register_agent(name="planner", version="1.0.0", tags=("plan",))
class PlannerAgent(GraphAgent): ...
LangGraph 在当前环境可能未安装,本模块通过 TYPE_CHECKING 守卫与运行时
探测实现可插拔接入:未安装时仍可使用内置的轻量执行回退。
Attributes#
Classes#
Agent 元配置。 |
|
Agent 包装为工具时的描述对象。 |
|
LangGraph Agent 抽象基类。 |
|
Agent 图结构的声明式规格。 |
|
LLM 模型配置 - 用于 Agent 内部对底层模型的参数化调用。 |
|
预置 ReAct 模式 Agent 模板。 |
Module Contents#
- class taolib.harness.agents.graph_agent.AgentConfig#
Bases:
pydantic.BaseModelAgent 元配置。
用于在注册、装配、序列化阶段声明一个 Agent 的全部静态属性。
- model_config#
- class taolib.harness.agents.graph_agent.AgentTool#
Agent 包装为工具时的描述对象。
可被任意支持
name/description/func三元组的工具协议消费 (包括 LangChainStructuredTool、OpenAI function-call 等)。- func: collections.abc.Callable[Ellipsis, collections.abc.Awaitable[Any]]#
- class taolib.harness.agents.graph_agent.GraphAgent(config: AgentConfig | collections.abc.Mapping[str, Any], *, registry: taolib.harness.core.registry.AgentRegistry | None = None, checkpointer: taolib.harness.runtime.checkpointer.HarnessCheckpointer | None = None, auto_register: bool = True)#
Bases:
abc.ABCLangGraph Agent 抽象基类。
生命周期:
实例化时自动向
AgentRegistry注册自身(除非显式禁用);首次调用
compile()时执行build_graph()并返回可执行图对象;as_tool()将 Agent 包装为可被其他 Agent 调用的工具;as_step()将 Agent 适配为 Metaflow Step 的同步可调用。
钩子
on_node_start/on_node_end/on_error用于观测节点级状态。构造 Agent。
- 参数:
config --
AgentConfig实例或可被其校验的字典。registry -- 自定义 Agent 注册表;缺省使用全局默认注册表。
checkpointer -- 自定义检查点适配器;缺省构造内存型实例。
auto_register -- 是否在构造时自动注册到
registry。
- async ainvoke(inputs: collections.abc.Mapping[str, Any] | None = None, config: collections.abc.Mapping[str, Any] | None = None) dict[str, Any]#
异步执行 Agent。
- as_step() collections.abc.Callable[Ellipsis, dict[str, Any]]#
将 Agent 包装为可在 Metaflow Step 中调用的同步函数(Agent-as-Step)。
返回的可调用接受任意
inputs字典,内部驱动invoke(), 从而让 Flow Step 可以无缝复用 Agent 的图执行能力。
- as_tool(*, name: str | None = None, description: str | None = None) AgentTool#
将 Agent 包装为可被其他 Agent 调用的工具(Agent-as-Tool)。
- compile(*, force: bool = False) _Compiled#
编译图谱声明为可执行对象。
优先使用 LangGraph 的
StateGraph;若环境未安装则回退到内置_CompiledStubGraph顺序执行器。- 参数:
force -- 是否强制重新编译(忽略缓存)。
- invoke(inputs: collections.abc.Mapping[str, Any] | None = None, config: collections.abc.Mapping[str, Any] | None = None) dict[str, Any]#
同步执行 Agent。
- property checkpointer: taolib.harness.runtime.checkpointer.HarnessCheckpointer#
关联的检查点适配器。
- config#
- config_class: ClassVar[type[AgentConfig]]#
- on_error: collections.abc.Callable[[str, BaseException], collections.abc.Awaitable[None] | None] | None = None#
- on_node_end: collections.abc.Callable[[str, dict[str, Any]], collections.abc.Awaitable[None] | None] | None = None#
- on_node_start: collections.abc.Callable[[str, dict[str, Any]], collections.abc.Awaitable[None] | None] | None = None#
- property registry_entry: taolib.harness.core.registry.RegistryEntry[Any] | None#
注册条目(若已注册)。
- class taolib.harness.agents.graph_agent.GraphSpec#
Agent 图结构的声明式规格。
子类
GraphAgent.build_graph()返回本结构后,由基类编译成可执行图。
- class taolib.harness.agents.graph_agent.LLMConfig#
Bases:
pydantic.BaseModelLLM 模型配置 - 用于 Agent 内部对底层模型的参数化调用。
- model_config#
- class taolib.harness.agents.graph_agent.ReActAgent(config: AgentConfig | collections.abc.Mapping[str, Any], *, registry: taolib.harness.core.registry.AgentRegistry | None = None, checkpointer: taolib.harness.runtime.checkpointer.HarnessCheckpointer | None = None, auto_register: bool = True)#
Bases:
GraphAgent预置 ReAct 模式 Agent 模板。
构造的图结构如下:
reason ──▶ act ──▶ observe ──▶ reason (循环直至终止条件) └──▶ end子类可重写
reason()、act()、observe()三个方法以注入 具体的 LLM/工具/解析逻辑;缺省实现仅做透传,便于在测试中校验图骨架。构造 Agent。
- 参数:
config --
AgentConfig实例或可被其校验的字典。registry -- 自定义 Agent 注册表;缺省使用全局默认注册表。
checkpointer -- 自定义检查点适配器;缺省构造内存型实例。
auto_register -- 是否在构造时自动注册到
registry。
- type taolib.harness.agents.graph_agent.NodeFn = Callable[[dict[str, Any]], dict[str, Any] | Awaitable[dict[str, Any]]]#
节点函数签名:接收当前状态字典,返回需要合并的更新字典(同步或异步)。
- type taolib.harness.agents.graph_agent.ToolFn = Callable[..., Any]#
工具函数签名:任意可调用对象,参数与返回值由具体工具决定。