当前位置: 首页 > news >正文

【LangGraph】六.多 Agent 协作:Subgraph 机制

写在前面

前面的文章里,我们学过流程控制:顺序、并行、路由、循环。那都是单个图内的节点编排。

但实际应用中,我们经常需要多个独立的 Agent协作:

  • 每个 Agent 有自己的职责(分析、决策、执行)
  • 每个 Agent 有自己的 State(独立的数据结构)
  • 多个 Agent 需要共享信息、协同工作

这就是多 Agent 协作要解决的问题。

多 Agent vs 多节点:

特性

多节点

多 Agent

独立性

节点共享 State

每个 Agent 有独立的 State

复用性

节点难以复用

Agent 可以独立复用

复杂度

适合简单流程

适合复杂系统

Subgraph 是实现多 Agent 协作的核心机制。这篇文章深入讲解 Subgraph 的工作原理和使用方法。


一、为什么需要 Subgraph

1.1 问题:复杂系统的状态爆炸

假设我们要构建一个代码审查系统:

  • 质量分析 Agent
  • 安全分析 Agent
  • 性能分析 Agent
  • 报告生成 Agent

如果所有 Agent 都在一个图里,State 会是什么样?

# ❌ 问题:State 字段爆炸 class State(TypedDict): code: str quality_analysis: str security_analysis: str performance_analysis: str quality_score: int security_score: int performance_score: int suggestions: list report: str # ... 还有更多中间字段

问题:

  • State 字段越来越多,难以维护
  • 不同 Agent 的字段混在一起,职责不清
  • 想复用一个 Agent,但 State 耦合太紧

1.2 解决:用 Subgraph 隔离上下文

Subgraph 的核心价值:每个子图有独立的 State,父图和子图通过明确的接口通信。

# 子图 1:质量分析 class QualityState(TypedDict): code: str analysis: str score: int # 子图 2:安全分析 class SecurityState(TypedDict): code: str analysis: str issues: list # 父图:协调多个子图 class ParentState(TypedDict): code: str quality_result: dict # 子图的结果 security_result: dict final_report: str

好处:

  • 每个子图的 State 职责单一
  • 子图可以独立开发、测试、复用
  • 父图只关心结果,不关心中间过程

二、Subgraph 核心语法

2.1 创建子图

子图就是一个普通的 StateGraph,可以独立编译和运行。

from typing import TypedDict from langgraph.graph import StateGraph, START, END # 1. 定义子图的 State class SubState(TypedDict): input: str output: str # 2. 定义子图的节点 def process_node(state: SubState) -> dict: result = f"处理:{state['input']}" return {"output": result} # 3. 创建并编译子图 sub_graph = StateGraph(SubState) sub_graph.add_node("process", process_node) sub_graph.set_entry_point("process") sub_graph.add_edge("process", END) sub_app = sub_graph.compile()

2.2 在父图中使用子图

子图编译后,可以作为节点添加到父图中。

# 1. 定义父图的 State class ParentState(TypedDict): data: str result: str # 2. 定义父图的节点(准备数据) def prepare_node(state: ParentState) -> dict: # 将父图的数据转换为子图的输入 return {"input": state["data"]} # 3. 定义父图的节点(处理结果) def finalize_node(state: ParentState) -> dict: # 从子图的结果中提取需要的数据 return {"result": state["output"]} # 4. 创建父图 parent_graph = StateGraph(ParentState) parent_graph.add_node("prepare", prepare_node) parent_graph.add_node("subgraph", sub_app) # 子图作为节点 parent_graph.add_node("finalize", finalize_node) # 5. 连接边 parent_graph.set_entry_point("prepare") parent_graph.add_edge("prepare", "subgraph") parent_graph.add_edge("subgraph", "finalize") parent_graph.add_edge("finalize", END) parent_app = parent_graph.compile()

2.3 关键问题:状态如何传递?

子图的输入:

子图从父图的 State 中读取输入。具体来说:

  • 子图执行时,会读取父图 State 中与子图 State 字段匹配的部分
  • 这些字段会被传递给子图作为输入

子图的输出:

子图执行完后,输出会合并到父图的 State中:

  • 子图返回的字段会添加到父图 State
  • 如果父图已有相同字段,会被覆盖(除非使用追加更新)

例子:

# 父图 State class ParentState(TypedDict): data: str output: str # 注意:这个字段名与子图的输出字段相同 # 子图 State class SubState(TypedDict): input: str output: str # 子图节点 def process_node(state: SubState) -> dict: return {"output": f"处理:{state['input']}"} # 执行流程: # 1. 父图执行 prepare_node,设置 input 字段 # 2. 子图执行,读取 input,返回 output # 3. 子图的 output 合并到父图 State # 4. 父图执行 finalize_node,读取 output

四、高级用法

4.1 子图嵌套

子图可以嵌套:父图包含子图,子图包含孙图。

# 孙图:错误处理 error_app = create_error_handler() # 子图:质量分析(包含错误处理) quality_graph = StateGraph(QualityState) quality_graph.add_node("error_handler", error_app) quality_graph.add_node("analyze", analyze_quality) # ... # 父图:审查系统(包含质量分析子图) review_graph = StateGraph(ReviewState) review_graph.add_node("quality", quality_app) # ...

适用场景:复杂系统分层设计。

4.2 条件调用子图

根据条件决定是否执行子图。

def should_run_security(state: ReviewState) -> str: # 如果代码包含敏感操作,需要安全分析 if "eval" in state["code"] or "exec" in state["code"]: return "security" else: return "skip_security" review_graph.add_conditional_edges( "quality", should_run_security, { "security": "security", "skip_security": "report", } )

适用场景:根据情况选择分析维度。

4.3 循环调用子图

迭代改进结果。

def should_improve(state: ReviewState) -> str: if state["quality_result"]["score"] < 80: return "improve" else: return "done" review_graph.add_conditional_edges( "quality", should_improve, { "improve": "refactor", # 重构子图 "done": "security", } )

适用场景:多轮迭代改进。


五、常见错误

错误 1:子图 State 设计不合理

# ❌ 错误:子图 State 包含父图专有字段 class SubState(TypedDict): code: str user_id: str # 这是父图的字段 session_token: str # 这也是父图的字段 # ✅ 正确:子图 State 只包含需要的字段 class SubState(TypedDict): code: str analysis: str score: int

错误 2:忘记子图需要独立编译

# ❌ 错误 parent_graph.add_node("subgraph", sub_graph) # 直接添加未编译的图 # ✅ 正确 sub_app = sub_graph.compile() # 先编译 parent_graph.add_node("subgraph", sub_app) # 再添加

错误 3:状态字段名不匹配

# 父图 State class ParentState(TypedDict): code: str result: dict # 子图 State class SubState(TypedDict): input: str # ❌ 父图没有 input 字段 output: str # ✅ 正确:字段名匹配 class ParentState(TypedDict): input: str # 父图有这个字段 output: dict class SubState(TypedDict): input: str output: str

小结

为什么需要 Subgraph:

  • 隔离不同 Agent 的 State
  • 提高代码复用性
  • 简化复杂系统设计

核心语法:

  1. 创建子图:定义 State、节点、编译
  2. 添加到父图:parent_graph.add_node("name", sub_app)
  3. 状态传递:字段名匹配自动传递

设计原则:

  • 每个子图职责单一
  • 子图 State 只包含必要字段
  • 父图协调流程,不关心具体逻辑

高级用法:

  • 子图嵌套:分层设计
  • 条件调用:根据情况选择
  • 循环调用:迭代改进
http://www.jsqmd.com/news/737452/

相关文章:

  • Python自动化监控B站UP主更新:异步轮询与邮件通知实践
  • DeepSeek V4 API 怎么接入 Python 项目?完整教程
  • 避坑指南:YOLOv5换MobileNetV3主干时,concat层和特征图对齐的那些坑我都帮你踩过了
  • 私有化旅行数据平台Triprive:自建部署与Docker容器化实践
  • 模拟IC设计避坑指南:手把手分析CMOS运放失调电压(从电阻失配到电流镜)
  • 构建个人AI记忆体:开源项目实战与架构解析
  • RDPWrap:解锁Windows远程桌面多用户功能的免费解决方案
  • 告别假阳性!用Cuckoo Filter优化你的LSM-Tree存储引擎(附Go代码实现)
  • 告别GEE代码恐惧!手把手教你用AppEEARS可视化下载MODIS GPP数据(附批量下载避坑指南)
  • 绝区零一条龙:智能自动化助手让你的游戏时间效率提升300%
  • Ultracite:现代CSS框架的功能优先设计与实战应用
  • OneMore插件终极指南:160+免费功能解锁OneNote完整生产力
  • MTKClient终极指南:解锁联发科设备的底层控制权
  • 征解
  • 保姆级教程:用EMQX CLI命令搞定认证规则、Dashboard用户一键备份与恢复
  • 告别枯燥文本:用Tree-sitter+Python把C++代码变成可交互的AST树(支持点击展开/折叠)
  • 手把手调试指南:用Debug玩转你的第一个MASM汇编程序(附常用命令清单)
  • PHP工程师必须掌握的LLM长连接底层机制:从Swoole EventLoop劫持到LLM context token生命周期管理
  • 3个技巧告别重复操作:用ok-ww实现鸣潮自动化战斗与资源管理
  • 避开RK3588 MPP解码的坑:分帧模式选择、内存配置与Info Change处理指南
  • 双系统Ubuntu22.04---(1)
  • 保姆级教程:用Vector CANoe的LIN Slave Conformance Tester搞定一致性测试
  • 抖音下载终极方案:3个技巧轻松掌握无水印视频批量下载
  • WebAI逆向工程:将网页AI服务封装为可调用API的实战指南
  • 为什么你的RTX 3080只能同时编码3路视频?聊聊NVENC限制背后的商业策略与技术取舍
  • 从可视化拖拽到SDF源码:Gazebo模型编辑器的“两面性”与进阶之路
  • Blender VRM插件终极指南:从零到精通的完整工作流
  • 5款惊艳VLC皮肤:告别单调界面,打造专属播放体验
  • 题解:AcWing 6023 合并石子
  • 开源代码审查平台Inspecto:从数据聚合到质量洞察的工程实践