人机协作 #
本文档中引用的文件
目录 #
简介 #
本教程基于 human_in_the_loop 和 dynamic_interrupt 示例,深入讲解如何在工作流中插入人工审核节点,实现“暂停-审批-继续”的交互模式。我们将分析 LangGraphGo 中中断机制的实现原理,包括中断条件设置、恢复凭证生成与验证,并提供内容审核、敏感操作确认等实际应用场景。
Section sources
- [human_in_the_loop\README.md](https://github.com/smallnest/langgraphgo/blob/main/examples/human_in_the_loop/README.md)
- [dynamic_interrupt\README.md](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/README.md)
项目结构 #
项目结构清晰地分为 checkpoint、examples、graph、prebuilt、showcases 和 tool 等主要目录。本教程重点关注 examples 目录下的 human_in_the_loop 和 dynamic_interrupt 示例,它们是实现人机协作的核心参考。
graph TD
A[langgraphgo] --> B[examples]
B --> C[human_in_the_loop]
B --> D[dynamic_interrupt]
A --> E[graph]
E --> F[核心中断逻辑]
C --> G[main.go]
C --> H[README.md]
D --> I[main.go]
D --> J[README.md]
Diagram sources
- [examples\human_in_the_loop\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/human_in_the_loop/main.go)
- [examples\dynamic_interrupt\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/main.go)
核心组件 #
本教程的核心组件包括 human_in_the_loop 和 dynamic_interrupt 两个示例,它们分别展示了静态和动态中断机制。human_in_the_loop 示例通过 InterruptBefore 配置在特定节点前暂停,而 dynamic_interrupt 示例则通过 graph.Interrupt() 函数在节点内部动态触发中断。
Section sources
- [human_in_the_loop\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/human_in_the_loop/main.go)
- [dynamic_interrupt\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/main.go)
架构概述 #
人机协作的架构基于 LangGraphGo 的图执行模型,通过配置中断和恢复机制,实现工作流的暂停与继续。核心架构包括工作流定义、中断配置、状态管理、恢复执行等部分。
graph TB
subgraph "工作流执行"
A[初始运行] --> B{是否中断?}
B --> |是| C[返回 GraphInterrupt]
B --> |否| D[完成执行]
C --> E[人工交互]
E --> F[更新状态]
F --> G[恢复执行]
G --> H[继续工作流]
H --> D
end
Diagram sources
- [graph\errors.go](https://github.com/smallnest/langgraphgo/blob/main/graph/errors.go)
- [graph\interrupt_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/interrupt_test.go)
详细组件分析 #
human_in_the_loop 示例分析 #
该示例展示了如何在工作流中插入一个“人工审批”节点。通过设置 InterruptBefore 配置,工作流在进入 human_approval 节点前暂停,等待人工审批。
静态中断机制 #
sequenceDiagram
participant 应用 as 应用程序
participant 可运行 as Runnable
participant 图 as 图
应用->>可运行 : InvokeWithConfig(初始状态, config)
可运行->>图 : 执行节点
图->>图 : 执行到 process_request
图->>图 : 准备进入 human_approval
图->>可运行 : 返回 GraphInterrupt 错误
可运行->>应用 : 返回 GraphInterrupt
应用->>应用 : 捕获中断,展示状态
应用->>应用 : 人工审批 (Approved = true)
应用->>可运行 : InvokeWithConfig(更新状态, resumeConfig)
可运行->>图 : 从 human_approval 恢复
图->>图 : 继续执行后续节点
图->>可运行 : 返回最终结果
可运行->>应用 : 返回最终结果
Diagram sources
- [human_in_the_loop\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/human_in_the_loop/main.go#L67-L118)
dynamic_interrupt 示例分析 #
该示例展示了如何在节点内部动态触发中断。通过调用 graph.Interrupt() 函数,节点可以在运行时暂停,等待外部输入。
动态中断机制 #
sequenceDiagram
participant 应用 as 应用程序
participant 可运行 as Runnable
participant 节点 as ask_name 节点
应用->>可运行 : Invoke(nil)
可运行->>节点 : 执行
节点->>节点 : graph.Interrupt("What is your name?")
节点->>可运行 : 返回 NodeInterrupt 错误
可运行->>应用 : 返回 GraphInterrupt 错误
应用->>应用 : 捕获中断,获取查询
应用->>应用 : 获取用户输入 ("Alice")
应用->>可运行 : InvokeWithConfig(nil, resumeConfig)
可运行->>节点 : 重新执行
节点->>节点 : graph.Interrupt() 返回 "Alice"
节点->>可运行 : 返回最终结果
可运行->>应用 : 返回最终结果
Diagram sources
- [dynamic_interrupt\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/main.go#L40-L70)
中断机制实现原理 #
LangGraphGo 的中断机制基于 GraphInterrupt 错误类型和 Config 配置。当工作流需要暂停时,会抛出 GraphInterrupt 错误,其中包含当前状态和中断节点信息。恢复执行时,通过 ResumeFrom 配置指定从哪个节点继续。
中断与恢复流程 #
flowchart TD
Start([开始]) --> CheckInterrupt["检查中断条件"]
CheckInterrupt --> |满足| CreateInterrupt["创建 GraphInterrupt 错误"]
CreateInterrupt --> ReturnError["返回错误"]
CheckInterrupt --> |不满足| ExecuteNode["执行节点逻辑"]
ExecuteNode --> CheckDynamic["检查动态中断"]
CheckDynamic --> |调用 Interrupt| CreateNodeInterrupt["创建 NodeInterrupt"]
CreateNodeInterrupt --> ReturnError
CheckDynamic --> |无中断| NextStep["下一步"]
ReturnError --> End([暂停])
NextStep --> End
Diagram sources
- [graph\errors.go](https://github.com/smallnest/langgraphgo/blob/main/graph/errors.go#L24-L41)
- [graph\resume_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/resume_test.go#L30-L81)
依赖分析 #
人机协作功能依赖于 LangGraphGo 核心库的中断、恢复和检查点功能。graph 包提供了 GraphInterrupt 错误类型、Config 结构体以及 Interrupt 函数,是实现人机协作的基础。
graph TD
A[human_in_the_loop] --> B[graph]
C[dynamic_interrupt] --> B[graph]
B --> D[GraphInterrupt]
B --> E[Config]
B --> F[Interrupt]
D --> G[Node]
D --> H[State]
D --> I[NextNodes]
E --> J[InterruptBefore]
E --> K[ResumeFrom]
E --> L[ResumeValue]
Diagram sources
- [graph\errors.go](https://github.com/smallnest/langgraphgo/blob/main/graph/errors.go)
- [graph\checkpointing.go](https://github.com/smallnest/langgraphgo/blob/main/graph/checkpointing.go)
性能考虑 #
使用中断机制时,需考虑状态序列化和持久化的开销。对于长时间运行的工作流,建议使用持久化存储(如 PostgreSQL、Redis)来保存检查点,以确保系统崩溃后能够恢复。
故障排除指南 #
常见问题包括中断未触发、恢复失败等。确保 InterruptBefore 或 InterruptAfter 的节点名称正确,恢复时传递正确的 ResumeFrom 配置和更新后的状态。
Section sources
- [graph\interrupt_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/interrupt_test.go)
- [graph\resume_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/resume_test.go)
结论 #
通过 human_in_the_loop 和 dynamic_interrupt 示例,我们学习了如何在 LangGraphGo 中实现人机协作。静态中断适用于预定义的审批节点,而动态中断则提供了更大的灵活性,允许在节点内部根据逻辑决定是否中断。这些机制为构建安全、可控的智能代理应用提供了强大支持。