-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson_5_Student.py
More file actions
322 lines (269 loc) · 86.8 KB
/
Lesson_5_Student.py
File metadata and controls
322 lines (269 loc) · 86.8 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from dotenv import load_dotenv
from uuid import uuid4
from typing import TypedDict, Annotated
import operator
from langgraph.graph import StateGraph, END
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, ToolMessage, AIMessage
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.checkpoint.sqlite import SqliteSaver
_ = load_dotenv()
memory = SqliteSaver.from_conn_string(":memory:")
"""
In previous examples we've annotated the `messages` state key
with the default `operator.add` or `+` reducer, which always
appends new messages to the end of the existing messages array.
Now, to support replacing existing messages, we annotate the
`messages` key with a customer reducer function, which replaces
messages with the same `id`, and appends them otherwise.
"""
def reduce_messages(left: list[AnyMessage], right: list[AnyMessage]) -> list[AnyMessage]:
# assign ids to messages that don't have them (this is for HumanMessages)
for message in right:
if not message.id:
message.id = str(uuid4())
# merge the new messages with the existing messages
merged = left.copy()
for message in right:
for i, existing in enumerate(merged):
# replace any existing messages with the same id
if existing.id == message.id:
merged[i] = message
break
else:
# append any new messages to the end
merged.append(message)
return merged
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], reduce_messages]
tool = TavilySearchResults(max_results=2)
# Manual human approval:
class Agent:
def __init__(self, model, tools, system="", checkpointer=None):
self.system = system
graph = StateGraph(AgentState)
graph.add_node("llm", self.call_openai)
graph.add_node("action", self.take_action)
graph.add_conditional_edges("llm", self.exists_action, {True: "action", False: END})
graph.add_edge("action", "llm")
graph.set_entry_point("llm")
self.graph = graph.compile(
checkpointer=checkpointer,
# add interrupt that triggers before we call node "action"
interrupt_before=["action"]
)
self.tools = {t.name: t for t in tools}
self.model = model.bind_tools(tools)
def call_openai(self, state: AgentState):
messages = state['messages']
if self.system:
messages = [SystemMessage(content=self.system)] + messages
message = self.model.invoke(messages)
return {'messages': [message]}
def exists_action(self, state: AgentState):
print("In exists_action:")
print(state)
result = state['messages'][-1]
return len(result.tool_calls) > 0
def take_action(self, state: AgentState):
tool_calls = state['messages'][-1].tool_calls
results = []
for t in tool_calls:
print(f"Calling: {t}")
result = self.tools[t['name']].invoke(t['args'])
results.append(ToolMessage(tool_call_id=t['id'], name=t['name'], content=str(result)))
print("Back to the model!")
return {'messages': results}
prompt = """You are a smart research assistant. Use the search engine to look up information. \
You are allowed to make multiple calls (either together or in sequence). \
Only look up information when you are sure of what you want. \
If you need to look up some information before asking a follow up question, you are allowed to do that!
"""
model = ChatOpenAI(model="gpt-3.5-turbo")
abot = Agent(model, [tool], system=prompt, checkpointer=memory)
messages = [HumanMessage(content="Whats the weather in SF?")]
thread = {"configurable": {"thread_id": "1"}}
# stream interrupts before entering "action" node
for event in abot.graph.stream({"messages": messages}, thread):
for v in event.values():
print("Event:")
print(v)
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in SF?', id='05708aa6-3b42-4076-b689-7899e3fc8df6'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}])]}
# Event:
# {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}])]}
print(abot.graph.get_state(thread))
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in SF?', id='05708aa6-3b42-4076-b689-7899e3fc8df6'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}])]}, next=('action',), config={'configurable': {'thread_id': '1', 'thread_ts': '1efcb6dc-ce95-64bd-8001-12855f362b96'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}])]}}}, created_at='2025-01-05T14:03:12.149193+00:00', parent_config={'configurable': {'thread_id': '1', 'thread_ts': '1efcb6dc-ce5c-6e85-8000-e6d7fa7c55af'}})
print(abot.graph.get_state(thread).next)
# ('action',)
# continue after interrupt (note that "messages" is Annotated such that None does not change the messages attribute from AgentState):
for event in abot.graph.stream(None, thread):
print("Event:")
for v in event.values():
print(v)
# Calling: {'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}
# Back to the model!
# Event:
# {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.775, \'lon\': -122.4183, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736084070, \'localtime\': \'2025-01-05 05:34\'}, \'current\': {\'last_updated_epoch\': 1736083800, \'last_updated\': \'2025-01-05 05:30\', \'temp_c\': 7.8, \'temp_f\': 46.0, \'is_day\': 0, \'condition\': {\'text\': \'Partly cloudy\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/116.png\', \'code\': 1003}, \'wind_mph\': 4.0, \'wind_kph\': 6.5, \'wind_degree\': 30, \'wind_dir\': \'NNE\', \'pressure_mb\': 1027.0, \'pressure_in\': 30.34, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 89, \'cloud\': 25, \'feelslike_c\': 6.8, \'feelslike_f\': 44.3, \'windchill_c\': 7.9, \'windchill_f\': 46.2, \'heatindex_c\': 8.0, \'heatindex_f\': 46.5, \'dewpoint_c\': 7.6, \'dewpoint_f\': 45.7, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 0.0, \'gust_mph\': 6.6, \'gust_kph\': 10.6}}"}, {\'url\': \'https://www.easeweather.com/north-america/united-states/california/city-and-county-of-san-francisco/san-francisco/may\', \'content\': \'Weather in San Francisco in May 2025 - Detailed Forecast Weather in San Francisco for May 2025 Your guide to San Francisco weather in May - trends and predictions In general, the average temperature in San Francisco at the beginning of May is 17.1\\xa0°F. San Francisco in May average weather Access San Francisco weather forecasts with a simple click. Temperatures trend during May in San Francisco We recommend that you check the San Francisco forecast closer to your planned date for the most up-to-date weather information. Explore the daily rainfall trends and prepare for San Franciscos May weather\\xa0💧 Get accurate weather forecasts for San Francisco, located at latitude 37.775 and longitude -122.419.\'}]', name='tavily_search_results_json', id='9fcd0ce0-fa50-4a4e-a6a6-c30128bc5cfa', tool_call_id='call_KIHI1A0NxP6RCdnJFYL654TM')]}
# In exists_action
# {'messages': [HumanMessage(content='Whats the weather in SF?', id='05708aa6-3b42-4076-b689-7899e3fc8df6'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.775, \'lon\': -122.4183, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736084070, \'localtime\': \'2025-01-05 05:34\'}, \'current\': {\'last_updated_epoch\': 1736083800, \'last_updated\': \'2025-01-05 05:30\', \'temp_c\': 7.8, \'temp_f\': 46.0, \'is_day\': 0, \'condition\': {\'text\': \'Partly cloudy\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/116.png\', \'code\': 1003}, \'wind_mph\': 4.0, \'wind_kph\': 6.5, \'wind_degree\': 30, \'wind_dir\': \'NNE\', \'pressure_mb\': 1027.0, \'pressure_in\': 30.34, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 89, \'cloud\': 25, \'feelslike_c\': 6.8, \'feelslike_f\': 44.3, \'windchill_c\': 7.9, \'windchill_f\': 46.2, \'heatindex_c\': 8.0, \'heatindex_f\': 46.5, \'dewpoint_c\': 7.6, \'dewpoint_f\': 45.7, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 0.0, \'gust_mph\': 6.6, \'gust_kph\': 10.6}}"}, {\'url\': \'https://www.easeweather.com/north-america/united-states/california/city-and-county-of-san-francisco/san-francisco/may\', \'content\': \'Weather in San Francisco in May 2025 - Detailed Forecast Weather in San Francisco for May 2025 Your guide to San Francisco weather in May - trends and predictions In general, the average temperature in San Francisco at the beginning of May is 17.1\\xa0°F. San Francisco in May average weather Access San Francisco weather forecasts with a simple click. Temperatures trend during May in San Francisco We recommend that you check the San Francisco forecast closer to your planned date for the most up-to-date weather information. Explore the daily rainfall trends and prepare for San Franciscos May weather\\xa0💧 Get accurate weather forecasts for San Francisco, located at latitude 37.775 and longitude -122.419.\'}]', name='tavily_search_results_json', id='9fcd0ce0-fa50-4a4e-a6a6-c30128bc5cfa', tool_call_id='call_KIHI1A0NxP6RCdnJFYL654TM'), AIMessage(content='The current weather in San Francisco is partly cloudy with a temperature of 46.0°F (7.8°C). The wind speed is 4.0 mph (6.5 kph) coming from the NNE direction. The humidity is 89%, and the visibility is 9.0 miles.', response_metadata={'token_usage': {'completion_tokens': 65, 'prompt_tokens': 784, 'total_tokens': 849, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-dee935fe-95d5-490a-b201-3374fd19ec74-0')]}
# Event:
# {'messages': [AIMessage(content='The current weather in San Francisco is partly cloudy with a temperature of 46.0°F (7.8°C). The wind speed is 4.0 mph (6.5 kph) coming from the NNE direction. The humidity is 89%, and the visibility is 9.0 miles.', response_metadata={'token_usage': {'completion_tokens': 65, 'prompt_tokens': 784, 'total_tokens': 849, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-dee935fe-95d5-490a-b201-3374fd19ec74-0')]}
print(abot.graph.get_state(thread))
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in SF?', id='05708aa6-3b42-4076-b689-7899e3fc8df6'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-6db23d53-026e-41ef-b43c-3a162adedfed-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_KIHI1A0NxP6RCdnJFYL654TM'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.775, \'lon\': -122.4183, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736084070, \'localtime\': \'2025-01-05 05:34\'}, \'current\': {\'last_updated_epoch\': 1736083800, \'last_updated\': \'2025-01-05 05:30\', \'temp_c\': 7.8, \'temp_f\': 46.0, \'is_day\': 0, \'condition\': {\'text\': \'Partly cloudy\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/116.png\', \'code\': 1003}, \'wind_mph\': 4.0, \'wind_kph\': 6.5, \'wind_degree\': 30, \'wind_dir\': \'NNE\', \'pressure_mb\': 1027.0, \'pressure_in\': 30.34, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 89, \'cloud\': 25, \'feelslike_c\': 6.8, \'feelslike_f\': 44.3, \'windchill_c\': 7.9, \'windchill_f\': 46.2, \'heatindex_c\': 8.0, \'heatindex_f\': 46.5, \'dewpoint_c\': 7.6, \'dewpoint_f\': 45.7, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 0.0, \'gust_mph\': 6.6, \'gust_kph\': 10.6}}"}, {\'url\': \'https://www.easeweather.com/north-america/united-states/california/city-and-county-of-san-francisco/san-francisco/may\', \'content\': \'Weather in San Francisco in May 2025 - Detailed Forecast Weather in San Francisco for May 2025 Your guide to San Francisco weather in May - trends and predictions In general, the average temperature in San Francisco at the beginning of May is 17.1\\xa0°F. San Francisco in May average weather Access San Francisco weather forecasts with a simple click. Temperatures trend during May in San Francisco We recommend that you check the San Francisco forecast closer to your planned date for the most up-to-date weather information. Explore the daily rainfall trends and prepare for San Franciscos May weather\\xa0💧 Get accurate weather forecasts for San Francisco, located at latitude 37.775 and longitude -122.419.\'}]', name='tavily_search_results_json', id='9fcd0ce0-fa50-4a4e-a6a6-c30128bc5cfa', tool_call_id='call_KIHI1A0NxP6RCdnJFYL654TM'), AIMessage(content='The current weather in San Francisco is partly cloudy with a temperature of 46.0°F (7.8°C). The wind speed is 4.0 mph (6.5 kph) coming from the NNE direction. The humidity is 89%, and the visibility is 9.0 miles.', response_metadata={'finish_reason': 'stop', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 65, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 784, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 849}}, id='run-dee935fe-95d5-490a-b201-3374fd19ec74-0')]}, next=(), config={'configurable': {'thread_id': '1', 'thread_ts': '1efcb6f6-9dc1-62b1-8003-869f7f786953'}}, metadata={'source': 'loop', 'step': 3, 'writes': {'llm': {'messages': [AIMessage(content='The current weather in San Francisco is partly cloudy with a temperature of 46.0°F (7.8°C). The wind speed is 4.0 mph (6.5 kph) coming from the NNE direction. The humidity is 89%, and the visibility is 9.0 miles.', response_metadata={'finish_reason': 'stop', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 65, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 784, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 849}}, id='run-dee935fe-95d5-490a-b201-3374fd19ec74-0')]}}}, created_at='2025-01-05T14:14:44.961330+00:00', parent_config={'configurable': {'thread_id': '1', 'thread_ts': '1efcb6f6-9509-62f8-8002-054aad6d0fc7'}})
# nothing left to be done:
print(abot.graph.get_state(thread).next)
# ()
# execute this in a loop:
messages = [HumanMessage("Whats the weather in LA?")]
thread = {"configurable": {"thread_id": "2"}}
for event in abot.graph.stream({"messages": messages}, thread):
for v in event.values():
print(v)
while abot.graph.get_state(thread).next:
print("\n", abot.graph.get_state(thread),"\n")
_input = input("proceed?")
if _input != "y":
print("aborting")
break
for event in abot.graph.stream(None, thread):
for v in event.values():
print(v)
# In exists_action
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='aff7ce5f-76e0-40ba-92c1-fe2d881e193e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-a2957166-681c-42e7-840b-9db2322a3242-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
# {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-a2957166-681c-42e7-840b-9db2322a3242-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='aff7ce5f-76e0-40ba-92c1-fe2d881e193e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-a2957166-681c-42e7-840b-9db2322a3242-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '2', 'thread_ts': '1efcb70f-3cfd-68fa-8001-69694601fb4c'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-a2957166-681c-42e7-840b-9db2322a3242-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-05T14:25:45.903520+00:00', parent_config={'configurable': {'thread_id': '2', 'thread_ts': '1efcb70f-3ca5-6069-8000-c147322ac50a'}})
# proceed? y
# Calling: {'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}
# Back to the model!
# {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736087176, \'localtime\': \'2025-01-05 06:26\'}, \'current\': {\'last_updated_epoch\': 1736086500, \'last_updated\': \'2025-01-05 06:15\', \'temp_c\': 8.9, \'temp_f\': 48.0, \'is_day\': 0, \'condition\': {\'text\': \'Clear\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/113.png\', \'code\': 1000}, \'wind_mph\': 4.7, \'wind_kph\': 7.6, \'wind_degree\': 358, \'wind_dir\': \'N\', \'pressure_mb\': 1020.0, \'pressure_in\': 30.11, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 77, \'cloud\': 0, \'feelslike_c\': 7.8, \'feelslike_f\': 46.1, \'windchill_c\': 9.8, \'windchill_f\': 49.6, \'heatindex_c\': 11.3, \'heatindex_f\': 52.4, \'dewpoint_c\': -6.7, \'dewpoint_f\': 19.9, \'vis_km\': 14.0, \'vis_miles\': 8.0, \'uv\': 0.0, \'gust_mph\': 9.9, \'gust_kph\': 15.9}}"}, {\'url\': \'https://www.weather2travel.com/california/los-angeles/may/\', \'content\': \'Los Angeles May weather guide. Check temperature, rainfall & sunshine averages & more in May 2025 for Los Angeles, California - USA.\'}]', name='tavily_search_results_json', id='0fefa01c-3983-4951-8441-e03a467868a9', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}
# In exists_action
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='aff7ce5f-76e0-40ba-92c1-fe2d881e193e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-a2957166-681c-42e7-840b-9db2322a3242-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736087176, \'localtime\': \'2025-01-05 06:26\'}, \'current\': {\'last_updated_epoch\': 1736086500, \'last_updated\': \'2025-01-05 06:15\', \'temp_c\': 8.9, \'temp_f\': 48.0, \'is_day\': 0, \'condition\': {\'text\': \'Clear\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/113.png\', \'code\': 1000}, \'wind_mph\': 4.7, \'wind_kph\': 7.6, \'wind_degree\': 358, \'wind_dir\': \'N\', \'pressure_mb\': 1020.0, \'pressure_in\': 30.11, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 77, \'cloud\': 0, \'feelslike_c\': 7.8, \'feelslike_f\': 46.1, \'windchill_c\': 9.8, \'windchill_f\': 49.6, \'heatindex_c\': 11.3, \'heatindex_f\': 52.4, \'dewpoint_c\': -6.7, \'dewpoint_f\': 19.9, \'vis_km\': 14.0, \'vis_miles\': 8.0, \'uv\': 0.0, \'gust_mph\': 9.9, \'gust_kph\': 15.9}}"}, {\'url\': \'https://www.weather2travel.com/california/los-angeles/may/\', \'content\': \'Los Angeles May weather guide. Check temperature, rainfall & sunshine averages & more in May 2025 for Los Angeles, California - USA.\'}]', name='tavily_search_results_json', id='0fefa01c-3983-4951-8441-e03a467868a9', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content="The current weather in Los Angeles is as follows:\n- Temperature: 8.9°C (48.0°F)\n- Condition: Clear\n- Wind: 7.6 kph from the North\n- Pressure: 1020.0 mb\n- Humidity: 77%\n- Visibility: 14.0 km\n- UV Index: 0.0\n- Cloud Cover: 0%\n- Feels like: 7.8°C (46.1°F)\n- Dewpoint: -6.7°C (19.9°F)\n- Windchill: 9.8°C (49.6°F)\n- Heat Index: 11.3°C (52.4°F)\n- Precipitation: 0.0 mm\n\nIt's currently nighttime in Los Angeles.", response_metadata={'token_usage': {'completion_tokens': 167, 'prompt_tokens': 648, 'total_tokens': 815, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-4ebbca42-0054-4413-804e-004fab77853b-0')]}
# {'messages': [AIMessage(content="The current weather in Los Angeles is as follows:\n- Temperature: 8.9°C (48.0°F)\n- Condition: Clear\n- Wind: 7.6 kph from the North\n- Pressure: 1020.0 mb\n- Humidity: 77%\n- Visibility: 14.0 km\n- UV Index: 0.0\n- Cloud Cover: 0%\n- Feels like: 7.8°C (46.1°F)\n- Dewpoint: -6.7°C (19.9°F)\n- Windchill: 9.8°C (49.6°F)\n- Heat Index: 11.3°C (52.4°F)\n- Precipitation: 0.0 mm\n\nIt's currently nighttime in Los Angeles.", response_metadata={'token_usage': {'completion_tokens': 167, 'prompt_tokens': 648, 'total_tokens': 815, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-4ebbca42-0054-4413-804e-004fab77853b-0')]}
# Modify State:
messages = [HumanMessage("Whats the weather in LA?")]
thread = {"configurable": {"thread_id": "3"}}
for event in abot.graph.stream({"messages": messages}, thread):
print("Event:")
for v in event.values():
print(v)
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
# Event:
# {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 152, 'total_tokens': 173, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
print(abot.graph.get_state(thread))
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcbab1-7775-6343-8001-8c773a1e2f04'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-05T21:21:56.531672+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcbab1-7741-6620-8000-46c901516ffa'}})
current_values = abot.graph.get_state(thread)
print(current_values.values['messages'][-1])
# AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])
print(current_values.values['messages'][-1].tool_calls)
# [{'name': 'tavily_search_results_json',
# 'args': {'query': 'Los Angeles current weather'},
# 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]
_id = current_values.values['messages'][-1].tool_calls[0]['id']
current_values.values['messages'][-1].tool_calls = [
{'name': 'tavily_search_results_json',
'args': {'query': 'current weather in Louisiana'},
'id': _id}
]
abot.graph.update_state(thread, current_values.values)
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
# {'configurable': {'thread_id': '3',
# 'thread_ts': '1efcbac4-93b5-6564-8002-8c965f427106'}}
# New LangGraph version with additional state information (in metadata), compared to previous version, used in video:
print(abot.graph.get_state(thread))
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcbac4-93b5-6564-8002-8c965f427106'}}, metadata={'source': 'update', 'step': 2, 'writes': {'llm': {'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-05T21:30:29.521333+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcbab1-7775-6343-8001-8c773a1e2f04'}})
for event in abot.graph.stream(None, thread):
print("Event:")
for v in event.values():
print(v)
# Calling: {'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}
# Back to the model!
# Event:
# {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Louisiana\', \'region\': \'Missouri\', \'country\': \'USA United States of America\', \'lat\': 39.4411, \'lon\': -91.0551, \'tz_id\': \'America/Chicago\', \'localtime_epoch\': 1736113376, \'localtime\': \'2025-01-05 15:42\'}, \'current\': {\'last_updated_epoch\': 1736112600, \'last_updated\': \'2025-01-05 15:30\', \'temp_c\': -6.6, \'temp_f\': 20.1, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 15.9, \'wind_kph\': 25.6, \'wind_degree\': 60, \'wind_dir\': \'ENE\', \'pressure_mb\': 1014.0, \'pressure_in\': 29.94, \'precip_mm\': 2.5, \'precip_in\': 0.1, \'humidity\': 92, \'cloud\': 100, \'feelslike_c\': -14.5, \'feelslike_f\': 6.0, \'windchill_c\': -12.9, \'windchill_f\': 8.7, \'heatindex_c\': -5.5, \'heatindex_f\': 22.2, \'dewpoint_c\': -7.1, \'dewpoint_f\': 19.3, \'vis_km\': 0.8, \'vis_miles\': 0.0, \'uv\': 0.2, \'gust_mph\': 23.7, \'gust_kph\': 38.2}}"}, {\'url\': \'https://world-weather.info/forecast/usa/new_orleans/may-2025/\', \'content\': "Weather in New Orleans in May 2025 (Louisiana) - Detailed Weather Forecast for a Month Weather World United States Weather in New Orleans Weather in New Orleans in May 2025 New Orleans Weather Forecast for May 2025, is based on previous years\' statistical data. +81°+72° +81°+72° +81°+72° +79°+70° +81°+73° +81°+73° +84°+73° +84°+73° +82°+75° +84°+75° +82°+75° +84°+75° +84°+75° +84°+75° +82°+73° +82°+73° +84°+75° +82°+73° +84°+75° +86°+77° +86°+77° +86°+75° +86°+77° +86°+77° +84°+77° +86°+77° +88°+77° +88°+79° +84°+75° +84°+75° +86°+77° Extended weather forecast in New Orleans HourlyWeek10-Day14-Day30-DayYear Weather in large and nearby cities Weather in Washington, D.C.+25° Baton Rouge+55° Slidell+57° Biloxi+55° Brookhaven+52° Gulfport+55° Hattiesburg+50° McComb+55° Moss Point+54° Pascagoula+55° Morgan City+61° Metairie Terrace+61° Bogalusa+55° Denham Springs+57° Franklin+57° Franklinton+54° Taft+57° Sunrise+50° world\'s temperature today day day Temperature units"}]', name='tavily_search_results_json', id='ceb6f3d6-0a4e-4cc3-aa63-68585b157983', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='20f8ddd7-86b7-448c-8c8f-e525baa00b3e'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-b3d5b007-b30b-4f1f-938f-9c6861651fbf-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Louisiana\', \'region\': \'Missouri\', \'country\': \'USA United States of America\', \'lat\': 39.4411, \'lon\': -91.0551, \'tz_id\': \'America/Chicago\', \'localtime_epoch\': 1736113376, \'localtime\': \'2025-01-05 15:42\'}, \'current\': {\'last_updated_epoch\': 1736112600, \'last_updated\': \'2025-01-05 15:30\', \'temp_c\': -6.6, \'temp_f\': 20.1, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 15.9, \'wind_kph\': 25.6, \'wind_degree\': 60, \'wind_dir\': \'ENE\', \'pressure_mb\': 1014.0, \'pressure_in\': 29.94, \'precip_mm\': 2.5, \'precip_in\': 0.1, \'humidity\': 92, \'cloud\': 100, \'feelslike_c\': -14.5, \'feelslike_f\': 6.0, \'windchill_c\': -12.9, \'windchill_f\': 8.7, \'heatindex_c\': -5.5, \'heatindex_f\': 22.2, \'dewpoint_c\': -7.1, \'dewpoint_f\': 19.3, \'vis_km\': 0.8, \'vis_miles\': 0.0, \'uv\': 0.2, \'gust_mph\': 23.7, \'gust_kph\': 38.2}}"}, {\'url\': \'https://world-weather.info/forecast/usa/new_orleans/may-2025/\', \'content\': "Weather in New Orleans in May 2025 (Louisiana) - Detailed Weather Forecast for a Month Weather World United States Weather in New Orleans Weather in New Orleans in May 2025 New Orleans Weather Forecast for May 2025, is based on previous years\' statistical data. +81°+72° +81°+72° +81°+72° +79°+70° +81°+73° +81°+73° +84°+73° +84°+73° +82°+75° +84°+75° +82°+75° +84°+75° +84°+75° +84°+75° +82°+73° +82°+73° +84°+75° +82°+73° +84°+75° +86°+77° +86°+77° +86°+75° +86°+77° +86°+77° +84°+77° +86°+77° +88°+77° +88°+79° +84°+75° +84°+75° +86°+77° Extended weather forecast in New Orleans HourlyWeek10-Day14-Day30-DayYear Weather in large and nearby cities Weather in Washington, D.C.+25° Baton Rouge+55° Slidell+57° Biloxi+55° Brookhaven+52° Gulfport+55° Hattiesburg+50° McComb+55° Moss Point+54° Pascagoula+55° Morgan City+61° Metairie Terrace+61° Bogalusa+55° Denham Springs+57° Franklin+57° Franklinton+54° Taft+57° Sunrise+50° world\'s temperature today day day Temperature units"}]', name='tavily_search_results_json', id='ceb6f3d6-0a4e-4cc3-aa63-68585b157983', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content='I found information about the current weather in Louisiana, but it seems I need to specify the location further. Would you like me to look up the weather for a specific city in Louisiana, like New Orleans?', response_metadata={'token_usage': {'completion_tokens': 43, 'prompt_tokens': 997, 'total_tokens': 1040, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-d1dfbb9a-f6a2-4edd-b158-102f9f52653e-0')]}
# Event:
# {'messages': [AIMessage(content='I found information about the current weather in Louisiana, but it seems I need to specify the location further. Would you like me to look up the weather for a specific city in Louisiana, like New Orleans?', response_metadata={'token_usage': {'completion_tokens': 43, 'prompt_tokens': 997, 'total_tokens': 1040, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-d1dfbb9a-f6a2-4edd-b158-102f9f52653e-0')]}
# Time Travel:
states = []
for state in abot.graph.get_state_history(thread):
print(state)
print('--')
states.append(state)
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Louisiana\', \'region\': \'Missouri\', \'country\': \'USA United States of America\', \'lat\': 39.4411, \'lon\': -91.0551, \'tz_id\': \'America/Chicago\', \'localtime_epoch\': 1736175005, \'localtime\': \'2025-01-06 08:50\'}, \'current\': {\'last_updated_epoch\': 1736174700, \'last_updated\': \'2025-01-06 08:45\', \'temp_c\': -6.7, \'temp_f\': 19.9, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 17.7, \'wind_kph\': 28.4, \'wind_degree\': 338, \'wind_dir\': \'NNW\', \'pressure_mb\': 1024.0, \'pressure_in\': 30.23, \'precip_mm\': 0.66, \'precip_in\': 0.03, \'humidity\': 82, \'cloud\': 100, \'feelslike_c\': -15.0, \'feelslike_f\': 5.0, \'windchill_c\': -16.2, \'windchill_f\': 2.8, \'heatindex_c\': -7.5, \'heatindex_f\': 18.5, \'dewpoint_c\': -9.4, \'dewpoint_f\': 15.1, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 1.0, \'gust_mph\': 27.0, \'gust_kph\': 43.5}}"}, {\'url\': \'https://www.metcheck.com/WEATHER/dayforecast.asp?location=Louisiana&dateFor=06/01/2025&locationID=2025022&lat=39.448940&lon=-91.051530\', \'content\': \'LIVE\\xa0▼ CHARTS/FORECASTS\\xa0▼ MODELS\\xa0▼ COMMUNITY\\xa0▼ DISCUSSIONS\\xa0▼ COMMERCIAL\\xa0▼ -7°c -6°c -17°c -6°c -19°c -5°c -10°c -3°c -10°c -2°c -6°c -2°c -10°c -3°c -12°c Weather Forecast for\\xa0Louisiana\\xa0for\\xa0Monday 6 January Day/Night Day / Night Wet/Dry Dry & Wet ☀\\xa0▲\\xa07:24\\xa0\\xa0\\xa0\\xa016:53\\xa0\\xa0▼\\xa0☼\\xa0 ◄ Sun 16 Days ▼ Tue ► New Today On Metcheck Today\\xa0▼ Live\\xa0▼ Models\\xa0▼ Charts\\xa0▼ 1 NEW weather warning 1 NEW discussion 1 NEW global discussion 2 NEW live discussions 56 NEW weather reports 1 NEW snow discussion Forecasters Discussion\\xa0► Live Discussion\\xa0► ▼ Precipitype ▼ Aurora Forecast ◄► Weather Reports ▼ ICON ▼ ECMWF ◄► Discussions Weather Reports Weather Gallery Data Site Changes Weather Stickies\'}]', name='tavily_search_results_json', id='77f13ffd-8d3d-427d-a89e-480459a80c43', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content='I found information about the current weather in Louisiana, not Los Angeles. The current weather in Louisiana is as follows:\n- Temperature: 19.9°F (feels like 5.0°F)\n- Condition: Overcast\n- Wind: 28.4 kph NNW\n- Humidity: 82%\n- Cloud Cover: 100%\n- Visibility: 9.0 miles\n\nIf you meant Los Angeles specifically, I can search for the weather in Los Angeles. Would you like me to do that?', response_metadata={'finish_reason': 'stop', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 109, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 932, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 1041}}, id='run-c72f1263-037b-4d23-b3b8-62615f9e952f-0')]}, next=(), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f9-91f7-6235-8004-f4d7b5795371'}}, metadata={'source': 'loop', 'step': 4, 'writes': {'llm': {'messages': [AIMessage(content='I found information about the current weather in Louisiana, not Los Angeles. The current weather in Louisiana is as follows:\n- Temperature: 19.9°F (feels like 5.0°F)\n- Condition: Overcast\n- Wind: 28.4 kph NNW\n- Humidity: 82%\n- Cloud Cover: 100%\n- Visibility: 9.0 miles\n\nIf you meant Los Angeles specifically, I can search for the weather in Los Angeles. Would you like me to do that?', response_metadata={'finish_reason': 'stop', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 109, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 932, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 1041}}, id='run-c72f1263-037b-4d23-b3b8-62615f9e952f-0')]}}}, created_at='2025-01-06T15:04:59.575539+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f9-805d-6a15-8003-c6e3b945e978'}})
# --
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Louisiana\', \'region\': \'Missouri\', \'country\': \'USA United States of America\', \'lat\': 39.4411, \'lon\': -91.0551, \'tz_id\': \'America/Chicago\', \'localtime_epoch\': 1736175005, \'localtime\': \'2025-01-06 08:50\'}, \'current\': {\'last_updated_epoch\': 1736174700, \'last_updated\': \'2025-01-06 08:45\', \'temp_c\': -6.7, \'temp_f\': 19.9, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 17.7, \'wind_kph\': 28.4, \'wind_degree\': 338, \'wind_dir\': \'NNW\', \'pressure_mb\': 1024.0, \'pressure_in\': 30.23, \'precip_mm\': 0.66, \'precip_in\': 0.03, \'humidity\': 82, \'cloud\': 100, \'feelslike_c\': -15.0, \'feelslike_f\': 5.0, \'windchill_c\': -16.2, \'windchill_f\': 2.8, \'heatindex_c\': -7.5, \'heatindex_f\': 18.5, \'dewpoint_c\': -9.4, \'dewpoint_f\': 15.1, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 1.0, \'gust_mph\': 27.0, \'gust_kph\': 43.5}}"}, {\'url\': \'https://www.metcheck.com/WEATHER/dayforecast.asp?location=Louisiana&dateFor=06/01/2025&locationID=2025022&lat=39.448940&lon=-91.051530\', \'content\': \'LIVE\\xa0▼ CHARTS/FORECASTS\\xa0▼ MODELS\\xa0▼ COMMUNITY\\xa0▼ DISCUSSIONS\\xa0▼ COMMERCIAL\\xa0▼ -7°c -6°c -17°c -6°c -19°c -5°c -10°c -3°c -10°c -2°c -6°c -2°c -10°c -3°c -12°c Weather Forecast for\\xa0Louisiana\\xa0for\\xa0Monday 6 January Day/Night Day / Night Wet/Dry Dry & Wet ☀\\xa0▲\\xa07:24\\xa0\\xa0\\xa0\\xa016:53\\xa0\\xa0▼\\xa0☼\\xa0 ◄ Sun 16 Days ▼ Tue ► New Today On Metcheck Today\\xa0▼ Live\\xa0▼ Models\\xa0▼ Charts\\xa0▼ 1 NEW weather warning 1 NEW discussion 1 NEW global discussion 2 NEW live discussions 56 NEW weather reports 1 NEW snow discussion Forecasters Discussion\\xa0► Live Discussion\\xa0► ▼ Precipitype ▼ Aurora Forecast ◄► Weather Reports ▼ ICON ▼ ECMWF ◄► Discussions Weather Reports Weather Gallery Data Site Changes Weather Stickies\'}]', name='tavily_search_results_json', id='77f13ffd-8d3d-427d-a89e-480459a80c43', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}, next=('llm',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f9-805d-6a15-8003-c6e3b945e978'}}, metadata={'source': 'loop', 'step': 3, 'writes': {'action': {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Louisiana\', \'region\': \'Missouri\', \'country\': \'USA United States of America\', \'lat\': 39.4411, \'lon\': -91.0551, \'tz_id\': \'America/Chicago\', \'localtime_epoch\': 1736175005, \'localtime\': \'2025-01-06 08:50\'}, \'current\': {\'last_updated_epoch\': 1736174700, \'last_updated\': \'2025-01-06 08:45\', \'temp_c\': -6.7, \'temp_f\': 19.9, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 17.7, \'wind_kph\': 28.4, \'wind_degree\': 338, \'wind_dir\': \'NNW\', \'pressure_mb\': 1024.0, \'pressure_in\': 30.23, \'precip_mm\': 0.66, \'precip_in\': 0.03, \'humidity\': 82, \'cloud\': 100, \'feelslike_c\': -15.0, \'feelslike_f\': 5.0, \'windchill_c\': -16.2, \'windchill_f\': 2.8, \'heatindex_c\': -7.5, \'heatindex_f\': 18.5, \'dewpoint_c\': -9.4, \'dewpoint_f\': 15.1, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 1.0, \'gust_mph\': 27.0, \'gust_kph\': 43.5}}"}, {\'url\': \'https://www.metcheck.com/WEATHER/dayforecast.asp?location=Louisiana&dateFor=06/01/2025&locationID=2025022&lat=39.448940&lon=-91.051530\', \'content\': \'LIVE\\xa0▼ CHARTS/FORECASTS\\xa0▼ MODELS\\xa0▼ COMMUNITY\\xa0▼ DISCUSSIONS\\xa0▼ COMMERCIAL\\xa0▼ -7°c -6°c -17°c -6°c -19°c -5°c -10°c -3°c -10°c -2°c -6°c -2°c -10°c -3°c -12°c Weather Forecast for\\xa0Louisiana\\xa0for\\xa0Monday 6 January Day/Night Day / Night Wet/Dry Dry & Wet ☀\\xa0▲\\xa07:24\\xa0\\xa0\\xa0\\xa016:53\\xa0\\xa0▼\\xa0☼\\xa0 ◄ Sun 16 Days ▼ Tue ► New Today On Metcheck Today\\xa0▼ Live\\xa0▼ Models\\xa0▼ Charts\\xa0▼ 1 NEW weather warning 1 NEW discussion 1 NEW global discussion 2 NEW live discussions 56 NEW weather reports 1 NEW snow discussion Forecasters Discussion\\xa0► Live Discussion\\xa0► ▼ Precipitype ▼ Aurora Forecast ◄► Weather Reports ▼ ICON ▼ ECMWF ◄► Discussions Weather Reports Weather Gallery Data Site Changes Weather Stickies\'}]', name='tavily_search_results_json', id='77f13ffd-8d3d-427d-a89e-480459a80c43', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}}}, created_at='2025-01-06T15:04:57.730074+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f9-2769-6793-8002-01880a77c80f'}})
# --
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f9-2769-6793-8002-01880a77c80f'}}, metadata={'source': 'update', 'step': 2, 'writes': {'llm': {'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in Louisiana'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-06T15:04:48.402616+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8b32-6718-8001-8770ea59d800'}})
# --
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8b32-6718-8001-8770ea59d800'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-06T15:04:32.022282+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8af0-62c4-8000-2e2b7758eaa5'}})
# --
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71')]}, next=('llm',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8af0-62c4-8000-2e2b7758eaa5'}}, metadata={'source': 'loop', 'step': 0, 'writes': None}, created_at='2025-01-06T15:04:31.995142+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8aec-6128-bfff-0925a33173e0'}})
# --
# StateSnapshot(values={'messages': []}, next=('__start__',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8aec-6128-bfff-0925a33173e0'}}, metadata={'source': 'input', 'step': -1, 'writes': {'messages': [HumanMessage(content='Whats the weather in LA?')]}}, created_at='2025-01-06T15:04:31.993466+00:00', parent_config=None)
# --
# To fetch the same state as in the video, the offset below is changed to -3 from -1. This accounts for the initial state __start__ and the first state that are now stored to state memory with the latest version of software:
to_replay = states[-3]
print(to_replay)
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8b32-6718-8001-8770ea59d800'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-06T15:04:32.022282+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8af0-62c4-8000-2e2b7758eaa5'}})
for event in abot.graph.stream(None, to_replay.config):
print("Event:")
for k, v in event.items():
print(v)
# Calling: {'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}
# Back to the model!
# Event:
# {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736176347, \'localtime\': \'2025-01-06 07:12\'}, \'current\': {\'last_updated_epoch\': 1736175600, \'last_updated\': \'2025-01-06 07:00\', \'temp_c\': 9.4, \'temp_f\': 48.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 2.2, \'wind_kph\': 3.6, \'wind_degree\': 53, \'wind_dir\': \'NE\', \'pressure_mb\': 1019.0, \'pressure_in\': 30.1, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 61, \'cloud\': 0, \'feelslike_c\': 9.6, \'feelslike_f\': 49.2, \'windchill_c\': 12.0, \'windchill_f\': 53.5, \'heatindex_c\': 12.6, \'heatindex_f\': 54.7, \'dewpoint_c\': -5.6, \'dewpoint_f\': 21.9, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 4.0, \'gust_mph\': 4.2, \'gust_kph\': 6.8}}"}, {\'url\': \'https://world-weather.info/forecast/usa/los_angeles/june-2025/\', \'content\': "Weather in Los Angeles in June 2025 (California) - Detailed Weather Forecast for a Month Weather Weather in Los Angeles Weather in Los Angeles in June 2025 Los Angeles Weather Forecast for June 2025, is based on previous years\' statistical data. +70°+61° +72°+63° +72°+63° +72°+63° +70°+63° +72°+63° +72°+63° +72°+63° +73°+64° +73°+63° +72°+61° +72°+61° +72°+61° +72°+63° +73°+64° +73°+64° +73°+64° +73°+64° +73°+64° +75°+64° +75°+66° +73°+64° +73°+63° +73°+63° +73°+64° +73°+63° +75°+64° +75°+64° +75°+64° +75°+64° Extended weather forecast in Los Angeles HourlyWeek10-Day14-Day30-DayYear Weather in large and nearby cities Weather in Washington, D.C.+30° Sacramento+45° Monterey Park+48° North Glendale+48° Norwalk+50° Pasadena+45° Rosemead+48° Santa Monica+50° South El Monte+48° Manhattan Beach+54° Inglewood+48° Bellflower+50° Beverly Hills+50° Burbank+50° Compton+50° Glenoaks Canyon+48° Lynwood+50° world\'s temperature today day day Temperature units"}]', name='tavily_search_results_json', id='0f3fdecc-8849-49dc-b6ee-25bbaf5f3652', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736176347, \'localtime\': \'2025-01-06 07:12\'}, \'current\': {\'last_updated_epoch\': 1736175600, \'last_updated\': \'2025-01-06 07:00\', \'temp_c\': 9.4, \'temp_f\': 48.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 2.2, \'wind_kph\': 3.6, \'wind_degree\': 53, \'wind_dir\': \'NE\', \'pressure_mb\': 1019.0, \'pressure_in\': 30.1, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 61, \'cloud\': 0, \'feelslike_c\': 9.6, \'feelslike_f\': 49.2, \'windchill_c\': 12.0, \'windchill_f\': 53.5, \'heatindex_c\': 12.6, \'heatindex_f\': 54.7, \'dewpoint_c\': -5.6, \'dewpoint_f\': 21.9, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 4.0, \'gust_mph\': 4.2, \'gust_kph\': 6.8}}"}, {\'url\': \'https://world-weather.info/forecast/usa/los_angeles/june-2025/\', \'content\': "Weather in Los Angeles in June 2025 (California) - Detailed Weather Forecast for a Month Weather Weather in Los Angeles Weather in Los Angeles in June 2025 Los Angeles Weather Forecast for June 2025, is based on previous years\' statistical data. +70°+61° +72°+63° +72°+63° +72°+63° +70°+63° +72°+63° +72°+63° +72°+63° +73°+64° +73°+63° +72°+61° +72°+61° +72°+61° +72°+63° +73°+64° +73°+64° +73°+64° +73°+64° +73°+64° +75°+64° +75°+66° +73°+64° +73°+63° +73°+63° +73°+64° +73°+63° +75°+64° +75°+64° +75°+64° +75°+64° Extended weather forecast in Los Angeles HourlyWeek10-Day14-Day30-DayYear Weather in large and nearby cities Weather in Washington, D.C.+30° Sacramento+45° Monterey Park+48° North Glendale+48° Norwalk+50° Pasadena+45° Rosemead+48° Santa Monica+50° South El Monte+48° Manhattan Beach+54° Inglewood+48° Bellflower+50° Beverly Hills+50° Burbank+50° Compton+50° Glenoaks Canyon+48° Lynwood+50° world\'s temperature today day day Temperature units"}]', name='tavily_search_results_json', id='0f3fdecc-8849-49dc-b6ee-25bbaf5f3652', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content='The current weather in Los Angeles is sunny with a temperature of 48.9°F (9.4°C). The wind speed is 3.6 km/h coming from the northeast direction. The humidity is at 61%, and there is no precipitation.', response_metadata={'token_usage': {'completion_tokens': 54, 'prompt_tokens': 980, 'total_tokens': 1034, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-7bc83e07-2581-4324-ab7b-0640108ee549-0')]}
# Event:
# {'messages': [AIMessage(content='The current weather in Los Angeles is sunny with a temperature of 48.9°F (9.4°C). The wind speed is 3.6 km/h coming from the northeast direction. The humidity is at 61%, and there is no precipitation.', response_metadata={'token_usage': {'completion_tokens': 54, 'prompt_tokens': 980, 'total_tokens': 1034, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-7bc83e07-2581-4324-ab7b-0640108ee549-0')]}
# Go back in time and edit:
print(to_replay)
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8b32-6718-8001-8770ea59d800'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-06T15:04:32.022282+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8af0-62c4-8000-2e2b7758eaa5'}})
_id = to_replay.values['messages'][-1].tool_calls[0]['id']
to_replay.values['messages'][-1].tool_calls = [{'name': 'tavily_search_results_json',
'args': {'query': 'current weather in LA, accuweather'},
'id': _id}]
branch_state = abot.graph.update_state(to_replay.config, to_replay.values)
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in LA, accuweather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}
for event in abot.graph.stream(None, branch_state):
print("Event:")
for k, v in event.items():
if k != "__end__":
print(v)
# Calling: {'name': 'tavily_search_results_json', 'args': {'query': 'current weather in LA, accuweather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}
# Back to the model!
# Event:
# {'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736177128, \'localtime\': \'2025-01-06 07:25\'}, \'current\': {\'last_updated_epoch\': 1736176500, \'last_updated\': \'2025-01-06 07:15\', \'temp_c\': 9.4, \'temp_f\': 48.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 2.2, \'wind_kph\': 3.6, \'wind_degree\': 53, \'wind_dir\': \'NE\', \'pressure_mb\': 1019.0, \'pressure_in\': 30.1, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 63, \'cloud\': 0, \'feelslike_c\': 9.6, \'feelslike_f\': 49.2, \'windchill_c\': 12.0, \'windchill_f\': 53.5, \'heatindex_c\': 12.6, \'heatindex_f\': 54.7, \'dewpoint_c\': -5.6, \'dewpoint_f\': 21.9, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 4.0, \'gust_mph\': 4.2, \'gust_kph\': 6.8}}"}, {\'url\': \'https://www.accuweather.com/en/us/los-angeles/90012/current-weather/347625\', \'content\': \'Los Angeles, CA Current Weather | AccuWeather Winter Weather Storm to deliver snow, rain and wind to Midwest, Northeast 1 hour ago Weather Forecasts Bomb cyclone with atmospheric river to blast Northwest 1 hour ago Astronomy Moon to align with Jupiter, Mars on Monday night 8 hours ago Severe Weather Severe thunderstorms continue to rumble through southern Plains 1 hour ago Hurricane Tropical Rainstorm Sara, now in Gulf, to drench southern US coast 1 hour ago More Stories AccuWeather Ready Business Health Hurricane Leisure and Recreation Severe Weather Space and Astronomy Sports Travel Weather News Winter Center AccuWeather Ready Business Health Hurricane Leisure and Recreation Severe Weather Space and Astronomy Sports Travel Weather News Winter Center\'}]', name='tavily_search_results_json', id='6d0671d5-5012-467e-812e-f861ac9df294', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA')]}
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in LA, accuweather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'Los Angeles\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 34.0522, \'lon\': -118.2428, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1736177128, \'localtime\': \'2025-01-06 07:25\'}, \'current\': {\'last_updated_epoch\': 1736176500, \'last_updated\': \'2025-01-06 07:15\', \'temp_c\': 9.4, \'temp_f\': 48.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 2.2, \'wind_kph\': 3.6, \'wind_degree\': 53, \'wind_dir\': \'NE\', \'pressure_mb\': 1019.0, \'pressure_in\': 30.1, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 63, \'cloud\': 0, \'feelslike_c\': 9.6, \'feelslike_f\': 49.2, \'windchill_c\': 12.0, \'windchill_f\': 53.5, \'heatindex_c\': 12.6, \'heatindex_f\': 54.7, \'dewpoint_c\': -5.6, \'dewpoint_f\': 21.9, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 4.0, \'gust_mph\': 4.2, \'gust_kph\': 6.8}}"}, {\'url\': \'https://www.accuweather.com/en/us/los-angeles/90012/current-weather/347625\', \'content\': \'Los Angeles, CA Current Weather | AccuWeather Winter Weather Storm to deliver snow, rain and wind to Midwest, Northeast 1 hour ago Weather Forecasts Bomb cyclone with atmospheric river to blast Northwest 1 hour ago Astronomy Moon to align with Jupiter, Mars on Monday night 8 hours ago Severe Weather Severe thunderstorms continue to rumble through southern Plains 1 hour ago Hurricane Tropical Rainstorm Sara, now in Gulf, to drench southern US coast 1 hour ago More Stories AccuWeather Ready Business Health Hurricane Leisure and Recreation Severe Weather Space and Astronomy Sports Travel Weather News Winter Center AccuWeather Ready Business Health Hurricane Leisure and Recreation Severe Weather Space and Astronomy Sports Travel Weather News Winter Center\'}]', name='tavily_search_results_json', id='6d0671d5-5012-467e-812e-f861ac9df294', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content='The current weather in Los Angeles is sunny with a temperature of 48.9°F (9.4°C). The wind speed is 2.2 mph (3.6 kph) coming from the northeast direction. The humidity is at 63%, and there is no precipitation at the moment.', response_metadata={'token_usage': {'completion_tokens': 63, 'prompt_tokens': 776, 'total_tokens': 839, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-54051422-45e6-4e19-83f8-308b803aa618-0')]}
# Event:
# {'messages': [AIMessage(content='The current weather in Los Angeles is sunny with a temperature of 48.9°F (9.4°C). The wind speed is 2.2 mph (3.6 kph) coming from the northeast direction. The humidity is at 63%, and there is no precipitation at the moment.', response_metadata={'token_usage': {'completion_tokens': 63, 'prompt_tokens': 776, 'total_tokens': 839, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-54051422-45e6-4e19-83f8-308b803aa618-0')]}
# Add message to a state at a given time:
print(to_replay)
# StateSnapshot(values={'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in LA, accuweather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}, next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8b32-6718-8001-8770ea59d800'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'llm': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}])]}}}, created_at='2025-01-06T15:04:32.022282+00:00', parent_config={'configurable': {'thread_id': '3', 'thread_ts': '1efcc3f8-8af0-62c4-8000-2e2b7758eaa5'}})
# Instead of calling tavilly, we want to mock a response by appending a new message into the state:
_id = to_replay.values['messages'][-1].tool_calls[0]['id']
state_update = {"messages": [ToolMessage(
tool_call_id=_id,
name="tavily_search_results_json",
content="54 degree celcius",
)]}
# sate_update is a new message, so when we are going to update the state in the graph, it is not going to replace an existing message, it is going to append it to the list of messages.
# By adding the message, we want to pretend an action has taken place (as oppose to modifying the existing state), we add the `as_node="action"` parameter, telling LangGraph that we are not only making a modification, but doing the modification as if we were the action node, so LangGraph no longer wants to enter the "action" node after this modification.
branch_and_add = abot.graph.update_state(
to_replay.config,
state_update,
as_node="action")
for event in abot.graph.stream(None, branch_and_add):
print("Event:")
for k, v in event.items():
print(v)
# In exists_action:
# {'messages': [HumanMessage(content='Whats the weather in LA?', id='1f09abfe-07fc-47e7-9318-10d80da2cc71'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"Los Angeles current weather"}', 'name': 'tavily_search_results_json'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA', 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'logprobs': None, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'token_usage': {'completion_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens': 152, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'total_tokens': 173}}, id='run-d92aa910-271b-4de2-a2a5-63c474384ee6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'Los Angeles current weather'}, 'id': 'call_8X7aeq2Msx7S82lrIWuPOcwA'}]), ToolMessage(content='54 degree celcius', name='tavily_search_results_json', id='b8d33c18-8ac9-4def-b58b-c1347e7199f0', tool_call_id='call_8X7aeq2Msx7S82lrIWuPOcwA'), AIMessage(content='The current weather in Los Angeles is 54 degrees Celsius.', response_metadata={'token_usage': {'completion_tokens': 13, 'prompt_tokens': 190, 'total_tokens': 203, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-d8344559-edac-4473-b985-eb287cfa14b5-0')]}
# Event:
# {'messages': [AIMessage(content='The current weather in Los Angeles is 54 degrees Celsius.', response_metadata={'token_usage': {'completion_tokens': 13, 'prompt_tokens': 190, 'total_tokens': 203, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-d8344559-edac-4473-b985-eb287cfa14b5-0')]}