Update Docs (#3315)

This commit is contained in:
Deshraj Yadav
2025-08-13 21:37:15 -07:00
committed by GitHub
parent 5e895c240a
commit 192db1844c
127 changed files with 2882 additions and 3317 deletions
+293 -42
View File
@@ -5,8 +5,6 @@ icon: "bolt"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
## AsyncMemory
The `AsyncMemory` class is a direct asynchronous interface to Mem0's in-process memory operations. Unlike the memory, which interacts with an API, `AsyncMemory` works directly with the underlying storage systems. This makes it ideal for applications where you want to embed Mem0 directly into your codebase.
@@ -46,13 +44,17 @@ All methods in `AsyncMemory` have the same parameters as the synchronous `Memory
Add a new memory asynchronously:
```python Python
await memory.add(
messages=[
{"role": "user", "content": "I'm travelling to SF"},
{"role": "assistant", "content": "That's great to hear!"}
],
user_id="alice"
)
try:
result = await memory.add(
messages=[
{"role": "user", "content": "I'm travelling to SF"},
{"role": "assistant", "content": "That's great to hear!"}
],
user_id="alice"
)
print("Memory added successfully:", result)
except Exception as e:
print(f"Error adding memory: {e}")
```
#### Retrieve memories
@@ -60,10 +62,14 @@ await memory.add(
Retrieve memories related to a query:
```python Python
await memory.search(
query="Where am I travelling?",
user_id="alice"
)
try:
results = await memory.search(
query="Where am I travelling?",
user_id="alice"
)
print("Found memories:", results)
except Exception as e:
print(f"Error searching memories: {e}")
```
#### List memories
@@ -71,12 +77,11 @@ await memory.search(
List all memories for a `user_id`, `agent_id`, and/or `run_id`:
```python Python
await memory.get_all(user_id="alice")
# Get memories with agent and run context
await memory.get_all(user_id="alice", agent_id="assistant")
await memory.get_all(user_id="alice", run_id="session-001")
await memory.get_all(user_id="alice", agent_id="assistant", run_id="session-001")
try:
all_memories = await memory.get_all(user_id="alice")
print(f"Retrieved {len(all_memories)} memories")
except Exception as e:
print(f"Error retrieving memories: {e}")
```
#### Get specific memory
@@ -84,7 +89,11 @@ await memory.get_all(user_id="alice", agent_id="assistant", run_id="session-001"
Retrieve a specific memory by its ID:
```python Python
await memory.get(memory_id="memory-id-here")
try:
specific_memory = await memory.get(memory_id="memory-id-here")
print("Retrieved memory:", specific_memory)
except Exception as e:
print(f"Error retrieving memory: {e}")
```
#### Update memory
@@ -92,10 +101,14 @@ await memory.get(memory_id="memory-id-here")
Update an existing memory by ID:
```python Python
await memory.update(
memory_id="memory-id-here",
data="I'm travelling to Seattle"
)
try:
updated_memory = await memory.update(
memory_id="memory-id-here",
data="I'm travelling to Seattle"
)
print("Memory updated successfully:", updated_memory)
except Exception as e:
print(f"Error updating memory: {e}")
```
#### Delete memory
@@ -103,7 +116,11 @@ await memory.update(
Delete a specific memory by ID:
```python Python
await memory.delete(memory_id="memory-id-here")
try:
result = await memory.delete(memory_id="memory-id-here")
print("Memory deleted successfully")
except Exception as e:
print(f"Error deleting memory: {e}")
```
#### Delete all memories
@@ -111,10 +128,16 @@ await memory.delete(memory_id="memory-id-here")
Delete all memories for a specific user, agent, or run:
```python Python
await memory.delete_all(user_id="alice")
try:
result = await memory.delete_all(user_id="alice")
print("All memories deleted successfully")
except Exception as e:
print(f"Error deleting memories: {e}")
```
Note: At least one filter (user_id, agent_id, or run_id) is required when using delete_all.
<Note>
At least one filter (user_id, agent_id, or run_id) is required when using delete_all.
</Note>
### Advanced Memory Organization
@@ -150,7 +173,11 @@ session_search = await memory.search("What do you know about me?", user_id="alic
Get the history of changes for a specific memory:
```python Python
await memory.history(memory_id="memory-id-here")
try:
history = await memory.history(memory_id="memory-id-here")
print("Memory history:", history)
except Exception as e:
print(f"Error retrieving history: {e}")
```
### Example: Concurrent Usage with Other APIs
@@ -166,22 +193,26 @@ async_openai_client = AsyncOpenAI()
async_memory = AsyncMemory()
async def chat_with_memories(message: str, user_id: str = "default_user") -> str:
# Retrieve relevant memories
search_result = await async_memory.search(query=message, user_id=user_id, limit=3)
relevant_memories = search_result["results"]
memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant_memories)
# Generate Assistant response
system_prompt = f"You are a helpful AI. Answer the question based on query and memories.\nUser Memories:\n{memories_str}"
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]
response = await async_openai_client.chat.completions.create(model="gpt-4o-mini", messages=messages)
assistant_response = response.choices[0].message.content
try:
# Retrieve relevant memories
search_result = await async_memory.search(query=message, user_id=user_id, limit=3)
relevant_memories = search_result["results"]
memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant_memories)
# Generate Assistant response
system_prompt = f"You are a helpful AI. Answer the question based on query and memories.\nUser Memories:\n{memories_str}"
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]
response = await async_openai_client.chat.completions.create(model="gpt-4o-mini", messages=messages)
assistant_response = response.choices[0].message.content
# Create new memories from the conversation
messages.append({"role": "assistant", "content": assistant_response})
await async_memory.add(messages, user_id=user_id)
# Create new memories from the conversation
messages.append({"role": "assistant", "content": assistant_response})
await async_memory.add(messages, user_id=user_id)
return assistant_response
return assistant_response
except Exception as e:
print(f"Error in chat_with_memories: {e}")
return "I apologize, but I encountered an error processing your request."
async def async_main():
print("Chat with AI (type 'exit' to quit)")
@@ -200,6 +231,226 @@ if __name__ == "__main__":
main()
```
## Error Handling and Best Practices
### Common Error Types
When working with `AsyncMemory`, you may encounter these common errors:
#### Connection and Configuration Errors
```python Python
import asyncio
from mem0 import AsyncMemory
from mem0.configs.base import MemoryConfig
async def handle_initialization_errors():
try:
# Initialize with custom config
config = MemoryConfig(
vector_store={"provider": "chroma", "config": {"path": "./chroma_db"}},
llm={"provider": "openai", "config": {"model": "gpt-4o-mini"}}
)
memory = AsyncMemory(config=config)
print("AsyncMemory initialized successfully")
except ValueError as e:
print(f"Configuration error: {e}")
except ConnectionError as e:
print(f"Connection error: {e}")
except Exception as e:
print(f"Unexpected initialization error: {e}")
asyncio.run(handle_initialization_errors())
```
#### Memory Operation Errors
```python Python
async def handle_memory_operation_errors():
memory = AsyncMemory()
try:
# Memory not found error
result = await memory.get(memory_id="non-existent-id")
except ValueError as e:
print(f"Invalid memory ID: {e}")
except Exception as e:
print(f"Memory retrieval error: {e}")
try:
# Invalid search parameters
results = await memory.search(query="", user_id="alice")
except ValueError as e:
print(f"Invalid search query: {e}")
except Exception as e:
print(f"Search error: {e}")
```
### Performance Optimization
#### Concurrent Operations
Take advantage of AsyncMemory's concurrent capabilities:
```python Python
async def batch_operations():
memory = AsyncMemory()
# Process multiple operations concurrently
tasks = []
for i in range(5):
task = memory.add(
messages=[{"role": "user", "content": f"Message {i}"}],
user_id=f"user_{i}"
)
tasks.append(task)
try:
results = await asyncio.gather(*tasks, return_exceptions=True)
for i, result in enumerate(results):
if isinstance(result, Exception):
print(f"Task {i} failed: {result}")
else:
print(f"Task {i} completed successfully")
except Exception as e:
print(f"Batch operation error: {e}")
```
#### Resource Management
Properly manage AsyncMemory lifecycle:
```python Python
import asyncio
from contextlib import asynccontextmanager
@asynccontextmanager
async def get_memory():
memory = AsyncMemory()
try:
yield memory
finally:
# Clean up resources if needed
pass
async def safe_memory_usage():
async with get_memory() as memory:
try:
result = await memory.search("test query", user_id="alice")
return result
except Exception as e:
print(f"Memory operation failed: {e}")
return None
```
### Timeout and Retry Strategies
Implement timeout and retry logic for robustness:
```python Python
async def with_timeout_and_retry(operation, max_retries=3, timeout=10.0):
for attempt in range(max_retries):
try:
result = await asyncio.wait_for(operation(), timeout=timeout)
return result
except asyncio.TimeoutError:
print(f"Timeout on attempt {attempt + 1}")
except Exception as e:
print(f"Error on attempt {attempt + 1}: {e}")
if attempt < max_retries - 1:
await asyncio.sleep(2 ** attempt) # Exponential backoff
raise Exception(f"Operation failed after {max_retries} attempts")
# Usage example
async def robust_memory_search():
memory = AsyncMemory()
async def search_operation():
return await memory.search("test query", user_id="alice")
try:
result = await with_timeout_and_retry(search_operation)
print("Search successful:", result)
except Exception as e:
print(f"Search failed permanently: {e}")
```
### Integration with Async Frameworks
#### FastAPI Integration
```python Python
from fastapi import FastAPI, HTTPException
from mem0 import AsyncMemory
import asyncio
app = FastAPI()
memory = AsyncMemory()
@app.post("/memories/")
async def add_memory(messages: list, user_id: str):
try:
result = await memory.add(messages=messages, user_id=user_id)
return {"status": "success", "data": result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories/search")
async def search_memories(query: str, user_id: str, limit: int = 10):
try:
result = await memory.search(query=query, user_id=user_id, limit=limit)
return {"status": "success", "data": result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### Troubleshooting Guide
| Issue | Possible Causes | Solutions |
|-------|----------------|-----------|
| **Initialization fails** | Missing dependencies, invalid config | Check dependencies, validate configuration |
| **Slow operations** | Large datasets, network latency | Implement caching, optimize queries |
| **Memory not found** | Invalid memory ID, deleted memory | Validate IDs, implement existence checks |
| **Connection timeouts** | Network issues, server overload | Implement retry logic, check network |
| **Out of memory errors** | Large batch operations | Process in smaller batches |
### Monitoring and Logging
Add comprehensive logging to your async memory operations:
```python Python
import logging
import time
from functools import wraps
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log_async_operation(operation_name):
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
start_time = time.time()
logger.info(f"Starting {operation_name}")
try:
result = await func(*args, **kwargs)
duration = time.time() - start_time
logger.info(f"{operation_name} completed in {duration:.2f}s")
return result
except Exception as e:
duration = time.time() - start_time
logger.error(f"{operation_name} failed after {duration:.2f}s: {e}")
raise
return wrapper
return decorator
@log_async_operation("Memory Add")
async def logged_memory_add(memory, messages, user_id):
return await memory.add(messages=messages, user_id=user_id)
```
If you have any questions or need further assistance, please don't hesitate to reach out:
<Snippet file="get-help.mdx" />
@@ -5,8 +5,6 @@ icon: "pencil"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
## Introduction to Custom Fact Extraction Prompt
Custom fact extraction prompt allow you to tailor the behavior of your Mem0 instance to specific use cases or domains.
@@ -4,7 +4,6 @@ icon: "pencil"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Update memory prompt is a prompt used to determine the action to be performed on the memory.
By customizing this prompt, you can control how the memory is updated.
@@ -5,13 +5,17 @@ icon: "image"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Mem0 extends its capabilities beyond text by supporting multimodal data. With this feature, users can seamlessly integrate images into their interactions—allowing Mem0 to extract relevant information.
Mem0 extends its capabilities beyond text by supporting multimodal data. With this feature, you can seamlessly integrate images into your interactions—allowing Mem0 to extract relevant information and context from visual content.
## How It Works
When a user submits an image, Mem0 processes it to extract textual information and other pertinent details. These details are then added to the user's memory, enhancing the system's ability to understand and recall multimodal inputs.
When you submit an image, Mem0:
1. **Processes the visual content** using advanced vision models
2. **Extracts textual information** and relevant details from the image
3. **Stores the extracted information** as searchable memories
4. **Maintains context** between visual and textual interactions
This enables more comprehensive understanding of user interactions that include both text and visual elements.
<CodeGroup>
```python Python
@@ -62,7 +66,246 @@ client.add(messages, user_id="alice")
```
</CodeGroup>
Using these methods, you can seamlessly incorporate various media types into your interactions, further enhancing Mem0's multimodal capabilities.
## Supported Image Formats
Mem0 supports common image formats:
- **JPEG/JPG** - Standard photos and images
- **PNG** - Images with transparency support
- **WebP** - Modern web-optimized format
- **GIF** - Animated and static graphics
## Local Files vs URLs
### Using Image URLs
Images can be referenced via publicly accessible URLs:
```python
content = {
"type": "image_url",
"image_url": {
"url": "https://example.com/my-image.jpg"
}
}
```
### Using Local Files
For local images, convert them to base64 format:
<CodeGroup>
```python Python
import base64
from mem0 import Memory
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
client = Memory()
# Encode local image
base64_image = encode_image("path/to/your/image.jpg")
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
]
client.add(messages, user_id="alice")
```
```javascript JavaScript
import fs from 'fs';
import { Memory } from 'mem0ai';
function encodeImage(imagePath) {
const imageBuffer = fs.readFileSync(imagePath);
return imageBuffer.toString('base64');
}
const client = new Memory();
// Encode local image
const base64Image = encodeImage("path/to/your/image.jpg");
const messages = [
{
role: "user",
content: [
{
type: "text",
text: "What's in this image?"
},
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${base64Image}`
}
}
]
}
];
await client.add(messages, { user_id: "alice" });
```
</CodeGroup>
## Advanced Examples
### Restaurant Menu Analysis
```python
from mem0 import Memory
client = Memory()
messages = [
{
"role": "user",
"content": "I'm looking at this restaurant menu. Help me remember my preferences."
},
{
"role": "user",
"content": {
"type": "image_url",
"image_url": {
"url": "https://example.com/restaurant-menu.jpg"
}
}
},
{
"role": "user",
"content": "I'm allergic to peanuts and prefer vegetarian options."
}
]
result = client.add(messages, user_id="user123")
print(result)
```
### Document Analysis
```python
# Analyzing receipts, invoices, or documents
messages = [
{
"role": "user",
"content": "Store this receipt information for my expense tracking."
},
{
"role": "user",
"content": {
"type": "image_url",
"image_url": {
"url": "https://example.com/receipt.jpg"
}
}
}
]
client.add(messages, user_id="user123")
```
## File Size and Performance Considerations
### Image Size Limits
- **Maximum file size**: 20MB per image
- **Recommended size**: Under 5MB for optimal performance
- **Resolution**: Images are automatically resized if needed
### Performance Tips
1. **Compress large images** before sending to reduce processing time
2. **Use appropriate formats** - JPEG for photos, PNG for graphics with text
3. **Batch processing** - Send multiple images in separate requests for better reliability
## Error Handling
Handle common errors when working with images:
<CodeGroup>
```python Python
from mem0 import Memory
from mem0.exceptions import InvalidImageError, FileSizeError
client = Memory()
try:
messages = [{
"role": "user",
"content": {
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"}
}
}]
result = client.add(messages, user_id="user123")
print("Image processed successfully")
except InvalidImageError:
print("Invalid image format or corrupted file")
except FileSizeError:
print("Image file too large")
except Exception as e:
print(f"Unexpected error: {e}")
```
```javascript JavaScript
import { Memory } from 'mem0ai';
const client = new Memory();
try {
const messages = [{
role: "user",
content: {
type: "image_url",
image_url: { url: "https://example.com/image.jpg" }
}
}];
const result = await client.add(messages, { user_id: "user123" });
console.log("Image processed successfully");
} catch (error) {
if (error.type === 'invalid_image') {
console.log("Invalid image format or corrupted file");
} else if (error.type === 'file_size_exceeded') {
console.log("Image file too large");
} else {
console.log(`Unexpected error: ${error.message}`);
}
}
```
</CodeGroup>
## Best Practices
### Image Selection
- **Use high-quality images** with clear, readable text and details
- **Ensure good lighting** in photos for better text extraction
- **Avoid heavily stylized fonts** that may be difficult to read
### Memory Context
- **Provide context** about what information you want extracted
- **Combine with text** to give Mem0 better understanding of the image's purpose
- **Be specific** about what aspects of the image are important
### Privacy and Security
- **Avoid sensitive information** in images (SSN, passwords, private data)
- **Use secure image hosting** for URLs to prevent unauthorized access
- **Consider local processing** for highly sensitive visual content
Using these methods, you can seamlessly incorporate various visual content types into your interactions, further enhancing Mem0's multimodal capabilities for more comprehensive memory management.
If you have any questions, please feel free to reach out to us using one of the following methods:
@@ -4,8 +4,6 @@ icon: "code"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Mem0 can be easily integrated into chat applications to enhance conversational agents with structured memory. Mem0's APIs are designed to be compatible with OpenAI's, with the goal of making it easy to leverage Mem0 in applications you may have already built.
If you have a `Mem0 API key`, you can use it to initialize the client. Alternatively, you can initialize Mem0 without an API key if you're using it locally.
-2
View File
@@ -4,8 +4,6 @@ icon: "server"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Mem0 provides a REST API server (written using FastAPI). Users can perform all operations through REST endpoints. The API also includes OpenAPI documentation, accessible at `/docs` when the server is running.
<Frame caption="APIs supported by Mem0 REST API Server">
@@ -5,8 +5,6 @@ icon: "list-check"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Graph Memory is a powerful feature that allows users to create and utilize complex relationships between pieces of information.
## Graph Memory supports the following features:
@@ -5,8 +5,6 @@ icon: "info"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Mem0 now supports **Graph Memory**.
With Graph Memory, users can now create and utilize complex relationships between pieces of information, allowing for more nuanced and context-aware responses.
This integration enables users to leverage the strengths of both vector-based and graph-based approaches, resulting in more accurate and comprehensive information retrieval and generation.
-2
View File
@@ -4,8 +4,6 @@ icon: "image"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Mem0 extends its capabilities beyond text by supporting multimodal data, including images. Users can seamlessly integrate images into their interactions, allowing Mem0 to extract pertinent information from visual content and enrich the memory system.
## How It Works
-2
View File
@@ -5,8 +5,6 @@ icon: "node"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
> Welcome to the Mem0 quickstart guide. This guide will help you get up and running with Mem0 in no time.
## Installation
-2
View File
@@ -4,8 +4,6 @@ icon: "eye"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
Welcome to Mem0 Open Source - a powerful, self-hosted memory management solution for AI agents and assistants. With Mem0 OSS, you get full control over your infrastructure while maintaining complete customization flexibility.
We offer two SDKs for Python and Node.js.
-2
View File
@@ -5,8 +5,6 @@ icon: "python"
iconType: "solid"
---
<Snippet file="blank-notif.mdx" />
> Welcome to the Mem0 quickstart guide. This guide will help you get up and running with Mem0 in no time.
## Installation