4 human in loop中间件

LangChain 1.0 实战:Human-in-the-Loop 中间件详解 ------ 让 AI 更安全、更可控

作者 :AI 工程师
日期 :2026 年 3 月 26 日
关键词:LangChain、HumanInTheLoopMiddleware、人机协作、Agent 安全、MCP

在大模型智能体(Agent)日益普及的今天,如何确保其行为安全、合规、可审计 ,成为落地关键。LangChain 1.0 推出的 HumanInTheLoopMiddleware(人机协作中间件),正是解决这一问题的利器。

本文将深入介绍该中间件的原理、配置方式与实战技巧,助你构建高可信度的 AI 应用


一、什么是 Human-in-the-Loop(HITL)?

Human-in-the-Loop(人机协作) 是一种在 AI 决策流程中引入人类监督的机制。当 Agent 尝试执行高风险操作 (如删除数据、发送邮件、修改配置)时,系统会自动暂停,等待人工审核后再继续。

✅ 典型场景:

  • 发送包含敏感信息的邮件
  • 执行 SQL 删除语句
  • 修改生产环境配置
  • 调用金融交易接口

LangChain 通过 HumanInTheLoopMiddleware 将 HITL 能力标准化,无缝集成到 Agent 工作流中。


二、核心能力:三种人工决策类型

当中间件触发中断后,人类可选择以下三种操作:

决策类型 说明 示例
approve(批准) 按原参数执行工具调用 直接发送草稿邮件
✏️ edit(编辑) 修改工具参数后执行 修改收件人或正文后再发
reject(拒绝) 取消操作,并反馈原因 拒绝发送,并要求重写

⚠️ 注意:是否允许 edit 取决于你在 interrupt_on 中的配置。


  1. 中断触发条件(interrupt_on)
    True:启用所有决策类型(approve/edit/reject)
    False:跳过审核
    字典形式:精细控制允许的决策类型
python 复制代码
HumanInTheLoopMiddleware(
            interrupt_on={
                "write_file": True, #写文件需要确认文件名和内容
                "execute_sql": True,# 执行SQL需要确认SQL语句
                "send_email": {
                    "allowed_decisions": ["approve", "edit", "reject"],
                    "description": "发送邮件需要确认收件人和内容"
                },
                "read_data": False,
            },
            description_prefix="敏感操作需要您的批准",
        )

三、快速上手:配置 HumanInTheLoopMiddleware

1. 简单的代码

python 复制代码
# -*- coding: utf-8 -*-
"""
agent_with_memory
Author: user
Date: 2026/3/16
Description:
"""
from langchain.agents import create_agent, AgentState
from langgraph.checkpoint.memory import InMemorySaver
from langchain_core.tools import tool
from langchain_community.callbacks import get_openai_callback
from langchain.chat_models import init_chat_model
from langchain.agents.middleware import HumanInTheLoopMiddleware
import os
from dotenv import load_dotenv
from langgraph.types import Command

load_dotenv(override=True)

model = init_chat_model(model="qwen2-72b", #
                        model_provider='openai',
                        api_key= os.getenv("api_key"),
                        base_url= os.getenv("base_url"),
                        temperature=0.3,
                        max_retries=4,
                        #max_tokens=10
                        )

# 1. 系统提示词
system_prompt = """你是多功能助手,可以执行写文件、执行SQL、发送邮件、读取数据等操作。"""

# 2. 定义工具
@tool
def write_file(filename: str, content: str) -> str:
    """写入文件"""
    with open(filename, "w", encoding="utf-8") as f:
        f.write(content)
    print("文件写入成功!")
    return f"已写入文件: {filename}"

@tool
def execute_sql(query: str) -> str:
    """执行 SQL 查询"""
    # 实际应用中连接数据库
    return f"执行 SQL: {query}"

@tool
def send_email(to: str, subject: str, body: str) -> str:
    """发送邮件"""
    return f"已发送邮件到 {to}"

@tool
def read_data(source: str) -> str:
    """读取数据(安全操作)"""
    return f"读取数据: {source}"



# 4. 添加记忆
checkpointer = InMemorySaver()

# 5. 创建 Agent
agent = create_agent(
    model=model,
    tools=[write_file, execute_sql,send_email,read_data],
    system_prompt=system_prompt,
    checkpointer=checkpointer,
    middleware=[
        HumanInTheLoopMiddleware(
            interrupt_on={
                "write_file": True, #写文件需要确认文件名和内容
                "execute_sql": True,# 执行SQL需要确认SQL语句
                "send_email": {
                    "allowed_decisions": ["approve", "edit", "reject"],
                    "description": "发送邮件需要确认收件人和内容"
                },
                "read_data": False,
            },
            description_prefix="敏感操作需要您的批准",
        ),
    ],
)

# 6. 运行对话
config = {"configurable": {"thread_id": "user-001"}}



# 初始调用
result = agent.invoke(
    {"messages": [{"role": "user", "content": "在文件 test.txt 中写入内容:Hello, World!"}]},
    config=config
)

# 中断后必须用 Command(resume=...) 恢复;HumanInTheLoopMiddleware 期望 HITLResponse:
# {"decisions": [{"type": "approve"|"edit"|"reject", ...}]}
# 不能传 {"decision": "approve"},否则 interrupt() 收不到决策,工具不会执行。
if result.get("__interrupt__"):
    print(f"等待审批: {result['__interrupt__']}")

    choice = input("批准? (y/n/e): ").lower().strip()

    if choice == "y":
        result = agent.invoke(
            Command(resume={"decisions": [{"type": "approve"}]}),
            config=config,
        )
    elif choice == "e":
        new_content = input("输入新的文件内容: ")
        result = agent.invoke(
            Command(
                resume={
                    "decisions": [
                        {
                            "type": "edit",
                            "edited_action": {
                                "name": "write_file",
                                "args": {
                                    "filename": "safe_test.txt",
                                    "content": new_content,
                                },
                            },
                        }
                    ]
                }
            ),
            config=config,
        )
    else:
        result = agent.invoke(
            Command(
                resume={
                    "decisions": [
                        {
                            "type": "reject",
                            "message": "用户拒绝执行 write_file",
                        }
                    ]
                }
            ),
            config=config,
        )

print(result['messages'][-1].content)

2 api接口对答题

相关推荐
曦樂~7 分钟前
【机器学习】分类Classification
人工智能·机器学习·分类
cd_949217217 分钟前
新北洋亮相2026 CHINASHOP:以“智印零售全生态”赋能效率与增长
大数据·人工智能·零售
lizz6669 分钟前
Hermes-Agent:使用SOUL.md设定智能体身份
人工智能
3Dmax效果图渲染研习社11 分钟前
ai生成的视频有没有版权?注意事项
人工智能·ai作画·aigc
llm大模型算法工程师weng11 分钟前
人格识别模型:理解人类行为差异的心理测量工具
人工智能
IoT物联网产品手记16 分钟前
IoT产品模块化架构设计:从功能堆叠到能力组合的系统方法
大数据·人工智能·物联网
C系语言19 分钟前
ONNX Runtime安装
人工智能·python·深度学习
OneThingAI21 分钟前
网心技术 | NemoClaw 深度解析,企业级 AI 运行时
人工智能·aigc·agent·openclaw·onethingai
慕涯AI24 分钟前
Agent 30 课程开发指南 - 第16课
人工智能·python
小程故事多_8029 分钟前
从基础Agent到复杂工作流,LangGraph如何用状态机重构智能体开发
人工智能·设计模式·重构·aigc·ai编程