ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

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

(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!}
返回列表