LangGraph简单图结构

python 复制代码
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langgraph.graph import MessagesState, StateGraph,START,END
from prompt_toolkit.contrib.regular_languages.regex_parser import AnyNode
from rich import print as rprint
from IPython import display
import operator
class State(TypedDict):
  n1: str
def node1(state: State) -> dict:
  return {
    "n1": "1111"
  }
def node2(state: State) -> dict:
  return {
    "n2": "2222"
  }

graph = (
  StateGraph(State)
  .add_node(node1)
  .add_node(node2)
  .add_edge(START, "node1")
  .add_edge(START, "node2")
).compile()
display.display(graph)
rprint(graph)

python 复制代码
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langgraph.graph import MessagesState, StateGraph,START,END
from prompt_toolkit.contrib.regular_languages.regex_parser import AnyNode
from rich import print as rprint
from IPython import display
import operator
import random
def node3(state: State) -> dict:
  return {
    "n2": "33"
  }
def node4(state: State) -> dict:
  return {
    "n2": "444"
  }
def node5(state: State) -> dict:
  return {
    "n2": "55"
  }
def route(state: State) -> dict:
  if random.random() >0.5:
    return node1
  else:
    return node2
def route2(state: State) -> dict:
  if random.random() >0.5:
    return node4
  else:
    return node5
builder=StateGraph(State)
builder.add_node(node1)
builder.add_node(node2)
builder.add_node(node3)
builder.add_node(node4)
builder.add_node(node5)
builder.add_conditional_edges(
  source=START,
  path=route,
  path_map={
    "node1": "node1",
    "node2": "node2"
  }
)
builder.add_edge("node1","node3")
builder.add_edge("node2","node3")
builder.add_conditional_edges(
  source="node3",
  path=route2,
  path_map={
    "node4": "node4",
    "node5": "node5"
  }
)
builder.add_edge("node4",END)
builder.add_edge("node5",END)
graph=builder.compile()

display.display(graph)

python 复制代码
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langgraph.graph import MessagesState, StateGraph,START,END
from prompt_toolkit.contrib.regular_languages.regex_parser import AnyNode
from rich import print as rprint
from IPython import display
import operator
class State(TypedDict):
  logs: Annotated[list[str],add_messages]
  list: Annotated[list[AnyNode], operator.add]
  cur_id: str

def node1(state: State) -> dict:
  return {
    "logs": ["node1 运行完毕"],
    "list": ["11111"],
    "cur_id": "9999-"
  }
def node2(state: State) -> dict:
  return {
    "logs": ["node2 运行完毕"],
    "list": ["22222"],
    "cur_id": "7777-"
  }

graph =(
  StateGraph(State)
  .add_node(node1)
  .add_node(node2)
  .add_edge(START, "node1")
  .add_edge("node1", "node2")
  .compile()
)
display.display(graph)
res=graph.invoke({"logs":"222"})
rprint(res)
相关推荐
制造业的搬运工3 小时前
智能家居工控PCB可靠性检测规范与制程管控实践
大数据·网络·人工智能·制造·pcb工艺
蓝羽飞鸟4 小时前
什么是自监督学习
人工智能·深度学习
wing984 小时前
从codex转战workbuddy使用一周的感受
前端·人工智能·后端
AI产品测评官4 小时前
从L3寻源智能体到全链路闭环ATS:拆解世纪云猎新一代AI招聘系统架构
人工智能·系统架构
Java成神之路-4 小时前
RAG 工程最优解:意图路由分流架构
人工智能·ai应用开发·springaialibaba
余槐i4 小时前
Firecrawl 实战:将网站转换为大模型可用数据
人工智能·python·工具·firecrawl
hsg774 小时前
简述: 人工智能 + 行动意见
人工智能
jason_renyu4 小时前
《月魁传》的终极预言:真正的 AI 未来,是超智能进化与万物互联
人工智能·万物互联·超人工智能·人工智能未来畅想
XiaoZhenHua984 小时前
VisionPro多相机高速检测性能优化实战:从图像堆积到稳定运行
人工智能·计算机视觉·自动化