Skip to content

Commit d96aed7

Browse files
committed
fix: notebooks
1 parent 81c9dea commit d96aed7

File tree

4 files changed

+100
-1129
lines changed

4 files changed

+100
-1129
lines changed

examples/create-react-agent-memory.ipynb

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,16 @@
146146
"\n",
147147
"tools = [get_weather]\n",
148148
"\n",
149-
"# We can add \"chat memory\" to the graph with LangGraph's checkpointer\n",
149+
"# We can add \"chat memory\" to the graph with LangGraph's Redis checkpointer\n",
150150
"# to retain the chat context between interactions\n",
151-
"from langgraph.checkpoint.memory import MemorySaver\n",
151+
"from langgraph.checkpoint.redis import RedisSaver\n",
152152
"\n",
153-
"memory = MemorySaver()\n",
153+
"# Set up Redis connection\n",
154+
"REDIS_URI = \"redis://redis:6379\"\n",
155+
"memory = None\n",
156+
"with RedisSaver.from_conn_string(REDIS_URI) as cp:\n",
157+
" cp.setup()\n",
158+
" memory = cp\n",
154159
"\n",
155160
"# Define the graph\n",
156161
"\n",
@@ -200,8 +205,8 @@
200205
"What's the weather in NYC?\n",
201206
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
202207
"Tool Calls:\n",
203-
" get_weather (call_LDM16pwsyYeZPQ78UlZCMs7n)\n",
204-
" Call ID: call_LDM16pwsyYeZPQ78UlZCMs7n\n",
208+
" get_weather (call_DVw2ApimJZrYstezZqRmZuO4)\n",
209+
" Call ID: call_DVw2ApimJZrYstezZqRmZuO4\n",
205210
" Args:\n",
206211
" location: New York City\n",
207212
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
@@ -244,20 +249,34 @@
244249
"What's it known for?\n",
245250
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
246251
"\n",
247-
"New York City is known for a variety of iconic landmarks, cultural institutions, and vibrant neighborhoods. Some of the most notable features include:\n",
252+
"New York City is known for many things, including:\n",
248253
"\n",
249-
"1. **Statue of Liberty**: A symbol of freedom and democracy, located on Liberty Island.\n",
250-
"2. **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere.\n",
251-
"3. **Central Park**: A large public park offering a natural oasis amidst the urban environment.\n",
252-
"4. **Empire State Building**: An iconic skyscraper offering panoramic views of the city.\n",
253-
"5. **Broadway**: Famous for its world-class theater productions and musicals.\n",
254-
"6. **Wall Street**: The financial hub of the city, home to the New York Stock Exchange.\n",
255-
"7. **Museums**: Including the Metropolitan Museum of Art, Museum of Modern Art (MoMA), and the American Museum of Natural History.\n",
256-
"8. **Diverse Cuisine**: A melting pot of cultures, offering a wide range of international foods.\n",
257-
"9. **Brooklyn Bridge**: A historic bridge connecting Manhattan and Brooklyn, known for its architectural beauty.\n",
258-
"10. **Cultural Diversity**: A rich tapestry of cultures and communities, making it a global city.\n",
254+
"1. **Landmarks and Attractions**: \n",
255+
" - The Statue of Liberty\n",
256+
" - Times Square\n",
257+
" - Central Park\n",
258+
" - Empire State Building\n",
259+
" - Broadway and its theaters\n",
259260
"\n",
260-
"These are just a few highlights of what makes New York City a unique and exciting place to visit or live.\n"
261+
"2. **Cultural Diversity**: NYC is a melting pot of cultures, with a rich tapestry of ethnic neighborhoods like Chinatown, Little Italy, and Harlem.\n",
262+
"\n",
263+
"3. **Financial Hub**: Home to Wall Street, NYC is a major global financial center.\n",
264+
"\n",
265+
"4. **Art and Museums**: The city boasts world-renowned museums such as the Metropolitan Museum of Art, the Museum of Modern Art (MoMA), and the Guggenheim Museum.\n",
266+
"\n",
267+
"5. **Cuisine**: Known for its diverse food scene, including iconic foods like New York-style pizza and bagels.\n",
268+
"\n",
269+
"6. **Fashion**: NYC is a fashion capital, hosting events like New York Fashion Week.\n",
270+
"\n",
271+
"7. **Media and Entertainment**: Headquarters for major media companies and a hub for television and film production.\n",
272+
"\n",
273+
"8. **Skyscrapers**: Known for its impressive skyline, including One World Trade Center and the Chrysler Building.\n",
274+
"\n",
275+
"9. **Public Transportation**: The NYC subway system is one of the largest and most complex in the world.\n",
276+
"\n",
277+
"10. **Sports**: Home to famous sports teams like the New York Yankees, New York Mets, New York Knicks, and New York Giants.\n",
278+
"\n",
279+
"These are just a few highlights of what makes New York City famous.\n"
261280
]
262281
}
263282
],

examples/cross-thread-persistence.ipynb

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,28 @@
129129
"metadata": {},
130130
"outputs": [],
131131
"source": [
132-
"from langgraph.store.memory import InMemoryStore\n",
133132
"from langchain_openai import OpenAIEmbeddings\n",
133+
"from langgraph.store.redis import RedisStore\n",
134+
"from langgraph.store.base import IndexConfig\n",
134135
"\n",
135-
"in_memory_store = InMemoryStore(\n",
136-
" index={\n",
137-
" \"embed\": OpenAIEmbeddings(model=\"text-embedding-3-small\"),\n",
138-
" \"dims\": 1536,\n",
139-
" }\n",
140-
")"
136+
"# Set up Redis connection\n",
137+
"REDIS_URI = \"redis://redis:6379\"\n",
138+
"\n",
139+
"# Create index configuration for vector search\n",
140+
"index_config: IndexConfig = {\n",
141+
" \"dims\": 1536,\n",
142+
" \"embed\": OpenAIEmbeddings(model=\"text-embedding-3-small\"),\n",
143+
" \"ann_index_config\": {\n",
144+
" \"vector_type\": \"vector\",\n",
145+
" },\n",
146+
" \"distance_type\": \"cosine\",\n",
147+
"}\n",
148+
"\n",
149+
"# Initialize the Redis store\n",
150+
"redis_store = None\n",
151+
"with RedisStore.from_conn_string(REDIS_URI, index=index_config) as s:\n",
152+
" s.setup()\n",
153+
" redis_store = s"
141154
]
142155
},
143156
{
@@ -153,7 +166,17 @@
153166
"execution_count": 4,
154167
"id": "2a30a362-528c-45ee-9df6-630d2d843588",
155168
"metadata": {},
156-
"outputs": [],
169+
"outputs": [
170+
{
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"21:40:15 redisvl.index.index INFO Index already exists, not overwriting.\n",
175+
"21:40:15 redisvl.index.index INFO Index already exists, not overwriting.\n",
176+
"21:40:15 redisvl.index.index INFO Index already exists, not overwriting.\n"
177+
]
178+
}
179+
],
157180
"source": [
158181
"import uuid\n",
159182
"from typing import Annotated\n",
@@ -162,7 +185,7 @@
162185
"from langchain_anthropic import ChatAnthropic\n",
163186
"from langchain_core.runnables import RunnableConfig\n",
164187
"from langgraph.graph import StateGraph, MessagesState, START\n",
165-
"from langgraph.checkpoint.memory import MemorySaver\n",
188+
"from langgraph.checkpoint.redis import RedisSaver\n",
166189
"from langgraph.store.base import BaseStore\n",
167190
"\n",
168191
"\n",
@@ -194,8 +217,15 @@
194217
"builder.add_node(\"call_model\", call_model)\n",
195218
"builder.add_edge(START, \"call_model\")\n",
196219
"\n",
220+
"# Set up Redis connection for checkpointer\n",
221+
"REDIS_URI = \"redis://redis:6379\"\n",
222+
"checkpointer = None\n",
223+
"with RedisSaver.from_conn_string(REDIS_URI) as cp:\n",
224+
" cp.setup()\n",
225+
" checkpointer = cp\n",
226+
"\n",
197227
"# NOTE: we're passing the store object here when compiling the graph\n",
198-
"graph = builder.compile(checkpointer=MemorySaver(), store=in_memory_store)\n",
228+
"graph = builder.compile(checkpointer=checkpointer, store=redis_store)\n",
199229
"# If you're using LangGraph Cloud or LangGraph Studio, you don't need to pass the store or checkpointer when compiling the graph, since it's done automatically."
200230
]
201231
},
@@ -243,7 +273,7 @@
243273
"Hi! Remember: my name is Bob\n",
244274
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
245275
"\n",
246-
"Hello Bob! It's nice to meet you. I'll remember that your name is Bob. How can I assist you today?\n"
276+
"Hello Bob! It's great to meet you. I'll remember that your name is Bob for our conversation. Is there anything specific you'd like to chat about or any questions you have? I'm here to help with information or discussion on a wide range of topics.\n"
247277
]
248278
}
249279
],
@@ -285,7 +315,7 @@
285315
"id": "80fd01ec-f135-4811-8743-daff8daea422",
286316
"metadata": {},
287317
"source": [
288-
"We can now inspect our in-memory store and verify that we have in fact saved the memories for the user:"
318+
"We can now inspect our Redis store and verify that we have in fact saved the memories for the user:"
289319
]
290320
},
291321
{
@@ -303,7 +333,7 @@
303333
}
304334
],
305335
"source": [
306-
"for memory in in_memory_store.search((\"memories\", \"1\")):\n",
336+
"for memory in redis_store.search((\"memories\", \"1\")):\n",
307337
" print(memory.value)"
308338
]
309339
},
@@ -330,7 +360,7 @@
330360
"what is my name?\n",
331361
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
332362
"\n",
333-
"I apologize, but I don't have any information about your name. As an AI assistant, I don't have access to personal information about users unless it's specifically provided in our conversation. If you'd like, you can tell me your name and I'll be happy to use it in our discussion.\n"
363+
"I apologize, but I don't have any information about your name. As an AI language model, I don't have access to personal information about users unless it has been explicitly provided in the conversation. If you'd like, you can tell me your name, and I'll be happy to use it in our conversation.\n"
334364
]
335365
}
336366
],

examples/persistence-functional.ipynb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,22 @@
183183
"execution_count": 4,
184184
"id": "87326ea6-34c5-46da-a41f-dda26ef9bd74",
185185
"metadata": {},
186-
"outputs": [],
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"21:41:23 redisvl.index.index INFO Index already exists, not overwriting.\n",
192+
"21:41:23 redisvl.index.index INFO Index already exists, not overwriting.\n",
193+
"21:41:23 redisvl.index.index INFO Index already exists, not overwriting.\n"
194+
]
195+
}
196+
],
187197
"source": [
188198
"from langchain_core.messages import BaseMessage\n",
189199
"from langgraph.graph import add_messages\n",
190200
"from langgraph.func import entrypoint, task\n",
191-
"from langgraph.checkpoint.memory import MemorySaver\n",
201+
"from langgraph.checkpoint.redis import RedisSaver\n",
192202
"\n",
193203
"\n",
194204
"@task\n",
@@ -197,7 +207,12 @@
197207
" return response\n",
198208
"\n",
199209
"\n",
200-
"checkpointer = MemorySaver()\n",
210+
"# Set up Redis connection for checkpointer\n",
211+
"REDIS_URI = \"redis://redis:6379\"\n",
212+
"checkpointer = None\n",
213+
"with RedisSaver.from_conn_string(REDIS_URI) as cp:\n",
214+
" cp.setup()\n",
215+
" checkpointer = cp\n",
201216
"\n",
202217
"\n",
203218
"@entrypoint(checkpointer=checkpointer)\n",
@@ -247,7 +262,7 @@
247262
"text": [
248263
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
249264
"\n",
250-
"Hi Bob! I'm Claude. Nice to meet you. How can I help you today?\n"
265+
"Hi Bob! I'm Claude. Nice to meet you! How can I help you today?\n"
251266
]
252267
}
253268
],
@@ -308,7 +323,7 @@
308323
"text": [
309324
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
310325
"\n",
311-
"I don't know your name unless you tell me. Each conversation with me starts fresh, so I don't have access to any previous conversations or personal information about you unless you share it.\n"
326+
"I don't know your name. Each conversation with me starts fresh, so I don't have access to any previous conversations or personal information about you unless you share it with me.\n"
312327
]
313328
}
314329
],

0 commit comments

Comments
 (0)