|
104 | 104 | "import os\n", |
105 | 105 | "import asyncio\n", |
106 | 106 | "from datetime import datetime\n", |
107 | | - "from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig\n", |
| 107 | + "from agent_memory_client import MemoryAPIClient as MemoryClient, MemoryClientConfig, ClientMemoryRecord\n", |
108 | 108 | "\n", |
109 | 109 | "# Initialize memory client\n", |
110 | 110 | "student_id = \"student_123\"\n", |
|
142 | 142 | "outputs": [], |
143 | 143 | "source": [ |
144 | 144 | "# Store student preferences\n", |
145 | | - "await memory_client.create_memory(\n", |
| 145 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
146 | 146 | " text=\"Student prefers online courses over in-person classes\",\n", |
147 | 147 | " memory_type=\"semantic\",\n", |
148 | 148 | " topics=[\"preferences\", \"course_format\"]\n", |
149 | | - ")\n", |
| 149 | + ")])\n", |
150 | 150 | "\n", |
151 | | - "await memory_client.create_memory(\n", |
| 151 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
152 | 152 | " text=\"Student's major is Computer Science with a focus on AI/ML\",\n", |
153 | 153 | " memory_type=\"semantic\",\n", |
154 | 154 | " topics=[\"academic_info\", \"major\"]\n", |
155 | | - ")\n", |
| 155 | + ")])\n", |
156 | 156 | "\n", |
157 | | - "await memory_client.create_memory(\n", |
| 157 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
158 | 158 | " text=\"Student wants to graduate in Spring 2026\",\n", |
159 | 159 | " memory_type=\"semantic\",\n", |
160 | 160 | " topics=[\"goals\", \"graduation\"]\n", |
161 | | - ")\n", |
| 161 | + ")])\n", |
162 | 162 | "\n", |
163 | | - "await memory_client.create_memory(\n", |
| 163 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
164 | 164 | " text=\"Student prefers morning classes, no classes on Fridays\",\n", |
165 | 165 | " memory_type=\"semantic\",\n", |
166 | 166 | " topics=[\"preferences\", \"schedule\"]\n", |
167 | | - ")\n", |
| 167 | + ")])\n", |
168 | 168 | "\n", |
169 | 169 | "print(\"✅ Stored 4 semantic memories (facts about the student)\")" |
170 | 170 | ] |
|
185 | 185 | "outputs": [], |
186 | 186 | "source": [ |
187 | 187 | "# Store course enrollment events\n", |
188 | | - "await memory_client.create_memory(\n", |
| 188 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
189 | 189 | " text=\"Student enrolled in CS101: Introduction to Programming on 2024-09-01\",\n", |
190 | 190 | " memory_type=\"episodic\",\n", |
191 | 191 | " topics=[\"enrollment\", \"courses\"],\n", |
192 | 192 | " metadata={\"course_code\": \"CS101\", \"date\": \"2024-09-01\"}\n", |
193 | | - ")\n", |
| 193 | + ")])\n", |
194 | 194 | "\n", |
195 | | - "await memory_client.create_memory(\n", |
| 195 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
196 | 196 | " text=\"Student completed CS101 with grade A on 2024-12-15\",\n", |
197 | 197 | " memory_type=\"episodic\",\n", |
198 | 198 | " topics=[\"completion\", \"grades\"],\n", |
199 | 199 | " metadata={\"course_code\": \"CS101\", \"grade\": \"A\", \"date\": \"2024-12-15\"}\n", |
200 | | - ")\n", |
| 200 | + ")])\n", |
201 | 201 | "\n", |
202 | | - "await memory_client.create_memory(\n", |
| 202 | + "await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
203 | 203 | " text=\"Student asked about machine learning courses on 2024-09-20\",\n", |
204 | 204 | " memory_type=\"episodic\",\n", |
205 | 205 | " topics=[\"inquiry\", \"machine_learning\"],\n", |
206 | 206 | " metadata={\"date\": \"2024-09-20\"}\n", |
207 | | - ")\n", |
| 207 | + ")])\n", |
208 | 208 | "\n", |
209 | 209 | "print(\"✅ Stored 3 episodic memories (events and experiences)\")" |
210 | 210 | ] |
|
231 | 231 | " limit=3\n", |
232 | 232 | ")\n", |
233 | 233 | "\n", |
234 | | - "for i, memory in enumerate(results, 1):\n", |
| 234 | + "for i, memory in enumerate(results.memories, 1):\n", |
235 | 235 | " print(f\"{i}. {memory.text}\")\n", |
236 | 236 | " print(f\" Type: {memory.memory_type} | Topics: {', '.join(memory.topics)}\")\n", |
237 | 237 | " print()" |
|
250 | 250 | " limit=3\n", |
251 | 251 | ")\n", |
252 | 252 | "\n", |
253 | | - "for i, memory in enumerate(results, 1):\n", |
| 253 | + "for i, memory in enumerate(results.memories, 1):\n", |
254 | 254 | " print(f\"{i}. {memory.text}\")\n", |
255 | 255 | " print(f\" Type: {memory.memory_type}\")\n", |
256 | 256 | " print()" |
|
269 | 269 | " limit=3\n", |
270 | 270 | ")\n", |
271 | 271 | "\n", |
272 | | - "for i, memory in enumerate(results, 1):\n", |
| 272 | + "for i, memory in enumerate(results.memories, 1):\n", |
273 | 273 | " print(f\"{i}. {memory.text}\")\n", |
274 | 274 | " print(f\" Type: {memory.memory_type}\")\n", |
275 | 275 | " if memory.metadata:\n", |
|
295 | 295 | "# Try to store an exact duplicate\n", |
296 | 296 | "print(\"Attempting to store exact duplicate...\")\n", |
297 | 297 | "try:\n", |
298 | | - " await memory_client.create_memory(\n", |
| 298 | + " await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
299 | 299 | " text=\"Student prefers online courses over in-person classes\",\n", |
300 | 300 | " memory_type=\"semantic\",\n", |
301 | 301 | " topics=[\"preferences\", \"course_format\"]\n", |
302 | | - " )\n", |
| 302 | + ")])\n", |
303 | 303 | " print(\"❌ Duplicate was stored (unexpected)\")\n", |
304 | 304 | "except Exception as e:\n", |
305 | 305 | " print(f\"✅ Duplicate rejected: {e}\")\n", |
306 | 306 | "\n", |
307 | 307 | "# Try to store a semantically similar memory\n", |
308 | 308 | "print(\"\\nAttempting to store semantically similar memory...\")\n", |
309 | 309 | "try:\n", |
310 | | - " await memory_client.create_memory(\n", |
| 310 | + " await memory_client.create_long_term_memory([ClientMemoryRecord(\n", |
311 | 311 | " text=\"Student likes taking classes online instead of on campus\",\n", |
312 | 312 | " memory_type=\"semantic\",\n", |
313 | 313 | " topics=[\"preferences\", \"course_format\"]\n", |
314 | | - " )\n", |
| 314 | + ")])\n", |
315 | 315 | " print(\"Memory stored (may be merged with existing similar memory)\")\n", |
316 | 316 | "except Exception as e:\n", |
317 | 317 | " print(f\"✅ Similar memory rejected: {e}\")" |
|
333 | 333 | "outputs": [], |
334 | 334 | "source": [ |
335 | 335 | "# Create a new memory client (simulating a new session)\n", |
336 | | - "new_session_client = MemoryClient(\n", |
337 | | - " user_id=student_id, # Same user\n", |
338 | | - " namespace=\"redis_university\"\n", |
| 336 | + "config = MemoryClientConfig(\n", |
| 337 | + " base_url=os.getenv(\"AGENT_MEMORY_URL\", \"http://localhost:8000\"),\n", |
| 338 | + " default_namespace=\"redis_university\"\n", |
339 | 339 | ")\n", |
| 340 | + "new_session_client = MemoryClient(config=config)\n", |
340 | 341 | "\n", |
341 | 342 | "print(\"New session started for the same student\\n\")\n", |
342 | 343 | "\n", |
343 | 344 | "# Search for memories from the new session\n", |
344 | 345 | "print(\"Query: 'What do I prefer?'\\n\")\n", |
345 | | - "results = await new_session_client.search_memories(\n", |
346 | | - " query=\"What do I prefer?\",\n", |
| 346 | + "results = await new_session_client.search_long_term_memory(\n", |
| 347 | + " text=\"What do I prefer?\",\n", |
347 | 348 | " limit=3\n", |
348 | 349 | ")\n", |
349 | 350 | "\n", |
350 | 351 | "print(\"✅ Memories accessible from new session:\\n\")\n", |
351 | | - "for i, memory in enumerate(results, 1):\n", |
| 352 | + "for i, memory in enumerate(results.memories, 1):\n", |
352 | 353 | " print(f\"{i}. {memory.text}\")\n", |
353 | | - " print()" |
| 354 | + " print()\n" |
354 | 355 | ] |
355 | 356 | }, |
356 | 357 | { |
|
370 | 371 | "print(\"All semantic memories (facts):\\n\")\n", |
371 | 372 | "results = await memory_client.search_long_term_memory(\n", |
372 | 373 | " query=\"\", # Empty query returns all\n", |
373 | | - " memory_types=\"semantic\",\n", |
| 374 | + " memory_type=\"semantic\",\n", |
374 | 375 | " limit=10\n", |
375 | 376 | ")\n", |
376 | 377 | "\n", |
377 | | - "for i, memory in enumerate(results, 1):\n", |
| 378 | + "for i, memory in enumerate(results.memories, 1):\n", |
378 | 379 | " print(f\"{i}. {memory.text}\")\n", |
379 | 380 | " print(f\" Topics: {', '.join(memory.topics)}\")\n", |
380 | 381 | " print()" |
|
390 | 391 | "print(\"All episodic memories (events):\\n\")\n", |
391 | 392 | "results = await memory_client.search_long_term_memory(\n", |
392 | 393 | " query=\"\",\n", |
393 | | - " memory_types=\"episodic\",\n", |
| 394 | + " memory_type=\"episodic\",\n", |
394 | 395 | " limit=10\n", |
395 | 396 | ")\n", |
396 | 397 | "\n", |
397 | | - "for i, memory in enumerate(results, 1):\n", |
| 398 | + "for i, memory in enumerate(results.memories, 1):\n", |
398 | 399 | " print(f\"{i}. {memory.text}\")\n", |
399 | 400 | " if memory.metadata:\n", |
400 | 401 | " print(f\" Metadata: {memory.metadata}\")\n", |
|
0 commit comments