主流 Agent 开发框架:LangGraph(精确控制、生产首选)、AutoGen(对话即开发)、LlamaIndex(数据索引、RAG 深度集成)、Coze/Dify(低代码)。核心选型原则:不要纠结用哪个框架,核心是 Agent Flow 搭建的思想,框架只是工具。LangGraph 的核心优势是支持有环图——实现执行->反思->修正的自我改进循环,这是区分它与普通流水线框架的关键。
框架选型对比
| 框架 | 核心概念 | 定位 | 适用场景 |
|---|---|---|---|
| LangGraph | 有向图(Nodes+Edges) | 精确控制 | 生产环境、企业级、复杂流程 |
| AutoGen | 对话(Conversation) | 快速开发 | 代码生成、多 Agent 辩论、快速原型 |
| LlamaIndex | 数据索引(Index) | RAG 深度 | 知识库查询、文档问答、数据管道 |
| Coze/Dify | 工作流(Workflow) | 可视化 | 非技术人员、简单 Bot、验证想法 |
from langgraph.graph import StateGraph from typing import TypedDict, List, Dict, Any, Optional # 1. 定义状态(在所有节点间传递的数据) class AgentState(TypedDict):messages: List[Dict[str, str]] # 对话历史
query: str # 当前问题
intent: str # 意图类型
plan: List[Dict[str, Any]] # 执行计划
current_step: int # 当前步骤
tool_results: List[Dict] # 工具执行结果
reasoning_steps: List[str] # 推理步骤记录
reflections: List[Dict] # 反思记录
should_continue: bool # 是否继续
iteration: int # 当前迭代次数
max_iterations: int # 最大迭代次数
final_answer: str # 最终答案
error: Optional[str] # 错误信息
# 2. 定义节点(处理状态的函数) def router_node(state: AgentState) -> dict:"""意图识别:决定走哪条路径"""
prompt = f"""分析用户问题意图:
问题: {state['query']}
可选意图: data_query / analysis / research / general
返回JSON: {{"intent": "...", "reason": "..."}}"""
result = json.loads(llm.simple_chat(prompt))
return {"intent": result["intent"]}
# 3. 条件边(动态路由) def route_by_intent(state: AgentState) -> str:if state["intent"] == "general":
return "critic" # 普通问答直接回答
return "planner" # 复杂任务需要规划
def should_continue(state: AgentState) -> str:if state["should_continue"]:
return "executor" # 继续执行
return "critic" # 生成最终答案
# 4. 构建图graph = StateGraph(AgentState)
graph.add_node("router", router_node)
graph.add_node("planner", PlannerNode())
graph.add_node("executor", ExecutorNode())
graph.add_node("reflector", ReflectorNode())
graph.add_node("critic", CriticNode())
graph.add_conditional_edges("router", route_by_intent)
graph.add_edge("planner", "executor")
graph.add_edge("executor", "reflector")
graph.add_conditional_edges("reflector", should_continue)
app = graph.compile()
result = app.invoke({"query": "分析银行股PE数据", "max_iterations": 3})
LangGraph 的核心优势:支持有环图
普通流水线(只能单向):A -> B -> C -> 输出
LangGraph 有环图(支持循环迭代):
executor -> reflector -> 置信度低?
|- 是 -> 回到 executor(循环迭代,自我修正)
|- 否 -> critic -> 输出
LangChain Memory 类型对比
# 按对话长度选择合适的 Memory 策略ConversationBufferMemory # 全量保留,适合短对话(<10轮)
ConversationBufferWindowMemory # 保留最近 N 轮,适合中等对话
ConversationSummaryMemory # 摘要压缩,适合长对话
VectorStoreRetrieverMemory # 语义检索历史,适合超长跨会话
# 生产推荐:Summary + Buffer 混合 # 最近 3 轮完整保留 + 早期历史生成摘要
LangGraph 是基于有向图的状态机,精确控制每一步流转,适合生产环境;AutoGen 是基于对话的多 Agent 协作,Agent 互相聊天解决问题,适合代码生成和快速原型验证。
LangChain Expression Language:用管道符 | 组合组件(prompt | llm | output_parser),比旧版 Chain 更简洁,支持流式输出和异步执行,是 LangChain 0.2+ 的新范式。
LlamaIndex 专注数据索引和 RAG,对文档处理、知识库构建支持更深度;LangChain 是通用 Agent 框架,工具集成更广。两者可结合:LlamaIndex 做 RAG,LangGraph 做 Agent 编排。
LangChain Memory 默认面向单会话,多用户并发时需要隔离。方案:以 user_id 为键存储 Memory(如 Redis),每次请求加载对应 user 的 Memory,会话结束后保存回 Redis。
被问LangChain 和 LangGraph 的区别,不要说LangGraph 是 LangChain 的升级版。正确答案:LangChain 是工具集合(LLM 调用、工具集成、Memory 管理等组件),LangGraph 是基于有向图的 Agent 编排框架。LangGraph 的核心优势是支持有环图(循环迭代),而传统 LangChain Chain 只能单向流转。在拓业智询中,我们用 LangGraph 构建了 Router->Planner->Executor->Reflector->Critic 五节点图,Reflector 发现结果不够好时会触发循环,回到 Executor 补充数据,这种有环图结构是 LangGraph 独有的能力,旧版 LangChain 无法实现。