-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_with_checkpointer.py
More file actions
59 lines (43 loc) · 1.79 KB
/
root_with_checkpointer.py
File metadata and controls
59 lines (43 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Root graph with checkpointer — enables interrupt() for multi-turn conversations.
When a node metaclass is the root graph (no parent), pass a checkpointer
to .compile() so interrupt() can persist state between turns.
"""
# ruff: noqa: N801, N805
import asyncio
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.types import Command, interrupt
from langchain_agentkit import node
class advisor(node):
llm = ChatOpenAI(model="gpt-4o")
async def handler(state, *, llm):
response = await llm.ainvoke(state["messages"])
# No tool calls — ask the user for input instead of ending
if not response.tool_calls:
user_reply = interrupt(response.content)
return {
"messages": [response, HumanMessage(content=user_reply)],
}
return {"messages": [response]}
async def main():
# Compile with a checkpointer for interrupt() support
graph = advisor.compile(checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "session-1"}}
# Turn 1: agent responds, hits interrupt() — graph pauses
result = await graph.ainvoke(
{"messages": [HumanMessage("Help me frame this problem")]},
config,
)
print(f"Agent: {result['messages'][-1].content}")
# Check that the graph is paused, not ended
state = await graph.aget_state(config)
assert bool(state.tasks), "Expected pending interrupt"
# Turn 2: resume with user input via Command(resume=...)
result = await graph.ainvoke(
Command(resume="B2B SaaS market declined 12% in Q3"),
config,
)
print(f"Agent: {result['messages'][-1].content}")
if __name__ == "__main__":
asyncio.run(main())