当前位置: 首页 > wzjs >正文

免费优化网站建设搜索引擎seo如何赚钱

免费优化网站建设,搜索引擎seo如何赚钱,山东省建设厅注册中心网站,网页传奇游戏怎么注销这篇文章我们将 LangGraph中的控制流(边)和状态更新(节点)结合起来使用。比如,我们希望同时执行状态更新并决定下一步要转到哪个节点,且这些操作在同一个节点中完成。而正好 LangGraph 提供了一种方法&…

这篇文章我们将 LangGraph中的控制流(边)和状态更新(节点)结合起来使用。比如,我们希望同时执行状态更新并决定下一步要转到哪个节点,且这些操作在同一个节点中完成。而正好 LangGraph 提供了一种方法,可以通过从节点函数返回一个 Command 对象来实现这一点。

def my_node(state: State) -> Command[Literal["my_other_node"]]:return Command(# state updateupdate={"foo": "bar"},# control flowgoto="my_other_node")

如果我们使用的是 subgraphs(子图) 我们还可以通过 Command 中指定 graph=Command.PARENT这种方式,实现节点从一个子图导航到其他子图。

def my_node(state: State) -> Command[Literal["my_other_node"]]:return Command(update={"foo": "bar"},goto="other_subgraph",  # where `other_subgraph` is a node in the parent graphgraph=Command.PARENT)

当我们从子图节点向父图节点发送更新的时候,且更新的键同时存在于父图和子图的状态模式中时,我们必须为父图状态中正在更新的键定义一个 reducer。
下面我们开始我们的例子。

定义节点和State

定义 graph State:

class State(TypedDict):foo: str

定义节点A,

def node_a(state: State) -> Command[Literal["node_b", "node_c"]]:print("Called A")value = random.choice(["a", "b"])# 判断应该跳转到哪个节点if value == "a":goto = "node_b"else:goto = "node_c"# Command 允许我们同时更新图状态并路由到下一个节点。return Command(# 更新图中的状态update={"foo": value},# 替换edgegoto=goto,)

定义节点b或者c:

def node_b(state: State):print("Called B")return {"foo": state["foo"] + "b"}def node_c(state: State):print("Called C")return {"foo": state["foo"] + "c"}

定义好了上面节点之后,现在,我们可以使用上述节点创建 StateGraph。需要注意的是,该图没有定义条件的路由edge!这是因为控制流是用 Command 内部 node_a 定义的。

builder = StateGraph(State)
builder.add_edge(START, "node_a")
builder.add_node(node_a)
builder.add_node(node_b)
builder.add_node(node_c)graph = builder.compile()

不知道大家注意到没有,我们的 node_a 使用了 Command 作为返回类型注解,例如 Command[Literal[“node_b”, “node_c”]]。这一步对于图的渲染是非常必要的,它会告诉 LangGraph:node_a 可以导航到 node_b 和 node_c。
现在我们看看图的·1展示效果:

from IPython.display import display, Imagedisplay(Image(graph.get_graph().draw_mermaid_png()))

在这里插入图片描述
我们来试试多次调用graph:

graph.invoke({"foo": ""})

将会看到,graph会根据节点A中的随机选择,采用不同的路径(A-> B或A-> C)。

导航到 parent graph 中的节点

现在,让我们演示如何从 subgraph 内部导航到 parent graph 中的不同节点。我们将通过将上述示例中的 node_a 转换为一个单节点图来实现这一点,然后将其作为子图添加到 parent graph 中。

import operator
from typing_extensions import Annotatedclass State(TypedDict):# N定义一个reducer 自动附加消息foo: Annotated[str, operator.add]def node_a(state: State):print("Called A")value = random.choice(["a", "b"])if value == "a":goto = "node_b"else:goto = "node_c"# Command 允许我们同时更新图状态并路由到下一个节点。return Command(update={"foo": value},goto=goto,# 这会告诉 LangGraph 导航到父图中的 node_b 或 node_c 这将导航到相对于子图的最近的父图graph=Command.PARENT,)subgraph = StateGraph(State).add_node(node_a).add_edge(START, "node_a").compile()def node_b(state: State):print("Called B")# 由于我们已经定义了一个 reducer,因此不需要手动将新字符附加到现有的 foo 值上。相反,reducer 会自动(通过 operator.add)将这些值附加到现有值中。return {"foo": "b"}def node_c(state: State):print("Called C")return {"foo": "c"}

然后我们来构造graph:

builder = StateGraph(State)
builder.add_edge(START, "subgraph")
builder.add_node("subgraph", subgraph)
builder.add_node(node_b)
builder.add_node(node_c)graph = builder.compile()

查看效果:

Called A
Called C
{'foo': 'bc'}

到这里我们的代码就结束了,我们通过这种方式展示了如何使用 LangGraph 的 Command 对象在子图中更新状态并导航到父图中的不同节点。并通过 reducer 简化状态管理,就是自动保存消息链。


文章转载自:

http://Zpq5Wxu9.ybnps.cn
http://mISXIJ2r.ybnps.cn
http://lDdhqbKQ.ybnps.cn
http://xqlU12x5.ybnps.cn
http://8ZzOmxZX.ybnps.cn
http://b7jJdumA.ybnps.cn
http://QY5kDhAQ.ybnps.cn
http://rSR0KcK8.ybnps.cn
http://sO1BBtay.ybnps.cn
http://iNw9Mo64.ybnps.cn
http://Y0EJKKNq.ybnps.cn
http://OeVnDdhX.ybnps.cn
http://LmxUUqB0.ybnps.cn
http://FIHGCf5u.ybnps.cn
http://Nn5uYiKq.ybnps.cn
http://KcF4lvF1.ybnps.cn
http://EAUYxnp2.ybnps.cn
http://UDlohJ8g.ybnps.cn
http://dlyR3WTV.ybnps.cn
http://NGisIPwA.ybnps.cn
http://r6mPmt1z.ybnps.cn
http://GUdf4pse.ybnps.cn
http://jX2IoYUC.ybnps.cn
http://0WrH3Yg5.ybnps.cn
http://11Nb2hWC.ybnps.cn
http://9NvDo3RV.ybnps.cn
http://CgJdxnA7.ybnps.cn
http://VHYaTipq.ybnps.cn
http://fAgu34Qd.ybnps.cn
http://AUVr2tya.ybnps.cn
http://www.dtcms.com/wzjs/742650.html

相关文章:

  • 有什么网站可以做跳转连接的南宁百姓网
  • wp网站打开太慢怎么做优化鼎湖网站建设公司
  • 共享办公室 设计搜索优化的培训免费咨询
  • 浦东新区建设工程安全质量监督站网站网站与微信对接
  • 购物网站html网站建设的基本技术
  • 网站整站下载器下载utf8网页乱码用excel可以做网站
  • 建站推荐网站健康东莞app
  • 建设投资公司网站做网站广告多少钱
  • 临淄区建设局网站北京招标代理公司排名
  • 财政局门户网站建设方案不用下载直接进入的app
  • 网站二次开发公司自建网站怎么关闭
  • 昆明网站建设解决方案如何在网站上推广自己的产品
  • 网站程序调试模式怎么做三秦网
  • 网上开店的货源渠道有哪些seo排名查询软件
  • 电子商务网站与建设实践报告广州致峰网站建设
  • 个人站长网站河北邯郸ktv
  • 免费网站去哪找做网站要收订金吗
  • 东莞技术好的网站建设推广重庆市建设执业注册中心网站
  • 浪漫网站建设制作表情包的软件
  • 餐饮网站建设网站桐乡市城市规划建设局网站
  • 专业的做网站公司郑州网站制作生产厂商定制
  • python做网站步骤网站建设专业的
  • 美食网站建设需求中国国防建设网站
  • 做水印的网站建行个人手机银行app下载
  • 网站未备案会怎么样成都百度推广优化
  • 如何阿里巴巴网站做推广方案python培训机构哪个好
  • 连云港网站建设案例网站开发硬件要求
  • 云南网站优化建站wordpress 手机菜单栏插件
  • 网站首页模板设计图wordpress用户增加插件
  • 河南十大外贸公司优化seo报价