
- Complete Mem0 OSS integration with hybrid datastore - PostgreSQL + pgvector for vector storage - Neo4j 5.18 for graph relationships - Google Gemini embeddings integration - Comprehensive monitoring with correlation IDs - Real-time statistics and performance tracking - Production-grade observability features - Clean repository with no exposed secrets
294 lines
No EOL
8.6 KiB
Markdown
294 lines
No EOL
8.6 KiB
Markdown
# LlamaIndex ReAct Agent with Mem0
|
|
|
|
## Overview
|
|
|
|
A ReAct agent combines reasoning and action capabilities, making it versatile for tasks requiring both thought processes (reasoning) and interaction with tools or APIs (acting). Mem0 as memory enhances these capabilities by allowing the agent to store and retrieve contextual information from past interactions.
|
|
|
|
This guide demonstrates how to create a ReAct Agent with LlamaIndex that uses Mem0 as the memory store, showcasing the dramatic difference between agents with and without memory capabilities.
|
|
|
|
## Setup
|
|
|
|
### Installation
|
|
|
|
```bash
|
|
pip install llama-index-core llama-index-memory-mem0
|
|
```
|
|
|
|
### Initialize the LLM
|
|
|
|
```python
|
|
import os
|
|
from llama_index.llms.openai import OpenAI
|
|
|
|
os.environ["OPENAI_API_KEY"] = "<your-openai-api-key>"
|
|
llm = OpenAI(model="gpt-4o")
|
|
```
|
|
|
|
### Initialize Mem0 Memory
|
|
|
|
You can find your API key [here](https://app.mem0.ai/dashboard/api-keys). Read about Mem0 [Open Source](https://docs.mem0.ai/open-source/overview).
|
|
|
|
```python
|
|
os.environ["MEM0_API_KEY"] = "<your-mem0-api-key>"
|
|
|
|
from llama_index.memory.mem0 import Mem0Memory
|
|
|
|
context = {"user_id": "david"}
|
|
memory_from_client = Mem0Memory.from_client(
|
|
context=context,
|
|
api_key=os.environ["MEM0_API_KEY"],
|
|
search_msg_limit=4, # optional, default is 5
|
|
)
|
|
```
|
|
|
|
### Create Agent Tools
|
|
|
|
These tools will be used by the agent to perform actions:
|
|
|
|
```python
|
|
from llama_index.core.tools import FunctionTool
|
|
|
|
def call_fn(name: str):
|
|
"""Call the provided name.
|
|
Args:
|
|
name: str (Name of the person)
|
|
"""
|
|
return f"Calling... {name}"
|
|
|
|
def email_fn(name: str):
|
|
"""Email the provided name.
|
|
Args:
|
|
name: str (Name of the person)
|
|
"""
|
|
return f"Emailing... {name}"
|
|
|
|
def order_food(name: str, dish: str):
|
|
"""Order food for the provided name.
|
|
Args:
|
|
name: str (Name of the person)
|
|
dish: str (Name of the dish)
|
|
"""
|
|
return f"Ordering {dish} for {name}"
|
|
|
|
# Create tool instances
|
|
call_tool = FunctionTool.from_defaults(fn=call_fn)
|
|
email_tool = FunctionTool.from_defaults(fn=email_fn)
|
|
order_food_tool = FunctionTool.from_defaults(fn=order_food)
|
|
```
|
|
|
|
### Initialize the Agent with Memory
|
|
|
|
```python
|
|
from llama_index.core.agent import FunctionCallingAgent
|
|
|
|
agent = FunctionCallingAgent.from_tools(
|
|
[call_tool, email_tool, order_food_tool],
|
|
llm=llm,
|
|
memory=memory_from_client, # Mem0 memory integration
|
|
verbose=True,
|
|
)
|
|
```
|
|
|
|
## Building User Context
|
|
|
|
Let's start by having the agent learn about the user through conversation:
|
|
|
|
### Introduction
|
|
|
|
**Input:**
|
|
```python
|
|
response = agent.chat("Hi, My name is David")
|
|
print(response)
|
|
```
|
|
|
|
**Output:**
|
|
```
|
|
> Running step bf44a75a-a920-4cf3-944e-b6e6b5695043. Step input: Hi, My name is David
|
|
Added user message to memory: Hi, My name is David
|
|
=== LLM Response ===
|
|
Hello, David! How can I assist you today?
|
|
```
|
|
|
|
### Learning Preferences
|
|
|
|
**Input:**
|
|
```python
|
|
response = agent.chat("I love to eat pizza on weekends")
|
|
print(response)
|
|
```
|
|
|
|
**Output:**
|
|
```
|
|
> Running step 845783b0-b85b-487c-baee-8460ebe8b38d. Step input: I love to eat pizza on weekends
|
|
Added user message to memory: I love to eat pizza on weekends
|
|
=== LLM Response ===
|
|
Pizza is a great choice for the weekend! If you'd like, I can help you order some. Just let me know what kind of pizza you prefer!
|
|
```
|
|
|
|
### Communication Preferences
|
|
|
|
**Input:**
|
|
```python
|
|
response = agent.chat("My preferred way of communication is email")
|
|
print(response)
|
|
```
|
|
|
|
**Output:**
|
|
```
|
|
> Running step 345842f0-f8a0-42ea-a1b7-612265d72a92. Step input: My preferred way of communication is email
|
|
Added user message to memory: My preferred way of communication is email
|
|
=== LLM Response ===
|
|
Got it! If you need any assistance or have any requests, feel free to let me know, and I can communicate with you via email.
|
|
```
|
|
|
|
## Comparing Agents: With vs Without Memory
|
|
|
|
### Using the Agent WITHOUT Memory
|
|
|
|
**Setup:**
|
|
```python
|
|
agent_no_memory = FunctionCallingAgent.from_tools(
|
|
[call_tool, email_tool, order_food_tool],
|
|
# memory is not provided
|
|
llm=llm,
|
|
verbose=True,
|
|
)
|
|
```
|
|
|
|
**Input:**
|
|
```python
|
|
response = agent_no_memory.chat("I am feeling hungry, order me something and send me the bill")
|
|
print(response)
|
|
```
|
|
|
|
**Output:**
|
|
```
|
|
> Running step e89eb75d-75e1-4dea-a8c8-5c3d4b77882d. Step input: I am feeling hungry, order me something and send me the bill
|
|
Added user message to memory: I am feeling hungry, order me something and send me the bill
|
|
=== LLM Response ===
|
|
Please let me know your name and the dish you'd like to order, and I'll take care of it for you!
|
|
```
|
|
|
|
**Result:** The agent has no memory of previous conversations and cannot act on user preferences.
|
|
|
|
### Using the Agent WITH Memory
|
|
|
|
**Setup:**
|
|
```python
|
|
agent_with_memory = FunctionCallingAgent.from_tools(
|
|
[call_tool, email_tool, order_food_tool],
|
|
llm=llm,
|
|
memory=memory_from_client, # Mem0 memory integration
|
|
verbose=True,
|
|
)
|
|
```
|
|
|
|
**Input:**
|
|
```python
|
|
response = agent_with_memory.chat("I am feeling hungry, order me something and send me the bill")
|
|
print(response)
|
|
```
|
|
|
|
**Output:**
|
|
```
|
|
> Running step 5e473db9-3973-4cb1-a5fd-860be0ab0006. Step input: I am feeling hungry, order me something and send me the bill
|
|
Added user message to memory: I am feeling hungry, order me something and send me the bill
|
|
=== Calling Function ===
|
|
Calling function: order_food with args: {"name": "David", "dish": "pizza"}
|
|
=== Function Output ===
|
|
Ordering pizza for David
|
|
=== Calling Function ===
|
|
Calling function: email_fn with args: {"name": "David"}
|
|
=== Function Output ===
|
|
Emailing... David
|
|
> Running step 38080544-6b37-4bb2-aab2-7670100d926e. Step input: None
|
|
=== LLM Response ===
|
|
I've ordered a pizza for you, and the bill has been sent to your email. Enjoy your meal! If there's anything else you need, feel free to let me know.
|
|
```
|
|
|
|
**Result:** The agent remembers:
|
|
- User's name is David
|
|
- User loves pizza on weekends
|
|
- User prefers email communication
|
|
- Automatically orders pizza and sends bill via email
|
|
|
|
## Key Benefits of Memory Integration
|
|
|
|
### 1. **Personalized Responses**
|
|
- Agents remember user preferences and act accordingly
|
|
- No need to repeat information in every conversation
|
|
|
|
### 2. **Contextual Decision Making**
|
|
- Agents can make informed decisions based on past interactions
|
|
- Improved user experience through continuity
|
|
|
|
### 3. **Efficient Interactions**
|
|
- Reduced friction in user-agent communication
|
|
- Faster task completion with fewer prompts needed
|
|
|
|
### 4. **Learning and Adaptation**
|
|
- Agents improve over time by learning from interactions
|
|
- Better understanding of user behavior patterns
|
|
|
|
## Configuration Options
|
|
|
|
### Memory Configuration
|
|
```python
|
|
memory_from_client = Mem0Memory.from_client(
|
|
context=context,
|
|
api_key=os.environ["MEM0_API_KEY"],
|
|
search_msg_limit=4, # Controls how many past messages to retrieve
|
|
)
|
|
```
|
|
|
|
### Context Parameters
|
|
- **user_id**: Unique identifier for memory isolation between users
|
|
- **search_msg_limit**: Number of relevant past messages to include in context
|
|
|
|
## Use Cases
|
|
|
|
### 1. **Personal Assistants**
|
|
- Remember user preferences, schedules, and habits
|
|
- Provide personalized recommendations and actions
|
|
|
|
### 2. **Customer Support Agents**
|
|
- Maintain conversation history and user preferences
|
|
- Provide consistent support across multiple interactions
|
|
|
|
### 3. **E-commerce Assistants**
|
|
- Remember shopping preferences and past purchases
|
|
- Suggest relevant products and services
|
|
|
|
### 4. **Educational Tutors**
|
|
- Track learning progress and adapt teaching methods
|
|
- Remember student strengths and areas for improvement
|
|
|
|
## Best Practices
|
|
|
|
### 1. **Context Management**
|
|
- Use meaningful user IDs for proper memory isolation
|
|
- Adjust search_msg_limit based on conversation complexity
|
|
|
|
### 2. **Tool Design**
|
|
- Create tools that can leverage memory context
|
|
- Design functions with clear parameter definitions
|
|
|
|
### 3. **Memory Hygiene**
|
|
- Regularly review and update memory contents
|
|
- Implement privacy controls for sensitive information
|
|
|
|
### 4. **Testing**
|
|
- Test agents both with and without memory to understand impact
|
|
- Validate memory persistence across sessions
|
|
|
|
## Conclusion
|
|
|
|
Integrating Mem0 with LlamaIndex ReAct agents transforms static, forgetful assistants into intelligent, context-aware companions. The dramatic difference between agents with and without memory demonstrates the power of persistent context in creating truly helpful AI assistants.
|
|
|
|
The combination enables:
|
|
- **Continuity** across conversations
|
|
- **Personalization** based on user preferences
|
|
- **Efficiency** through reduced repetition
|
|
- **Intelligence** through accumulated context
|
|
|
|
This integration makes AI agents more human-like in their ability to remember and build upon past interactions, creating a foundation for truly intelligent and helpful AI systems. |