深度智能体-人机回环

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

)

相关推荐
张彦峰ZYF13 小时前
从嵌入、表征到潜空间:理解大模型向量世界的三种视角
人工智能·大模型·向量空间
倾颜13 小时前
从本地 Ollama 到线上多模型 Runtime:接入 DeepSeek / Qwen 的实战复盘
langchain·next.js·deepseek
伊布拉西莫14 小时前
LangChain LCEL源码深度剖析
python·langchain
沪漂阿龙15 小时前
《LangChain 系列》Human-in-the-loop:什么时候必须让人工介入?
人工智能·架构·langchain
桜吹雪15 小时前
所有智能体架构(3):Planning(计划任务)
javascript·人工智能·langchain
troubles maker16 小时前
LoRA: Low-Rank Adaptation of Large Language Models
语言模型·自然语言处理·lora·大模型·peft
SXJR18 小时前
spring boot + langchain4j +milvus实现向量存储
java·spring boot·后端·大模型·milvus·rag·langchain4j
智海观潮18 小时前
UniScientist:30B开源科研大模型突破,重构AI自主研究范式
人工智能·ai·大模型
技术达芬奇18 小时前
开启你的 Agent 时代:LangChain + LangGraph 项目开发入门与语言堆栈抉择
langchain·agent
BSD_HY18 小时前
2026年FSR传感器行业报告:市场规模、竞争格局与发展趋势
人机交互·制造·fsr·薄膜开关·深圳工厂