
Multi-Agent 协作研究助手一个基于 LangChain 的多 Agent 协作 Demo展示 4 个 AI Agent 如何协同完成研究任务。功能特点4个AI Agent协作协调者、研究员、分析师、撰稿人实时进度显示每个Agent的工作步骤实时可见流式输出基于SSE的实时响应一键停止研究过程中可随时取消深色主题UI现代化的界面设计Agent 角色Agent职责工作内容协调者 (Coordinator)任务编排接收主题分配任务给其他Agent研究员 (Researcher)信息收集生成搜索关键词调用搜索工具收集资料分析师 (Analyst)数据分析分析研究资料提炼关键洞察撰稿人 (Writer)报告生成基于分析结果撰写结构化研究报告快速开始1. 环境要求Python 3.10API Key免费2. 安装# 克隆项目gitcloneyour-repo-urlcdagent_demo# 创建虚拟环境python-mvenv .venv .venv\Scripts\activate# Windows# source .venv/bin/activate # Linux/Mac# 安装依赖pipinstall-rrequirements.txt3. 配置 API Key复制.env.example为.env填入你的 API Keycopy .env.example .env编辑.env文件OPENAI_API_KEYms-你的密钥 OPENAI_BASE_URLhttps://api-inference.modelscope.cn/v1 MODEL_NAMEZhipuAI/GLM-5.24. 运行python web_app.py浏览器打开http://localhost:5000项目结构agent_demo/ ├── web_app.py # Flask Web UI 主入口 ├── main.py # 命令行入口备用 ├── agents/ │ ├── __init__.py # 包初始化加载环境变量 │ ├── coordinator.py # 协调者Agent - 任务编排 │ ├── researcher.py # 研究员Agent - 信息收集 │ ├── analyst.py # 分析师Agent - 数据分析 │ └── writer.py # 撰稿人Agent - 报告生成 ├── tools/ │ ├── search_tool.py # 搜索工具模拟数据 │ └── math_tool.py # 计算工具 ├── templates/ │ └── index.html # Web页面模板 ├── static/ │ ├── style.css # 深色主题样式 │ └── app.js # 前端交互逻辑 ├── .env.example # 环境变量模板 ├── .env # 环境变量需自行配置 ├── requirements.txt # Python依赖 └── README.md # 项目说明核心代码协调者Agent (coordinator.py)defcoordinator_agent(topic:str,on_stepNone)-str:协调者Agent编排多Agent协作流程defnotify(agent_name,status,content):ifon_step:on_step(agent_name,status,content)notify(coordinator,working,f收到研究主题「{topic}」)# Step 1: 研究员收集信息notify(researcher,working,研究员已接任务开始收集信息...)research_dataresearcher_agent(topic,on_stepresearcher_progress)notify(researcher,done,research_data)# Step 2: 分析师分析资料notify(analyst,working,分析师已接任务开始分析资料...)analysis_dataanalyst_agent(topic,research_data,on_stepanalyst_progress)notify(analyst,done,analysis_data)# Step 3: 撰稿人生成报告notify(writer,working,撰稿人已接任务开始撰写报告...)reportwriter_agent(topic,analysis_data,on_stepwriter_progress)notify(writer,done,report)returnreport研究员Agent (researcher.py)defresearcher_agent(topic:str,on_stepNone)-str:研究员Agent搜索收集主题相关信息defnotify(msg):ifon_step:on_step(msg)# 生成搜索关键词notify(正在分析研究主题生成搜索关键词...)responsellm.invoke(messages)# 逐个搜索fori,kwinenumerate(keywords[:5]):notify(f正在搜索第{i1}/{len(keywords)}个关键词{kw})resultsearch.invoke(kw)# 整理结果notify(搜索完成正在整理研究资料...)summaryllm.invoke(summary_messages)returnsummary.contentFlask SSE 流式输出 (web_app.py)app.route(/api/research,methods[POST])defresearch():defgenerate():event_queuequeue.Queue()defon_step(agent_name,status,content):event_queue.put({agent:agent_name,status:status,content:content})# 后台线程运行Agentthreadthreading.Thread(targetrun_agent)thread.start()# 实时推送事件whileTrue:eventevent_queue.get(timeout0.5)ifeventisNone:yieldfdata:{json.dumps({agent:done})}\n\nbreakyieldfdata:{json.dumps(event)}\n\nreturnResponse(generate(),mimetypetext/event-stream)前端实时更新 (app.js)// SSE接收流式数据constreaderresponse.body.getReader();while(true){const{done,value}awaitreader.read();consteventJSON.parse(line.slice(6));if(event.statusworking){// Agent开始工作setAgentStatus(event.agent,working);addMessage(event.agent,event.content,true);}elseif(event.statusprogress){// 更新进度updateLastProgress(event.agent,event.content);}elseif(event.statusdone){// Agent完成setAgentStatus(event.agent,done);addMessage(event.agent,event.content);}}技术栈技术用途LangChainAgent编排框架FlaskWeb后端SSE实时流式输出Markdown报告渲染RichCLI美化输出扩展方向接入真实搜索API替换模拟搜索数据接入Bing/Google搜索添加更多Agent如代码执行Agent、数据分析Agent支持多轮对话添加会话历史和上下文记忆接入向量数据库实现RAG检索增强生成部署上线使用Docker部署配置域名和HTTPSLicenseMIT