首页/新闻资讯/正文详情

(LangGraph教程)1. Introduction——Lesson 2: Simple Graph(State状态、Nodes节点、Edges边(普通边、条件边)、图的构建、图的调用)

发布时间:2026/9/23 7:50:53 来源:云帆数科 栏目:资讯中心
(LangGraph教程)1. Introduction——Lesson 2: Simple Graph(State状态、Nodes节点、Edges边(普通边、条件边)、图的构建、图的调用)
https://academy.langchain.com/courses/intro-to-langgraphhttps://github.com/shangxiang0907/langchain-academy文章目录Lesson 2: Simple Graphsimple-graph.mdThe Simplest Graph 最简单的图State 状态Nodes 节点Edges 边普通边、条件边Graph Construction 图的构建Graph Invocation 图的调用Lesson 2: Simple Graph中文第2课简单图Notebook Reference: simple-graph.ipynb中文笔记本参考文件simple-graph.ipynbDownload Notebook onGitHub中文在GitHub上下载笔记本。View Notebook onGoogle Colab中文在Google Colab上查看笔记本。simple-graph.mdThe Simplest Graph 最简单的图Let’s build a simple graph with 3 nodes and one conditional edge.我们来构建一个包含 3 个节点和一条条件边的简单图。%%capture--no-stderr%pip install--quiet-U langgraphState 状态First, define the State of the graph.首先定义图的 State。The State schema serves as the input schema for all Nodes and Edges in the graph.状态State模式作为图中所有节点Nodes和边Edges的输入模式。Let’s use theTypedDictclass from python’stypingmodule as our schema, which provides type hints for the keys.我们使用 Pythontyping模块中的TypedDict类作为我们的模式它为键提供类型提示。fromtyping_extensionsimportTypedDictclassState(TypedDict):graph_state:strNodes 节点Nodes are just python functions.节点Nodes 就是普通的 Python 函数。The first positional argument is the state, as defined above.第一个位置参数是上文定义的状态state。Because the state is aTypedDictwith schema as defined above, each node can access the key,graph_state, withstate[graph_state].由于该状态是一个TypedDict其模式如上所定义因此每个节点均可通过state[graph_state]访问键graph_state。Each node returns a new value of the state keygraph_state.每个节点返回状态键graph_state的新值。By default, the new value returned by each node will override the prior state value.默认情况下每个节点返回的新值将覆盖之前的状态值。defnode_1(state):print(---Node 1---)return{graph_state:state[graph_state] I am}defnode_2(state):print(---Node 2---)return{graph_state:state[graph_state] happy!}defnode_3(state):print(---Node 3---)return{graph_state:state[graph_state] sad!}Edges 边普通边、条件边Edges connect the nodes.边Edges 连接各个节点。Normal Edges are used if you want toalwaysgo from, for example,node_1tonode_2.若希望始终从例如node_1跳转到node_2则使用普通边Normal Edges。Conditional Edges are used if you want tooptionallyroute between nodes.若希望可选地在节点之间路由则使用条件边Conditional Edges。Conditional edges are implemented as functions that return the next node to visit based on some logic.条件边以函数形式实现该函数根据某些逻辑返回下一个要访问的节点。importrandomfromtypingimportLiteraldefdecide_mood(state)-Literal[node_2,node_3]:# Often, we will use state to decide on the next node to visituser_inputstate[graph_state]# Here, lets just do a 50 / 50 split between nodes 2, 3ifrandom.random()0.5:# 50% of the time, we return Node 2returnnode_2# 50% of the time, we return Node 3returnnode_3Graph Construction 图的构建Now, we build the graph from our components defined above.现在我们基于上文定义的组件来构建图。The StateGraph class is the graph class that we can use.StateGraph 类 是我们可用的图类。First, we initialize a StateGraph with theStateclass we defined above.首先我们使用上文定义的State类初始化一个 StateGraph。Then, we add our nodes and edges.然后我们添加节点和边。We use theSTARTNode, a special node that sends user input to the graph, to indicate where to start our graph.我们使用START节点一种特殊节点它将用户输入发送至图中以指明图的起始位置。TheENDNode is a special node that represents a terminal node.END节点 是一种表示终止节点的特殊节点。Finally, we compile our graph to perform a few basic checks on the graph structure.最后我们编译图以对图结构执行若干基本检查。We can visualize the graph as a Mermaid diagram.我们可以将图可视化为一张 Mermaid 图。fromIPython.displayimportImage,displayfromlanggraph.graphimportStateGraph,START,END# Build graphbuilderStateGraph(State)builder.add_node(node_1,node_1)builder.add_node(node_2,node_2)builder.add_node(node_3,node_3)# Logicbuilder.add_edge(START,node_1)builder.add_conditional_edges(node_1,decide_mood)builder.add_edge(node_2,END)builder.add_edge(node_3,END)# Addgraphbuilder.compile()# Viewdisplay(Image(graph.get_graph().draw_mermaid_png()))Graph Invocation 图的调用The compiled graph implements the runnable protocol.已编译的图实现了 runnable 协议。This provides a standard way to execute LangChain components.这为执行 LangChain 组件提供了标准方式。invokeis one of the standard methods in this interface.invoke是该接口中的标准方法之一。The input is a dictionary{graph_state: Hi, this is lance.}, which sets the initial value for our graph state dict.输入是一个字典{graph_state: Hi, this is lance.}用于设置图状态字典的初始值。Wheninvokeis called, the graph starts execution from theSTARTnode.当调用invoke时图从START节点开始执行。It progresses through the defined nodes (node_1,node_2,node_3) in order.它按顺序遍历已定义的节点node_1、node_2、node_3。The conditional edge will traverse from node1to node2or3using a 50/50 decision rule.条件边将依据 50/50 决策规则从节点1跳转至节点2或3。Each node function receives the current state and returns a new value, which overrides the graph state.每个节点函数接收当前状态并返回一个新值该值将覆盖图状态。The execution continues until it reaches theENDnode.执行持续进行直至到达END节点。graph.invoke({graph_state:Hi, this is Lance.})---Node 1--- ---Node 3--- {graph_state: Hi, this is Lance. I am sad!}invokeruns the entire graph synchronously.invoke同步运行整个图。This waits for each step to complete before moving to the next.它会等待每一步完成后再进入下一步。It returns the final state of the graph after all nodes have executed.它返回所有节点执行完毕后的图最终状态。In this case, it returns the state afternode_3has completed:本例中它返回node_3执行完毕后的状态{graph_state: Hi, this is Lance. I am sad!}

相关推荐

Flutter Card组件在鸿蒙平台的开发实践
Flutter Card组件在鸿蒙平台的开发实践

1. 项目概述在移动应用开发领域,跨平台框架Flutter因其高效的渲染性能和一致的UI体验而广受欢迎。最近,随着HarmonyOS(鸿蒙系统)的快速发展,开发者们开始探索如何将Flutter应用无缝迁移到鸿蒙平台。本文将重点介绍Flut… · 2026/9/23 7:50:53

搞定万能声卡驱动器常见坑的保姆级教程
搞定万能声卡驱动器常见坑的保姆级教程

搞定万能声卡驱动器常见坑的保姆级教程 官方文档翻了三遍还是没看懂,配置完直接报错,这种抓不住重点的折磨谁懂?别再死磕那些晦涩难懂的参数表了,这篇保姆级教程直接把你从坑里捞出来。… · 2026/9/23 7:50:53

校园闲置交易平台源码实战:从解压到上线的完整避坑指南
校园闲置交易平台源码实战:从解压到上线的完整避坑指南

简介:这是一套面向高校学生与Java初学者、课程设计者的校园二手交易平台完整源码,基于JSPSSM(SpringSpringMVCMyBatis)与MySQL实现,可用于毕业设计、课程实训或二次开发。前台涵盖分类浏览、商品搜索、登录注册、关注与… · 2026/9/23 7:50:47

免费AI学习平台搭建实战:从学习路径设计到模型量化部署
免费AI学习平台搭建实战:从学习路径设计到模型量化部署

1. 从“看教程”到“做项目”:我对免费AI学习平台的重新理解这几年AI爆火之后,我数不清被问过多少次“想学AI,从哪儿开始”。网上资料确实是海量的,但问题恰恰出在“海量”这两个字上——今天有人推荐看吴恩达的课,明天… · 2026/9/23 8:37:26

英语偏旁部首入门到精通:揭秘代码里的字符拆解逻辑
英语偏旁部首入门到精通:揭秘代码里的字符拆解逻辑

英语偏旁部首入门到精通:揭秘代码里的字符拆解逻辑 复制来的代码跑不通,报错信息满屏红字,你盯着屏幕抓耳挠腮,根本不知道从哪下手调。这种“黑盒”体验,是每个开发者从新手迈向 入门到精通… · 2026/9/23 8:37:19

vray渲染器踩坑实录
vray渲染器踩坑实录

V-Ray渲染器性能优化避坑:3个让出图慢10倍的致命错误 复制来的V-Ray渲染参数跑不通,或者跑出来的图黑乎乎一片、噪点满天飞,是不是让你抓狂?别急,这通常是场景设置和硬件配置的冲突,不是你的错。很多新手卡在第一步,因为直接套用网上通用… · 2026/9/23 8:37:19

无线运动耳机性能优化实战:告别堆栈报错
无线运动耳机性能优化实战:告别堆栈报错

无线运动耳机性能优化实战:告别堆栈报错 盯着满屏红色的StackTrace,眼睛都花了还是找不到Bug在哪?别急,这行代码没报错,但你的无线运动耳机在剧烈运动时音频断连、延迟高企,这才是真正的“性能优化”噩梦。很多开发者一上来就调参数,结果… · 2026/9/23 8:36:54

FPGA进位链实现高精度TDC的原理与工程实践
FPGA进位链实现高精度TDC的原理与工程实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/9/23 8:36:47

yfd 入门到精通:3 步搞定 StackTrace 报错与底层原理
yfd 入门到精通:3 步搞定 StackTrace 报错与底层原理

yfd 入门到精通:3 步搞定 StackTrace 报错与底层原理 面对满屏红色的 StackTrace,你是不是只想把电脑摔了?别急,这不仅是你的噩梦,也是所有开发者从入门到精通必须跨越的坎。yfd… · 2026/9/23 8:36:47

3招搞定手机怎么下载微信面试难题实战项目解析
3招搞定手机怎么下载微信面试难题实战项目解析

3招搞定手机怎么下载微信面试难题实战项目解析 面试被问“手机怎么下载微信”背后的原理,90%的人答不上来。别笑,这看似弱智的问题,实则是考察你对移动应用分发机制、安全校验及网络协议理解的试金石。我带过不少校招新人,他们背了八股文,却连一个A… · 2026/9/23 0:00:03

你有新短消息请注意查收:3个新手避坑指南搞定消息系统选型
你有新短消息请注意查收:3个新手避坑指南搞定消息系统选型

你有新短消息请注意查收:3个新手避坑指南搞定消息系统选型 面试被问“高并发下如何保证消息不丢失”,你张口就是“用Redis”,结果面试官追问“如果Redis宕机了怎么办”,你瞬间卡壳。这种场景太常见了,很多新手在背八股文时,只记住了技术名词… · 2026/9/23 0:00:29

Win7无线热点配置工具源码解析:解决API失效的3个实战技巧
Win7无线热点配置工具源码解析:解决API失效的3个实战技巧

Win7无线热点配置工具源码解析:解决API失效的3个实战技巧 Win7无线热点配置工具在Win10/11上跑不动?不是你的问题,是版本升级后 API 全变了。很多老项目里的 netsh wlan… · 2026/9/23 0:00:36

了解更多?预约专属演示

我们的顾问将为您一对一讲解产品与方案

企业微信二维码