This commit is contained in:
gqt
2026-08-16 15:37:10 +08:00
parent b3f3b72f07
commit 6163f5b1be
5 changed files with 472 additions and 13557 deletions
File diff suppressed because it is too large Load Diff
+84
View File
@@ -0,0 +1,84 @@
from typing import TypedDict
from loguru import logger
import time
from langgraph.types import interrupt, Command
class OverallState(TypedDict):
initial_input: str
node_a1: str
node_a2: str
node_b: str
def node_a1(state: OverallState) -> OverallState:
time.sleep(1)
logger.info("node_a1")
return OverallState(node_a1="node_a1")
def node_a2(state: OverallState) -> OverallState:
time.sleep(3)
logger.info("node_a2")
return OverallState(node_a2="node_a2")
def node_b(state: OverallState) -> OverallState:
time.sleep(1)
res = interrupt("hello")
logger.info("node_b")
return OverallState(node_b=f"node_b,res={res}")
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
builder = StateGraph(state_schema=OverallState)
builder.add_node("node_a1", node_a1)
builder.add_node("node_a2", node_a2)
builder.add_node("node_b", node_b)
builder.add_edge(START, "node_a1")
builder.add_edge(START, "node_a2")
builder.add_edge(["node_a1", "node_a2"], "node_b")
builder.add_edge("node_b", END)
checkpoint_saver = InMemorySaver()
graph = builder.compile(checkpointer=checkpoint_saver)
from IPython.display import display
display(graph)
from rich import print as rp
config = {
"configurable": {
"thread_id": "12345"
}
}
for chunk in graph.stream(
{"initial_input": "init"},
stream_mode=["checkpoints"],
config=config
):
print("=" * 50)
rp(chunk)
print("=" * 50)
rp(list(graph.get_state_history(config=config)))
print("**" * 50)
for chunk in graph.stream(
Command(resume="interrupt_node_b_res"),
stream_mode=["checkpoints"],
config=config
):
print("=" * 50)
rp(chunk)
print("=" * 50)
rp(list(graph.get_state_history(config=config)))
+119
View File
@@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true
},
"source": [
"from typing import TypedDict\n",
"from loguru import logger\n",
"import time\n",
"\n",
"\n",
"class OverallState(TypedDict):\n",
" initial_input: str\n",
" node_a1: str\n",
" node_a2: str\n",
" node_b: str\n",
"\n",
"\n",
"def node_a1(state: OverallState) -> OverallState:\n",
" time.sleep(1)\n",
" logger.info(\"node_a1\")\n",
" return OverallState(node_a1=\"node_a1\")\n",
"\n",
"\n",
"def node_a2(state: OverallState) -> OverallState:\n",
" time.sleep(3)\n",
" logger.info(\"node_a2\")\n",
"\n",
" return OverallState(node_a2=\"node_a2\")\n",
"\n",
"\n",
"def node_b(state: OverallState) -> OverallState:\n",
" time.sleep(1)\n",
" logger.info(\"node_b\")\n",
" return OverallState(node_b=\"node_b\")"
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"from langgraph.graph import StateGraph, START, END\n",
"from langgraph.checkpoint.memory import InMemorySaver\n",
"\n",
"builder = StateGraph(state_schema=OverallState)\n",
"builder.add_node(\"node_a1\", node_a1)\n",
"builder.add_node(\"node_a2\", node_a2)\n",
"builder.add_node(\"node_b\", node_b)\n",
"\n",
"builder.add_edge(START, \"node_a1\")\n",
"builder.add_edge(START, \"node_a2\")\n",
"builder.add_edge([\"node_a1\", \"node_a2\"], \"node_b\")\n",
"builder.add_edge(\"node_b\", END)\n",
"\n",
"checkpoint_saver = InMemorySaver()\n",
"\n",
"graph = builder.compile(checkpointer=checkpoint_saver)\n",
"\n",
"from IPython.display import display\n",
"\n",
"display(graph)"
],
"id": "87e186e33e43ad5f",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"from rich import print as rp\n",
"\n",
"config = {\n",
" \"configurable\": {\n",
" \"thread_id\": \"12345\"\n",
" }\n",
"}\n",
"\n",
"for chunk in graph.stream(\n",
" {\"initial_input\": \"init\"},\n",
" stream_mode=[\"checkpoints\"],\n",
" config=config\n",
"):\n",
" print(\"=\" * 50)\n",
" # print(chunk)\n",
" rp(chunk)"
],
"id": "80bb26c320de30c7",
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+86
View File
@@ -0,0 +1,86 @@
from typing import TypedDict
from loguru import logger
import time
from langgraph.types import interrupt, Command
# https://xbsheng.github.io/atguigu-note/langgraph/%E8%AF%BE%E4%BB%B6/05-LangGraph%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7
class OverallState(TypedDict):
initial_input: str
node_a1: str
node_a2: str
node_b: str
def node_a1(state: OverallState) -> OverallState:
time.sleep(1)
logger.info("node_a1")
return OverallState(node_a1="node_a1")
def node_a2(state: OverallState) -> OverallState:
time.sleep(3)
logger.info("node_a2")
return OverallState(node_a2="node_a2")
def node_b(state: OverallState) -> OverallState:
time.sleep(1)
res = interrupt("hello")
logger.info("node_b")
return OverallState(node_b=f"node_b,res={res}")
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
builder = StateGraph(state_schema=OverallState)
builder.add_node("node_a1", node_a1)
builder.add_node("node_a2", node_a2)
builder.add_node("node_b", node_b)
builder.add_edge(START, "node_a1")
builder.add_edge(START, "node_a2")
builder.add_edge(["node_a1", "node_a2"], "node_b")
builder.add_edge("node_b", END)
checkpoint_saver = InMemorySaver()
graph = builder.compile(checkpointer=checkpoint_saver)
from IPython.display import display
display(graph)
from rich import print as rp
config = {
"configurable": {
"thread_id": "12345"
}
}
for chunk in graph.stream(
{"initial_input": "init"},
stream_mode=["debug"],
config=config
):
print("=" * 50)
rp(chunk)
# print("=" * 50)
# rp(list(graph.get_state_history(config=config)))
print("**" * 50)
for chunk in graph.stream(
Command(resume="interrupt_node_b_res"),
stream_mode=["debug"],
config=config
):
print("=" * 50)
rp(chunk)
# print("=" * 50)
# rp(list(graph.get_state_history(config=config)))
View File