Skip to content

Commit af26356

Browse files
committed
Update reference agent to use memory client's LangChain integration
Updated create_memory_tools() to: - Use get_memory_tools() from agent_memory_client.integrations.langchain - Require session_id and user_id parameters - Remove manual tool definitions (80+ lines of code removed\!) - Updated advanced_agent_example.py to pass required parameters This keeps the reference agent in sync with the updated notebook patterns.
1 parent 036ff05 commit af26356

File tree

2 files changed

+24
-96
lines changed

2 files changed

+24
-96
lines changed

python-recipes/context-engineering/reference-agent/examples/advanced_agent_example.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,31 @@ class AdvancedClassAgent:
4141
def __init__(
4242
self,
4343
student_id: str,
44+
session_id: str = "default_session",
4445
model: str = "gpt-4o",
4546
enable_tool_filtering: bool = True,
4647
enable_memory_tools: bool = False
4748
):
4849
self.student_id = student_id
50+
self.session_id = session_id
4951
self.llm = ChatOpenAI(model=model, temperature=0.7)
5052
self.course_manager = CourseManager()
5153
self.memory_client = MemoryClient(
5254
user_id=student_id,
5355
namespace="redis_university"
5456
)
55-
57+
5658
# Configuration
5759
self.enable_tool_filtering = enable_tool_filtering
5860
self.enable_memory_tools = enable_memory_tools
59-
61+
6062
# Create tools
6163
self.course_tools = create_course_tools(self.course_manager)
62-
self.memory_tools = create_memory_tools(self.memory_client) if enable_memory_tools else []
64+
self.memory_tools = create_memory_tools(
65+
self.memory_client,
66+
session_id=self.session_id,
67+
user_id=self.student_id
68+
) if enable_memory_tools else []
6369

6470
# Organize tools by category (for filtering)
6571
self.tool_groups = {

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

Lines changed: 15 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,6 @@ class CheckPrerequisitesInput(BaseModel):
4646
)
4747

4848

49-
class StoreMemoryInput(BaseModel):
50-
"""Input schema for storing memories."""
51-
text: str = Field(description="The information to remember")
52-
memory_type: str = Field(
53-
default="semantic",
54-
description="Type of memory: 'semantic' for facts, 'episodic' for events"
55-
)
56-
topics: List[str] = Field(
57-
default=[],
58-
description="Topics/tags for this memory (e.g., ['preferences', 'courses'])"
59-
)
60-
61-
62-
class SearchMemoriesInput(BaseModel):
63-
"""Input schema for searching memories."""
64-
query: str = Field(description="What to search for in memories")
65-
limit: int = Field(default=5, description="Maximum number of memories to retrieve")
66-
67-
6849
# Course Tools
6950
def create_course_tools(course_manager: CourseManager):
7051
"""
@@ -184,87 +165,28 @@ async def check_prerequisites(course_code: str, completed_courses: List[str]) ->
184165

185166

186167
# Memory Tools
187-
def create_memory_tools(memory_client: MemoryAPIClient):
168+
def create_memory_tools(memory_client: MemoryAPIClient, session_id: str, user_id: str):
188169
"""
189-
Create memory-related tools.
170+
Create memory-related tools using the memory client's built-in LangChain integration.
190171
191172
These tools are demonstrated in Section 3, notebook 04_memory_tools.ipynb.
192173
They give the LLM explicit control over memory operations.
193-
"""
194-
195-
@tool(args_schema=StoreMemoryInput)
196-
async def store_memory(text: str, memory_type: str = "semantic", topics: List[str] = []) -> str:
197-
"""
198-
Store important information in long-term memory.
199-
200-
Use this tool when:
201-
- Student shares preferences (e.g., "I prefer online courses")
202-
- Student states goals (e.g., "I want to graduate in 2026")
203-
- Student provides important facts (e.g., "My major is Computer Science")
204-
- You learn something that should be remembered for future sessions
205-
206-
Do NOT use for:
207-
- Temporary conversation context (working memory handles this)
208-
- Trivial details
209-
- Information that changes frequently
210-
211-
Examples:
212-
- text="Student prefers morning classes", memory_type="semantic", topics=["preferences", "schedule"]
213-
- text="Student completed CS101 with grade A", memory_type="episodic", topics=["courses", "grades"]
214-
"""
215-
try:
216-
from agent_memory_client.models import ClientMemoryRecord
217-
218-
# Note: user_id should be passed from the calling context
219-
# For now, we'll let the client use its default namespace
220-
memory = ClientMemoryRecord(
221-
text=text,
222-
memory_type=memory_type,
223-
topics=topics if topics else ["general"]
224-
)
225174
226-
await memory_client.create_long_term_memory([memory])
227-
return f"✅ Stored memory: {text}"
228-
except Exception as e:
229-
return f"❌ Failed to store memory: {str(e)}"
230-
231-
@tool(args_schema=SearchMemoriesInput)
232-
async def search_memories(query: str, limit: int = 5) -> str:
233-
"""
234-
Search for relevant memories using semantic search.
235-
236-
Use this tool when:
237-
- You need to recall information about the student
238-
- Student asks "What do you know about me?"
239-
- You need context from previous sessions
240-
- Making personalized recommendations
241-
242-
The search uses semantic matching, so natural language queries work well.
243-
244-
Examples:
245-
- query="student preferences" → finds preference-related memories
246-
- query="completed courses" → finds course completion records
247-
- query="goals" → finds student's stated goals
248-
"""
249-
try:
250-
results = await memory_client.search_long_term_memory(
251-
text=query,
252-
limit=limit
253-
)
175+
Args:
176+
memory_client: The memory client instance
177+
session_id: Session ID for the conversation
178+
user_id: User ID for the student
254179
255-
if not results.memories:
256-
return "No relevant memories found."
180+
Returns:
181+
List of LangChain StructuredTool objects for memory operations
182+
"""
183+
from agent_memory_client.integrations.langchain import get_memory_tools
257184

258-
result = f"Found {len(results.memories)} relevant memories:\n\n"
259-
for i, memory in enumerate(results.memories, 1):
260-
result += f"{i}. {memory.text}\n"
261-
result += f" Type: {memory.memory_type} | Topics: {', '.join(memory.topics)}\n\n"
262-
263-
return result
264-
except Exception as e:
265-
return f"❌ Failed to search memories: {str(e)}"
266-
267-
return [store_memory, search_memories]
185+
return get_memory_tools(
186+
memory_client=memory_client,
187+
session_id=session_id,
188+
user_id=user_id
189+
)
268190

269191

270192
# Tool Selection Helpers (from Section 4, notebook 04_tool_optimization.ipynb)

0 commit comments

Comments
 (0)