Skip to content

Commit 0482676

Browse files
committed
Fix agent.py to use actual MemoryAPIClient API
- Use get_or_create_working_memory() which returns tuple[bool, WorkingMemory] - Use put_working_memory() instead of save_working_memory() - Use create_long_term_memory() with ClientMemoryRecord objects - Use search_long_term_memory() instead of search_memories() - Pass user_id to all methods as required by the API
1 parent ad8de72 commit 0482676

File tree

1 file changed

+35
-10
lines changed
  • python-recipes/context-engineering/reference-agent/redis_context_course

1 file changed

+35
-10
lines changed

python-recipes/context-engineering/reference-agent/redis_context_course/agent.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ async def _load_working_memory(self, state: AgentState) -> AgentState:
123123
124124
This is the first node in the graph, loading context for the current turn.
125125
"""
126-
# Get working memory for this session
127-
working_memory = await self.memory_client.get_working_memory(
126+
# Get or create working memory for this session
127+
_, working_memory = await self.memory_client.get_or_create_working_memory(
128128
session_id=self.session_id,
129+
user_id=self.student_id,
129130
model_name="gpt-4o"
130131
)
131132

@@ -225,9 +226,25 @@ async def _save_working_memory(self, state: AgentState) -> AgentState:
225226
# Save to working memory
226227
# The Agent Memory Server will automatically extract important memories
227228
# to long-term storage based on its configured extraction strategy
228-
await self.memory_client.save_working_memory(
229+
from agent_memory_client import WorkingMemory, MemoryMessage
230+
231+
# Convert messages to MemoryMessage format
232+
memory_messages = [MemoryMessage(**msg) for msg in messages]
233+
234+
# Create WorkingMemory object
235+
working_memory = WorkingMemory(
229236
session_id=self.session_id,
230-
messages=messages
237+
user_id=self.student_id,
238+
messages=memory_messages,
239+
memories=[],
240+
data={}
241+
)
242+
243+
await self.memory_client.put_working_memory(
244+
session_id=self.session_id,
245+
memory=working_memory,
246+
user_id=self.student_id,
247+
model_name="gpt-4o"
231248
)
232249

233250
return state
@@ -346,11 +363,16 @@ async def _store_memory_tool(
346363
memory_type: Type of memory - "semantic" for facts/preferences, "episodic" for events
347364
topics: Related topics for filtering (e.g., ["preferences", "courses"])
348365
"""
349-
await self.memory_client.create_memory(
366+
from agent_memory_client import ClientMemoryRecord
367+
368+
memory = ClientMemoryRecord(
350369
text=text,
370+
user_id=self.student_id,
351371
memory_type=memory_type,
352372
topics=topics or []
353373
)
374+
375+
await self.memory_client.create_long_term_memory([memory])
354376
return f"Stored in long-term memory: {text}"
355377

356378
@tool
@@ -366,16 +388,19 @@ async def _search_memories_tool(
366388
query: Search query (e.g., "student preferences")
367389
limit: Maximum number of results to return
368390
"""
369-
memories = await self.memory_client.search_memories(
370-
query=query,
391+
from agent_memory_client import UserId
392+
393+
results = await self.memory_client.search_long_term_memory(
394+
text=query,
395+
user_id=UserId(eq=self.student_id),
371396
limit=limit
372397
)
373398

374-
if not memories:
399+
if not results.memories:
375400
return "No relevant memories found."
376401

377-
result = f"Found {len(memories)} relevant memories:\n\n"
378-
for i, memory in enumerate(memories, 1):
402+
result = f"Found {len(results.memories)} relevant memories:\n\n"
403+
for i, memory in enumerate(results.memories, 1):
379404
result += f"{i}. {memory.text}\n"
380405
if memory.topics:
381406
result += f" Topics: {', '.join(memory.topics)}\n"

0 commit comments

Comments
 (0)