-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanggraph-python-checkpointer-proof.py
More file actions
186 lines (159 loc) · 5.85 KB
/
Copy pathlanggraph-python-checkpointer-proof.py
File metadata and controls
186 lines (159 loc) · 5.85 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import importlib.metadata
import json
from typing import Any, TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, START, StateGraph
PRIVATE_PHRASE = "quiet LangGraph Python checkpoint note"
THREAD_ID = "atrib-python-checkpointer-smoke"
class ProcurementState(TypedDict):
request: str
private_note: str
answer: str
steps: list[str]
class RecordingInMemorySaver(InMemorySaver):
def __init__(self) -> None:
super().__init__()
self.events: list[dict[str, Any]] = []
def get_tuple(self, config: Any) -> Any:
result = super().get_tuple(config)
checkpoint = getattr(result, "checkpoint", None) if result is not None else None
self._append(
{
"operation": "get_tuple",
"config": summarize_config(config),
"found": result is not None,
"checkpoint_id": checkpoint.get("id") if isinstance(checkpoint, dict) else None,
}
)
return result
def put(
self,
config: Any,
checkpoint: Any,
metadata: Any,
new_versions: Any,
) -> Any:
result = super().put(config, checkpoint, metadata, new_versions)
channel_values = checkpoint.get("channel_values", {}) if isinstance(checkpoint, dict) else {}
self._append(
{
"operation": "put",
"config": summarize_config(config),
"checkpoint_id": checkpoint.get("id") if isinstance(checkpoint, dict) else None,
"metadata": json_safe(metadata),
"channel_keys": sorted(channel_values.keys()),
"channel_values": json_safe(channel_values),
"new_version_keys": sorted(new_versions.keys()) if hasattr(new_versions, "keys") else [],
}
)
return result
def put_writes(
self,
config: Any,
writes: Any,
task_id: str,
task_path: str = "",
) -> None:
result = super().put_writes(config, writes, task_id, task_path)
self._append(
{
"operation": "put_writes",
"config": summarize_config(config),
"task_id": task_id,
"task_path": task_path,
"write_count": len(writes),
"write_channels": [write[0] for write in writes],
"writes": json_safe(writes),
}
)
return result
def _append(self, event: dict[str, Any]) -> None:
event["index"] = len(self.events)
self.events.append(event)
def summarize_config(config: Any) -> dict[str, Any]:
configurable = (config or {}).get("configurable", {})
return {
key: configurable[key]
for key in ("thread_id", "checkpoint_ns", "checkpoint_id")
if key in configurable
}
def json_safe(value: Any) -> Any:
try:
json.dumps(value, sort_keys=True)
return value
except TypeError:
if isinstance(value, tuple):
return [json_safe(item) for item in value]
if isinstance(value, list):
return [json_safe(item) for item in value]
if isinstance(value, dict):
return {str(key): json_safe(item) for key, item in value.items()}
return repr(value)
def draft(state: ProcurementState) -> ProcurementState:
return {
"request": state["request"],
"private_note": state["private_note"],
"answer": "",
"steps": state.get("steps", []) + ["draft"],
}
def approve(state: ProcurementState) -> ProcurementState:
return {
"request": state["request"],
"private_note": state["private_note"],
"answer": f"approved {state['request']}",
"steps": state.get("steps", []) + ["approve"],
}
def run_proof() -> dict[str, Any]:
builder = StateGraph(ProcurementState)
builder.add_node("draft", draft)
builder.add_node("approve", approve)
builder.add_edge(START, "draft")
builder.add_edge("draft", "approve")
builder.add_edge("approve", END)
saver = RecordingInMemorySaver()
graph = builder.compile(checkpointer=saver)
config = {"configurable": {"thread_id": THREAD_ID}}
result = graph.invoke(
{
"request": "atlas-kit order",
"private_note": PRIVATE_PHRASE,
"answer": "",
"steps": [],
},
config,
)
state_snapshot = graph.get_state(config)
return {
"ok": True,
"langgraph_version": importlib.metadata.version("langgraph"),
"workflow": {
"graph": "StateGraph",
"compile": "compile(checkpointer=InMemorySaver())",
"nodes": ["draft", "approve"],
"thread_id": THREAD_ID,
},
"checkpointer": {
"class": "InMemorySaver",
"operations": sorted({event["operation"] for event in saver.events}),
},
"events": saver.events,
"summary": {
"event_count": len(saver.events),
"get_tuple_count": sum(1 for event in saver.events if event["operation"] == "get_tuple"),
"put_count": sum(1 for event in saver.events if event["operation"] == "put"),
"put_writes_count": sum(
1 for event in saver.events if event["operation"] == "put_writes"
),
"private_phrase_in_events": PRIVATE_PHRASE in json.dumps(saver.events),
"private_phrase_in_state": PRIVATE_PHRASE in json.dumps(state_snapshot.values),
"workflow_completed": result["answer"] == "approved atlas-kit order",
},
"final_output": {
"answer": result["answer"],
"steps": result["steps"],
},
}
if __name__ == "__main__":
print(json.dumps(run_proof(), sort_keys=True))