-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble-texting.py
210 lines (154 loc) · 6.27 KB
/
double-texting.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# ===============================================
# Double texting
# ===============================================
# Users can send multiple messages in a row before the prior run(s) complete,
# and we want to ensure that we handle this gracefully.
# -----------------------------------------------
# Reject any new runs until the prior run(s) complete
# -----------------------------------------------
from langgraph_sdk import get_client
url_for_cli_deployment = "http://localhost:8123"
client = get_client(url=url_for_cli_deployment)
# -----------------------------------------------
import httpx
from langchain_core.messages import HumanMessage
# # Create a thread
# thread = await client.threads.create()
# # Create to dos
# user_input_1 = "Add a ToDo to follow-up with DI Repairs."
# user_input_2 = "Add a ToDo to mount dresser to the wall."
# config = {"configurable": {"user_id": "Test-Double-Texting"}}
# graph_name = "task_maistro"
# run = await client.runs.create(
# thread["thread_id"],
# graph_name,
# input={"messages": [HumanMessage(content=user_input_1)]},
# config=config,
# )
# try:
# await client.runs.create(
# thread["thread_id"],
# graph_name,
# input={"messages": [HumanMessage(content=user_input_2)]},
# config=config,
# multitask_strategy="reject",
# )
# except httpx.HTTPStatusError as e:
# print("Failed to start concurrent run", e)
# -----------------------------------------------
from langchain_core.messages import convert_to_messages
# # Wait until the original run completes
# await client.runs.join(thread["thread_id"], run["run_id"])
# # Get the state of the thread
# state = await client.threads.get_state(thread["thread_id"])
# for m in convert_to_messages(state["values"]["messages"]):
# m.pretty_print()
# -----------------------------------------------
# Enqueue any new runs until the prior run(s) complete
# -----------------------------------------------
# # Create a new thread
# thread = await client.threads.create()
# # Create new ToDos
# user_input_1 = "Send Erik his t-shirt gift this weekend."
# user_input_2 = "Get cash and pay nanny for 2 weeks. Do this by Friday."
# config = {"configurable": {"user_id": "Test-Double-Texting"}}
# graph_name = "task_maistro"
# first_run = await client.runs.create(
# thread["thread_id"],
# graph_name,
# input={"messages": [HumanMessage(content=user_input_1)]},
# config=config,
# )
# second_run = await client.runs.create(
# thread["thread_id"],
# graph_name,
# input={"messages": [HumanMessage(content=user_input_2)]},
# config=config,
# multitask_strategy="enqueue",
# )
# # Wait until the second run completes
# await client.runs.join(thread["thread_id"], second_run["run_id"])
# # Get the state of the thread
# state = await client.threads.get_state(thread["thread_id"])
# for m in convert_to_messages(state["values"]["messages"]):
# m.pretty_print()
# -----------------------------------------------
# Interrupt the current run and save all the work
# that has been done so far
# -----------------------------------------------
import asyncio
# Create a new thread
thread = await client.threads.create()
# -----------------------------------------------
# Create new ToDos
user_input_1 = "Give me a summary of my ToDos due tomrrow."
user_input_2 = "Never mind, create a ToDo to Order Ham for Thanksgiving by next Friday."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"
interrupted_run = await client.runs.create(
thread["thread_id"],
graph_name,
input={"messages": [HumanMessage(content=user_input_1)]},
config=config,
)
# -----------------------------------------------
# Wait for some of run 1 to complete so that
# we can see it in the thread
await asyncio.sleep(1)
second_run = await client.runs.create(
thread["thread_id"],
graph_name,
input={"messages": [HumanMessage(content=user_input_2)]},
config=config,
multitask_strategy="interrupt",
)
# -----------------------------------------------
# Wait until the second run completes
await client.runs.join(thread["thread_id"], second_run["run_id"])
# -----------------------------------------------
# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
# for m in convert_to_messages(state["values"]["messages"]):
# m.pretty_print()
# -----------------------------------------------
# Confirm that the first run was interrupted
# print((await client.runs.get(thread["thread_id"], interrupted_run["run_id"]))["status"])
# -----------------------------------------------
# Rollback to interrupt prior run
# -----------------------------------------------
# Create a new thread
thread = await client.threads.create()
# -----------------------------------------------
# Create new ToDos
user_input_1 = "Add a ToDo to call to make appointment at Yoga."
user_input_2 = "Actually, add a ToDo to drop by Yoga in person on Sunday."
config = {"configurable": {"user_id": "Test-Double-Texting"}}
graph_name = "task_maistro"
rolled_back_run = await client.runs.create(
thread["thread_id"],
graph_name,
input={"messages": [HumanMessage(content=user_input_1)]},
config=config,
)
second_run = await client.runs.create(
thread["thread_id"],
graph_name,
input={"messages": [HumanMessage(content=user_input_2)]},
config=config,
multitask_strategy="rollback",
)
# -----------------------------------------------
# Wait until the second run completes
await client.runs.join(thread["thread_id"], second_run["run_id"])
# -----------------------------------------------
# Get the state of the thread
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
m.pretty_print()
# -----------------------------------------------
# Confirm that the original run was deleted
try:
await client.runs.get(thread["thread_id"], rolled_back_run["run_id"])
except httpx.HTTPStatusError as _:
print("Original run was correctly deleted")
# -----------------------------------------------