深度智能体-人机回环

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

)

相关推荐
我是大卫2 小时前
【图】一图理解LLM、LangChain、RAG、AI Agent、MCP、token、context、prompt、tool概念
langchain·llm·agent
phltxy3 小时前
LangChain v1 Agent核心能力详解
大数据·人工智能·langchain
thesky1234564 小时前
27届大模型岗面试准备(五):预训练全流程拆解——从数据清洗到 Tokenizer 再到 PT 的每
人工智能·面试·大模型·预训练·tokenizer
就是一顿骚操作4 小时前
Agent Loop 入门到深入:从 ReAct 到 LangGraph、Agents SDK 与 MCP
人工智能·大模型·llm·论文解读·ai agent
YUS云生4 小时前
大模型学习·第40天:LangChain框架入门——从RAG原理到模型调用的统一接口
学习·langchain
在水一缸4 小时前
CarPlay Is Additive:从技术视角重新审视车载系统的“叠加”哲学
车载系统·系统架构·移动开发·人机交互·车联网·carplay
就是一顿骚操作6 小时前
Hermes Agent 入门到深入:会学习的开源 AI Agent 是怎么工作的
人工智能·大模型·论文解读·ai agent·hermes agent
BraveWang6 小时前
【LangChain 1.x】14、补充|Runtime 运行时与上下文工程
langchain
meilindehuzi_a7 小时前
Node.js + LangChain.js + Milvus 实战:从 EPUB 入库到《天龙八部》RAG 问答系统
javascript·langchain·node.js
爱奥尼欧9 小时前
【LangChain】6.聊天模型-工具调用
langchain