2026/6/20 3:00:01
网站建设
项目流程
网上购物网站模板,欧派全屋定制多少钱一平米,济南市建设招标中心网站,与建设部网站前期导览#xff1a;
大家好#xff0c;本期我们继续研究Open Deep Research这个项目的源代码。上一期我们学习了动态模型配置的实现#xff0c;本期我们将重点关注多Agent架构的设计与实现。
在多Agent系统中#xff0c;如何设计Agent之间的协作机制、如何实现任务的分解…前期导览大家好本期我们继续研究Open Deep Research这个项目的源代码。上一期我们学习了动态模型配置的实现本期我们将重点关注多Agent架构的设计与实现。在多Agent系统中如何设计Agent之间的协作机制、如何实现任务的分解与委派、如何管理不同Agent的状态这些都是多Agent架构设计中的核心问题。其实看了Open Deep Research的源代码可以发现从当前比较主流的Agent定义出发这个项目主要涉及两个Agent通过子图实现一个负责研究活动的监督、组织一个负责具体实施研究活动。下面我们来深入理解下Open Deep Research的架构设计思路与实现方式。Open Deep Research的三级分层嵌套结构概览从整体上来看Open Deep Research采用了三级分层嵌套的图结构来实现多Agent协作每一层都有明确的职责分工。先上一个图给大家感受一下大图太大有点糊后面会有分开的清晰版•第一层主图deep\_researcher负责整个研究流程的协调与管理。主图包含四个核心阶段用户澄清clarify_with_user用于确认研究需求、研究计划write_research_brief用于将用户输入转换为详细的研究简报、研究执行research_supervisor通过调用监督者子图来执行实际研究、最终报告生成final_report_generation用于综合所有研究结果生成最终报告。•第二层监督者子图supervisor\_subgraph作为主图的一个节点负责研究任务的分解与委派。监督者分析研究简报通过ConductResearch工具将复杂研究任务分解为多个独立的子任务并可以并行委派给多个研究者子图实例。监督者还使用ResearchComplete工具判断研究是否完成使用think_tool进行战略规划确保研究方向的正确性和完整性。•第三层研究者子图researcher\_subgraph在监督者子图内部被动态调用负责执行具体的研究任务。每个研究者子图接收一个特定的研究主题使用搜索工具tavily_search、web_search、MCP工具等进行信息收集使用think_tool进行战略规划最后通过compress_research节点压缩研究结果并返回给监督者。这种分层嵌套的设计实现了职责分离和并行执行主图负责流程编排监督者负责任务分解研究者负责具体执行。多个研究者子图可以并行工作提高研究效率。下面我们通过学习源代码来理解这个三级分层嵌套结构的实现方式第一层主图结构分析Open Deep Research的主图采用线性管道结构节点按顺序执行比较简单结合源代码deep_researcher.py Lines 701-719来看下deep_researcher_builder StateGraph( AgentState, inputAgentInputState, config_schemaConfiguration)# Add main workflow nodes for the complete research processdeep_researcher_builder.add_node(clarify_with_user, clarify_with_user) # User clarification phasedeep_researcher_builder.add_node(write_research_brief, write_research_brief) # Research planning phasedeep_researcher_builder.add_node(research_supervisor, supervisor_subgraph) # Research execution phasedeep_researcher_builder.add_node(final_report_generation, final_report_generation) # Report generation phase# Define main workflow edges for sequential executiondeep_researcher_builder.add_edge(START, clarify_with_user) # Entry pointdeep_researcher_builder.add_edge(research_supervisor, final_report_generation) # Research to reportdeep_researcher_builder.add_edge(final_report_generation, END) # Final exit point# Compile the complete deep researcher workflowdeep_researcher deep_researcher_builder.compile()从上述代码可以看出主图中设置了4个节点分别是•节点1clarify_with_user- 用来澄清用户真实意图的普通节点•节点2write_research_brief- 生成研究计划、简报的普通节点•节点3research_supervisor-监督者子图负责执行具体的研究工作•节点4final_report_generation- 生成最终报告的普通节点。从节点间的连接方式来看主图内部采用了线性管道的布局模式即节点按固定顺序依次执行•START→clarify_with_user•clarify_with_user→write_research_brief通过Command连接•write_research_brief→research_supervisor通过Command连接•research_supervisor→final_report_generation•final_report_generation→END这里有个小点主图流程中虽然clarify_with_user和write_research_brief节点在运行顺序上是线性的但它们没有通过Edge来连接而是使用了Command的这是因为这两个节点都需要在跳转时同时更新state所以用Command来实现是最好的。因为本期重点是在多层架构的设计思路上state的具体设计与管理、更新机制这个内容得单独做一期所以这里就先不展开了。回到主题主图的这种线性管道布局非常适配按固定顺序执行的业务流程比如我们这里的研究流程从总体来说是比较线性的大致分为“澄清 → 规划 → 研究 → 报告”四个过程。其中由于“研究”从行为方式上来说肯定是最复杂的因此可以把“研究”相关的逻辑单独做成一个子图让主图来调用这个子图子图负责具体执行研究活动向主图输出研究成果即可。第二层监督者子图结构分析监督者子图采用了循环结构形成迭代式决策循环首先结合源代码deep_researcher.py Lines 351-363先来看下监督者子图的整体架构# Supervisor Subgraph Construction# Creates the supervisor workflow that manages research delegation and coordinationsupervisor_builder StateGraph(SupervisorState, config_schemaConfiguration)# Add supervisor nodes for research managementsupervisor_builder.add_node(supervisor, supervisor) # Main supervisor logicsupervisor_builder.add_node(supervisor_tools, supervisor_tools) # Tool execution handler# Define supervisor workflow edgessupervisor_builder.add_edge(START, supervisor) # Entry point to supervisor# Compile supervisor subgraph for use in main workflowsupervisor_subgraph supervisor_builder.compile()从上述代码可以看出监督者子图中设置了2个节点分别是•节点1supervisor- 监督者决策节点负责分析研究简报决定委派哪些任务•节点2supervisor_tools- 工具执行节点负责执行监督者调用的工具包括调用研究者子图从节点连接方式来看子图的入口START直接连接了supervisor而后续又通过Command在两个节点间形成循环。下面我们结合节点函数的源代码来分析。supervisor节点决策阶段注意因为本期不分析项目的state相关的数据流情况故省略非相关代码。async def supervisor(state: SupervisorState, config: RunnableConfig) - Command[Literal[supervisor_tools]]: ...... # Available tools: research delegation, completion signaling, and strategic thinking lead_researcher_tools [ConductResearch, ResearchComplete, think_tool] # Configure model with tools, retry logic, and model settings research_model ( configurable_model .bind_tools(lead_researcher_tools) ......首先Open Deep Research为supervisor中的模型绑定了ConductResearch,ResearchComplete,think_tool三个工具。# Step 2: Generate supervisor response based on current context supervisor_messages state.get(supervisor_messages, []) response await research_model.ainvoke(supervisor_messages)然后supervisor从state中获取到前序的write_research_brief节点生supervisor_messages传递给模型。由于模型绑定了Tools所以会决定调用哪些工具生成包含 tool_calls 的 AIMessage。这里有一个非常关键的点由于bind\_tools()只是告诉模型有哪些工具可以使用让模型可以在响应中生成工具调用请求所以这里await research\_model.ainvoke(supervisor\_messages)不会实际执行工具而只会生成tool\_calls。# Step 3: Update state and proceed to tool execution return Command( gotosupervisor_tools, update{ supervisor_messages: [response], research_iterations: state.get(research_iterations, 0) 1 } )最后通过Command跳转并将response其中包括携带了tool_calls的 AIMessage更新给supervisor_tools然后再具体执行工具的使用。supervisor_tools节点执行阶段首先supervisor_tools节点会检查退出条件# Step 1: Extract current state and check exit conditions configurable Configuration.from_runnable_config(config) supervisor_messages state.get(supervisor_messages, []) research_iterations state.get(research_iterations, 0) most_recent_message supervisor_messages[-1] # Define exit criteria for research phase exceeded_allowed_iterations research_iterations configurable.max_researcher_iterations no_tool_calls not most_recent_message.tool_calls research_complete_tool_call any( tool_call[name] ResearchComplete for tool_call in most_recent_message.tool_calls ) # Exit if any termination condition is met if exceeded_allowed_iterations or no_tool_calls or research_complete_tool_call: return Command( gotoEND, update{ notes: get_notes_from_tool_calls(supervisor_messages), research_brief: state.get(research_brief, ) } )代码设定了三种退出条件即超过迭代次数、无工具调用或调用ResearchComplete只要满足其一节点就会返回END结束循环。否则节点会继续处理think_tool和ConductResearch两种类型的工具调用。# Step 3: Return command with all tool results update_payload[supervisor_messages] all_tool_messages return Command( gotosupervisor, updateupdate_payload )工具执行结束的执行结果将被封装为ToolMessage并返回给supervisor节点supervisor节点会分析研究结果决定是否需要进一步研究如果是的则又会生成工具调用响应转发给supervisor_tools节点从而实现迭代式的研究循环直到满足退出条件。ConductResearch工具调用研究者子图在supervisor_tools节点中还包含了一层嵌套即supervisor_tools节点在执行ConductResearch工具时会并行调用多个研究者子图实例。关键代码deep_researcher.py Lines 222-233如下# Execute research tasks in parallel research_tasks [ researcher_subgraph.ainvoke({ researcher_messages: [ HumanMessage(contenttool_call[args][research_topic]) ], research_topic: tool_call[args][research_topic] }, config) for tool_call in allowed_conduct_research_calls ] tool_results await asyncio.gather(*research_tasks)这里的原理稍微复杂一点因为本期主要是理结构就先不展开了。简单地来讲这里使用了列表推导式创建多个异步任务对象。其中researcher_subgraph.ainvoke()返回的是协程对象coroutine而不是立即执行的结果。而asyncio.gather(*research_tasks)中的*操作符将列表展开为多个参数gather函数会同时启动所有这些协程让它们并行运行然后await会等待所有任务完成。第三层研究者子图结构分析研究者子图采用带条件分支的循环结构支持自主探索和条件退出首先结合源代码deep_researcher.py Lines 587-605来看下研究者子图的整体架构# Researcher Subgraph Construction# Creates individual researcher workflow for conducting focused research on specific topicsresearcher_builder StateGraph( ResearcherState, outputResearcherOutputState, config_schemaConfiguration)# Add researcher nodes for research execution and compressionresearcher_builder.add_node(researcher, researcher) # Main researcher logicresearcher_builder.add_node(researcher_tools, researcher_tools) # Tool execution handlerresearcher_builder.add_node(compress_research, compress_research) # Research compression# Define researcher workflow edgesresearcher_builder.add_edge(START, researcher) # Entry point to researcherresearcher_builder.add_edge(compress_research, END) # Exit point after compression# Compile researcher subgraph for parallel execution by supervisorresearcher_subgraph researcher_builder.compile()从上述代码可以看出监督者子图中设置了3个节点分别是•节点1researcher- 研究者决策节点决定使用哪些工具进行搜索•节点2researcher_tools- 工具执行节点执行搜索工具调用•节点3compress_research- 压缩节点整理研究结果整个监督者子图的工作流比较简单就不展开每个节点函数的代码分析了。主要是通过Command在researcher和researcher_tools间构建循环基本步骤包括researcher节点分析研究主题决定调用哪些搜索工具researcher_tools节点执行这些工具调用可以并行执行多个搜索执行完工具后researcher_tools检查退出条件• 如果满足退出条件如超过迭代次数或调用ResearchComplete跳转到compress_research• 否则返回到researcher继续循环。compress_research节点将研究结果压缩整理并用它来更新对应的state然后结束研究。总结Open Deep Research的多Agent架构采用了三级分层嵌套结构实现了职责分离和并行执行•主图层线性管道结构负责澄清→规划→研究→报告的整体流程编排•监督者子图层循环结构负责任务分解与委派通过ConductResearch工具并行调用多个研究者子图•研究者子图层带条件分支的循环结构负责具体的研究执行和结果压缩同时Open Deep Research的设计模式将决策与执行分离到了不同节点。supervisor/researcher节点通过bind_tools()生成tool_calls但不执行supervisor_tools/researcher_tools节点负责实际执行工具。这种分离机制提供了更好的控制流、错误处理和状态管理能力。通过LangGraph的子图复用机制和异步并行执行系统实现了高效的多Agent协作为构建复杂的多Agent系统提供了很好的参考范例。学AI大模型的正确顺序千万不要搞错了2026年AI风口已来各行各业的AI渗透肉眼可见超多公司要么转型做AI相关产品要么高薪挖AI技术人才机遇直接摆在眼前有往AI方向发展或者本身有后端编程基础的朋友直接冲AI大模型应用开发转岗超合适就算暂时不打算转岗了解大模型、RAG、Prompt、Agent这些热门概念能上手做简单项目也绝对是求职加分王给大家整理了超全最新的AI大模型应用开发学习清单和资料手把手帮你快速入门学习路线:✅大模型基础认知—大模型核心原理、发展历程、主流模型GPT、文心一言等特点解析✅核心技术模块—RAG检索增强生成、Prompt工程实战、Agent智能体开发逻辑✅开发基础能力—Python进阶、API接口调用、大模型开发框架LangChain等实操✅应用场景开发—智能问答系统、企业知识库、AIGC内容生成工具、行业定制化大模型应用✅项目落地流程—需求拆解、技术选型、模型调优、测试上线、运维迭代✅面试求职冲刺—岗位JD解析、简历AI项目包装、高频面试题汇总、模拟面经以上6大模块看似清晰好上手实则每个部分都有扎实的核心内容需要吃透我把大模型的学习全流程已经整理好了抓住AI时代风口轻松解锁职业新可能希望大家都能把握机遇实现薪资/职业跃迁这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】