图结构工作流 #
本文档中引用的文件
目录 #
- 简介
- 图结构基本概念
- 核心组件架构
- 节点(Node)详解
- 边(Edge)与条件边(Conditional Edge)
- 图的编译与执行
- 并行执行机制
- 子图嵌套与复合图
- 状态管理与模式
- 配置与流程控制
- 错误处理与中断机制
- 实际应用案例
- 总结
简介 #
langgraphgo 是一个强大的图结构工作流引擎,它提供了构建复杂应用程序流程的理论基础和实践工具。该框架的核心思想是将业务逻辑抽象为有向无环图(DAG),其中节点表示可执行的操作单元,边定义节点间的执行顺序,条件边则允许基于运行时状态动态决定执行路径。
图结构工作流的优势在于:
- 模块化设计:每个节点都是独立的功能单元
- 灵活路由:支持条件分支和动态路径选择
- 并行执行:充分利用多核处理器性能
- 状态管理:统一的状态更新和持久化机制
- 可观测性:内置的追踪和回调系统
图结构基本概念 #
图的基本组成单元 #
在 langgraphgo 中,图结构由以下基本单元构成:
graph TD
A["节点Node"] --> B["可执行函数单元"]
A --> C["接收状态输入"]
A --> D["返回状态输出"]
E["边Edge"] --> F["静态连接"]
E --> G["确定执行顺序"]
H["条件边Conditional Edge"] --> I["动态路由"]
H --> J["基于运行时状态"]
K["入口点Entry Point"] --> L["流程起始位置"]
K --> M["必须设置"]
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L52-L89)
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L10-L32)
核心数据结构 #
框架定义了几个关键的数据结构来表示图的各个组成部分:
| 组件 | 类型 | 描述 | 作用 |
|---|---|---|---|
| Node | struct | 节点对象 | 包含名称和执行函数 |
| Edge | struct | 边对象 | 定义节点间的连接关系 |
| MessageGraph | struct | 消息图 | 基础图结构,支持消息传递 |
| StateGraph | struct | 状态图 | 基于状态管理的图结构 |
段落来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L52-L89)
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L10-L32)
核心组件架构 #
MessageGraph 架构 #
MessageGraph 是框架的基础图结构,提供了完整的图操作功能:
classDiagram
class MessageGraph {
+map[string]Node nodes
+[]Edge edges
+map[string]func state string conditionalEdges
+string entryPoint
+StateMerger stateMerger
+StateSchema Schema
+AddNode(name, fn)
+AddEdge(from, to)
+AddConditionalEdge(from, condition)
+SetEntryPoint(name)
+Compile() Runnable
}
class Node {
+string Name
+func Function
}
class Edge {
+string From
+string To
}
class Runnable {
+MessageGraph graph
+Tracer tracer
+Invoke(ctx, state) Interface
+SetTracer(tracer)
}
MessageGraph --> Node : "包含"
MessageGraph --> Edge : "包含"
MessageGraph --> Runnable : "编译为"
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L74-L93)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L140-L146)
StateGraph 扩展功能 #
StateGraph 在 MessageGraph 的基础上增加了状态管理和重试机制:
classDiagram
class StateGraph {
+map[string]Node nodes
+[]Edge edges
+map[string]func state string conditionalEdges
+string entryPoint
+RetryPolicy retryPolicy
+StateMerger stateMerger
+StateSchema Schema
+SetRetryPolicy(policy)
+Compile() StateRunnable
}
class RetryPolicy {
+int MaxRetries
+BackoffStrategy BackoffStrategy
+[]string RetryableErrors
}
class StateRunnable {
+StateGraph graph
+Invoke(ctx, state) interface
+executeNodeWithRetry(node, state)
}
StateGraph --> RetryPolicy : "配置"
StateGraph --> StateRunnable : "编译为"
图表来源
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L10-L32)
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L34-L49)
段落来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L74-L93)
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L10-L32)
节点(Node)详解 #
节点定义与特性 #
节点是图结构中的基本执行单元,每个节点都包含一个可执行的函数:
flowchart TD
A["节点创建"] --> B["指定唯一名称"]
B --> C["定义执行函数"]
C --> D["函数签名:<br/>func(ctx, state) (interface, error)"]
D --> E["添加到图中"]
E --> F["节点就绪"]
G["执行流程"] --> H["并发调用"]
H --> I["状态传递"]
I --> J["结果合并"]
J --> K["下一节点"]
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L52-L60)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L103-L109)
节点执行机制 #
节点的执行遵循严格的生命周期管理:
| 阶段 | 描述 | 处理方式 |
|---|---|---|
| 初始化 | 创建节点实例 | 通过 AddNode 方法 |
| 并发执行 | 多个节点同时运行 | 使用 goroutine 并发 |
| 状态传递 | 将当前状态传递给节点 | 作为函数参数 |
| 结果处理 | 收集节点执行结果 | 合并到主状态 |
| 错误处理 | 处理节点执行错误 | 返回 GraphInterrupt |
段落来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L174-L492)
边(Edge)与条件边(Conditional Edge) #
静态边(Edge) #
静态边定义了图的固定执行路径:
sequenceDiagram
participant N1 as "节点1"
participant E as "边"
participant N2 as "节点2"
N1->>E : 执行完成
E->>N2 : 传递状态
N2->>N2 : 执行逻辑
N2->>N2 : 返回结果
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L111-L117)
条件边(Conditional Edge) #
条件边允许基于运行时状态动态决定执行路径:
flowchart TD
A["条件边判断"] --> B{"状态检查"}
B --> |条件1| C["节点A"]
B --> |条件2| D["节点B"]
B --> |默认| E["节点C"]
F["条件函数"] --> G["接收状态"]
G --> H["返回目标节点"]
H --> I["动态路由"]
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L119-L123)
- [examples/conditional_edges_example/main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/conditional_edges_example/main.go#L66-L88)
边的类型对比 #
| 特性 | 静态边 | 条件边 |
|---|---|---|
| 定义时机 | 编译时确定 | 运行时计算 |
| 路径数量 | 固定一条 | 可能多条 |
| 性能开销 | 低 | 中等 |
| 灵活性 | 低 | 高 |
| 使用场景 | 简单线性流程 | 复杂决策流程 |
段落来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L111-L123)
图的编译与执行 #
编译过程 #
图的编译是一个关键步骤,它将图结构转换为可执行的 Runnable 实例:
flowchart TD
A["图构建"] --> B["验证入口点"]
B --> C{"入口点存在?"}
C --> |否| D["返回 ErrEntryPointNotSet"]
C --> |是| E["创建 Runnable"]
E --> F["设置追踪器"]
F --> G["编译完成"]
H["编译检查"] --> I["节点存在性"]
I --> J["边的有效性"]
J --> K["循环检测"]
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L148-L159)
执行流程 #
Invoke 方法驱动整个图的执行过程:
sequenceDiagram
participant R as "Runnable"
participant C as "上下文"
participant N as "节点"
participant S as "状态"
R->>C : 初始化上下文
R->>S : 设置初始状态
R->>R : 获取入口节点
loop 执行循环
R->>N : 并发执行节点
N->>S : 更新状态
R->>R : 计算下一节点
R->>R : 检查中断点
alt 执行完成
R->>R : 返回最终状态
else 继续执行
R->>R : 更新当前节点
end
end
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L174-L492)
执行配置 #
框架支持丰富的执行配置选项:
| 配置项 | 类型 | 描述 | 用途 |
|---|---|---|---|
| Configurable | map | 可配置参数 | 线程ID、用户ID等 |
| Metadata | map | 元数据信息 | 请求ID、标签等 |
| Callbacks | []Callback | 回调函数列表 | 监听执行事件 |
| InterruptBefore | []string | 中断前节点 | 暂停执行 |
| InterruptAfter | []string | 中断后节点 | 暂停执行 |
段落来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L174-L492)
- [examples/configuration/main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/configuration/main.go#L53-L62)
并行执行机制 #
并行节点架构 #
框架提供了多种并行执行模式来提高性能:
classDiagram
class ParallelNode {
+[]Node nodes
+string name
+Execute(ctx, state) interface
}
class MapReduceNode {
+[]Node mapNodes
+func reducer
+string name
+Execute(ctx, state) interface
}
class ParallelNode {
+Execute() []interface
+collectResults()
}
ParallelNode --> Node : "包含多个"
MapReduceNode --> ParallelNode : "扩展"
图表来源
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L9-L21)
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L101-L116)
并行执行流程 #
flowchart TD
A["启动并行执行"] --> B["创建工作协程"]
B --> C["并发执行节点"]
C --> D["收集执行结果"]
D --> E{"是否有错误?"}
E --> |是| F["返回错误"]
E --> |否| G["合并结果"]
G --> H["返回并行结果"]
I["错误处理"] --> J["记录第一个错误"]
J --> K["等待所有协程完成"]
K --> L["清理资源"]
图表来源
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L23-L83)
MapReduce 模式 #
MapReduce 是一种特殊的并行模式,先并行处理再聚合结果:
graph LR
A["输入数据"] --> B["Map阶段<br/>并行处理"]
B --> C["Reduce阶段<br/>结果聚合"]
C --> D["最终结果"]
B1["Worker 1"] --> C
B2["Worker 2"] --> C
B3["Worker 3"] --> C
图表来源
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L101-L132)
段落来源
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L23-L83)
- [parallel.go](https://github.com/smallnest/langgraphgo/blob/main/graph/parallel.go#L101-L132)
子图嵌套与复合图 #
子图概念 #
子图允许将复杂的流程分解为更小的、可重用的组件:
graph TD
A["主图"] --> B["子图1"]
A --> C["子图2"]
A --> D["普通节点"]
B --> B1["验证子图"]
B --> B2["格式检查"]
B --> B3["内容清理"]
C --> C1["处理子图"]
C --> C2["内容转换"]
C --> C3["内容增强"]
图表来源
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L8-L27)
子图实现机制 #
classDiagram
class Subgraph {
+string name
+MessageGraph graph
+Runnable runnable
+Execute(ctx, state) interface
}
class RecursiveSubgraph {
+string name
+MessageGraph graph
+int maxDepth
+func condition
+Execute(ctx, state) interface
+executeRecursive(ctx, state, depth)
}
class CompositeGraph {
+map[string]MessageGraph graphs
+MessageGraph main
+AddGraph(name, graph)
+Connect(from, to, transform)
+Compile() Runnable
}
Subgraph --> MessageGraph : "包装"
RecursiveSubgraph --> MessageGraph : "递归执行"
CompositeGraph --> MessageGraph : "组合多个"
图表来源
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L8-L27)
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L108-L140)
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L56-L96)
嵌套深度控制 #
递归子图提供了深度控制机制,防止无限递归:
flowchart TD
A["递归开始"] --> B{"检查最大深度"}
B --> |超过限制| C["返回当前状态"]
B --> |未超限| D{"检查继续条件"}
D --> |满足条件| E["编译并执行子图"]
D --> |不满足| C
E --> F{"递归调用"}
F --> |继续| A
F --> |结束| G["返回最终结果"]
图表来源
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L130-L159)
段落来源
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L8-L27)
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L108-L140)
- [subgraph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/subgraph.go#L56-L96)
状态管理与模式 #
状态模式架构 #
框架提供了灵活的状态管理模式,支持多种数据结构和更新策略:
classDiagram
class StateSchema {
<<interface>>
+Init() interface
+Update(current, new) interface
}
class CleaningStateSchema {
<<interface>>
+Cleanup(state) interface
}
class MapSchema {
+map[string]Reducer Reducers
+map[string]bool EphemeralKeys
+Init() interface
+Update(current, new) interface
+Cleanup(state) interface
+RegisterReducer(key, reducer)
+RegisterChannel(key, reducer, isEphemeral)
}
class Reducer {
<<function>>
+call(current, new) interface
}
StateSchema <|-- CleaningStateSchema
StateSchema <|-- MapSchema
MapSchema --> Reducer : "使用"
图表来源
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L12-L27)
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L29-L55)
常用状态模式 #
| 模式 | 描述 | 使用场景 | 示例 |
|---|---|---|---|
| 覆盖模式 | 直接替换旧值 | 简单状态更新 | 用户信息更新 |
| 追加模式 | 添加到现有集合 | 列表累积 | 日志记录、任务队列 |
| 自定义模式 | 复杂状态合并 | 业务特定逻辑 | 分数计算、计数器 |
状态更新流程 #
sequenceDiagram
participant N as "节点"
participant S as "状态模式"
participant R as "Reducer"
participant T as "目标状态"
N->>S : 提交新状态
S->>S : 检查键是否存在
alt 有自定义Reducer
S->>R : 调用Reducer
R->>T : 生成合并状态
else 默认覆盖
S->>T : 直接赋值
end
T->>S : 返回最终状态
S->>N : 更新成功
图表来源
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L62-L99)
段落来源
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L12-L27)
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L29-L55)
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L62-L99)
配置与流程控制 #
配置系统架构 #
框架提供了分层的配置系统,支持运行时动态调整:
graph TD
A["配置层次"] --> B["全局配置"]
A --> C["图级配置"]
A --> D["节点配置"]
B --> B1["线程ID"]
B --> B2["超时设置"]
B --> B3["重试策略"]
C --> C1["入口点"]
C --> C2["状态模式"]
C --> C3["回调函数"]
D --> D1["中断点"]
D --> D2["优先级"]
D --> D3["资源限制"]
图表来源
- [examples/configuration/main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/configuration/main.go#L53-L62)
流程控制机制 #
| 控制类型 | 实现方式 | 用途 | 示例 |
|---|---|---|---|
| 中断控制 | InterruptBefore/After | 暂停执行 | 人工审核、调试 |
| 条件跳转 | 条件边 | 动态路由 | 错误处理、功能开关 |
| 循环控制 | 递归子图 | 重复执行 | 数据处理、批处理 |
| 并行控制 | 并行节点 | 性能优化 | 多任务处理 |
配置传递机制 #
sequenceDiagram
participant U as "用户"
participant R as "Runnable"
participant C as "上下文"
participant N as "节点"
U->>R : InvokeWithConfig(config)
R->>C : 创建配置上下文
R->>N : 传递上下文
N->>C : 获取配置信息
C->>N : 返回配置数据
N->>N : 使用配置执行
图表来源
- [examples/configuration/main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/configuration/main.go#L23-L41)
段落来源
- [examples/configuration/main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/configuration/main.go#L53-L62)
错误处理与中断机制 #
错误处理架构 #
框架提供了多层次的错误处理机制:
flowchart TD
A["节点执行"] --> B{"是否出错?"}
B --> |否| C["正常返回"]
B --> |是| D["错误分类"]
D --> E["节点中断"]
D --> F["可重试错误"]
D --> G["致命错误"]
E --> H["GraphInterrupt"]
F --> I["重试机制"]
G --> J["终止执行"]
I --> K{"重试次数"}
K --> |未超限| L["延迟重试"]
K --> |已超限| J
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L24-L41)
中断机制 #
中断机制允许在执行过程中暂停和恢复:
stateDiagram-v2
[*] --> 执行中
执行中 --> 中断前检查 : 检查中断点
中断前检查 --> 中断 : 需要中断
中断前检查 --> 执行中 : 继续执行
中断 --> 暂停执行
暂停执行 --> 恢复执行 : 提供恢复值
恢复执行 --> 执行中
执行中 --> 执行完成 : 正常结束
执行完成 --> [*]
中断 --> 错误结束 : 无法恢复
错误结束 --> [*]
图表来源
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L24-L41)
- [context.go](https://github.com/smallnest/langgraphgo/blob/main/graph/context.go#L7-L16)
重试策略 #
StateGraph 提供了完善的重试机制:
| 策略类型 | 描述 | 适用场景 | 示例 |
|---|---|---|---|
| 固定间隔 | 每次重试间隔相同 | 网络请求、数据库查询 | HTTP API调用 |
| 指数退避 | 间隔逐渐增加 | 网络不稳定环境 | 文件上传 |
| 线性退避 | 间隔线性增长 | 渐进式降级 | 负载均衡 |
段落来源
- [state_graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/state_graph.go#L299-L396)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L24-L41)
实际应用案例 #
基础工作流示例 #
基础示例展示了图结构的基本使用方法:
graph LR
A["开始"] --> B["处理节点"]
B --> C["结束"]
D["节点函数"] --> E["接收输入"]
E --> F["处理逻辑"]
F --> G["返回结果"]
图表来源
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/basic_example/main.go#L25-L35)
并行执行案例 #
并行执行展示了如何利用多核处理器:
graph TD
A["开始"] --> B["分支A"]
A --> C["分支B"]
A --> D["分支C"]
B --> E["聚合器"]
C --> E
D --> E
E --> F["结束"]
style B fill:#e1f5fe
style C fill:#e8f5e8
style D fill:#fff3e0
图表来源
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/parallel_execution/main.go#L63-L77)
条件路由案例 #
条件边展示了动态路由的能力:
flowchart TD
A["分析意图"] --> B{"关键词匹配"}
B --> |问句| C["问题处理"]
B --> |命令| D["命令处理"]
B --> |反馈| E["反馈处理"]
B --> |默认| C
C --> F["结束"]
D --> F
E --> F
图表来源
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/conditional_edges_example/main.go#L66-L88)
子图嵌套案例 #
子图展示了模块化设计的优势:
graph TD
A["接收文档"] --> B["验证子图"]
B --> C["处理子图"]
C --> D["结束"]
subgraph "验证子图"
B1["格式检查"]
B2["内容清理"]
B1 --> B2
B2 --> B3["验证完成"]
end
subgraph "处理子图"
C1["内容转换"]
C2["内容增强"]
C1 --> C2
C2 --> C3["处理完成"]
end
图表来源
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/subgraph/main.go#L21-L60)
段落来源
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/basic_example/main.go#L25-L35)
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/parallel_execution/main.go#L63-L77)
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/conditional_edges_example/main.go#L66-L88)
- [main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/subgraph/main.go#L21-L60)
总结 #
langgraphgo 的图结构工作流机制为开发者提供了一个强大而灵活的框架来构建复杂的业务流程。通过节点、边、条件边等基本组件,可以构建从简单线性流程到复杂并行和嵌套流程的各种应用场景。
核心优势 #
- 模块化设计:每个节点都是独立的功能单元,便于开发和维护
- 灵活路由:支持静态边和条件边,能够处理复杂的业务逻辑分支
- 高性能执行:内置并行执行机制,充分利用现代硬件资源
- 状态管理:提供多种状态模式,适应不同的数据处理需求
- 可观测性:完整的追踪和回调系统,便于监控和调试
最佳实践建议 #
- 合理划分节点:保持节点功能单一,避免过于复杂的逻辑
- 谨慎使用条件边:过多的条件判断会影响可读性和性能
- 充分利用并行:对于独立的任务,优先考虑并行执行
- 设计良好的状态模式:根据业务需求选择合适的更新策略
- 适当的错误处理:为关键节点配置重试机制和错误恢复策略
发展方向 #
langgraphgo 的图结构工作流机制为未来的扩展奠定了坚实的基础,包括但不限于:
- 更丰富的可视化工具
- 更智能的自动优化
- 更强大的分布式执行能力
- 更完善的监控和调试功能
通过深入理解和掌握这些概念和机制,开发者可以构建出更加高效、可靠和可维护的应用程序流程。