新闻详情

新闻详情

首页 / 资讯中心 / 详情

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

发布时间:2026/11/3 16:07:05来源:尧图网络
(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!}
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/3 7:07:39

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/3 12:09:35

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/3 12:09:38

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/3 12:09:41

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/2 5:53:27

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/2 5:53:27

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉