深度智能体-人机回环

1.概述

与图和智能体一样,在深度智能体中也需要对一些有潜在风险的工具调用进行检查和确认,可以在创建深度智能体时通过interrupt_on来对不同的工具配置不同的检查策略。对于工具---策略键值对,如果策略为True,则允许所有的策略(approve, edit, reject),如果策略为False,则不允许工具有中断,策略为allowed:{},则可以更细粒度控制策略。本文将对深度智能体的人机回环进行详细介绍。

2.实现人机回环

1)定义两个工具

如下示例代码中有两个工具,get_enterprise_info从store查询企业信息,save_enterprise_info把企业信息保存到store中:

from langchain.tools import tool, ToolRuntime

from typing import Any

@tool

def get_enterprise_info(uniscid: str, runtime: ToolRuntime) -> dictstr, Any:

"""Look up enterprise info."""

store = runtime.store

enterprise_info = store.get(("enterprises",), uniscid)

return enterprise_info if enterprise_info else "Unknown enterprise"

@tool

def save_enterprise_info(uniscid: str, enterprise_info: dictstr, Any, runtime: ToolRuntime) -> str:

"""Save enterprise info."""

store = runtime.store

store.put(("enterprises",), uniscid, enterprise_info)

return "Successfully saved enterprise info."

2)创建大模型实例

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(

model = 'qwen-plus',

api_key = "sk-*",

base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1")

3)创建智能体设置中断

from langgraph.checkpoint.memory import InMemorySaver

from langgraph.store.memory import InMemoryStore

from deepagents import create_deep_agent

saver = InMemorySaver() #必须有检查点,才能支持用户回环

store = InMemoryStore()

agent = create_deep_agent(

model=llm,

tools=get_enterprise_info, save_enterprise_info,

interrupt_on={

"get_enterprise_info": False, # No interrupts needed

"save_enterprise_info": {"allowed_decisions": "approve", "edit", "reject"},

},

checkpointer=saver,

store=store

)

4)处理中断

允许工具调用:

if result.get("interrupt"):

Extract interrupt information

interrupts = result"__interrupt__"0.value

action_requests = interrupts"action_requests"

review_configs = interrupts"review_configs"

Create a lookup map from tool name to review config

config_map = {cfg"action_name": cfg for cfg in review_configs}

Display the pending actions to the user

for action in action_requests:

review_config = config_mapaction\["name"]

print(f"Tool: {action'name'}")

print(f"Arguments: {action'args'}")

print(f"Allowed decisions: {review_config'allowed_decisions'}")

Get user decisions (one per action_request, in order)

decisions = [

{"type": "approve"} # User approved the deletion

]

Resume execution with decisions

result = agent.invoke(

Command(resume={"decisions": decisions}),

config=config # Must use the same config!

)

Process final result

print(result"messages"-1.content)

拒绝工具调用,仅修改decisions即可:

#以上同上

decisions = [

{"type": "approve"} # User approved the deletion

]

#以下同上

对调用参数进行修改,仅修改decisions即可,其中传入修改后的参数:

"decisions": [

{

"type": "edit",

修改后的数据

"edited_action": {

工具名

"name": "save_enterprise_info",

调用工具时的参数.

"args": {

'uniscid': '51234569876543210',

'enterprise_info':{

"name": "东邪西毒文化娱乐有限公司",

"legal": "黄老邪",

"type": "内资企业"

}

},

}

}

]

3.多工具调用

当调用多个工具产生中断时,所有的中断会打包在一起,在恢复中断时,需要按照顺序分别提供恢复策略:

decisions = [

{"type": "approve"}, # 对于第一个中断,采取同意操作

{"type": "reject"} # 对于第二个中断,采取拒绝操作

]

4.子智能体中断处理

子智能体可以有自己的中断,如下代码说明:

agent = create_deep_agent(

model = llm,

tools=get_enterprise_info, save_enterprise_info,

interrupt_on={

"get_enterprise_info": True,

"save_enterprise_info": False,

},

subagents=[{

"name": "enterprise-info-manager",

"description": "Manages enterprise information operations",

"system_prompt": "You are a file enterprise management assistant.",

"tools": get_enterprise_info, save_enterprise_info,

"interrupt_on": {

在子智能体中覆盖主智能体中的策略

"get_enterprise_info": True,

"save_enterprise_info": True, # 与主智能体不同

}

}],

checkpointer=checkpointer

)

相关推荐
circuitsosk7 小时前
AI输出的“质检员”:构建智能体质量评估、异常检测与人工兜底的三层防线
人工智能·python·microsoft·正则表达式·langchain
l12586511 小时前
# RAG重排序实战:硅基流动bge-reranker-v2-m3在线API vs 本地CrossEncoder,一篇讲透两种方案
数据库·人工智能·python·深度学习·算法·机器学习·langchain
JaydenAI13 小时前
[基于AgentEvals的自动化评估-06]以LLM-as-a-Judge评估LangGraph执行轨迹
ai·langchain·agent·evaluation·openevals·agentevals
北斗落凡尘15 小时前
LangGraph 入门实战(10)--时光回溯
python·langchain
拾年27515 小时前
让 AI 自己卷自己:50 行代码实现「LLM 当评委 + 多选一」的 Harness 工程
langchain·llm
漂流瓶jz15 小时前
一文读懂大模型生态:分类/参数/结构/训练/GPU/评测/排行/社区
人工智能·大模型·llm·openai·nvidia·deepseek·anthropic
Raas10015 小时前
MAI Gateway(魔芋企业级AI网关):私有化部署的身份同步与数据内控设计
网关·网络安全·大模型·gateway·mai gateway·企业级网关
虎鲸不是鱼16 小时前
【RTX Pro 4500】Qwen3.8 27B NVFP4在LM Studio挂载多模态模型
大模型·多模态·qwen·lm studio·千问
dogstarhuang16 小时前
实战:用 API 网关统一接入 GPT-5.6,多模型路由怎么省下 80% 成本
网络·gpt·大模型·api网关·ai推理·模型选型·按量计费