Skip to content

Commit 4e1c4c2

Browse files
committed
Update notebooks to use MemoryAPIClient directly (partial)
- Updated imports to use agent_memory_client - Fixed get_or_create_working_memory to unpack tuple - Fixed search_long_term_memory parameter (text= instead of query=) - Added script to automate notebook fixes Note: Some notebooks still need manual fixes for save_working_memory calls which need to be converted to put_working_memory with WorkingMemory objects. This will be done in follow-up commits.
1 parent 93a3559 commit 4e1c4c2

File tree

8 files changed

+240
-34
lines changed

8 files changed

+240
-34
lines changed

python-recipes/context-engineering/notebooks/section-3-memory/01_working_memory_with_extraction_strategies.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
"outputs": [],
183183
"source": [
184184
"# Simulate a conversation using working memory\n",
185-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
185+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
186186
"\n",
187187
"# Ensure memory_client and session_id are defined (in case cells are run out of order)\n",
188188
"if 'memory_client' not in globals():\n",
@@ -220,7 +220,7 @@
220220
"print(\"like preferences and goals to long-term memory.\")\n",
221221
"\n",
222222
"# Retrieve working memory\n",
223-
"working_memory = await memory_client.get_or_create_working_memory(\n",
223+
"_, working_memory = await memory_client.get_or_create_working_memory(\n",
224224
" session_id=session_id,\n",
225225
" model_name=\"gpt-4o\"\n",
226226
")\n",
@@ -292,7 +292,7 @@
292292
"source": [
293293
"# Check what was extracted to long-term memory\n",
294294
"import asyncio\n",
295-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
295+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
296296
"\n",
297297
"# Ensure memory_client is defined (in case cells are run out of order)\n",
298298
"if 'memory_client' not in globals():\n",
@@ -307,15 +307,15 @@
307307
"await asyncio.sleep(2) # Give the extraction process time to complete\n",
308308
"\n",
309309
"# Search for extracted memories\n",
310-
"extracted_memories = await memory_client.search_memories(\n",
310+
"extracted_memories = await memory_client.search_long_term_memory(\n",
311311
" query=\"preferences goals\",\n",
312312
" limit=10\n",
313313
")\n",
314314
"\n",
315315
"print(\"🧠 Extracted to Long-term Memory\")\n",
316316
"print(\"=\" * 50)\n",
317317
"\n",
318-
"if extracted_memories:\n",
318+
"if extracted_memories.memories:\n",
319319
" for i, memory in enumerate(extracted_memories, 1):\n",
320320
" print(f\"{i}. {memory.text}\")\n",
321321
" print(f\" Type: {memory.memory_type} | Topics: {', '.join(memory.topics)}\")\n",

python-recipes/context-engineering/notebooks/section-3-memory/02_long_term_memory.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"import os\n",
105105
"import asyncio\n",
106106
"from datetime import datetime\n",
107-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
107+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
108108
"\n",
109109
"# Initialize memory client\n",
110110
"student_id = \"student_123\"\n",
@@ -226,7 +226,7 @@
226226
"source": [
227227
"# Search for preferences\n",
228228
"print(\"Query: 'What does the student prefer?'\\n\")\n",
229-
"results = await memory_client.search_memories(\n",
229+
"results = await memory_client.search_long_term_memory(\n",
230230
" query=\"What does the student prefer?\",\n",
231231
" limit=3\n",
232232
")\n",
@@ -245,7 +245,7 @@
245245
"source": [
246246
"# Search for academic information\n",
247247
"print(\"Query: 'What is the student studying?'\\n\")\n",
248-
"results = await memory_client.search_memories(\n",
248+
"results = await memory_client.search_long_term_memory(\n",
249249
" query=\"What is the student studying?\",\n",
250250
" limit=3\n",
251251
")\n",
@@ -264,7 +264,7 @@
264264
"source": [
265265
"# Search for course history\n",
266266
"print(\"Query: 'What courses has the student taken?'\\n\")\n",
267-
"results = await memory_client.search_memories(\n",
267+
"results = await memory_client.search_long_term_memory(\n",
268268
" query=\"What courses has the student taken?\",\n",
269269
" limit=3\n",
270270
")\n",
@@ -368,7 +368,7 @@
368368
"source": [
369369
"# Get all semantic memories\n",
370370
"print(\"All semantic memories (facts):\\n\")\n",
371-
"results = await memory_client.search_memories(\n",
371+
"results = await memory_client.search_long_term_memory(\n",
372372
" query=\"\", # Empty query returns all\n",
373373
" memory_types=\"semantic\",\n",
374374
" limit=10\n",
@@ -388,7 +388,7 @@
388388
"source": [
389389
"# Get all episodic memories\n",
390390
"print(\"All episodic memories (events):\\n\")\n",
391-
"results = await memory_client.search_memories(\n",
391+
"results = await memory_client.search_long_term_memory(\n",
392392
" query=\"\",\n",
393393
" memory_types=\"episodic\",\n",
394394
" limit=10\n",

python-recipes/context-engineering/notebooks/section-3-memory/03_memory_integration.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"from datetime import datetime\n",
114114
"from langchain_openai import ChatOpenAI\n",
115115
"from langchain_core.messages import SystemMessage, HumanMessage, AIMessage\n",
116-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
116+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
117117
"\n",
118118
"# Initialize\n",
119119
"student_id = \"student_456\"\n",
@@ -161,7 +161,7 @@
161161
"\n",
162162
"# Step 1: Load working memory (empty for first turn)\n",
163163
"print(\"\\n1. Loading working memory...\")\n",
164-
"working_memory = await memory_client.get_or_create_working_memory(\n",
164+
"_, working_memory = await memory_client.get_or_create_working_memory(\n",
165165
" session_id=session_id_1,\n",
166166
" model_name=\"gpt-4o\"\n",
167167
")\n",
@@ -170,7 +170,7 @@
170170
"# Step 2: Search long-term memory (empty for first interaction)\n",
171171
"print(\"\\n2. Searching long-term memory...\")\n",
172172
"user_query = \"Hi! I'm interested in learning about databases.\"\n",
173-
"long_term_memories = await memory_client.search_memories(\n",
173+
"long_term_memories = await memory_client.search_long_term_memory(\n",
174174
" query=user_query,\n",
175175
" limit=3\n",
176176
")\n",
@@ -220,7 +220,7 @@
220220
"\n",
221221
"# Step 1: Load working memory (now has Turn 1)\n",
222222
"print(\"\\n1. Loading working memory...\")\n",
223-
"working_memory = await memory_client.get_or_create_working_memory(\n",
223+
"_, working_memory = await memory_client.get_or_create_working_memory(\n",
224224
" session_id=session_id_1,\n",
225225
" model_name=\"gpt-4o\"\n",
226226
")\n",
@@ -230,7 +230,7 @@
230230
"# Step 2: Search long-term memory\n",
231231
"print(\"\\n2. Searching long-term memory...\")\n",
232232
"user_query_2 = \"I prefer online courses and morning classes.\"\n",
233-
"long_term_memories = await memory_client.search_memories(\n",
233+
"long_term_memories = await memory_client.search_long_term_memory(\n",
234234
" query=user_query_2,\n",
235235
" limit=3\n",
236236
")\n",
@@ -296,7 +296,7 @@
296296
"\n",
297297
"# Search for extracted memories\n",
298298
"print(\"\\nSearching for extracted memories...\\n\")\n",
299-
"memories = await memory_client.search_memories(\n",
299+
"memories = await memory_client.search_long_term_memory(\n",
300300
" query=\"student preferences\",\n",
301301
" limit=5\n",
302302
")\n",
@@ -332,7 +332,7 @@
332332
"\n",
333333
"# Step 1: Load working memory (empty - new session)\n",
334334
"print(\"\\n1. Loading working memory...\")\n",
335-
"working_memory = await memory_client.get_or_create_working_memory(\n",
335+
"_, working_memory = await memory_client.get_or_create_working_memory(\n",
336336
" session_id=session_id_2,\n",
337337
" model_name=\"gpt-4o\"\n",
338338
")\n",
@@ -342,7 +342,7 @@
342342
"# Step 2: Search long-term memory (has data from Session 1)\n",
343343
"print(\"\\n2. Searching long-term memory...\")\n",
344344
"user_query_3 = \"What database courses do you recommend for me?\"\n",
345-
"long_term_memories = await memory_client.search_memories(\n",
345+
"long_term_memories = await memory_client.search_long_term_memory(\n",
346346
" query=user_query_3,\n",
347347
" limit=5\n",
348348
")\n",
@@ -404,16 +404,16 @@
404404
"\n",
405405
"# Check all memories about the student\n",
406406
"print(\"\\nAll memories about this student:\\n\")\n",
407-
"all_memories = await memory_client.search_memories(\n",
407+
"all_memories = await memory_client.search_long_term_memory(\n",
408408
" query=\"\", # Empty query returns all\n",
409409
" limit=20\n",
410410
")\n",
411411
"\n",
412-
"semantic_memories = [m for m in all_memories if m.memory_type == \"semantic\"]\n",
413-
"episodic_memories = [m for m in all_memories if m.memory_type == \"episodic\"]\n",
412+
"semantic_memories = [m for m in all_memories if m.memory_type == \"semantic\"].memories\n",
413+
"episodic_memories = [m for m in all_memories if m.memory_type == \"episodic\"].memories\n",
414414
"\n",
415415
"print(f\"Semantic memories (facts): {len(semantic_memories)}\")\n",
416-
"for memory in semantic_memories:\n",
416+
"for memory in semantic_memories.memories:\n",
417417
" print(f\" - {memory.text}\")\n",
418418
"\n",
419419
"print(f\"\\nEpisodic memories (events): {len(episodic_memories)}\")\n",

python-recipes/context-engineering/notebooks/section-3-memory/04_memory_tools.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"from langchain_core.tools import tool\n",
128128
"from pydantic import BaseModel, Field\n",
129129
"from typing import List, Optional\n",
130-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
130+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
131131
"\n",
132132
"# Initialize\n",
133133
"student_id = \"student_memory_tools\"\n",
@@ -248,7 +248,7 @@
248248
" - query=\"goals\" → finds student's stated goals\n",
249249
" \"\"\"\n",
250250
" try:\n",
251-
" memories = await memory_client.search_memories(\n",
251+
" memories = await memory_client.search_long_term_memory(\n",
252252
" query=query,\n",
253253
" limit=limit\n",
254254
" )\n",

python-recipes/context-engineering/notebooks/section-4-optimizations/01_context_window_management.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"import tiktoken\n",
125125
"from langchain_openai import ChatOpenAI\n",
126126
"from langchain_core.messages import SystemMessage, HumanMessage, AIMessage\n",
127-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
127+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
128128
"\n",
129129
"# Initialize\n",
130130
"student_id = \"student_context_demo\"\n",
@@ -301,7 +301,7 @@
301301
"async def have_conversation_turn(user_message, session_id):\n",
302302
" \"\"\"Simulate a conversation turn.\"\"\"\n",
303303
" # Get working memory\n",
304-
" working_memory = await memory_client.get_or_create_working_memory(\n",
304+
" _, working_memory = await memory_client.get_or_create_working_memory(\n",
305305
" session_id=session_id,\n",
306306
" model_name=\"gpt-4o\"\n",
307307
" )\n",
@@ -401,7 +401,7 @@
401401
"# Check working memory state\n",
402402
"print(\"\\nChecking working memory state...\\n\")\n",
403403
"\n",
404-
"working_memory = await memory_client.get_or_create_working_memory(\n",
404+
"_, working_memory = await memory_client.get_or_create_working_memory(\n",
405405
" session_id=session_id,\n",
406406
" model_name=\"gpt-4o\"\n",
407407
")\n",

python-recipes/context-engineering/notebooks/section-4-optimizations/03_grounding_with_memory.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"import asyncio\n",
106106
"from langchain_openai import ChatOpenAI\n",
107107
"from langchain_core.messages import SystemMessage, HumanMessage, AIMessage\n",
108-
"from redis_context_course import MemoryClient, MemoryClientConfig\n",
108+
"from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n",
109109
"\n",
110110
"# Initialize\n",
111111
"student_id = \"student_789\"\n",
@@ -150,13 +150,13 @@
150150
" \"\"\"Helper function to process a conversation turn.\"\"\"\n",
151151
" \n",
152152
" # Search long-term memory for context\n",
153-
" memories = await memory_client.search_memories(\n",
153+
" memories = await memory_client.search_long_term_memory(\n",
154154
" query=user_message,\n",
155155
" limit=5\n",
156156
" )\n",
157157
" \n",
158158
" # Build context from memories\n",
159-
" memory_context = \"\\n\".join([f\"- {m.text}\" for m in memories]) if memories else \"None\"\n",
159+
" memory_context = \"\\n\".join([f\"- {m.text}\" for m in memories]) if memories else \"None\".memories\n",
160160
" \n",
161161
" system_prompt = f\"\"\"You are a helpful class scheduling agent for Redis University.\n",
162162
"\n",
@@ -409,13 +409,13 @@
409409
"print(\"=\" * 80)\n",
410410
"\n",
411411
"# Get all memories\n",
412-
"all_memories = await memory_client.search_memories(\n",
412+
"all_memories = await memory_client.search_long_term_memory(\n",
413413
" query=\"\",\n",
414414
" limit=20\n",
415415
")\n",
416416
"\n",
417417
"print(\"\\nMemories that enable grounding:\\n\")\n",
418-
"for i, memory in enumerate(all_memories, 1):\n",
418+
"for i, memory in enumerate(all_memories, 1).memories:\n",
419419
" print(f\"{i}. {memory.text}\")\n",
420420
" print(f\" Type: {memory.memory_type} | Topics: {', '.join(memory.topics)}\")\n",
421421
" print()\n",

python-recipes/context-engineering/notebooks/section-4-optimizations/05_crafting_data_for_llms.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
"}\n",
438438
"\n",
439439
"# Get memories\n",
440-
"memories = await memory_client.search_memories(\n",
440+
"memories = await memory_client.search_long_term_memory(\n",
441441
" query=\"\", # Get all\n",
442442
" limit=20\n",
443443
")\n",

0 commit comments

Comments
 (0)