目录参考文档1 LangChain/LangGraph 对比2 常见多智能体架构3 LangGraph的基本构成3.1 使用节点构成图3.2 创建图3.2.1 安装langgraph3.2.2 创建节点之间流转的数据State3.2.3 创建节点node3.2.4 通过边edge连接node3.2.5 编译图3.2.6 运行图3.3 图的可视化3.3.1 通过ASCII 控制台打印3.3.2 使用markdown3.3.3 保存markdown图像3.3.4 直接画图3.3.5 画图依赖包graphviz参考文档https://www.bilibili.com/video/BV1R5V26xE7x?spm_id_from333.788.player.switchvd_source045329e591c0a8451922c76067d8b77cp481 LangChain/LangGraph 对比langchain适用于单智能体简单对话管理LangGraph多智能体和记忆管理是对langchain的封装避免了复杂的会话状态管理。2 常见多智能体架构3 LangGraph的基本构成3.1 使用节点构成图3.2 创建图demofrom langgraph.graph import END, START, StateGraph from typing import TypedDict # 状态模型字典类型 class State(TypedDict): input: int # 输入值 output: int # 输出状态 def node1(state: State) - State: return {output: state[input] 1} def node2(state: State) - State: return {output: state[output] * 2} graph StateGraph(State) graph.add_node(node1, node1) graph.add_node(node2, node2) graph.add_edge(START, node1) graph.add_edge(node1, node2) graph.add_edge(node2, END) compiled_graph graph.compile() # compiled_graph.get_graph().draw_png(graph.png) result compiled_graph.invoke(State(input0)) print(result)3.2.1 安装langgraphpip install -U langgraph3.2.2 创建节点之间流转的数据State使用字典类型作为数据流转的类型不同节点之间共享一个对象from typing import TypedDict # 状态模型字典类型 class State(TypedDict): input: int # 输入值 output: int # 输出状态3.2.3 创建节点node节点就是一个个功能函数模块节点内部对状态State进行变更。1 创建状态图时需要添加状态类型作为参数2 图通通过add_node添加节点# 1 定义节点 def node1(state: State) - State: return {output: state[input] 1} def node2(state: State) - State: return {output: state[output] * 2} # 2 创建图时指定数据流转的类型 graph StateGraph(State) # 3 图添加节点 graph.add_node(node1, node1) graph.add_node(node2, node2)3.2.4 通过边edge连接node通过add_edge绑定节点确定了数据流转的顺序。起始节点为langgraph中定义的START终止节点为langgraph中定义的END# 起始节点为START终止节点为END graph.add_edge(START, node1) graph.add_edge(node1, node2) graph.add_edge(node2, END)3.2.5 编译图图只有编译后才能够运行时执行compiled_graph graph.compile()3.2.6 运行图图在激活时需要传入初始化的状态State(input0)这里没有对state的output进行初始化是由于代码中node1中会对state的output进行初始化。result compiled_graph.invoke(State(input0)) print(result)3.3 图的可视化个人认为开发时使用ASCII画图就行配置简单显示够用如果要输出用于外发个人认为输出markdown文本就行然后用专业的工具进行展示和显示效果优化。Online FlowChart Diagrams Editor - Mermaid Live Editor3.3.1 通过ASCII 控制台打印安装依赖包pip install grandalf添加打印逻辑ascii_graph compiled_graph.get_graph().draw_ascii() print(ascii_graph)效果3.3.2 使用markdown将markdown保存为文件然后进行vscode的预览mermaid_graph compiled_graph.get_graph().draw_mermaid() with open(graph.md, w) as f: f.write(mermaid\n) # 添加起始标记 f.write(mermaid_graph) f.write(\n) # 添加结束标记3.3.3 保存markdown图像mermaid_graph compiled_graph.get_graph().draw_mermaid_png() with open(md.png, wb) as f: f.write(mermaid_graph)3.3.4 直接画图compiled_graph.get_graph().draw_png(graph.png)3.3.5 画图依赖包graphvizmac电脑安装系统级依赖包brew install graphviz配置环境变量export GRAPHVIZ_DIR$(brew --prefix graphviz)安装python库pip install -U pygraphviz \ --config-settings--global-optionbuild_ext \ --config-settings--global-option-I$GRAPHVIZ_DIR/include \ --config-settings--global-option-L$GRAPHVIZ_DIR/lib
Langgraph学习一:基本流程
发布时间:2026/6/26 21:08:17
目录参考文档1 LangChain/LangGraph 对比2 常见多智能体架构3 LangGraph的基本构成3.1 使用节点构成图3.2 创建图3.2.1 安装langgraph3.2.2 创建节点之间流转的数据State3.2.3 创建节点node3.2.4 通过边edge连接node3.2.5 编译图3.2.6 运行图3.3 图的可视化3.3.1 通过ASCII 控制台打印3.3.2 使用markdown3.3.3 保存markdown图像3.3.4 直接画图3.3.5 画图依赖包graphviz参考文档https://www.bilibili.com/video/BV1R5V26xE7x?spm_id_from333.788.player.switchvd_source045329e591c0a8451922c76067d8b77cp481 LangChain/LangGraph 对比langchain适用于单智能体简单对话管理LangGraph多智能体和记忆管理是对langchain的封装避免了复杂的会话状态管理。2 常见多智能体架构3 LangGraph的基本构成3.1 使用节点构成图3.2 创建图demofrom langgraph.graph import END, START, StateGraph from typing import TypedDict # 状态模型字典类型 class State(TypedDict): input: int # 输入值 output: int # 输出状态 def node1(state: State) - State: return {output: state[input] 1} def node2(state: State) - State: return {output: state[output] * 2} graph StateGraph(State) graph.add_node(node1, node1) graph.add_node(node2, node2) graph.add_edge(START, node1) graph.add_edge(node1, node2) graph.add_edge(node2, END) compiled_graph graph.compile() # compiled_graph.get_graph().draw_png(graph.png) result compiled_graph.invoke(State(input0)) print(result)3.2.1 安装langgraphpip install -U langgraph3.2.2 创建节点之间流转的数据State使用字典类型作为数据流转的类型不同节点之间共享一个对象from typing import TypedDict # 状态模型字典类型 class State(TypedDict): input: int # 输入值 output: int # 输出状态3.2.3 创建节点node节点就是一个个功能函数模块节点内部对状态State进行变更。1 创建状态图时需要添加状态类型作为参数2 图通通过add_node添加节点# 1 定义节点 def node1(state: State) - State: return {output: state[input] 1} def node2(state: State) - State: return {output: state[output] * 2} # 2 创建图时指定数据流转的类型 graph StateGraph(State) # 3 图添加节点 graph.add_node(node1, node1) graph.add_node(node2, node2)3.2.4 通过边edge连接node通过add_edge绑定节点确定了数据流转的顺序。起始节点为langgraph中定义的START终止节点为langgraph中定义的END# 起始节点为START终止节点为END graph.add_edge(START, node1) graph.add_edge(node1, node2) graph.add_edge(node2, END)3.2.5 编译图图只有编译后才能够运行时执行compiled_graph graph.compile()3.2.6 运行图图在激活时需要传入初始化的状态State(input0)这里没有对state的output进行初始化是由于代码中node1中会对state的output进行初始化。result compiled_graph.invoke(State(input0)) print(result)3.3 图的可视化个人认为开发时使用ASCII画图就行配置简单显示够用如果要输出用于外发个人认为输出markdown文本就行然后用专业的工具进行展示和显示效果优化。Online FlowChart Diagrams Editor - Mermaid Live Editor3.3.1 通过ASCII 控制台打印安装依赖包pip install grandalf添加打印逻辑ascii_graph compiled_graph.get_graph().draw_ascii() print(ascii_graph)效果3.3.2 使用markdown将markdown保存为文件然后进行vscode的预览mermaid_graph compiled_graph.get_graph().draw_mermaid() with open(graph.md, w) as f: f.write(mermaid\n) # 添加起始标记 f.write(mermaid_graph) f.write(\n) # 添加结束标记3.3.3 保存markdown图像mermaid_graph compiled_graph.get_graph().draw_mermaid_png() with open(md.png, wb) as f: f.write(mermaid_graph)3.3.4 直接画图compiled_graph.get_graph().draw_png(graph.png)3.3.5 画图依赖包graphvizmac电脑安装系统级依赖包brew install graphviz配置环境变量export GRAPHVIZ_DIR$(brew --prefix graphviz)安装python库pip install -U pygraphviz \ --config-settings--global-optionbuild_ext \ --config-settings--global-option-I$GRAPHVIZ_DIR/include \ --config-settings--global-option-L$GRAPHVIZ_DIR/lib