深度智能体-人机回环

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

)

相关推荐
Raas1001 小时前
MAI Gateway(魔芋企业级AI网关)技术辨析:AI网关和大模型网关区别?选型必读
大数据·人工智能·大模型·gateway·mai gateway·企业级产品
是Dream呀11 小时前
一个 AI Agent 是怎么长出来的:提示词、上下文与 Harness 工程
人工智能·大模型·agent
JavaPub-rodert12 小时前
LangChain 从入门到 Agent 实战:用 Python 搭建一个真正能调用工具和知识库的 AI 助手
人工智能·python·langchain
烬羽16 小时前
让大模型输出 JSON:从"正则剥 markdown"到"让它调个工具",一条可靠性升级之路
langchain·llm·openai
thesky12345617 小时前
27届大模型面试准备(七十九):大模型异构算力适配与跨芯片部署工程——算子适配、图编译与性能对齐
大模型·面试准备
梦想不只是梦与想20 小时前
大模型系列(二):技术基础与核心能力
python·大模型·token
tachibana221 小时前
复杂的 RAG 范式
数据库·人工智能·ai·大模型·agent
CoderJia程序员甲21 小时前
GitHub 热榜项目 - 周榜(2026-09-06)
ai·大模型·llm·github·ai教程
梦想的颜色21 小时前
【AI速览】GPT‑6 Astra 硬核深度解析:不是噱头 AGI,而是面向端到端 Agent 工作流的前沿旗舰
gpt·大模型·openai·agent·deepseek·gpt6‑astra·大模型横评
爱笑的k1121 小时前
深度学习常见层输入输出维度对照参考
大模型·ai infra