Aspen theme for 1.x (#3473)
This commit is contained in:
@@ -28,8 +28,8 @@ See the list of supported LLMs below.
|
||||
<Card title="Together" href="/components/llms/models/together" />
|
||||
<Card title="Groq" href="/components/llms/models/groq" />
|
||||
<Card title="Litellm" href="/components/llms/models/litellm" />
|
||||
<Card title="Mistral AI" href="/components/llms/models/mistral_ai" />
|
||||
<Card title="Google AI" href="/components/llms/models/google_ai" />
|
||||
<Card title="Mistral AI" href="/components/llms/models/mistral_AI" />
|
||||
<Card title="Google AI" href="/components/llms/models/google_AI" />
|
||||
<Card title="AWS bedrock" href="/components/llms/models/aws_bedrock" />
|
||||
<Card title="DeepSeek" href="/components/llms/models/deepseek" />
|
||||
<Card title="xAI" href="/components/llms/models/xAI" />
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
---
|
||||
title: Configuration
|
||||
icon: "gear"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## How to define configurations?
|
||||
|
||||
The `reranker` configuration is defined as an object with two main keys:
|
||||
- `provider`: The name of the reranker provider (e.g., "cohere", "sentence_transformer", "huggingface", "llm_reranker")
|
||||
- `config`: A nested dictionary containing provider-specific settings
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
Here's how to configure a reranker with Mem0:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-api-key",
|
||||
"top_n": 10,
|
||||
"model": "rerank-english-v3.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memory = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Configuration Parameters
|
||||
|
||||
| Parameter | Description | Required | Default |
|
||||
|-----------|-------------|----------|---------|
|
||||
| `provider` | Reranker provider name | Yes | - |
|
||||
| `config` | Provider-specific configuration | Yes | - |
|
||||
|
||||
### Common Config Parameters
|
||||
|
||||
| Parameter | Description | Providers |
|
||||
|-----------|-------------|-----------|
|
||||
| `api_key` | API key for the service | Cohere, Hugging Face |
|
||||
| `model` | Model name to use | All |
|
||||
| `top_n` | Number of results to return | All |
|
||||
| `device` | Device to run on (cpu/cuda/mps) | Sentence Transformer, Hugging Face |
|
||||
|
||||
## Provider-Specific Examples
|
||||
|
||||
### Cohere
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-cohere-api-key",
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Sentence Transformer
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cpu",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Hugging Face
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"api_key": "your-hf-token",
|
||||
"model": "BAAI/bge-reranker-large",
|
||||
"top_n": 8
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### LLM Reranker
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-key"
|
||||
}
|
||||
},
|
||||
"top_n": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
You can combine rerankers with other components:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-key"
|
||||
}
|
||||
},
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"collection_name": "memories",
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-cohere-key",
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For provider-specific configuration details, visit the individual reranker pages.
|
||||
@@ -0,0 +1,217 @@
|
||||
---
|
||||
title: Custom Prompts
|
||||
icon: "pencil"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
When using LLM rerankers, you can customize the prompts used for ranking to better suit your specific use case and domain.
|
||||
|
||||
## Default Prompt
|
||||
|
||||
The default LLM reranker prompt is designed to be general-purpose:
|
||||
|
||||
```
|
||||
Given a query and a list of memory entries, rank the memory entries based on their relevance to the query.
|
||||
Rate each memory on a scale of 1-10 where 10 is most relevant.
|
||||
|
||||
Query: {query}
|
||||
|
||||
Memory entries:
|
||||
{memories}
|
||||
|
||||
Provide your ranking as a JSON array with scores for each memory.
|
||||
```
|
||||
|
||||
## Custom Prompt Configuration
|
||||
|
||||
You can provide a custom prompt template when configuring the LLM reranker:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
custom_prompt = """
|
||||
You are an expert at ranking memories for a personal AI assistant.
|
||||
Given a user query and a list of memory entries, rank each memory based on:
|
||||
1. Direct relevance to the query
|
||||
2. Temporal relevance (recent memories may be more important)
|
||||
3. Emotional significance
|
||||
4. Actionability
|
||||
|
||||
Query: {query}
|
||||
User Context: {user_context}
|
||||
|
||||
Memory entries:
|
||||
{memories}
|
||||
|
||||
Rate each memory from 1-10 and provide reasoning.
|
||||
Return as JSON: {{"rankings": [{{"index": 0, "score": 8, "reason": "..."}}]}}
|
||||
"""
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-key"
|
||||
}
|
||||
},
|
||||
"custom_prompt": custom_prompt,
|
||||
"top_n": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memory = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Prompt Variables
|
||||
|
||||
Your custom prompt can use the following variables:
|
||||
|
||||
| Variable | Description |
|
||||
|----------|-------------|
|
||||
| `{query}` | The search query |
|
||||
| `{memories}` | The list of memory entries to rank |
|
||||
| `{user_id}` | The user ID (if available) |
|
||||
| `{user_context}` | Additional user context (if provided) |
|
||||
|
||||
## Domain-Specific Examples
|
||||
|
||||
### Customer Support
|
||||
```python
|
||||
customer_support_prompt = """
|
||||
You are ranking customer support conversation memories.
|
||||
Prioritize memories that:
|
||||
- Relate to the current customer issue
|
||||
- Show previous resolution patterns
|
||||
- Indicate customer preferences or constraints
|
||||
|
||||
Query: {query}
|
||||
Customer Context: Previous interactions with this customer
|
||||
|
||||
Memories:
|
||||
{memories}
|
||||
|
||||
Rank each memory 1-10 based on support relevance.
|
||||
"""
|
||||
```
|
||||
|
||||
### Educational Content
|
||||
```python
|
||||
educational_prompt = """
|
||||
Rank these learning memories for a student query.
|
||||
Consider:
|
||||
- Prerequisite knowledge requirements
|
||||
- Learning progression and difficulty
|
||||
- Relevance to current learning objectives
|
||||
|
||||
Student Query: {query}
|
||||
Learning Context: {user_context}
|
||||
|
||||
Available memories:
|
||||
{memories}
|
||||
|
||||
Score each memory for educational value (1-10).
|
||||
"""
|
||||
```
|
||||
|
||||
### Personal Assistant
|
||||
```python
|
||||
personal_assistant_prompt = """
|
||||
Rank personal memories for relevance to the user's query.
|
||||
Consider:
|
||||
- Recent vs. historical importance
|
||||
- Personal preferences and habits
|
||||
- Contextual relationships between memories
|
||||
|
||||
Query: {query}
|
||||
Personal context: {user_context}
|
||||
|
||||
Memories to rank:
|
||||
{memories}
|
||||
|
||||
Provide relevance scores (1-10) with brief explanations.
|
||||
"""
|
||||
```
|
||||
|
||||
## Advanced Prompt Techniques
|
||||
|
||||
### Multi-Criteria Ranking
|
||||
```python
|
||||
multi_criteria_prompt = """
|
||||
Evaluate memories using multiple criteria:
|
||||
|
||||
1. RELEVANCE (40%): How directly related to the query
|
||||
2. RECENCY (20%): How recent the memory is
|
||||
3. IMPORTANCE (25%): Personal or business significance
|
||||
4. ACTIONABILITY (15%): How useful for next steps
|
||||
|
||||
Query: {query}
|
||||
Context: {user_context}
|
||||
|
||||
Memories:
|
||||
{memories}
|
||||
|
||||
For each memory, provide:
|
||||
- Overall score (1-10)
|
||||
- Breakdown by criteria
|
||||
- Final ranking recommendation
|
||||
|
||||
Format: JSON with detailed scoring
|
||||
"""
|
||||
```
|
||||
|
||||
### Contextual Ranking
|
||||
```python
|
||||
contextual_prompt = """
|
||||
Consider the following context when ranking memories:
|
||||
- Current user situation: {user_context}
|
||||
- Time of day: {current_time}
|
||||
- Recent activities: {recent_activities}
|
||||
|
||||
Query: {query}
|
||||
|
||||
Rank these memories considering both direct relevance and contextual appropriateness:
|
||||
{memories}
|
||||
|
||||
Provide contextually-aware relevance scores (1-10).
|
||||
"""
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Be Specific**: Clearly define what makes a memory relevant for your use case
|
||||
2. **Use Examples**: Include examples in your prompt for better model understanding
|
||||
3. **Structure Output**: Specify the exact JSON format you want returned
|
||||
4. **Test Iteratively**: Refine your prompt based on actual ranking performance
|
||||
5. **Consider Token Limits**: Keep prompts concise while being comprehensive
|
||||
|
||||
## Prompt Testing
|
||||
|
||||
You can test different prompts by comparing ranking results:
|
||||
|
||||
```python
|
||||
# Test multiple prompt variations
|
||||
prompts = [
|
||||
default_prompt,
|
||||
custom_prompt_v1,
|
||||
custom_prompt_v2
|
||||
]
|
||||
|
||||
for i, prompt in enumerate(prompts):
|
||||
config["reranker"]["config"]["custom_prompt"] = prompt
|
||||
memory = Memory.from_config(config)
|
||||
|
||||
results = memory.search("test query", user_id="test_user")
|
||||
print(f"Prompt {i+1} results: {results}")
|
||||
```
|
||||
|
||||
## Common Issues
|
||||
|
||||
- **Too Long**: Keep prompts under token limits for your chosen LLM
|
||||
- **Too Vague**: Be specific about ranking criteria
|
||||
- **Inconsistent Format**: Ensure JSON output format is clearly specified
|
||||
- **Missing Context**: Include relevant variables for your use case
|
||||
@@ -0,0 +1,116 @@
|
||||
---
|
||||
title: Cohere
|
||||
---
|
||||
|
||||
Cohere provides state-of-the-art reranking models that can significantly improve the relevance of search results. Cohere's rerankers are optimized for various languages and use cases.
|
||||
|
||||
## Usage
|
||||
|
||||
To use Cohere's reranker with Mem0:
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["COHERE_API_KEY"] = "your-cohere-api-key"
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-cohere-api-key", # Can also use environment variable
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memory = Memory.from_config(config)
|
||||
|
||||
# Use memory as usual
|
||||
memory.add("I love playing basketball", user_id="alice")
|
||||
memory.add("I enjoy watching movies", user_id="alice")
|
||||
|
||||
# Search will now use Cohere reranking
|
||||
results = memory.search("What sports does Alice like?", user_id="alice")
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
| Parameter | Description | Default |
|
||||
|-----------|-------------|---------|
|
||||
| `api_key` | Cohere API key | Required |
|
||||
| `model` | Cohere rerank model | `rerank-english-v3.0` |
|
||||
| `top_n` | Number of results to return | `10` |
|
||||
|
||||
## Available Models
|
||||
|
||||
- `rerank-english-v3.0`: Latest English reranking model
|
||||
- `rerank-multilingual-v3.0`: Multilingual reranking model
|
||||
- `rerank-english-v2.0`: Previous English model
|
||||
- `rerank-multilingual-v2.0`: Previous multilingual model
|
||||
|
||||
## Example with Different Models
|
||||
|
||||
### English Reranker
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-cohere-api-key",
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Multilingual Reranker
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-cohere-api-key",
|
||||
"model": "rerank-multilingual-v3.0",
|
||||
"top_n": 8
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
You can set your Cohere API key as an environment variable:
|
||||
|
||||
```bash
|
||||
export COHERE_API_KEY="your-cohere-api-key"
|
||||
```
|
||||
|
||||
Then use the config without specifying the API key:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Getting Your API Key
|
||||
|
||||
1. Sign up at [Cohere](https://cohere.ai/)
|
||||
2. Navigate to the API keys section in your dashboard
|
||||
3. Generate a new API key
|
||||
4. Use this key in your configuration
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
- Cohere rerankers work best with 10-100 candidate documents
|
||||
- Higher `top_n` values provide more comprehensive reranking but may increase latency
|
||||
- The v3.0 models generally provide better performance than v2.0 models
|
||||
@@ -0,0 +1,352 @@
|
||||
---
|
||||
title: Hugging Face Reranker
|
||||
description: 'Access thousands of reranking models from Hugging Face Hub'
|
||||
icon: "face-smile"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The Hugging Face reranker provider gives you access to thousands of reranking models available on the Hugging Face Hub. This includes popular models like BAAI's BGE rerankers and other state-of-the-art cross-encoder models.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Basic Setup
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cpu"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### Configuration Parameters
|
||||
|
||||
| Parameter | Type | Default | Description |
|
||||
|-----------|------|---------|-------------|
|
||||
| `model` | str | Required | Hugging Face model identifier |
|
||||
| `device` | str | "cpu" | Device to run model on ("cpu", "cuda", "mps") |
|
||||
| `batch_size` | int | 32 | Batch size for processing |
|
||||
| `max_length` | int | 512 | Maximum input sequence length |
|
||||
| `trust_remote_code` | bool | False | Allow remote code execution |
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-large",
|
||||
"device": "cuda",
|
||||
"batch_size": 16,
|
||||
"max_length": 512,
|
||||
"trust_remote_code": False,
|
||||
"model_kwargs": {
|
||||
"torch_dtype": "float16"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Popular Models
|
||||
|
||||
### BGE Rerankers (Recommended)
|
||||
|
||||
```python
|
||||
# Base model - good balance of speed and quality
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Large model - better quality, slower
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-large",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# v2 models - latest improvements
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-v2-m3",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Multilingual Models
|
||||
|
||||
```python
|
||||
# Multilingual BGE reranker
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-v2-multilingual",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Domain-Specific Models
|
||||
|
||||
```python
|
||||
# For code search
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "microsoft/codebert-base",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# For biomedical content
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "dmis-lab/biobert-base-cased-v1.1",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Add some memories
|
||||
m.add("I love hiking in the mountains", user_id="alice")
|
||||
m.add("Pizza is my favorite food", user_id="alice")
|
||||
m.add("I enjoy reading science fiction books", user_id="alice")
|
||||
|
||||
# Search with reranking
|
||||
results = m.search(
|
||||
"What outdoor activities do I enjoy?",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
|
||||
for result in results["results"]:
|
||||
print(f"Memory: {result['memory']}")
|
||||
print(f"Score: {result['score']:.3f}")
|
||||
```
|
||||
|
||||
### Batch Processing
|
||||
|
||||
```python
|
||||
# Process multiple queries efficiently
|
||||
queries = [
|
||||
"What are my hobbies?",
|
||||
"What food do I like?",
|
||||
"What books interest me?"
|
||||
]
|
||||
|
||||
results = []
|
||||
for query in queries:
|
||||
result = m.search(query, user_id="alice", rerank=True)
|
||||
results.append(result)
|
||||
```
|
||||
|
||||
## Performance Optimization
|
||||
|
||||
### GPU Acceleration
|
||||
|
||||
```python
|
||||
# Use GPU for better performance
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cuda",
|
||||
"batch_size": 64, # Increase batch size for GPU
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Memory Optimization
|
||||
|
||||
```python
|
||||
# For limited memory environments
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cpu",
|
||||
"batch_size": 8, # Smaller batch size
|
||||
"max_length": 256, # Shorter sequences
|
||||
"model_kwargs": {
|
||||
"torch_dtype": "float16" # Half precision
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Model Comparison
|
||||
|
||||
| Model | Size | Quality | Speed | Memory | Best For |
|
||||
|-------|------|---------|-------|---------|----------|
|
||||
| bge-reranker-base | 278M | Good | Fast | Low | General use |
|
||||
| bge-reranker-large | 560M | Better | Medium | Medium | High quality needs |
|
||||
| bge-reranker-v2-m3 | 568M | Best | Medium | Medium | Latest improvements |
|
||||
| bge-reranker-v2-multilingual | 568M | Good | Medium | Medium | Multiple languages |
|
||||
|
||||
## Error Handling
|
||||
|
||||
```python
|
||||
try:
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Reranking failed: {e}")
|
||||
# Fall back to vector search only
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
rerank=False
|
||||
)
|
||||
```
|
||||
|
||||
## Custom Models
|
||||
|
||||
### Using Private Models
|
||||
|
||||
```python
|
||||
# Use a private model from Hugging Face
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "your-org/custom-reranker",
|
||||
"device": "cuda",
|
||||
"use_auth_token": "your-hf-token"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Local Model Path
|
||||
|
||||
```python
|
||||
# Use a locally downloaded model
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "/path/to/local/model",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Choose the Right Model**: Balance quality vs speed based on your needs
|
||||
2. **Use GPU**: Significantly faster than CPU for larger models
|
||||
3. **Optimize Batch Size**: Tune based on your hardware capabilities
|
||||
4. **Monitor Memory**: Watch GPU/CPU memory usage with large models
|
||||
5. **Cache Models**: Download once and reuse to avoid repeated downloads
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
**Out of Memory Error**
|
||||
```python
|
||||
# Reduce batch size and sequence length
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"batch_size": 4,
|
||||
"max_length": 256
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Model Download Issues**
|
||||
```python
|
||||
# Set cache directory
|
||||
import os
|
||||
os.environ["TRANSFORMERS_CACHE"] = "/path/to/cache"
|
||||
|
||||
# Or use offline mode
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"local_files_only": True
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**CUDA Not Available**
|
||||
```python
|
||||
import torch
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cuda" if torch.cuda.is_available() else "cpu"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Reranker Overview" icon="sort" href="/components/rerankers/overview">
|
||||
Learn about reranking concepts
|
||||
</Card>
|
||||
<Card title="Configuration Guide" icon="gear" href="/components/rerankers/config">
|
||||
Detailed configuration options
|
||||
</Card>
|
||||
</CardGroup>
|
||||
@@ -0,0 +1,491 @@
|
||||
---
|
||||
title: LLM Reranker
|
||||
description: 'Use any language model as a reranker with custom prompts'
|
||||
icon: "robot"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The LLM reranker allows you to use any supported language model as a reranker. This approach uses prompts to instruct the LLM to score and rank memories based on their relevance to the query. While slower than specialized rerankers, it offers maximum flexibility and can be fine-tuned with custom prompts.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Basic Setup
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### Configuration Parameters
|
||||
|
||||
| Parameter | Type | Default | Description |
|
||||
|-----------|------|---------|-------------|
|
||||
| `llm` | dict | Required | LLM configuration object |
|
||||
| `top_k` | int | 10 | Number of results to rerank |
|
||||
| `temperature` | float | 0.0 | LLM temperature for consistency |
|
||||
| `custom_prompt` | str | None | Custom reranking prompt |
|
||||
| `score_range` | tuple | (0, 10) | Score range for relevance |
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "anthropic",
|
||||
"config": {
|
||||
"model": "claude-3-sonnet-20240229",
|
||||
"api_key": "your-anthropic-api-key"
|
||||
}
|
||||
},
|
||||
"top_k": 15,
|
||||
"temperature": 0.0,
|
||||
"score_range": (1, 5),
|
||||
"custom_prompt": """
|
||||
Rate the relevance of each memory to the query on a scale of 1-5.
|
||||
Consider semantic similarity, context, and practical utility.
|
||||
Only provide the numeric score.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Supported LLM Providers
|
||||
|
||||
### OpenAI
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-api-key",
|
||||
"temperature": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Anthropic
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "anthropic",
|
||||
"config": {
|
||||
"model": "claude-3-sonnet-20240229",
|
||||
"api_key": "your-anthropic-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Ollama (Local)
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "ollama",
|
||||
"config": {
|
||||
"model": "llama2",
|
||||
"ollama_base_url": "http://localhost:11434"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Azure OpenAI
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "azure_openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-azure-api-key",
|
||||
"azure_endpoint": "https://your-resource.openai.azure.com/",
|
||||
"azure_deployment": "gpt-4-deployment"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Custom Prompts
|
||||
|
||||
### Default Prompt Behavior
|
||||
|
||||
The default prompt asks the LLM to score relevance on a 0-10 scale:
|
||||
|
||||
```
|
||||
Given a query and a memory, rate how relevant the memory is to answering the query.
|
||||
Score from 0 (completely irrelevant) to 10 (perfectly relevant).
|
||||
Only provide the numeric score.
|
||||
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
Score:
|
||||
```
|
||||
|
||||
### Custom Prompt Examples
|
||||
|
||||
#### Domain-Specific Scoring
|
||||
|
||||
```python
|
||||
custom_prompt = """
|
||||
You are a medical information specialist. Rate how relevant each memory is for answering the medical query.
|
||||
Consider clinical accuracy, specificity, and practical applicability.
|
||||
Rate from 1-10 where:
|
||||
- 1-3: Irrelevant or potentially harmful
|
||||
- 4-6: Somewhat relevant but incomplete
|
||||
- 7-8: Relevant and helpful
|
||||
- 9-10: Highly relevant and clinically useful
|
||||
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
Score:
|
||||
"""
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-api-key"
|
||||
}
|
||||
},
|
||||
"custom_prompt": custom_prompt
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Contextual Relevance
|
||||
|
||||
```python
|
||||
contextual_prompt = """
|
||||
Rate how well this memory answers the specific question asked.
|
||||
Consider:
|
||||
- Direct relevance to the question
|
||||
- Completeness of information
|
||||
- Recency and accuracy
|
||||
- Practical usefulness
|
||||
|
||||
Rate 1-5:
|
||||
1 = Not relevant
|
||||
2 = Slightly relevant
|
||||
3 = Moderately relevant
|
||||
4 = Very relevant
|
||||
5 = Perfectly answers the question
|
||||
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
Score:
|
||||
"""
|
||||
```
|
||||
|
||||
#### Conversational Context
|
||||
|
||||
```python
|
||||
conversation_prompt = """
|
||||
You are helping evaluate which memories are most useful for a conversational AI assistant.
|
||||
Rate how helpful this memory would be for generating a relevant response.
|
||||
|
||||
Consider:
|
||||
- Direct relevance to user's intent
|
||||
- Emotional appropriateness
|
||||
- Factual accuracy
|
||||
- Conversation flow
|
||||
|
||||
Rate 0-10:
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
Score:
|
||||
"""
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Add memories
|
||||
m.add("I'm allergic to peanuts", user_id="alice")
|
||||
m.add("I love Italian food", user_id="alice")
|
||||
m.add("I'm vegetarian", user_id="alice")
|
||||
|
||||
# Search with LLM reranking
|
||||
results = m.search(
|
||||
"What foods should I avoid?",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
|
||||
for result in results["results"]:
|
||||
print(f"Memory: {result['memory']}")
|
||||
print(f"LLM Score: {result['score']:.2f}")
|
||||
```
|
||||
|
||||
### Batch Processing with Error Handling
|
||||
|
||||
```python
|
||||
def safe_llm_rerank_search(query, user_id, max_retries=3):
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
return m.search(query, user_id=user_id, rerank=True)
|
||||
except Exception as e:
|
||||
print(f"Attempt {attempt + 1} failed: {e}")
|
||||
if attempt == max_retries - 1:
|
||||
# Fall back to vector search
|
||||
return m.search(query, user_id=user_id, rerank=False)
|
||||
|
||||
# Use the safe function
|
||||
results = safe_llm_rerank_search("What are my preferences?", "alice")
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### Speed vs Quality Trade-offs
|
||||
|
||||
| Model Type | Speed | Quality | Cost | Best For |
|
||||
|------------|-------|---------|------|----------|
|
||||
| GPT-3.5 Turbo | Fast | Good | Low | High-volume applications |
|
||||
| GPT-4 | Medium | Excellent | Medium | Quality-critical applications |
|
||||
| Claude 3 Sonnet | Medium | Excellent | Medium | Balanced performance |
|
||||
| Ollama Local | Variable | Good | Free | Privacy-sensitive applications |
|
||||
|
||||
### Optimization Strategies
|
||||
|
||||
```python
|
||||
# Fast configuration for high-volume use
|
||||
fast_config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-3.5-turbo",
|
||||
"api_key": "your-api-key"
|
||||
}
|
||||
},
|
||||
"top_k": 5, # Limit candidates
|
||||
"temperature": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# High-quality configuration
|
||||
quality_config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-api-key"
|
||||
}
|
||||
},
|
||||
"top_k": 15,
|
||||
"temperature": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Use Cases
|
||||
|
||||
### Multi-Step Reasoning
|
||||
|
||||
```python
|
||||
reasoning_prompt = """
|
||||
Evaluate this memory's relevance using multi-step reasoning:
|
||||
|
||||
1. What is the main intent of the query?
|
||||
2. What key information does the memory contain?
|
||||
3. How directly does the memory address the query?
|
||||
4. What additional context might be needed?
|
||||
|
||||
Based on this analysis, rate relevance 1-10:
|
||||
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
|
||||
Analysis:
|
||||
Step 1 (Intent):
|
||||
Step 2 (Information):
|
||||
Step 3 (Directness):
|
||||
Step 4 (Context):
|
||||
Final Score:
|
||||
"""
|
||||
```
|
||||
|
||||
### Comparative Ranking
|
||||
|
||||
```python
|
||||
comparative_prompt = """
|
||||
You will see a query and multiple memories. Rank them in order of relevance.
|
||||
Consider which memories best answer the question and would be most helpful.
|
||||
|
||||
Query: {query}
|
||||
|
||||
Memories to rank:
|
||||
{memories}
|
||||
|
||||
Provide scores 1-10 for each memory, considering their relative usefulness.
|
||||
"""
|
||||
```
|
||||
|
||||
### Emotional Intelligence
|
||||
|
||||
```python
|
||||
emotional_prompt = """
|
||||
Consider both factual relevance and emotional appropriateness.
|
||||
Rate how suitable this memory is for responding to the user's query.
|
||||
|
||||
Factors to consider:
|
||||
- Factual accuracy and relevance
|
||||
- Emotional tone and sensitivity
|
||||
- User's likely emotional state
|
||||
- Appropriateness of response
|
||||
|
||||
Query: {query}
|
||||
Memory: {memory}
|
||||
Emotional Context: {context}
|
||||
Score (1-10):
|
||||
"""
|
||||
```
|
||||
|
||||
## Error Handling and Fallbacks
|
||||
|
||||
```python
|
||||
class RobustLLMReranker:
|
||||
def __init__(self, primary_config, fallback_config=None):
|
||||
self.primary = Memory.from_config(primary_config)
|
||||
self.fallback = Memory.from_config(fallback_config) if fallback_config else None
|
||||
|
||||
def search(self, query, user_id, max_retries=2):
|
||||
# Try primary LLM reranker
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
return self.primary.search(query, user_id=user_id, rerank=True)
|
||||
except Exception as e:
|
||||
print(f"Primary reranker attempt {attempt + 1} failed: {e}")
|
||||
|
||||
# Try fallback reranker
|
||||
if self.fallback:
|
||||
try:
|
||||
return self.fallback.search(query, user_id=user_id, rerank=True)
|
||||
except Exception as e:
|
||||
print(f"Fallback reranker failed: {e}")
|
||||
|
||||
# Final fallback: vector search only
|
||||
return self.primary.search(query, user_id=user_id, rerank=False)
|
||||
|
||||
# Usage
|
||||
primary_config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {"llm": {"provider": "openai", "config": {"model": "gpt-4"}}}
|
||||
}
|
||||
}
|
||||
|
||||
fallback_config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {"llm": {"provider": "openai", "config": {"model": "gpt-3.5-turbo"}}}
|
||||
}
|
||||
}
|
||||
|
||||
reranker = RobustLLMReranker(primary_config, fallback_config)
|
||||
results = reranker.search("What are my preferences?", "alice")
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use Specific Prompts**: Tailor prompts to your domain and use case
|
||||
2. **Set Temperature to 0**: Ensure consistent scoring across runs
|
||||
3. **Limit Top-K**: Don't rerank too many candidates to control costs
|
||||
4. **Implement Fallbacks**: Always have a backup plan for API failures
|
||||
5. **Monitor Costs**: Track API usage, especially with expensive models
|
||||
6. **Cache Results**: Consider caching reranking results for repeated queries
|
||||
7. **Test Prompts**: Experiment with different prompts to find what works best
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
**Inconsistent Scores**
|
||||
- Set temperature to 0.0
|
||||
- Use more specific prompts
|
||||
- Consider using multiple calls and averaging
|
||||
|
||||
**API Rate Limits**
|
||||
- Implement exponential backoff
|
||||
- Use cheaper models for high-volume scenarios
|
||||
- Add retry logic with delays
|
||||
|
||||
**Poor Ranking Quality**
|
||||
- Refine your custom prompt
|
||||
- Try different LLM models
|
||||
- Add examples to your prompt
|
||||
|
||||
## Next Steps
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Custom Prompts Guide" icon="pencil" href="/components/rerankers/custom-prompts">
|
||||
Learn to craft effective reranking prompts
|
||||
</Card>
|
||||
<Card title="Performance Optimization" icon="bolt" href="/components/rerankers/optimization">
|
||||
Optimize LLM reranker performance
|
||||
</Card>
|
||||
</CardGroup>
|
||||
@@ -0,0 +1,162 @@
|
||||
---
|
||||
title: Sentence Transformer
|
||||
---
|
||||
|
||||
Sentence Transformer rerankers use cross-encoder models that are specifically designed for ranking tasks. These models can run locally and provide good reranking performance without external API calls.
|
||||
|
||||
## Usage
|
||||
|
||||
To use Sentence Transformer reranker with Mem0:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cpu",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memory = Memory.from_config(config)
|
||||
|
||||
# Use memory as usual
|
||||
memory.add("I love playing basketball", user_id="alice")
|
||||
memory.add("I enjoy watching movies", user_id="alice")
|
||||
|
||||
# Search will now use Sentence Transformer reranking
|
||||
results = memory.search("What sports does Alice like?", user_id="alice")
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
| Parameter | Description | Default |
|
||||
|-----------|-------------|---------|
|
||||
| `model` | Sentence Transformer cross-encoder model | `cross-encoder/ms-marco-MiniLM-L-6-v2` |
|
||||
| `device` | Device to run on (`cpu`, `cuda`, `mps`) | `cpu` |
|
||||
| `top_n` | Number of results to return | `10` |
|
||||
|
||||
## Popular Models
|
||||
|
||||
### Lightweight Models
|
||||
- `cross-encoder/ms-marco-MiniLM-L-6-v2`: Fast and efficient
|
||||
- `cross-encoder/ms-marco-MiniLM-L-4-v2`: Even faster, slightly lower accuracy
|
||||
- `cross-encoder/ms-marco-MiniLM-L-2-v2`: Fastest, good for real-time applications
|
||||
|
||||
### High-Performance Models
|
||||
- `cross-encoder/ms-marco-electra-base`: Better accuracy, larger model
|
||||
- `ms-marco-MiniLM-L-12-v2`: Balanced performance and speed
|
||||
- `cross-encoder/qnli-electra-base`: Good for question-answering tasks
|
||||
|
||||
## Device Configuration
|
||||
|
||||
### CPU Usage
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cpu",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### GPU Usage (CUDA)
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-electra-base",
|
||||
"device": "cuda",
|
||||
"top_n": 15
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Apple Silicon (MPS)
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "mps",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
The sentence-transformers library is required:
|
||||
|
||||
```bash
|
||||
pip install sentence-transformers
|
||||
```
|
||||
|
||||
For GPU support with CUDA:
|
||||
```bash
|
||||
pip install sentence-transformers torch
|
||||
```
|
||||
|
||||
## Performance Optimization
|
||||
|
||||
### Model Selection
|
||||
- Use MiniLM models for faster inference
|
||||
- Use larger models (electra-base) for better accuracy
|
||||
- Consider the trade-off between speed and quality
|
||||
|
||||
### Device Optimization
|
||||
- Use GPU (`cuda` or `mps`) for larger models
|
||||
- CPU is sufficient for MiniLM models
|
||||
- Batch processing improves GPU utilization
|
||||
|
||||
### Memory Considerations
|
||||
```python
|
||||
# For memory-constrained environments
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-2-v2", # Smallest model
|
||||
"device": "cpu",
|
||||
"top_n": 5 # Fewer results to process
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Custom Models
|
||||
|
||||
You can use any Sentence Transformer cross-encoder model:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "your-custom-model-name",
|
||||
"device": "cpu",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Advantages
|
||||
|
||||
- **Local Processing**: No external API calls required
|
||||
- **Privacy**: Data stays on your infrastructure
|
||||
- **Cost Effective**: No per-request charges
|
||||
- **Fast**: Especially with GPU acceleration
|
||||
- **Customizable**: Can fine-tune on your specific data
|
||||
@@ -0,0 +1,312 @@
|
||||
---
|
||||
title: Performance Optimization
|
||||
icon: "bolt"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
Optimizing reranker performance is crucial for maintaining fast search response times while improving result quality. This guide covers best practices for different reranker types.
|
||||
|
||||
## General Optimization Principles
|
||||
|
||||
### Candidate Set Size
|
||||
The number of candidates sent to the reranker significantly impacts performance:
|
||||
|
||||
```python
|
||||
# Optimal candidate sizes for different rerankers
|
||||
config_map = {
|
||||
"cohere": {"initial_candidates": 100, "top_n": 10},
|
||||
"sentence_transformer": {"initial_candidates": 50, "top_n": 10},
|
||||
"huggingface": {"initial_candidates": 30, "top_n": 5},
|
||||
"llm_reranker": {"initial_candidates": 20, "top_n": 5}
|
||||
}
|
||||
```
|
||||
|
||||
### Batching Strategy
|
||||
Process multiple queries efficiently:
|
||||
|
||||
```python
|
||||
# Configure for batch processing
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"batch_size": 16, # Process multiple candidates at once
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Provider-Specific Optimizations
|
||||
|
||||
### Cohere Optimization
|
||||
|
||||
```python
|
||||
# Optimized Cohere configuration
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"top_n": 10,
|
||||
"max_chunks_per_doc": 10, # Limit chunk processing
|
||||
"return_documents": False # Reduce response size
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Best Practices:**
|
||||
- Use v3.0 models for better speed/accuracy balance
|
||||
- Limit candidates to 100 or fewer
|
||||
- Cache API responses when possible
|
||||
- Monitor API rate limits
|
||||
|
||||
### Sentence Transformer Optimization
|
||||
|
||||
```python
|
||||
# Performance-optimized configuration
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cuda", # Use GPU when available
|
||||
"batch_size": 32,
|
||||
"top_n": 10,
|
||||
"max_length": 512 # Limit input length
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Device Optimization:**
|
||||
```python
|
||||
import torch
|
||||
|
||||
# Auto-detect best device
|
||||
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"device": device,
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Hugging Face Optimization
|
||||
|
||||
```python
|
||||
# Optimized for Hugging Face models
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"use_fp16": True, # Half precision for speed
|
||||
"max_length": 512,
|
||||
"batch_size": 8,
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### LLM Reranker Optimization
|
||||
|
||||
```python
|
||||
# Optimized LLM reranker configuration
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-3.5-turbo", # Faster than gpt-4
|
||||
"temperature": 0, # Deterministic results
|
||||
"max_tokens": 500 # Limit response length
|
||||
}
|
||||
},
|
||||
"batch_ranking": True, # Rank multiple at once
|
||||
"top_n": 5, # Fewer results for faster processing
|
||||
"timeout": 10 # Request timeout
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Performance Monitoring
|
||||
|
||||
### Latency Tracking
|
||||
```python
|
||||
import time
|
||||
from mem0 import Memory
|
||||
|
||||
def measure_reranker_performance(config, queries, user_id):
|
||||
memory = Memory.from_config(config)
|
||||
|
||||
latencies = []
|
||||
for query in queries:
|
||||
start_time = time.time()
|
||||
results = memory.search(query, user_id=user_id)
|
||||
latency = time.time() - start_time
|
||||
latencies.append(latency)
|
||||
|
||||
return {
|
||||
"avg_latency": sum(latencies) / len(latencies),
|
||||
"max_latency": max(latencies),
|
||||
"min_latency": min(latencies)
|
||||
}
|
||||
```
|
||||
|
||||
### Memory Usage Monitoring
|
||||
```python
|
||||
import psutil
|
||||
import os
|
||||
|
||||
def monitor_memory_usage():
|
||||
process = psutil.Process(os.getpid())
|
||||
return {
|
||||
"memory_mb": process.memory_info().rss / 1024 / 1024,
|
||||
"memory_percent": process.memory_percent()
|
||||
}
|
||||
```
|
||||
|
||||
## Caching Strategies
|
||||
|
||||
### Result Caching
|
||||
```python
|
||||
from functools import lru_cache
|
||||
import hashlib
|
||||
|
||||
class CachedReranker:
|
||||
def __init__(self, config):
|
||||
self.memory = Memory.from_config(config)
|
||||
self.cache_size = 1000
|
||||
|
||||
@lru_cache(maxsize=1000)
|
||||
def search_cached(self, query_hash, user_id):
|
||||
return self.memory.search(query, user_id=user_id)
|
||||
|
||||
def search(self, query, user_id):
|
||||
query_hash = hashlib.md5(f"{query}_{user_id}".encode()).hexdigest()
|
||||
return self.search_cached(query_hash, user_id)
|
||||
```
|
||||
|
||||
### Model Caching
|
||||
```python
|
||||
# Pre-load models to avoid initialization overhead
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"cache_folder": "/path/to/model/cache",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Parallel Processing
|
||||
|
||||
### Async Configuration
|
||||
```python
|
||||
import asyncio
|
||||
from mem0 import Memory
|
||||
|
||||
async def parallel_search(config, queries, user_id):
|
||||
memory = Memory.from_config(config)
|
||||
|
||||
# Process multiple queries concurrently
|
||||
tasks = [
|
||||
memory.search_async(query, user_id=user_id)
|
||||
for query in queries
|
||||
]
|
||||
|
||||
results = await asyncio.gather(*tasks)
|
||||
return results
|
||||
```
|
||||
|
||||
## Hardware Optimization
|
||||
|
||||
### GPU Configuration
|
||||
```python
|
||||
# Optimize for GPU usage
|
||||
import torch
|
||||
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.set_per_process_memory_fraction(0.8) # Reserve GPU memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"device": "cuda",
|
||||
"model": "cross-encoder/ms-marco-electra-base",
|
||||
"batch_size": 64, # Larger batch for GPU
|
||||
"fp16": True # Half precision
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### CPU Optimization
|
||||
```python
|
||||
import torch
|
||||
|
||||
# Optimize CPU threading
|
||||
torch.set_num_threads(4) # Adjust based on your CPU
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"device": "cpu",
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"num_workers": 4 # Parallel processing
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Benchmarking Different Configurations
|
||||
|
||||
```python
|
||||
def benchmark_rerankers():
|
||||
configs = [
|
||||
{"provider": "cohere", "model": "rerank-english-v3.0"},
|
||||
{"provider": "sentence_transformer", "model": "cross-encoder/ms-marco-MiniLM-L-6-v2"},
|
||||
{"provider": "huggingface", "model": "BAAI/bge-reranker-base"}
|
||||
]
|
||||
|
||||
test_queries = ["sample query 1", "sample query 2", "sample query 3"]
|
||||
|
||||
results = {}
|
||||
for config in configs:
|
||||
provider = config["provider"]
|
||||
performance = measure_reranker_performance(
|
||||
{"reranker": {"provider": provider, "config": config}},
|
||||
test_queries,
|
||||
"test_user"
|
||||
)
|
||||
results[provider] = performance
|
||||
|
||||
return results
|
||||
```
|
||||
|
||||
## Production Best Practices
|
||||
|
||||
1. **Model Selection**: Choose the right balance of speed vs. accuracy
|
||||
2. **Resource Allocation**: Monitor CPU/GPU usage and memory consumption
|
||||
3. **Error Handling**: Implement fallbacks for reranker failures
|
||||
4. **Load Balancing**: Distribute reranking load across multiple instances
|
||||
5. **Monitoring**: Track latency, throughput, and error rates
|
||||
6. **Caching**: Cache frequent queries and model predictions
|
||||
7. **Batch Processing**: Group similar queries for efficient processing
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
title: Overview
|
||||
icon: "info"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
Rerankers enhance the quality of search results by re-ordering the initial retrieval results using more sophisticated scoring mechanisms. They act as a secondary ranking layer that can significantly improve the relevance of retrieved memories.
|
||||
|
||||
## How Rerankers Work
|
||||
|
||||
1. **Initial Retrieval**: Vector search returns candidate memories based on semantic similarity
|
||||
2. **Reranking**: The reranker evaluates and re-scores these candidates using more complex criteria
|
||||
3. **Final Results**: Returns the top-k memories with improved relevance ordering
|
||||
|
||||
## Benefits
|
||||
|
||||
- **Improved Precision**: Better ranking of relevant memories
|
||||
- **Context Awareness**: More sophisticated understanding of query-memory relationships
|
||||
- **Performance**: Can improve results without changing the underlying vector store
|
||||
|
||||
## Supported Rerankers
|
||||
|
||||
Mem0 supports several reranker models:
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Cohere" href="/components/rerankers/models/cohere" />
|
||||
<Card title="Sentence Transformer" href="/components/rerankers/models/sentence_transformer" />
|
||||
<Card title="Hugging Face" href="/components/rerankers/models/huggingface" />
|
||||
<Card title="LLM Reranker" href="/components/rerankers/models/llm_reranker" />
|
||||
</CardGroup>
|
||||
|
||||
## Usage
|
||||
|
||||
Rerankers are configured as part of the memory configuration:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"api_key": "your-api-key",
|
||||
"top_n": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memory = Memory.from_config(config)
|
||||
```
|
||||
|
||||
For detailed configuration options, see the [Config](./config) page.
|
||||
+559
-318
@@ -2,194 +2,389 @@
|
||||
"$schema": "https://mintlify.com/docs.json",
|
||||
"name": "Mem0",
|
||||
"description": "Mem0 is a self-improving memory layer for LLM applications, enabling personalized AI experiences that save costs and delight users.",
|
||||
"theme": "maple",
|
||||
"theme": "aspen",
|
||||
"colors": {
|
||||
"primary": "#6c60f0",
|
||||
"light": "#E6FFA2",
|
||||
"dark": "#a3df02"
|
||||
"primary": "#2553eb",
|
||||
"light": "#6084fa",
|
||||
"dark": "#2553eb"
|
||||
},
|
||||
"favicon": "/logo/favicon.png",
|
||||
"navigation": {
|
||||
"anchors": [
|
||||
"versions": [
|
||||
{
|
||||
"anchor": "Documentation",
|
||||
"icon": "book-open",
|
||||
"tabs": [
|
||||
"version": "v1.0 Beta",
|
||||
"anchors": [
|
||||
{
|
||||
"tab": "Documentation",
|
||||
"groups": [
|
||||
"anchor": "Documentation",
|
||||
"icon": "book-open",
|
||||
"tabs": [
|
||||
{
|
||||
"group": "Getting Started",
|
||||
"icon": "rocket",
|
||||
"pages": [
|
||||
"introduction",
|
||||
"quickstart",
|
||||
"faqs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Core Concepts",
|
||||
"icon": "brain",
|
||||
"pages": [
|
||||
"core-concepts/memory-types",
|
||||
"tab": "Documentation",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Memory Operations",
|
||||
"icon": "gear",
|
||||
"group": "Getting Started",
|
||||
"icon": "rocket",
|
||||
"pages": [
|
||||
"core-concepts/memory-operations/add",
|
||||
"core-concepts/memory-operations/search",
|
||||
"core-concepts/memory-operations/update",
|
||||
"core-concepts/memory-operations/delete"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Platform",
|
||||
"icon": "globe",
|
||||
"pages": [
|
||||
"platform/overview",
|
||||
"platform/quickstart",
|
||||
"platform/advanced-memory-operations",
|
||||
{
|
||||
"group": "Features",
|
||||
"icon": "star",
|
||||
"pages": [
|
||||
"platform/features/platform-overview",
|
||||
"platform/features/contextual-add",
|
||||
"platform/features/async-client",
|
||||
"platform/features/graph-memory",
|
||||
"platform/features/advanced-retrieval",
|
||||
"platform/features/criteria-retrieval",
|
||||
"platform/features/multimodal-support",
|
||||
"platform/features/selective-memory",
|
||||
"platform/features/custom-categories",
|
||||
"platform/features/custom-instructions",
|
||||
"platform/features/direct-import",
|
||||
"platform/features/memory-export",
|
||||
"platform/features/timestamp",
|
||||
"platform/features/expiration-date",
|
||||
"platform/features/webhooks",
|
||||
"platform/features/feedback-mechanism",
|
||||
"platform/features/group-chat"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Open Source",
|
||||
"icon": "code-branch",
|
||||
"pages": [
|
||||
"open-source/overview",
|
||||
"open-source/python-quickstart",
|
||||
"open-source/node-quickstart",
|
||||
{
|
||||
"group": "Features",
|
||||
"icon": "star",
|
||||
"pages": [
|
||||
"open-source/features/overview",
|
||||
"open-source/features/async-memory",
|
||||
"open-source/features/openai_compatibility",
|
||||
"open-source/features/custom-fact-extraction-prompt",
|
||||
"open-source/features/custom-update-memory-prompt",
|
||||
"open-source/features/multimodal-support",
|
||||
"open-source/features/rest-api"
|
||||
"introduction",
|
||||
"quickstart",
|
||||
"faqs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Graph Memory",
|
||||
"icon": "spider-web",
|
||||
"pages": [
|
||||
"open-source/graph_memory/overview",
|
||||
"open-source/graph_memory/features"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "LLMs",
|
||||
"group": "Core Concepts",
|
||||
"icon": "brain",
|
||||
"pages": [
|
||||
"components/llms/overview",
|
||||
"components/llms/config",
|
||||
"core-concepts/memory-types",
|
||||
{
|
||||
"group": "Supported LLMs",
|
||||
"icon": "list",
|
||||
"group": "Memory Operations",
|
||||
"icon": "gear",
|
||||
"pages": [
|
||||
"components/llms/models/openai",
|
||||
"components/llms/models/anthropic",
|
||||
"components/llms/models/azure_openai",
|
||||
"components/llms/models/ollama",
|
||||
"components/llms/models/together",
|
||||
"components/llms/models/groq",
|
||||
"components/llms/models/litellm",
|
||||
"components/llms/models/mistral_AI",
|
||||
"components/llms/models/google_AI",
|
||||
"components/llms/models/aws_bedrock",
|
||||
"components/llms/models/deepseek",
|
||||
"components/llms/models/xAI",
|
||||
"components/llms/models/sarvam",
|
||||
"components/llms/models/lmstudio",
|
||||
"components/llms/models/langchain",
|
||||
"components/llms/models/vllm"
|
||||
"core-concepts/memory-operations/add",
|
||||
"core-concepts/memory-operations/search",
|
||||
"core-concepts/memory-operations/update",
|
||||
"core-concepts/memory-operations/delete"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Vector Databases",
|
||||
"icon": "database",
|
||||
"group": "Platform",
|
||||
"icon": "globe",
|
||||
"pages": [
|
||||
"components/vectordbs/overview",
|
||||
"components/vectordbs/config",
|
||||
"platform/overview",
|
||||
"platform/quickstart",
|
||||
"platform/advanced-memory-operations",
|
||||
{
|
||||
"group": "Supported Vector Databases",
|
||||
"icon": "server",
|
||||
"group": "Features",
|
||||
"icon": "star",
|
||||
"pages": [
|
||||
"components/vectordbs/dbs/qdrant",
|
||||
"components/vectordbs/dbs/chroma",
|
||||
"components/vectordbs/dbs/pgvector",
|
||||
"components/vectordbs/dbs/milvus",
|
||||
"components/vectordbs/dbs/pinecone",
|
||||
"components/vectordbs/dbs/mongodb",
|
||||
"components/vectordbs/dbs/azure",
|
||||
"components/vectordbs/dbs/redis",
|
||||
"components/vectordbs/dbs/valkey",
|
||||
"components/vectordbs/dbs/elasticsearch",
|
||||
"components/vectordbs/dbs/opensearch",
|
||||
"components/vectordbs/dbs/supabase",
|
||||
"components/vectordbs/dbs/upstash-vector",
|
||||
"components/vectordbs/dbs/vectorize",
|
||||
"components/vectordbs/dbs/vertex_ai",
|
||||
"components/vectordbs/dbs/weaviate",
|
||||
"components/vectordbs/dbs/faiss",
|
||||
"components/vectordbs/dbs/langchain",
|
||||
"components/vectordbs/dbs/baidu",
|
||||
"components/vectordbs/dbs/s3_vectors",
|
||||
"components/vectordbs/dbs/databricks",
|
||||
"components/vectordbs/dbs/neptune_analytics"
|
||||
"platform/features/platform-overview",
|
||||
"platform/features/contextual-add",
|
||||
"platform/features/async-client",
|
||||
"platform/features/graph-memory",
|
||||
"platform/features/advanced-retrieval",
|
||||
"platform/features/criteria-retrieval",
|
||||
"platform/features/multimodal-support",
|
||||
"platform/features/selective-memory",
|
||||
"platform/features/custom-categories",
|
||||
"platform/features/custom-instructions",
|
||||
"platform/features/direct-import",
|
||||
"platform/features/memory-export",
|
||||
"platform/features/timestamp",
|
||||
"platform/features/expiration-date",
|
||||
"platform/features/webhooks",
|
||||
"platform/features/feedback-mechanism",
|
||||
"platform/features/group-chat"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Embedding Models",
|
||||
"icon": "layer-group",
|
||||
"group": "Open Source",
|
||||
"icon": "code-branch",
|
||||
"pages": [
|
||||
"components/embedders/overview",
|
||||
"components/embedders/config",
|
||||
"open-source/overview",
|
||||
"open-source/python-quickstart",
|
||||
"open-source/node-quickstart",
|
||||
{
|
||||
"group": "Supported Embedding Models",
|
||||
"icon": "list",
|
||||
"group": "Features",
|
||||
"icon": "star",
|
||||
"pages": [
|
||||
"components/embedders/models/openai",
|
||||
"components/embedders/models/azure_openai",
|
||||
"components/embedders/models/ollama",
|
||||
"components/embedders/models/huggingface",
|
||||
"components/embedders/models/vertexai",
|
||||
"components/embedders/models/google_AI",
|
||||
"components/embedders/models/lmstudio",
|
||||
"components/embedders/models/together",
|
||||
"components/embedders/models/langchain",
|
||||
"components/embedders/models/aws_bedrock"
|
||||
"open-source/features/overview",
|
||||
"open-source/features/async-memory",
|
||||
"open-source/features/openai_compatibility",
|
||||
"open-source/features/custom-fact-extraction-prompt",
|
||||
"open-source/features/custom-update-memory-prompt",
|
||||
"open-source/features/multimodal-support",
|
||||
"open-source/features/rest-api",
|
||||
"open-source/features/metadata-filtering",
|
||||
"open-source/features/reranker-search"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Graph Memory",
|
||||
"icon": "spider-web",
|
||||
"pages": [
|
||||
"open-source/graph_memory/overview",
|
||||
"open-source/graph_memory/features"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "LLMs",
|
||||
"icon": "brain",
|
||||
"pages": [
|
||||
"components/llms/overview",
|
||||
"components/llms/config",
|
||||
{
|
||||
"group": "Supported LLMs",
|
||||
"icon": "list",
|
||||
"pages": [
|
||||
"components/llms/models/openai",
|
||||
"components/llms/models/anthropic",
|
||||
"components/llms/models/azure_openai",
|
||||
"components/llms/models/ollama",
|
||||
"components/llms/models/together",
|
||||
"components/llms/models/groq",
|
||||
"components/llms/models/litellm",
|
||||
"components/llms/models/mistral_AI",
|
||||
"components/llms/models/google_AI",
|
||||
"components/llms/models/aws_bedrock",
|
||||
"components/llms/models/deepseek",
|
||||
"components/llms/models/xAI",
|
||||
"components/llms/models/sarvam",
|
||||
"components/llms/models/lmstudio",
|
||||
"components/llms/models/langchain",
|
||||
"components/llms/models/vllm"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Vector Databases",
|
||||
"icon": "database",
|
||||
"pages": [
|
||||
"components/vectordbs/overview",
|
||||
"components/vectordbs/config",
|
||||
{
|
||||
"group": "Supported Vector Databases",
|
||||
"icon": "server",
|
||||
"pages": [
|
||||
"components/vectordbs/dbs/qdrant",
|
||||
"components/vectordbs/dbs/chroma",
|
||||
"components/vectordbs/dbs/pgvector",
|
||||
"components/vectordbs/dbs/milvus",
|
||||
"components/vectordbs/dbs/pinecone",
|
||||
"components/vectordbs/dbs/mongodb",
|
||||
"components/vectordbs/dbs/azure",
|
||||
"components/vectordbs/dbs/redis",
|
||||
"components/vectordbs/dbs/valkey",
|
||||
"components/vectordbs/dbs/elasticsearch",
|
||||
"components/vectordbs/dbs/opensearch",
|
||||
"components/vectordbs/dbs/supabase",
|
||||
"components/vectordbs/dbs/upstash-vector",
|
||||
"components/vectordbs/dbs/vectorize",
|
||||
"components/vectordbs/dbs/vertex_ai",
|
||||
"components/vectordbs/dbs/weaviate",
|
||||
"components/vectordbs/dbs/faiss",
|
||||
"components/vectordbs/dbs/langchain",
|
||||
"components/vectordbs/dbs/baidu",
|
||||
"components/vectordbs/dbs/s3_vectors",
|
||||
"components/vectordbs/dbs/databricks",
|
||||
"components/vectordbs/dbs/neptune_analytics"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Embedding Models",
|
||||
"icon": "layer-group",
|
||||
"pages": [
|
||||
"components/embedders/overview",
|
||||
"components/embedders/config",
|
||||
{
|
||||
"group": "Supported Embedding Models",
|
||||
"icon": "list",
|
||||
"pages": [
|
||||
"components/embedders/models/openai",
|
||||
"components/embedders/models/azure_openai",
|
||||
"components/embedders/models/ollama",
|
||||
"components/embedders/models/huggingface",
|
||||
"components/embedders/models/vertexai",
|
||||
"components/embedders/models/google_AI",
|
||||
"components/embedders/models/lmstudio",
|
||||
"components/embedders/models/together",
|
||||
"components/embedders/models/langchain",
|
||||
"components/embedders/models/aws_bedrock"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Rerankers (New in v1.0)",
|
||||
"icon": "sort",
|
||||
"pages": [
|
||||
"components/rerankers/overview",
|
||||
"components/rerankers/config",
|
||||
{
|
||||
"group": "Supported Rerankers",
|
||||
"icon": "list",
|
||||
"pages": [
|
||||
"components/rerankers/models/cohere",
|
||||
"components/rerankers/models/sentence_transformer",
|
||||
"components/rerankers/models/huggingface",
|
||||
"components/rerankers/models/llm_reranker"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Migration Guide",
|
||||
"icon": "arrow-right",
|
||||
"pages": [
|
||||
"migration/v0-to-v1",
|
||||
"migration/breaking-changes",
|
||||
"migration/api-changes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "OpenMemory",
|
||||
"icon": "square-terminal",
|
||||
"pages": [
|
||||
"openmemory/overview",
|
||||
"openmemory/quickstart",
|
||||
"openmemory/integrations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Contribution",
|
||||
"icon": "handshake",
|
||||
"pages": [
|
||||
"contributing/development",
|
||||
"contributing/documentation"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Examples",
|
||||
"groups": [
|
||||
{
|
||||
"group": "💡 Examples",
|
||||
"icon": "lightbulb",
|
||||
"pages": [
|
||||
"examples",
|
||||
"examples/aws_example",
|
||||
"examples/aws_neptune_analytics_hybrid_store",
|
||||
"examples/mem0-demo",
|
||||
"examples/ai_companion_js",
|
||||
"examples/collaborative-task-agent",
|
||||
"examples/llamaindex-multiagent-learning-system",
|
||||
"examples/personalized-search-tavily-mem0",
|
||||
"examples/eliza_os",
|
||||
"examples/mem0-mastra",
|
||||
"examples/mem0-with-ollama",
|
||||
"examples/personal-ai-tutor",
|
||||
"examples/customer-support-agent",
|
||||
"examples/personal-travel-assistant",
|
||||
"examples/llama-index-mem0",
|
||||
"examples/chrome-extension",
|
||||
"examples/memory-guided-content-writing",
|
||||
"examples/multimodal-demo",
|
||||
"examples/personalized-deep-research",
|
||||
"examples/mem0-agentic-tool",
|
||||
"examples/openai-inbuilt-tools",
|
||||
"examples/mem0-openai-voice-demo",
|
||||
"examples/mem0-google-adk-healthcare-assistant",
|
||||
"examples/email_processing",
|
||||
"examples/youtube-assistant"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Integrations",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Integrations",
|
||||
"icon": "plug",
|
||||
"pages": [
|
||||
"integrations",
|
||||
"integrations/langchain",
|
||||
"integrations/langgraph",
|
||||
"integrations/llama-index",
|
||||
"integrations/agno",
|
||||
"integrations/autogen",
|
||||
"integrations/crewai",
|
||||
"integrations/openai-agents-sdk",
|
||||
"integrations/google-ai-adk",
|
||||
"integrations/mastra",
|
||||
"integrations/vercel-ai-sdk",
|
||||
"integrations/livekit",
|
||||
"integrations/pipecat",
|
||||
"integrations/elevenlabs",
|
||||
"integrations/aws-bedrock",
|
||||
"integrations/flowise",
|
||||
"integrations/langchain-tools",
|
||||
"integrations/agentops",
|
||||
"integrations/keywords",
|
||||
"integrations/dify",
|
||||
"integrations/raycast"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "API Reference",
|
||||
"icon": "square-terminal",
|
||||
"groups": [
|
||||
{
|
||||
"group": "API Reference",
|
||||
"icon": "terminal",
|
||||
"pages": [
|
||||
"api-reference",
|
||||
{
|
||||
"group": "Memory APIs",
|
||||
"icon": "microchip",
|
||||
"pages": [
|
||||
"api-reference/memory/add-memories",
|
||||
"api-reference/memory/v2-search-memories",
|
||||
"api-reference/memory/v1-search-memories",
|
||||
"api-reference/memory/v2-get-memories",
|
||||
"api-reference/memory/v1-get-memories",
|
||||
"api-reference/memory/history-memory",
|
||||
"api-reference/memory/get-memory",
|
||||
"api-reference/memory/update-memory",
|
||||
"api-reference/memory/batch-update",
|
||||
"api-reference/memory/delete-memory",
|
||||
"api-reference/memory/batch-delete",
|
||||
"api-reference/memory/delete-memories",
|
||||
"api-reference/memory/create-memory-export",
|
||||
"api-reference/memory/get-memory-export",
|
||||
"api-reference/memory/feedback"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Entities APIs",
|
||||
"icon": "users",
|
||||
"pages": [
|
||||
"api-reference/entities/get-users",
|
||||
"api-reference/entities/delete-user"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Organizations APIs",
|
||||
"icon": "building",
|
||||
"pages": [
|
||||
"api-reference/organization/create-org",
|
||||
"api-reference/organization/get-orgs",
|
||||
"api-reference/organization/get-org",
|
||||
"api-reference/organization/get-org-members",
|
||||
"api-reference/organization/add-org-member",
|
||||
"api-reference/organization/delete-org"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Project APIs",
|
||||
"icon": "folder",
|
||||
"pages": [
|
||||
"api-reference/project/create-project",
|
||||
"api-reference/project/get-projects",
|
||||
"api-reference/project/get-project",
|
||||
"api-reference/project/get-project-members",
|
||||
"api-reference/project/add-project-member",
|
||||
"api-reference/project/delete-project"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Webhook APIs",
|
||||
"icon": "webhook",
|
||||
"pages": [
|
||||
"api-reference/webhook/create-webhook",
|
||||
"api-reference/webhook/get-webhook",
|
||||
"api-reference/webhook/update-webhook",
|
||||
"api-reference/webhook/delete-webhook"
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -197,177 +392,223 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Contribution",
|
||||
"icon": "handshake",
|
||||
"pages": [
|
||||
"contributing/development",
|
||||
"contributing/documentation"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "OpenMemory",
|
||||
"icon": "square-terminal",
|
||||
"pages": [
|
||||
"openmemory/overview",
|
||||
"openmemory/quickstart",
|
||||
"openmemory/integrations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Examples",
|
||||
"groups": [
|
||||
{
|
||||
"group": "💡 Examples",
|
||||
"icon": "lightbulb",
|
||||
"pages": [
|
||||
"examples",
|
||||
"examples/aws_example",
|
||||
"examples/aws_neptune_analytics_hybrid_store",
|
||||
"examples/mem0-demo",
|
||||
"examples/ai_companion_js",
|
||||
"examples/collaborative-task-agent",
|
||||
"examples/llamaindex-multiagent-learning-system",
|
||||
"examples/personalized-search-tavily-mem0",
|
||||
"examples/eliza_os",
|
||||
"examples/mem0-mastra",
|
||||
"examples/mem0-with-ollama",
|
||||
"examples/personal-ai-tutor",
|
||||
"examples/customer-support-agent",
|
||||
"examples/personal-travel-assistant",
|
||||
"examples/llama-index-mem0",
|
||||
"examples/chrome-extension",
|
||||
"examples/memory-guided-content-writing",
|
||||
"examples/multimodal-demo",
|
||||
"examples/personalized-deep-research",
|
||||
"examples/mem0-agentic-tool",
|
||||
"examples/openai-inbuilt-tools",
|
||||
"examples/mem0-openai-voice-demo",
|
||||
"examples/mem0-google-adk-healthcare-assistant",
|
||||
"examples/email_processing",
|
||||
"examples/youtube-assistant"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Integrations",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Integrations",
|
||||
"icon": "plug",
|
||||
"pages": [
|
||||
"integrations",
|
||||
"integrations/langchain",
|
||||
"integrations/langgraph",
|
||||
"integrations/llama-index",
|
||||
"integrations/agno",
|
||||
"integrations/autogen",
|
||||
"integrations/crewai",
|
||||
"integrations/openai-agents-sdk",
|
||||
"integrations/google-ai-adk",
|
||||
"integrations/mastra",
|
||||
"integrations/vercel-ai-sdk",
|
||||
"integrations/livekit",
|
||||
"integrations/pipecat",
|
||||
"integrations/elevenlabs",
|
||||
"integrations/aws-bedrock",
|
||||
"integrations/flowise",
|
||||
"integrations/langchain-tools",
|
||||
"integrations/agentops",
|
||||
"integrations/keywords",
|
||||
"integrations/dify",
|
||||
"integrations/raycast"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "API Reference",
|
||||
"icon": "square-terminal",
|
||||
"groups": [
|
||||
{
|
||||
"group": "API Reference",
|
||||
"icon": "terminal",
|
||||
"pages": [
|
||||
"api-reference",
|
||||
"tab": "Changelog",
|
||||
"icon": "clock",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Memory APIs",
|
||||
"icon": "microchip",
|
||||
"group": "Product Updates",
|
||||
"icon": "rocket",
|
||||
"pages": [
|
||||
"api-reference/memory/add-memories",
|
||||
"api-reference/memory/v2-search-memories",
|
||||
"api-reference/memory/v1-search-memories",
|
||||
"api-reference/memory/v2-get-memories",
|
||||
"api-reference/memory/v1-get-memories",
|
||||
"api-reference/memory/history-memory",
|
||||
"api-reference/memory/get-memory",
|
||||
"api-reference/memory/update-memory",
|
||||
"api-reference/memory/batch-update",
|
||||
"api-reference/memory/delete-memory",
|
||||
"api-reference/memory/batch-delete",
|
||||
"api-reference/memory/delete-memories",
|
||||
"api-reference/memory/create-memory-export",
|
||||
"api-reference/memory/get-memory-export",
|
||||
"api-reference/memory/feedback"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Entities APIs",
|
||||
"icon": "users",
|
||||
"pages": [
|
||||
"api-reference/entities/get-users",
|
||||
"api-reference/entities/delete-user"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Organizations APIs",
|
||||
"icon": "building",
|
||||
"pages": [
|
||||
"api-reference/organization/create-org",
|
||||
"api-reference/organization/get-orgs",
|
||||
"api-reference/organization/get-org",
|
||||
"api-reference/organization/get-org-members",
|
||||
"api-reference/organization/add-org-member",
|
||||
"api-reference/organization/delete-org"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Project APIs",
|
||||
"icon": "folder",
|
||||
"pages": [
|
||||
"api-reference/project/create-project",
|
||||
"api-reference/project/get-projects",
|
||||
"api-reference/project/get-project",
|
||||
"api-reference/project/get-project-members",
|
||||
"api-reference/project/add-project-member",
|
||||
"api-reference/project/delete-project"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Webhook APIs",
|
||||
"icon": "webhook",
|
||||
"pages": [
|
||||
"api-reference/webhook/create-webhook",
|
||||
"api-reference/webhook/get-webhook",
|
||||
"api-reference/webhook/update-webhook",
|
||||
"api-reference/webhook/delete-webhook"
|
||||
"changelog"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"version": "v0.x Legacy",
|
||||
"anchors": [
|
||||
{
|
||||
"tab": "Changelog",
|
||||
"icon": "clock",
|
||||
"groups": [
|
||||
"anchor": "Documentation",
|
||||
"icon": "book-open",
|
||||
"tabs": [
|
||||
{
|
||||
"group": "Product Updates",
|
||||
"icon": "rocket",
|
||||
"pages": [
|
||||
"changelog"
|
||||
"tab": "Documentation",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Getting Started",
|
||||
"icon": "rocket",
|
||||
"pages": [
|
||||
"v0x/introduction",
|
||||
"v0x/quickstart",
|
||||
"v0x/faqs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Core Concepts",
|
||||
"icon": "brain",
|
||||
"pages": [
|
||||
"v0x/core-concepts/memory-types",
|
||||
{
|
||||
"group": "Memory Operations",
|
||||
"icon": "gear",
|
||||
"pages": [
|
||||
"v0x/core-concepts/memory-operations/add",
|
||||
"v0x/core-concepts/memory-operations/search",
|
||||
"v0x/core-concepts/memory-operations/update",
|
||||
"v0x/core-concepts/memory-operations/delete"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Open Source",
|
||||
"icon": "code-branch",
|
||||
"pages": [
|
||||
"v0x/open-source/overview",
|
||||
"v0x/open-source/python-quickstart",
|
||||
"v0x/open-source/node-quickstart",
|
||||
{
|
||||
"group": "LLMs",
|
||||
"icon": "brain",
|
||||
"pages": [
|
||||
"v0x/components/llms/overview",
|
||||
"v0x/components/llms/config",
|
||||
{
|
||||
"group": "Supported LLMs",
|
||||
"icon": "list",
|
||||
"pages": [
|
||||
"v0x/components/llms/models/openai",
|
||||
"v0x/components/llms/models/anthropic",
|
||||
"v0x/components/llms/models/azure_openai",
|
||||
"v0x/components/llms/models/ollama",
|
||||
"v0x/components/llms/models/together",
|
||||
"v0x/components/llms/models/groq",
|
||||
"v0x/components/llms/models/litellm",
|
||||
"v0x/components/llms/models/mistral_AI",
|
||||
"v0x/components/llms/models/google_AI",
|
||||
"v0x/components/llms/models/aws_bedrock",
|
||||
"v0x/components/llms/models/deepseek",
|
||||
"v0x/components/llms/models/xAI",
|
||||
"v0x/components/llms/models/sarvam",
|
||||
"v0x/components/llms/models/lmstudio",
|
||||
"v0x/components/llms/models/langchain",
|
||||
"v0x/components/llms/models/vllm"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Vector Databases",
|
||||
"icon": "database",
|
||||
"pages": [
|
||||
"v0x/components/vectordbs/overview",
|
||||
"v0x/components/vectordbs/config",
|
||||
{
|
||||
"group": "Supported Vector Databases",
|
||||
"icon": "server",
|
||||
"pages": [
|
||||
"v0x/components/vectordbs/dbs/qdrant",
|
||||
"v0x/components/vectordbs/dbs/chroma",
|
||||
"v0x/components/vectordbs/dbs/pgvector",
|
||||
"v0x/components/vectordbs/dbs/milvus",
|
||||
"v0x/components/vectordbs/dbs/pinecone",
|
||||
"v0x/components/vectordbs/dbs/mongodb",
|
||||
"v0x/components/vectordbs/dbs/azure",
|
||||
"v0x/components/vectordbs/dbs/redis",
|
||||
"v0x/components/vectordbs/dbs/valkey",
|
||||
"v0x/components/vectordbs/dbs/elasticsearch",
|
||||
"v0x/components/vectordbs/dbs/opensearch",
|
||||
"v0x/components/vectordbs/dbs/supabase",
|
||||
"v0x/components/vectordbs/dbs/upstash-vector",
|
||||
"v0x/components/vectordbs/dbs/vectorize",
|
||||
"v0x/components/vectordbs/dbs/vertex_ai",
|
||||
"v0x/components/vectordbs/dbs/weaviate",
|
||||
"v0x/components/vectordbs/dbs/faiss",
|
||||
"v0x/components/vectordbs/dbs/langchain",
|
||||
"v0x/components/vectordbs/dbs/baidu",
|
||||
"v0x/components/vectordbs/dbs/s3_vectors",
|
||||
"v0x/components/vectordbs/dbs/databricks",
|
||||
"v0x/components/vectordbs/dbs/neptune_analytics"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Embedding Models",
|
||||
"icon": "layer-group",
|
||||
"pages": [
|
||||
"v0x/components/embedders/overview",
|
||||
"v0x/components/embedders/config",
|
||||
{
|
||||
"group": "Supported Embedding Models",
|
||||
"icon": "list",
|
||||
"pages": [
|
||||
"v0x/components/embedders/models/openai",
|
||||
"v0x/components/embedders/models/azure_openai",
|
||||
"v0x/components/embedders/models/ollama",
|
||||
"v0x/components/embedders/models/huggingface",
|
||||
"v0x/components/embedders/models/vertexai",
|
||||
"v0x/components/embedders/models/google_AI",
|
||||
"v0x/components/embedders/models/lmstudio",
|
||||
"v0x/components/embedders/models/together",
|
||||
"v0x/components/embedders/models/langchain",
|
||||
"v0x/components/embedders/models/aws_bedrock"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Examples",
|
||||
"groups": [
|
||||
{
|
||||
"group": "💡 Examples",
|
||||
"icon": "lightbulb",
|
||||
"pages": [
|
||||
"v0x/examples/aws_example",
|
||||
"v0x/examples/aws_neptune_analytics_hybrid_store",
|
||||
"v0x/examples/mem0-demo",
|
||||
"v0x/examples/ai_companion_js",
|
||||
"v0x/examples/collaborative-task-agent",
|
||||
"v0x/examples/llamaindex-multiagent-learning-system",
|
||||
"v0x/examples/personalized-search-tavily-mem0",
|
||||
"v0x/examples/eliza_os",
|
||||
"v0x/examples/mem0-mastra",
|
||||
"v0x/examples/mem0-with-ollama",
|
||||
"v0x/examples/personal-ai-tutor",
|
||||
"v0x/examples/customer-support-agent",
|
||||
"v0x/examples/personal-travel-assistant",
|
||||
"v0x/examples/llama-index-mem0",
|
||||
"v0x/examples/chrome-extension",
|
||||
"v0x/examples/memory-guided-content-writing",
|
||||
"v0x/examples/multimodal-demo",
|
||||
"v0x/examples/personalized-deep-research",
|
||||
"v0x/examples/mem0-agentic-tool",
|
||||
"v0x/examples/openai-inbuilt-tools",
|
||||
"v0x/examples/mem0-openai-voice-demo",
|
||||
"v0x/examples/mem0-google-adk-healthcare-assistant",
|
||||
"v0x/examples/email_processing",
|
||||
"v0x/examples/youtube-assistant"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tab": "Integrations",
|
||||
"groups": [
|
||||
{
|
||||
"group": "Integrations",
|
||||
"icon": "plug",
|
||||
"pages": [
|
||||
"v0x/integrations/langchain",
|
||||
"v0x/integrations/langgraph",
|
||||
"v0x/integrations/llama-index",
|
||||
"v0x/integrations/agno",
|
||||
"v0x/integrations/autogen",
|
||||
"v0x/integrations/crewai",
|
||||
"v0x/integrations/openai-agents-sdk",
|
||||
"v0x/integrations/google-ai-adk",
|
||||
"v0x/integrations/mastra",
|
||||
"v0x/integrations/vercel-ai-sdk",
|
||||
"v0x/integrations/livekit",
|
||||
"v0x/integrations/pipecat",
|
||||
"v0x/integrations/elevenlabs",
|
||||
"v0x/integrations/aws-bedrock",
|
||||
"v0x/integrations/flowise",
|
||||
"v0x/integrations/langchain-tools",
|
||||
"v0x/integrations/agentops",
|
||||
"v0x/integrations/keywords",
|
||||
"v0x/integrations/dify",
|
||||
"v0x/integrations/raycast"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ Explore how **Mem0** can power real-world applications and bring personalized, i
|
||||
Integrate **Mem0** into **YouTube's** native UI, providing personalized responses with video context.
|
||||
</Card>
|
||||
|
||||
<Card title="Document Writing Assistant" icon="pen" href="/examples/document-writing">
|
||||
<Card title="Memory-Guided Content Writing" icon="pen" href="/examples/memory-guided-content-writing">
|
||||
Create a **Writing Assistant** that understands and adapts to your unique style, improving consistency and productivity.
|
||||
</Card>
|
||||
|
||||
|
||||
@@ -215,28 +215,6 @@ Here are the available integrations for Mem0:
|
||||
>
|
||||
Build AI applications with persistent memory using Dify and Mem0.
|
||||
</Card>
|
||||
<Card
|
||||
title="MCP Server"
|
||||
icon={
|
||||
<svg
|
||||
viewBox="0 0 180 180"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="24"
|
||||
height="24"
|
||||
>
|
||||
<path
|
||||
d="M45 45 L135 45 M45 90 L135 90 M45 135 L135 135"
|
||||
stroke="currentColor"
|
||||
strokeWidth="12"
|
||||
strokeLinecap="round"
|
||||
fill="none"
|
||||
/>
|
||||
</svg>
|
||||
}
|
||||
href="/integrations/mcp-server"
|
||||
>
|
||||
Integrate Mem0 as an MCP Server in Cursor.
|
||||
</Card>
|
||||
<Card
|
||||
title="Livekit"
|
||||
icon={
|
||||
|
||||
@@ -135,6 +135,6 @@ Integrating Mem0 with Keywords AI provides a powerful combination for building A
|
||||
|
||||
For more information, refer to:
|
||||
- [Keywords AI Documentation](https://docs.keywordsai.co)
|
||||
- [Mem0 Platform]((https://app.mem0.ai/))
|
||||
- [Mem0 Platform](https://app.mem0.ai/)
|
||||
|
||||
<Snippet file="get-help.mdx" />
|
||||
|
||||
@@ -20,7 +20,7 @@ Most current agents are stateless: they process a query, generate a response, an
|
||||
Stateful agents, powered by Mem0, are different. They retain context, recall what matters, and behave more intelligently over time.
|
||||
|
||||
<Frame>
|
||||
<img src="../images/stateless-vs-stateful-agent.png" />
|
||||
<img src="/images/stateless-vs-stateful-agent.png" />
|
||||
</Frame>
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ Stateful agents, powered by Mem0, are different. They retain context, recall wha
|
||||
Mem0 sits alongside your retriever, planner, and LLM. Unlike retrieval-based systems (like RAG), Mem0 tracks past interactions, stores long-term knowledge, and evolves the agent’s behavior.
|
||||
|
||||
<Frame>
|
||||
<img src="../images/memory-agent-stack.png" />
|
||||
<img src="/images/memory-agent-stack.png" />
|
||||
</Frame>
|
||||
|
||||
Memory is not about pushing more tokens into a prompt but about intelligently remembering context that matters. This distinction matters:
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
@@ -0,0 +1,551 @@
|
||||
---
|
||||
title: API Reference Changes
|
||||
description: 'Complete API changes between v0.x and v1.0 Beta'
|
||||
icon: "code"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
This page documents all API changes between Mem0 v0.x and v1.0 Beta, organized by component and method.
|
||||
|
||||
## Memory Class Changes
|
||||
|
||||
### Constructor
|
||||
|
||||
#### v0.x
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# Basic initialization
|
||||
m = Memory()
|
||||
|
||||
# With configuration
|
||||
config = {
|
||||
"version": "v1.0", # Supported in v0.x
|
||||
"vector_store": {...}
|
||||
}
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
#### v1.0 Beta
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# Basic initialization (same)
|
||||
m = Memory()
|
||||
|
||||
# With configuration
|
||||
config = {
|
||||
"version": "v1.1", # v1.1+ only
|
||||
"vector_store": {...},
|
||||
# New optional features
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {...}
|
||||
}
|
||||
}
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### add() Method
|
||||
|
||||
#### v0.x Signature
|
||||
```python
|
||||
def add(
|
||||
self,
|
||||
messages,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
metadata: dict = None,
|
||||
filters: dict = None,
|
||||
output_format: str = None, # ❌ REMOVED
|
||||
version: str = None, # ❌ REMOVED
|
||||
async_mode: bool = None # ❌ REMOVED
|
||||
) -> Union[List[dict], dict]
|
||||
```
|
||||
|
||||
#### v1.0 Beta Signature
|
||||
```python
|
||||
def add(
|
||||
self,
|
||||
messages,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
metadata: dict = None,
|
||||
filters: dict = None,
|
||||
infer: bool = True # ✅ NEW: Control memory inference
|
||||
) -> dict # Always returns dict with "results" key
|
||||
```
|
||||
|
||||
#### Changes Summary
|
||||
|
||||
| Parameter | v0.x | v1.0 Beta | Change |
|
||||
|-----------|------|-----------|---------|
|
||||
| `messages` | ✅ | ✅ | Unchanged |
|
||||
| `user_id` | ✅ | ✅ | Unchanged |
|
||||
| `agent_id` | ✅ | ✅ | Unchanged |
|
||||
| `run_id` | ✅ | ✅ | Unchanged |
|
||||
| `metadata` | ✅ | ✅ | Unchanged |
|
||||
| `filters` | ✅ | ✅ | Unchanged |
|
||||
| `output_format` | ✅ | ❌ | **REMOVED** |
|
||||
| `version` | ✅ | ❌ | **REMOVED** |
|
||||
| `async_mode` | ✅ | ❌ | **REMOVED** |
|
||||
| `infer` | ❌ | ✅ | **NEW** |
|
||||
|
||||
#### Response Format Changes
|
||||
|
||||
**v0.x Response (variable format):**
|
||||
```python
|
||||
# With output_format="v1.0"
|
||||
[
|
||||
{
|
||||
"id": "mem_123",
|
||||
"memory": "User loves pizza",
|
||||
"event": "ADD"
|
||||
}
|
||||
]
|
||||
|
||||
# With output_format="v1.1"
|
||||
{
|
||||
"results": [
|
||||
{
|
||||
"id": "mem_123",
|
||||
"memory": "User loves pizza",
|
||||
"event": "ADD"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
**v1.0 Beta Response (standardized):**
|
||||
```python
|
||||
# Always returns this format
|
||||
{
|
||||
"results": [
|
||||
{
|
||||
"id": "mem_123",
|
||||
"memory": "User loves pizza",
|
||||
"metadata": {...},
|
||||
"event": "ADD"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### search() Method
|
||||
|
||||
#### v0.x Signature
|
||||
```python
|
||||
def search(
|
||||
self,
|
||||
query: str,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
limit: int = 100,
|
||||
filters: dict = None, # Basic key-value only
|
||||
output_format: str = None, # ❌ REMOVED
|
||||
version: str = None # ❌ REMOVED
|
||||
) -> Union[List[dict], dict]
|
||||
```
|
||||
|
||||
#### v1.0 Beta Signature
|
||||
```python
|
||||
def search(
|
||||
self,
|
||||
query: str,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
limit: int = 100,
|
||||
filters: dict = None, # ✅ ENHANCED: Advanced operators
|
||||
rerank: bool = True # ✅ NEW: Reranking support
|
||||
) -> dict # Always returns dict with "results" key
|
||||
```
|
||||
|
||||
#### Enhanced Filtering
|
||||
|
||||
**v0.x Filters (basic):**
|
||||
```python
|
||||
# Simple key-value filtering only
|
||||
filters = {
|
||||
"category": "food",
|
||||
"user_id": "alice"
|
||||
}
|
||||
```
|
||||
|
||||
**v1.0 Beta Filters (enhanced):**
|
||||
```python
|
||||
# Advanced filtering with operators
|
||||
filters = {
|
||||
"AND": [
|
||||
{"category": "food"},
|
||||
{"score": {"gte": 0.8}},
|
||||
{
|
||||
"OR": [
|
||||
{"priority": "high"},
|
||||
{"urgent": True}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# Comparison operators
|
||||
filters = {
|
||||
"score": {"gt": 0.5}, # Greater than
|
||||
"priority": {"gte": 5}, # Greater than or equal
|
||||
"rating": {"lt": 3}, # Less than
|
||||
"confidence": {"lte": 0.9}, # Less than or equal
|
||||
"status": {"eq": "active"}, # Equal
|
||||
"archived": {"ne": True}, # Not equal
|
||||
"tags": {"in": ["work", "personal"]}, # In list
|
||||
"category": {"nin": ["spam", "deleted"]} # Not in list
|
||||
}
|
||||
```
|
||||
|
||||
### get_all() Method
|
||||
|
||||
#### v0.x Signature
|
||||
```python
|
||||
def get_all(
|
||||
self,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
filters: dict = None,
|
||||
output_format: str = None, # ❌ REMOVED
|
||||
version: str = None # ❌ REMOVED
|
||||
) -> Union[List[dict], dict]
|
||||
```
|
||||
|
||||
#### v1.0 Beta Signature
|
||||
```python
|
||||
def get_all(
|
||||
self,
|
||||
user_id: str = None,
|
||||
agent_id: str = None,
|
||||
run_id: str = None,
|
||||
filters: dict = None # ✅ ENHANCED: Advanced operators
|
||||
) -> dict # Always returns dict with "results" key
|
||||
```
|
||||
|
||||
### update() Method
|
||||
|
||||
#### No Breaking Changes
|
||||
```python
|
||||
# Same signature in both versions
|
||||
def update(
|
||||
self,
|
||||
memory_id: str,
|
||||
data: str
|
||||
) -> dict
|
||||
```
|
||||
|
||||
### delete() Method
|
||||
|
||||
#### No Breaking Changes
|
||||
```python
|
||||
# Same signature in both versions
|
||||
def delete(
|
||||
self,
|
||||
memory_id: str
|
||||
) -> dict
|
||||
```
|
||||
|
||||
### delete_all() Method
|
||||
|
||||
#### No Breaking Changes
|
||||
```python
|
||||
# Same signature in both versions
|
||||
def delete_all(
|
||||
self,
|
||||
user_id: str
|
||||
) -> dict
|
||||
```
|
||||
|
||||
## AsyncMemory Class Changes
|
||||
|
||||
### Enhanced Async Support
|
||||
|
||||
#### v0.x (Limited)
|
||||
```python
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
# Basic async support
|
||||
async_m = AsyncMemory()
|
||||
result = await async_m.add("content", user_id="alice", async_mode=True)
|
||||
```
|
||||
|
||||
#### v1.0 Beta (Optimized)
|
||||
```python
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
# Optimized async by default
|
||||
async_m = AsyncMemory()
|
||||
result = await async_m.add("content", user_id="alice") # async_mode removed
|
||||
|
||||
# All methods are now properly async-optimized
|
||||
results = await async_m.search("query", user_id="alice", rerank=True)
|
||||
```
|
||||
|
||||
## Configuration Changes
|
||||
|
||||
### Memory Configuration
|
||||
|
||||
#### v0.x Config Options
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {...},
|
||||
"llm": {...},
|
||||
"embedder": {...},
|
||||
"graph_store": {...},
|
||||
"version": "v1.0", # ❌ v1.0 no longer supported
|
||||
"history_db_path": "...",
|
||||
"custom_fact_extraction_prompt": "..."
|
||||
}
|
||||
```
|
||||
|
||||
#### v1.0 Beta Config Options
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {...},
|
||||
"llm": {...},
|
||||
"embedder": {...},
|
||||
"graph_store": {...},
|
||||
"reranker": { # ✅ NEW: Reranker support
|
||||
"provider": "cohere",
|
||||
"config": {...}
|
||||
},
|
||||
"version": "v1.1", # ✅ v1.1+ only
|
||||
"history_db_path": "...",
|
||||
"custom_fact_extraction_prompt": "...",
|
||||
"custom_update_memory_prompt": "..." # ✅ NEW: Custom update prompt
|
||||
}
|
||||
```
|
||||
|
||||
### New Configuration Options
|
||||
|
||||
#### Reranker Configuration
|
||||
```python
|
||||
# Cohere reranker
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-api-key",
|
||||
"top_k": 10
|
||||
}
|
||||
}
|
||||
|
||||
# Sentence Transformer reranker
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
|
||||
# Hugging Face reranker
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cuda"
|
||||
}
|
||||
}
|
||||
|
||||
# LLM-based reranker
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Error Handling Changes
|
||||
|
||||
### New Error Types
|
||||
|
||||
#### v0.x Errors
|
||||
```python
|
||||
# Generic exceptions
|
||||
try:
|
||||
result = m.add("content", user_id="alice", version="v1.0")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
```
|
||||
|
||||
#### v1.0 Beta Errors
|
||||
```python
|
||||
# More specific error handling
|
||||
try:
|
||||
result = m.add("content", user_id="alice")
|
||||
except ValueError as e:
|
||||
if "v1.0 API format is no longer supported" in str(e):
|
||||
# Handle version compatibility error
|
||||
pass
|
||||
elif "Invalid filter operator" in str(e):
|
||||
# Handle filter syntax error
|
||||
pass
|
||||
except TypeError as e:
|
||||
# Handle parameter errors
|
||||
pass
|
||||
except Exception as e:
|
||||
# Handle unexpected errors
|
||||
pass
|
||||
```
|
||||
|
||||
### Validation Changes
|
||||
|
||||
#### Stricter Parameter Validation
|
||||
|
||||
**v0.x (Lenient):**
|
||||
```python
|
||||
# Unknown parameters might be ignored
|
||||
result = m.add("content", user_id="alice", unknown_param="value")
|
||||
```
|
||||
|
||||
**v1.0 Beta (Strict):**
|
||||
```python
|
||||
# Unknown parameters raise TypeError
|
||||
try:
|
||||
result = m.add("content", user_id="alice", unknown_param="value")
|
||||
except TypeError as e:
|
||||
print(f"Invalid parameter: {e}")
|
||||
```
|
||||
|
||||
## Response Schema Changes
|
||||
|
||||
### Memory Object Schema
|
||||
|
||||
#### v0.x Schema
|
||||
```python
|
||||
{
|
||||
"id": "mem_123",
|
||||
"memory": "User loves pizza",
|
||||
"user_id": "alice",
|
||||
"metadata": {...},
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"updated_at": "2024-01-01T00:00:00Z",
|
||||
"score": 0.95 # In search results
|
||||
}
|
||||
```
|
||||
|
||||
#### v1.0 Beta Schema (Enhanced)
|
||||
```python
|
||||
{
|
||||
"id": "mem_123",
|
||||
"memory": "User loves pizza",
|
||||
"user_id": "alice",
|
||||
"agent_id": "assistant", # ✅ More context
|
||||
"run_id": "session_001", # ✅ More context
|
||||
"metadata": {...},
|
||||
"categories": ["food"], # ✅ NEW: Auto-categorization
|
||||
"immutable": false, # ✅ NEW: Immutability flag
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"updated_at": "2024-01-01T00:00:00Z",
|
||||
"score": 0.95, # In search results
|
||||
"rerank_score": 0.98 # ✅ NEW: If reranking used
|
||||
}
|
||||
```
|
||||
|
||||
## Migration Code Examples
|
||||
|
||||
### Simple Migration
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory()
|
||||
|
||||
# Add with deprecated parameters
|
||||
result = m.add(
|
||||
"I love pizza",
|
||||
user_id="alice",
|
||||
output_format="v1.1",
|
||||
version="v1.0"
|
||||
)
|
||||
|
||||
# Handle variable response format
|
||||
if isinstance(result, list):
|
||||
memories = result
|
||||
else:
|
||||
memories = result.get("results", [])
|
||||
|
||||
for memory in memories:
|
||||
print(memory["memory"])
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory()
|
||||
|
||||
# Add without deprecated parameters
|
||||
result = m.add(
|
||||
"I love pizza",
|
||||
user_id="alice"
|
||||
)
|
||||
|
||||
# Always dict format with "results" key
|
||||
for memory in result["results"]:
|
||||
print(memory["memory"])
|
||||
```
|
||||
|
||||
### Advanced Migration
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Basic filtering
|
||||
results = m.search(
|
||||
"food preferences",
|
||||
user_id="alice",
|
||||
filters={"category": "food"},
|
||||
output_format="v1.1"
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# Enhanced filtering with reranking
|
||||
results = m.search(
|
||||
"food preferences",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "food"},
|
||||
{"score": {"gte": 0.8}}
|
||||
]
|
||||
},
|
||||
rerank=True
|
||||
)
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
||||
| Component | v0.x | v1.0 Beta | Status |
|
||||
|-----------|------|-----------|---------|
|
||||
| `add()` method | Variable response | Standardized response | ⚠️ Breaking |
|
||||
| `search()` method | Basic filtering | Enhanced filtering + reranking | ⚠️ Breaking |
|
||||
| `get_all()` method | Variable response | Standardized response | ⚠️ Breaking |
|
||||
| Response format | Variable | Always `{"results": [...]}` | ⚠️ Breaking |
|
||||
| Reranking | ❌ Not available | ✅ Full support | ✅ New feature |
|
||||
| Advanced filtering | ❌ Basic only | ✅ Full operators | ✅ Enhancement |
|
||||
| Error handling | Generic | Specific error types | ✅ Improvement |
|
||||
|
||||
<Info>
|
||||
Use this reference to systematically update your codebase. Test each change thoroughly before deploying to production.
|
||||
</Info>
|
||||
@@ -0,0 +1,404 @@
|
||||
---
|
||||
title: Breaking Changes in v1.0 Beta
|
||||
description: 'Complete list of breaking changes when upgrading from v0.x to v1.0 Beta'
|
||||
icon: "triangle-exclamation"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
<Warning>
|
||||
**Important:** This page lists all breaking changes. Please review carefully before upgrading.
|
||||
</Warning>
|
||||
|
||||
## API Version Changes
|
||||
|
||||
### Removed v1.0 API Support
|
||||
|
||||
**Breaking Change:** The v1.0 API format is completely removed and no longer supported.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# This was supported in v0.x
|
||||
config = {
|
||||
"version": "v1.0" # ❌ No longer supported
|
||||
}
|
||||
|
||||
result = m.add(
|
||||
"memory content",
|
||||
user_id="alice",
|
||||
output_format="v1.0" # ❌ No longer supported
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# v1.1 is the minimum supported version
|
||||
config = {
|
||||
"version": "v1.1" # ✅ Required minimum
|
||||
}
|
||||
|
||||
result = m.add(
|
||||
"memory content",
|
||||
user_id="alice"
|
||||
# output_format parameter removed
|
||||
)
|
||||
```
|
||||
|
||||
**Error Message:**
|
||||
```
|
||||
ValueError: The v1.0 API format is no longer supported in mem0ai 1.0.0+.
|
||||
Please use v1.1 format which returns a dict with 'results' key.
|
||||
```
|
||||
|
||||
## Parameter Removals
|
||||
|
||||
### 1. output_format Parameter
|
||||
|
||||
**Removed from all methods:**
|
||||
- `add()`
|
||||
- `search()`
|
||||
- `get_all()`
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
result = m.add("content", user_id="alice", output_format="v1.1")
|
||||
search_results = m.search("query", user_id="alice", output_format="v1.1")
|
||||
all_memories = m.get_all(user_id="alice", output_format="v1.1")
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
result = m.add("content", user_id="alice")
|
||||
search_results = m.search("query", user_id="alice")
|
||||
all_memories = m.get_all(user_id="alice")
|
||||
```
|
||||
|
||||
### 2. version Parameter in Method Calls
|
||||
|
||||
**Breaking Change:** Version parameter removed from method calls.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
result = m.add("content", user_id="alice", version="v1.0")
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
result = m.add("content", user_id="alice")
|
||||
```
|
||||
|
||||
### 3. async_mode Parameter
|
||||
|
||||
**Breaking Change:** Async mode is now default and the parameter is removed.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Optional async mode
|
||||
result = m.add("content", user_id="alice", async_mode=True)
|
||||
result = m.add("content", user_id="alice", async_mode=False) # Sync mode
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# Always async by design, parameter removed
|
||||
result = m.add("content", user_id="alice")
|
||||
|
||||
# For async operations, use AsyncMemory
|
||||
from mem0 import AsyncMemory
|
||||
async_m = AsyncMemory()
|
||||
result = await async_m.add("content", user_id="alice")
|
||||
```
|
||||
|
||||
## Response Format Changes
|
||||
|
||||
### Standardized Response Structure
|
||||
|
||||
**Breaking Change:** All responses now return a standardized dictionary format.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Could return different formats based on output_format parameter
|
||||
result = m.add("content", user_id="alice", output_format="v1.0")
|
||||
# Returns: [{"id": "...", "memory": "...", "event": "ADD"}]
|
||||
|
||||
result = m.add("content", user_id="alice", output_format="v1.1")
|
||||
# Returns: {"results": [{"id": "...", "memory": "...", "event": "ADD"}]}
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# Always returns standardized format
|
||||
result = m.add("content", user_id="alice")
|
||||
# Always returns: {"results": [{"id": "...", "memory": "...", "event": "ADD"}]}
|
||||
|
||||
# Access results consistently
|
||||
for memory in result["results"]:
|
||||
print(memory["memory"])
|
||||
```
|
||||
|
||||
## Configuration Changes
|
||||
|
||||
### Version Configuration
|
||||
|
||||
**Breaking Change:** Default API version changed.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# v1.0 was supported
|
||||
config = {
|
||||
"version": "v1.0" # ❌ No longer supported
|
||||
}
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# v1.1 is minimum, v1.1 is default
|
||||
config = {
|
||||
"version": "v1.1" # ✅ Minimum supported
|
||||
}
|
||||
|
||||
# Or omit for default
|
||||
config = {
|
||||
# version defaults to v1.1
|
||||
}
|
||||
```
|
||||
|
||||
### Memory Configuration
|
||||
|
||||
**Breaking Change:** Some configuration options have changed defaults.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# Default configuration in v0.x
|
||||
m = Memory() # Used default settings suitable for v0.x
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# Default configuration optimized for v1.0 Beta
|
||||
m = Memory() # Uses v1.1+ optimized defaults
|
||||
|
||||
# Explicit configuration recommended
|
||||
config = {
|
||||
"version": "v1.1",
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
}
|
||||
}
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Method Signature Changes
|
||||
|
||||
### Search Method
|
||||
|
||||
**Enhanced but backward compatible:**
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
results = m.search(
|
||||
"query",
|
||||
user_id="alice",
|
||||
filters={"key": "value"} # Simple key-value only
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# Basic usage remains the same
|
||||
results = m.search("query", user_id="alice")
|
||||
|
||||
# Enhanced filtering available (optional)
|
||||
results = m.search(
|
||||
"query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"key": "value"},
|
||||
{"score": {"gte": 0.8}}
|
||||
]
|
||||
},
|
||||
rerank=True # New parameter
|
||||
)
|
||||
```
|
||||
|
||||
## Error Handling Changes
|
||||
|
||||
### New Error Types
|
||||
|
||||
**Breaking Change:** More specific error types and messages.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
try:
|
||||
result = m.add("content", user_id="alice", version="v1.0")
|
||||
except Exception as e:
|
||||
print(f"Generic error: {e}")
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
try:
|
||||
result = m.add("content", user_id="alice")
|
||||
except ValueError as e:
|
||||
if "v1.0 API format is no longer supported" in str(e):
|
||||
# Handle version error specifically
|
||||
print("Please upgrade your code to use v1.1+ format")
|
||||
else:
|
||||
print(f"Value error: {e}")
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {e}")
|
||||
```
|
||||
|
||||
### Validation Changes
|
||||
|
||||
**Breaking Change:** Stricter parameter validation.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Some invalid parameters might have been ignored
|
||||
result = m.add(
|
||||
"content",
|
||||
user_id="alice",
|
||||
invalid_param="ignored" # Might have been silently ignored
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
# Strict validation - unknown parameters cause errors
|
||||
try:
|
||||
result = m.add(
|
||||
"content",
|
||||
user_id="alice",
|
||||
invalid_param="value" # ❌ Will raise TypeError
|
||||
)
|
||||
except TypeError as e:
|
||||
print(f"Invalid parameter: {e}")
|
||||
```
|
||||
|
||||
## Import Changes
|
||||
|
||||
### No Breaking Changes in Imports
|
||||
|
||||
**Good News:** Import statements remain the same.
|
||||
|
||||
```python
|
||||
# These imports work in both v0.x and v1.0 Beta
|
||||
from mem0 import Memory, AsyncMemory
|
||||
from mem0 import MemoryConfig
|
||||
```
|
||||
|
||||
## Dependency Changes
|
||||
|
||||
### Minimum Python Version
|
||||
|
||||
**Potential Breaking Change:** Check Python version requirements.
|
||||
|
||||
#### Before (v0.x)
|
||||
- Python 3.8+ supported
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
- Python 3.9+ required (check current requirements)
|
||||
|
||||
### Package Dependencies
|
||||
|
||||
**Breaking Change:** Some dependencies updated with potential breaking changes.
|
||||
|
||||
```bash
|
||||
# Check for conflicts after upgrade
|
||||
pip install --upgrade mem0ai
|
||||
pip check # Verify no dependency conflicts
|
||||
```
|
||||
|
||||
## Data Migration
|
||||
|
||||
### Database Schema
|
||||
|
||||
**Good News:** No database schema changes required.
|
||||
|
||||
- Existing memories remain compatible
|
||||
- No data migration required
|
||||
- Vector store data unchanged
|
||||
|
||||
### Memory Format
|
||||
|
||||
**Good News:** Memory storage format unchanged.
|
||||
|
||||
- Existing memories work with v1.0 Beta
|
||||
- Search continues to work with old memories
|
||||
- No re-indexing required
|
||||
|
||||
## Testing Changes
|
||||
|
||||
### Test Updates Required
|
||||
|
||||
**Breaking Change:** Update tests for new response format.
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
def test_add_memory():
|
||||
result = m.add("content", user_id="alice")
|
||||
assert isinstance(result, list) # ❌ No longer true
|
||||
assert len(result) > 0
|
||||
```
|
||||
|
||||
#### After (v1.0 Beta)
|
||||
```python
|
||||
def test_add_memory():
|
||||
result = m.add("content", user_id="alice")
|
||||
assert isinstance(result, dict) # ✅ Always dict
|
||||
assert "results" in result # ✅ Always has results key
|
||||
assert len(result["results"]) > 0
|
||||
```
|
||||
|
||||
## Rollback Considerations
|
||||
|
||||
### Safe Rollback Process
|
||||
|
||||
If you need to rollback:
|
||||
|
||||
```bash
|
||||
# 1. Rollback package
|
||||
pip install mem0ai==0.1.20 # Last stable v0.x
|
||||
|
||||
# 2. Revert code changes
|
||||
git checkout previous_commit
|
||||
|
||||
# 3. Test functionality
|
||||
python test_mem0_functionality.py
|
||||
```
|
||||
|
||||
### Data Safety
|
||||
|
||||
- **Safe:** Memories stored in v0.x format work with v1.0 Beta
|
||||
- **Safe:** Rollback doesn't lose data
|
||||
- **Safe:** Vector store data remains intact
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Review all breaking changes** in your codebase
|
||||
2. **Update method calls** to remove deprecated parameters
|
||||
3. **Update response handling** to use standardized format
|
||||
4. **Test thoroughly** with your existing data
|
||||
5. **Update error handling** for new error types
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Migration Guide" icon="arrow-right" href="/migration/v0-to-v1">
|
||||
Step-by-step migration instructions
|
||||
</Card>
|
||||
<Card title="API Changes" icon="code" href="/migration/api-changes">
|
||||
Complete API reference changes
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<Warning>
|
||||
**Need Help?** If you encounter issues during migration, check our [GitHub Discussions](https://github.com/mem0ai/mem0/discussions) or community support channels.
|
||||
</Warning>
|
||||
@@ -0,0 +1,491 @@
|
||||
---
|
||||
title: Migrating from v0.x to v1.0 Beta
|
||||
description: 'Complete guide to upgrade your Mem0 implementation to version 1.0.0 Beta'
|
||||
icon: "arrow-right"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
<Warning>
|
||||
**Breaking Changes Ahead!** Mem0 1.0.0 Beta introduces several breaking changes. Please read this guide carefully before upgrading.
|
||||
</Warning>
|
||||
|
||||
## Overview
|
||||
|
||||
Mem0 1.0.0 Beta is a major release that modernizes the API, improves performance, and adds powerful new features. This guide will help you migrate your existing v0.x implementation to the new version.
|
||||
|
||||
## Key Changes Summary
|
||||
|
||||
| Feature | v0.x | v1.0.0 Beta | Migration Required |
|
||||
|---------|------|-------------|-------------------|
|
||||
| API Version | v1.0 supported | v1.0 **removed**, v1.1+ only | ✅ Yes |
|
||||
| Async Mode | Optional | Default and required | ✅ Yes |
|
||||
| Output Format Parameter | Supported | **Removed** | ✅ Yes |
|
||||
| Response Format | Mixed | Standardized `{"results": [...]}` | ✅ Yes |
|
||||
| Metadata Filtering | Basic | Enhanced with operators | ⚠️ Optional |
|
||||
| Reranking | Not available | Full support | ⚠️ Optional |
|
||||
|
||||
## Step-by-Step Migration
|
||||
|
||||
### 1. Update Installation
|
||||
|
||||
```bash
|
||||
# Update to the latest version
|
||||
pip install --upgrade mem0ai
|
||||
```
|
||||
|
||||
### 2. Remove Deprecated Parameters
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# These parameters are no longer supported
|
||||
m = Memory()
|
||||
result = m.add(
|
||||
"I love pizza",
|
||||
user_id="alice",
|
||||
output_format="v1.0", # ❌ REMOVED
|
||||
version="v1.0" # ❌ REMOVED
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# Clean, simplified API
|
||||
m = Memory()
|
||||
result = m.add(
|
||||
"I love pizza",
|
||||
user_id="alice"
|
||||
# output_format and version parameters removed
|
||||
)
|
||||
```
|
||||
|
||||
### 3. Update Configuration
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"version": "v1.0" # ❌ No longer supported
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"version": "v1.1" # ✅ v1.1 is the minimum supported version
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### 4. Handle Response Format Changes
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Response could be a list or dict depending on version
|
||||
result = m.add("I love coffee", user_id="alice")
|
||||
|
||||
if isinstance(result, list):
|
||||
# Handle list format
|
||||
for item in result:
|
||||
print(item["memory"])
|
||||
else:
|
||||
# Handle dict format
|
||||
print(result["results"])
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
# Response is always a standardized dict with "results" key
|
||||
result = m.add("I love coffee", user_id="alice")
|
||||
|
||||
# Always access via "results" key
|
||||
for item in result["results"]:
|
||||
print(item["memory"])
|
||||
```
|
||||
|
||||
### 5. Update Search Operations
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
# Basic search
|
||||
results = m.search("What do I like?", user_id="alice")
|
||||
|
||||
# With filters
|
||||
results = m.search(
|
||||
"What do I like?",
|
||||
user_id="alice",
|
||||
filters={"category": "food"}
|
||||
)
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
# Same basic search API
|
||||
results = m.search("What do I like?", user_id="alice")
|
||||
|
||||
# Enhanced filtering with operators (optional upgrade)
|
||||
results = m.search(
|
||||
"What do I like?",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "food"},
|
||||
{"rating": {"gte": 8}}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
# New: Reranking support (optional)
|
||||
results = m.search(
|
||||
"What do I like?",
|
||||
user_id="alice",
|
||||
rerank=True # Requires reranker configuration
|
||||
)
|
||||
```
|
||||
|
||||
### 6. Migrate Async Operations
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
# Async was optional
|
||||
async_memory = AsyncMemory()
|
||||
|
||||
async def add_memory():
|
||||
result = await async_memory.add(
|
||||
"I enjoy hiking",
|
||||
user_id="alice",
|
||||
async_mode=True # ❌ Parameter removed
|
||||
)
|
||||
return result
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
# Async is the default mode
|
||||
async_memory = AsyncMemory()
|
||||
|
||||
async def add_memory():
|
||||
result = await async_memory.add(
|
||||
"I enjoy hiking",
|
||||
user_id="alice"
|
||||
# async_mode parameter removed - always async
|
||||
)
|
||||
return result
|
||||
```
|
||||
|
||||
## Configuration Migration
|
||||
|
||||
### Basic Configuration
|
||||
|
||||
#### Before (v0.x)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-3.5-turbo",
|
||||
"api_key": "your-key"
|
||||
}
|
||||
},
|
||||
"version": "v1.0"
|
||||
}
|
||||
```
|
||||
|
||||
#### After (v1.0.0 Beta)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-3.5-turbo",
|
||||
"api_key": "your-key"
|
||||
}
|
||||
},
|
||||
"version": "v1.1", # Minimum supported version
|
||||
|
||||
# New optional features
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Enhanced Features (Optional)
|
||||
|
||||
```python
|
||||
# Take advantage of new features
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-key"
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-small",
|
||||
"api_key": "your-key"
|
||||
}
|
||||
},
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
|
||||
}
|
||||
},
|
||||
"version": "v1.1"
|
||||
}
|
||||
```
|
||||
|
||||
## Error Handling Migration
|
||||
|
||||
### Before (v0.x)
|
||||
```python
|
||||
try:
|
||||
result = m.add("memory", user_id="alice", version="v1.0")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
```
|
||||
|
||||
### After (v1.0.0 Beta)
|
||||
```python
|
||||
try:
|
||||
result = m.add("memory", user_id="alice")
|
||||
except ValueError as e:
|
||||
if "v1.0 API format is no longer supported" in str(e):
|
||||
print("Please upgrade your code to use v1.1+ format")
|
||||
else:
|
||||
print(f"Error: {e}")
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {e}")
|
||||
```
|
||||
|
||||
## Testing Your Migration
|
||||
|
||||
### 1. Basic Functionality Test
|
||||
|
||||
```python
|
||||
def test_basic_functionality():
|
||||
m = Memory()
|
||||
|
||||
# Test add
|
||||
result = m.add("I love testing", user_id="test_user")
|
||||
assert "results" in result
|
||||
assert len(result["results"]) > 0
|
||||
|
||||
# Test search
|
||||
search_results = m.search("testing", user_id="test_user")
|
||||
assert "results" in search_results
|
||||
|
||||
# Test get_all
|
||||
all_memories = m.get_all(user_id="test_user")
|
||||
assert "results" in all_memories
|
||||
|
||||
print("✅ Basic functionality test passed")
|
||||
|
||||
test_basic_functionality()
|
||||
```
|
||||
|
||||
### 2. Enhanced Features Test
|
||||
|
||||
```python
|
||||
def test_enhanced_features():
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Test reranking
|
||||
m.add("I love advanced features", user_id="test_user")
|
||||
results = m.search("features", user_id="test_user", rerank=True)
|
||||
assert "results" in results
|
||||
|
||||
# Test enhanced filtering
|
||||
results = m.search(
|
||||
"features",
|
||||
user_id="test_user",
|
||||
filters={"user_id": {"eq": "test_user"}}
|
||||
)
|
||||
assert "results" in results
|
||||
|
||||
print("✅ Enhanced features test passed")
|
||||
|
||||
test_enhanced_features()
|
||||
```
|
||||
|
||||
## Common Migration Issues
|
||||
|
||||
### Issue 1: Version Error
|
||||
|
||||
**Error:**
|
||||
```
|
||||
ValueError: The v1.0 API format is no longer supported in mem0ai 1.0.0+
|
||||
```
|
||||
|
||||
**Solution:**
|
||||
```python
|
||||
# Remove version parameters or set to v1.1+
|
||||
config = {
|
||||
# ... other config
|
||||
"version": "v1.1" # or remove entirely for default
|
||||
}
|
||||
```
|
||||
|
||||
### Issue 2: Response Format Error
|
||||
|
||||
**Error:**
|
||||
```
|
||||
KeyError: 'results'
|
||||
```
|
||||
|
||||
**Solution:**
|
||||
```python
|
||||
# Always access response via "results" key
|
||||
result = m.add("memory", user_id="alice")
|
||||
memories = result["results"] # Not result directly
|
||||
```
|
||||
|
||||
### Issue 3: Parameter Error
|
||||
|
||||
**Error:**
|
||||
```
|
||||
TypeError: add() got an unexpected keyword argument 'output_format'
|
||||
```
|
||||
|
||||
**Solution:**
|
||||
```python
|
||||
# Remove deprecated parameters
|
||||
result = m.add(
|
||||
"memory",
|
||||
user_id="alice"
|
||||
# Remove: output_format, version, async_mode
|
||||
)
|
||||
```
|
||||
|
||||
## Rollback Plan
|
||||
|
||||
If you encounter issues during migration:
|
||||
|
||||
### 1. Immediate Rollback
|
||||
|
||||
```bash
|
||||
# Downgrade to last v0.x version
|
||||
pip install mem0ai==0.1.20 # Replace with your last working version
|
||||
```
|
||||
|
||||
### 2. Gradual Migration
|
||||
|
||||
```python
|
||||
# Test both versions side by side
|
||||
import mem0_v0 # Your old version
|
||||
import mem0 # New version
|
||||
|
||||
def compare_results(query, user_id):
|
||||
old_results = mem0_v0.search(query, user_id=user_id)
|
||||
new_results = mem0.search(query, user_id=user_id)
|
||||
|
||||
print("Old format:", old_results)
|
||||
print("New format:", new_results["results"])
|
||||
```
|
||||
|
||||
## Performance Improvements
|
||||
|
||||
### Before (v0.x)
|
||||
```python
|
||||
# Sequential operations
|
||||
result1 = m.add("memory 1", user_id="alice")
|
||||
result2 = m.add("memory 2", user_id="alice")
|
||||
result3 = m.search("query", user_id="alice")
|
||||
```
|
||||
|
||||
### After (v1.0.0 Beta)
|
||||
```python
|
||||
# Better async performance
|
||||
async def batch_operations():
|
||||
async_memory = AsyncMemory()
|
||||
|
||||
# Concurrent operations
|
||||
results = await asyncio.gather(
|
||||
async_memory.add("memory 1", user_id="alice"),
|
||||
async_memory.add("memory 2", user_id="alice"),
|
||||
async_memory.search("query", user_id="alice")
|
||||
)
|
||||
return results
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Complete the migration** using this guide
|
||||
2. **Test thoroughly** with your existing data
|
||||
3. **Explore new features** like enhanced filtering and reranking
|
||||
4. **Update your documentation** to reflect the new API
|
||||
5. **Monitor performance** and optimize as needed
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<Card title="Breaking Changes" icon="triangle-exclamation" href="/migration/breaking-changes">
|
||||
Detailed list of all breaking changes
|
||||
</Card>
|
||||
<Card title="API Changes" icon="code" href="/migration/api-changes">
|
||||
Complete API reference changes
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
<Info>
|
||||
Need help with migration? Check our [GitHub Discussions](https://github.com/mem0ai/mem0/discussions) or reach out to our community for support.
|
||||
</Info>
|
||||
@@ -0,0 +1,389 @@
|
||||
---
|
||||
title: Enhanced Metadata Filtering
|
||||
description: 'Advanced filtering capabilities for precise memory retrieval in Mem0 1.0.0 Beta'
|
||||
icon: "filter"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
<Info>
|
||||
Enhanced metadata filtering is available in **Mem0 1.0.0 Beta** and later versions. This feature provides powerful filtering capabilities with logical operators and comparison functions.
|
||||
</Info>
|
||||
|
||||
## Overview
|
||||
|
||||
Mem0 1.0.0 Beta introduces enhanced metadata filtering that allows you to perform complex queries on your memory metadata. You can now use logical operators, comparison functions, and advanced filtering patterns to retrieve exactly the memories you need.
|
||||
|
||||
## Basic Filtering
|
||||
|
||||
### Simple Key-Value Filtering
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory()
|
||||
|
||||
# Search with simple metadata filters
|
||||
results = m.search(
|
||||
"What are my preferences?",
|
||||
user_id="alice",
|
||||
filters={"category": "preferences"}
|
||||
)
|
||||
```
|
||||
|
||||
### Exact Match Filtering
|
||||
|
||||
```python
|
||||
# Multiple exact match filters
|
||||
results = m.search(
|
||||
"movie recommendations",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"category": "entertainment",
|
||||
"type": "recommendation",
|
||||
"priority": "high"
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Advanced Filtering with Operators
|
||||
|
||||
### Comparison Operators
|
||||
|
||||
```python
|
||||
# Greater than / Less than
|
||||
results = m.search(
|
||||
"recent activities",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"score": {"gt": 0.8}, # score > 0.8
|
||||
"priority": {"gte": 5}, # priority >= 5
|
||||
"confidence": {"lt": 0.9}, # confidence < 0.9
|
||||
"rating": {"lte": 3} # rating <= 3
|
||||
}
|
||||
)
|
||||
|
||||
# Equality operators
|
||||
results = m.search(
|
||||
"specific content",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"status": {"eq": "active"}, # status == "active"
|
||||
"archived": {"ne": True} # archived != True
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### List-based Operators
|
||||
|
||||
```python
|
||||
# In / Not in operators
|
||||
results = m.search(
|
||||
"multi-category search",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"category": {"in": ["food", "travel", "entertainment"]},
|
||||
"status": {"nin": ["deleted", "archived"]}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### String Operators
|
||||
|
||||
```python
|
||||
# Text matching operators
|
||||
results = m.search(
|
||||
"content search",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"title": {"contains": "meeting"}, # case-sensitive contains
|
||||
"description": {"icontains": "important"}, # case-insensitive contains
|
||||
"tags": {"contains": "urgent"}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### Wildcard Matching
|
||||
|
||||
```python
|
||||
# Match any value for a field
|
||||
results = m.search(
|
||||
"all with category",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"category": "*" # Any memory that has a category field
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Logical Operators
|
||||
|
||||
### AND Operations
|
||||
|
||||
```python
|
||||
# Logical AND - all conditions must be true
|
||||
results = m.search(
|
||||
"complex query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "work"},
|
||||
{"priority": {"gte": 7}},
|
||||
{"status": {"ne": "completed"}}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### OR Operations
|
||||
|
||||
```python
|
||||
# Logical OR - any condition can be true
|
||||
results = m.search(
|
||||
"flexible query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"OR": [
|
||||
{"category": "urgent"},
|
||||
{"priority": {"gte": 9}},
|
||||
{"deadline": {"contains": "today"}}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### NOT Operations
|
||||
|
||||
```python
|
||||
# Logical NOT - exclude matches
|
||||
results = m.search(
|
||||
"exclusion query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"NOT": [
|
||||
{"category": "archived"},
|
||||
{"status": "deleted"}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### Complex Nested Logic
|
||||
|
||||
```python
|
||||
# Combine multiple logical operators
|
||||
results = m.search(
|
||||
"advanced query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{
|
||||
"OR": [
|
||||
{"category": "work"},
|
||||
{"category": "personal"}
|
||||
]
|
||||
},
|
||||
{"priority": {"gte": 5}},
|
||||
{
|
||||
"NOT": [
|
||||
{"status": "archived"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Real-world Examples
|
||||
|
||||
### Project Management Filtering
|
||||
|
||||
```python
|
||||
# Find high-priority active tasks
|
||||
results = m.search(
|
||||
"What tasks need attention?",
|
||||
user_id="project_manager",
|
||||
filters={
|
||||
"AND": [
|
||||
{"project": {"in": ["alpha", "beta"]}},
|
||||
{"priority": {"gte": 8}},
|
||||
{"status": {"ne": "completed"}},
|
||||
{
|
||||
"OR": [
|
||||
{"assignee": "alice"},
|
||||
{"assignee": "bob"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### Customer Support Filtering
|
||||
|
||||
```python
|
||||
# Find recent unresolved tickets
|
||||
results = m.search(
|
||||
"pending support issues",
|
||||
agent_id="support_bot",
|
||||
filters={
|
||||
"AND": [
|
||||
{"ticket_status": {"ne": "resolved"}},
|
||||
{"priority": {"in": ["high", "critical"]}},
|
||||
{"created_date": {"gte": "2024-01-01"}},
|
||||
{
|
||||
"NOT": [
|
||||
{"category": "spam"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### Content Recommendation Filtering
|
||||
|
||||
```python
|
||||
# Personalized content filtering
|
||||
results = m.search(
|
||||
"recommend content",
|
||||
user_id="reader123",
|
||||
filters={
|
||||
"AND": [
|
||||
{
|
||||
"OR": [
|
||||
{"genre": {"in": ["sci-fi", "fantasy"]}},
|
||||
{"author": {"contains": "favorite"}}
|
||||
]
|
||||
},
|
||||
{"rating": {"gte": 4.0}},
|
||||
{"read_status": {"ne": "completed"}},
|
||||
{"language": "english"}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### Indexing Strategy
|
||||
|
||||
```python
|
||||
# Ensure your vector store supports indexing on filtered fields
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333,
|
||||
# Enable indexing on frequently filtered fields
|
||||
"indexed_fields": ["category", "priority", "status", "user_id"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Filter Optimization
|
||||
|
||||
```python
|
||||
# More efficient: Filter on indexed fields first
|
||||
good_filters = {
|
||||
"AND": [
|
||||
{"user_id": "alice"}, # Indexed field first
|
||||
{"category": "work"}, # Then other indexed fields
|
||||
{"content": {"contains": "meeting"}} # Text search last
|
||||
]
|
||||
}
|
||||
|
||||
# Less efficient: Complex operations first
|
||||
avoid_filters = {
|
||||
"AND": [
|
||||
{"description": {"icontains": "complex text search"}}, # Expensive first
|
||||
{"user_id": "alice"} # Indexed field last
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Vector Store Compatibility
|
||||
|
||||
Different vector stores support different filtering capabilities:
|
||||
|
||||
### Qdrant
|
||||
- Full support for all operators
|
||||
- Efficient nested logical operations
|
||||
- Indexed field optimization
|
||||
|
||||
### Chroma
|
||||
- Basic operators (eq, ne, gt, lt, gte, lte)
|
||||
- Simple logical operations
|
||||
- Limited nested operations
|
||||
|
||||
### Pinecone
|
||||
- Good support for comparison operators
|
||||
- In/nin operations
|
||||
- Limited text operations
|
||||
|
||||
### Weaviate
|
||||
- Full operator support
|
||||
- Advanced text operations
|
||||
- Efficient filtering
|
||||
|
||||
## Error Handling
|
||||
|
||||
```python
|
||||
try:
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"invalid_operator": {"unknown": "value"}
|
||||
}
|
||||
)
|
||||
except ValueError as e:
|
||||
print(f"Filter error: {e}")
|
||||
# Fallback to simple filtering
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
filters={"category": "general"}
|
||||
)
|
||||
```
|
||||
|
||||
## Migration from Simple Filters
|
||||
|
||||
### Before (v0.x)
|
||||
```python
|
||||
# Simple key-value filtering only
|
||||
results = m.search(
|
||||
"query",
|
||||
user_id="alice",
|
||||
filters={"category": "work", "status": "active"}
|
||||
)
|
||||
```
|
||||
|
||||
### After (v1.0.0 Beta)
|
||||
```python
|
||||
# Enhanced filtering with operators
|
||||
results = m.search(
|
||||
"query",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "work"},
|
||||
{"status": {"ne": "archived"}},
|
||||
{"priority": {"gte": 5}}
|
||||
]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use Indexed Fields**: Filter on indexed fields for better performance
|
||||
2. **Combine Operators**: Use logical operators to create precise queries
|
||||
3. **Test Filter Performance**: Benchmark complex filters with your data
|
||||
4. **Graceful Degradation**: Implement fallbacks for unsupported operations
|
||||
5. **Validate Filters**: Check filter syntax before executing queries
|
||||
|
||||
<Info>
|
||||
Enhanced metadata filtering provides powerful capabilities for precise memory retrieval. Start with simple filters and gradually adopt more complex patterns as needed.
|
||||
</Info>
|
||||
@@ -44,14 +44,14 @@ Choose your preferred approach:
|
||||
|
||||
- **[Python Quickstart](../python-quickstart)**: Get started with Python SDK
|
||||
- **[Node.js Quickstart](../node-quickstart)**: Use Mem0 with Node.js/TypeScript
|
||||
- **[Examples](../examples)**: Explore real-world use cases and implementations
|
||||
- **[Examples](/examples)**: Explore real-world use cases and implementations
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Explore [specific features](./async-memory) in detail
|
||||
- Learn about [graph memory](../graph_memory/overview) capabilities
|
||||
- Set up [vector databases](../components/vectordbs/overview) and [LLM integrations](../components/llms/overview)
|
||||
- Check out our [examples](../examples) for practical implementations
|
||||
- Set up [vector databases](/components/vectordbs/overview) and [LLM integrations](/components/llms/overview)
|
||||
- Check out our [examples](/examples) for practical implementations
|
||||
- Join our [Discord community](https://mem0.dev/DiD) for support
|
||||
|
||||
We're excited to see what you'll build with Mem0 open-source. Let's create smarter, more personalized AI experiences together!
|
||||
|
||||
@@ -0,0 +1,418 @@
|
||||
---
|
||||
title: Reranker-Enhanced Search
|
||||
description: 'Improve search relevance with reranking models in Mem0 1.0.0 Beta'
|
||||
icon: "sort"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
<Info>
|
||||
Reranker-enhanced search is available in **Mem0 1.0.0 Beta** and later versions. This feature significantly improves search relevance by using specialized reranking models to reorder search results.
|
||||
</Info>
|
||||
|
||||
## Overview
|
||||
|
||||
Rerankers are specialized models that improve the quality of search results by reordering initially retrieved memories. They work as a second-stage ranking system that analyzes the semantic relationship between your query and retrieved memories to provide more relevant results.
|
||||
|
||||
## How Reranking Works
|
||||
|
||||
1. **Initial Vector Search**: Retrieves candidate memories using vector similarity
|
||||
2. **Reranking**: Specialized model analyzes query-memory relationships
|
||||
3. **Reordering**: Results are reordered based on semantic relevance
|
||||
4. **Enhanced Results**: Final results with improved relevance scores
|
||||
|
||||
## Configuration
|
||||
|
||||
### Basic Setup
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### Supported Providers
|
||||
|
||||
#### Cohere Reranker
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-api-key",
|
||||
"top_k": 10, # Number of results to rerank
|
||||
"return_documents": True
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Sentence Transformer Reranker
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cuda", # Use GPU if available
|
||||
"max_length": 512
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Hugging Face Reranker
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "BAAI/bge-reranker-base",
|
||||
"device": "cuda",
|
||||
"batch_size": 32
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### LLM-based Reranker
|
||||
|
||||
```python
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "llm_reranker",
|
||||
"config": {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-api-key"
|
||||
}
|
||||
},
|
||||
"top_k": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Reranked Search
|
||||
|
||||
```python
|
||||
# Reranking is enabled by default when configured
|
||||
results = m.search(
|
||||
"What are my food preferences?",
|
||||
user_id="alice"
|
||||
)
|
||||
|
||||
# Results are automatically reranked for better relevance
|
||||
for result in results["results"]:
|
||||
print(f"Memory: {result['memory']}")
|
||||
print(f"Score: {result['score']}")
|
||||
```
|
||||
|
||||
### Controlling Reranking
|
||||
|
||||
```python
|
||||
# Enable reranking explicitly
|
||||
results_with_rerank = m.search(
|
||||
"What movies do I like?",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
|
||||
# Disable reranking for this search
|
||||
results_without_rerank = m.search(
|
||||
"What movies do I like?",
|
||||
user_id="alice",
|
||||
rerank=False
|
||||
)
|
||||
|
||||
# Compare the difference in results
|
||||
print("With reranking:", len(results_with_rerank["results"]))
|
||||
print("Without reranking:", len(results_without_rerank["results"]))
|
||||
```
|
||||
|
||||
### Combining with Filters
|
||||
|
||||
```python
|
||||
# Reranking works with metadata filtering
|
||||
results = m.search(
|
||||
"important work tasks",
|
||||
user_id="alice",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "work"},
|
||||
{"priority": {"gte": 7}}
|
||||
]
|
||||
},
|
||||
rerank=True,
|
||||
limit=20
|
||||
)
|
||||
```
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
### Complete Configuration Example
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4",
|
||||
"api_key": "your-openai-api-key"
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-small",
|
||||
"api_key": "your-openai-api-key"
|
||||
}
|
||||
},
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-api-key",
|
||||
"top_k": 15,
|
||||
"return_documents": True
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
### Async Support
|
||||
|
||||
```python
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
# Reranking works with async operations
|
||||
async_memory = AsyncMemory.from_config(config)
|
||||
|
||||
async def search_with_rerank():
|
||||
results = await async_memory.search(
|
||||
"What are my preferences?",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
return results
|
||||
|
||||
# Use in async context
|
||||
import asyncio
|
||||
results = asyncio.run(search_with_rerank())
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### When to Use Reranking
|
||||
|
||||
✅ **Good Use Cases:**
|
||||
- Complex semantic queries
|
||||
- Domain-specific searches
|
||||
- When precision is more important than speed
|
||||
- Large memory collections
|
||||
- Ambiguous or nuanced queries
|
||||
|
||||
❌ **Avoid When:**
|
||||
- Simple keyword matching
|
||||
- Real-time applications with strict latency requirements
|
||||
- Small memory collections
|
||||
- High-frequency searches where cost matters
|
||||
|
||||
### Performance Optimization
|
||||
|
||||
```python
|
||||
# Optimize reranking performance
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
||||
"device": "cuda", # Use GPU
|
||||
"batch_size": 32, # Process in batches
|
||||
"top_k": 10, # Limit candidates
|
||||
"max_length": 256 # Reduce if appropriate
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Cost Management
|
||||
|
||||
```python
|
||||
# For API-based rerankers like Cohere
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-api-key",
|
||||
"top_k": 5, # Reduce to control API costs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Use reranking selectively
|
||||
def smart_search(query, user_id, use_rerank=None):
|
||||
# Automatically decide when to use reranking
|
||||
if use_rerank is None:
|
||||
use_rerank = len(query.split()) > 3 # Complex queries only
|
||||
|
||||
return m.search(query, user_id=user_id, rerank=use_rerank)
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
```python
|
||||
try:
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
rerank=True
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Reranking failed: {e}")
|
||||
# Gracefully fall back to vector search
|
||||
results = m.search(
|
||||
"test query",
|
||||
user_id="alice",
|
||||
rerank=False
|
||||
)
|
||||
```
|
||||
|
||||
## Reranker Comparison
|
||||
|
||||
| Provider | Latency | Quality | Cost | Local Deploy |
|
||||
|----------|---------|---------|------|--------------|
|
||||
| Cohere | Medium | High | API Cost | ❌ |
|
||||
| Sentence Transformer | Low | Good | Free | ✅ |
|
||||
| Hugging Face | Low-Medium | Variable | Free | ✅ |
|
||||
| LLM Reranker | High | Very High | API Cost | Depends |
|
||||
|
||||
## Real-world Examples
|
||||
|
||||
### Customer Support
|
||||
|
||||
```python
|
||||
# Improve support ticket relevance
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "cohere",
|
||||
"config": {
|
||||
"model": "rerank-english-v3.0",
|
||||
"api_key": "your-cohere-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Find relevant support cases
|
||||
results = m.search(
|
||||
"customer having login issues with mobile app",
|
||||
agent_id="support_bot",
|
||||
filters={"category": "technical_support"},
|
||||
rerank=True
|
||||
)
|
||||
```
|
||||
|
||||
### Content Recommendation
|
||||
|
||||
```python
|
||||
# Better content matching
|
||||
results = m.search(
|
||||
"science fiction books with space exploration themes",
|
||||
user_id="reader123",
|
||||
filters={"content_type": "book_recommendation"},
|
||||
rerank=True,
|
||||
limit=10
|
||||
)
|
||||
|
||||
for result in results["results"]:
|
||||
print(f"Recommendation: {result['memory']}")
|
||||
print(f"Relevance: {result['score']:.3f}")
|
||||
```
|
||||
|
||||
### Personal Assistant
|
||||
|
||||
```python
|
||||
# Enhanced personal queries
|
||||
results = m.search(
|
||||
"What restaurants did I enjoy last month that had good vegetarian options?",
|
||||
user_id="foodie_user",
|
||||
filters={
|
||||
"AND": [
|
||||
{"category": "dining"},
|
||||
{"rating": {"gte": 4}},
|
||||
{"date": {"gte": "2024-01-01"}}
|
||||
]
|
||||
},
|
||||
rerank=True
|
||||
)
|
||||
```
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### From v0.x (No Reranking)
|
||||
|
||||
```python
|
||||
# v0.x - basic vector search
|
||||
results = m.search("query", user_id="alice")
|
||||
```
|
||||
|
||||
### To v1.0.0 Beta (With Reranking)
|
||||
|
||||
```python
|
||||
# Add reranker configuration
|
||||
config = {
|
||||
"reranker": {
|
||||
"provider": "sentence_transformer",
|
||||
"config": {
|
||||
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Same search API, better results
|
||||
results = m.search("query", user_id="alice") # Automatically reranked
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start Simple**: Begin with Sentence Transformers for local deployment
|
||||
2. **Monitor Performance**: Track both relevance improvements and latency
|
||||
3. **Cost Awareness**: Use API-based rerankers judiciously
|
||||
4. **Selective Usage**: Apply reranking where it provides the most value
|
||||
5. **Fallback Strategy**: Always handle reranking failures gracefully
|
||||
6. **Test Different Models**: Experiment to find the best fit for your domain
|
||||
|
||||
<Info>
|
||||
Reranker-enhanced search significantly improves result relevance. Start with a local model and upgrade to API-based solutions as your needs grow.
|
||||
</Info>
|
||||
@@ -285,7 +285,7 @@ m = Memory.from_config(config_dict=config)
|
||||
|
||||
- For issues related to authentication, refer to the [boto3 client configuration options](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html).
|
||||
|
||||
- For more details on how to connect, configure, and use the graph_memory graph store, see the [Neptune Analytics example notebook](examples/graph-db-demo/neptune-analytics-example.ipynb).
|
||||
- For more details on how to connect, configure, and use the graph_memory graph store, see the Neptune Analytics example in our [AWS example guide](/examples/aws_example#aws-bedrock-and-aoss).
|
||||
|
||||
### Initialize Kuzu
|
||||
|
||||
|
||||
@@ -9,34 +9,34 @@ Learn about the key features and capabilities that make Mem0 a powerful platform
|
||||
## Core Features
|
||||
|
||||
<CardGroup>
|
||||
<Card title="Advanced Retrieval" icon="magnifying-glass" href="advanced-retrieval">
|
||||
<Card title="Advanced Retrieval" icon="magnifying-glass" href="/platform/features/advanced-retrieval">
|
||||
Superior search results using state-of-the-art algorithms, including keyword search, reranking, and filtering capabilities.
|
||||
</Card>
|
||||
<Card title="Contextual Add" icon="square-plus" href="contextual-add">
|
||||
<Card title="Contextual Add" icon="square-plus" href="/platform/features/contextual-add">
|
||||
Only send your latest conversation history - we automatically retrieve the rest and generate properly contextualized memories.
|
||||
</Card>
|
||||
<Card title="Multimodal Support" icon="photo-film" href="multimodal-support">
|
||||
<Card title="Multimodal Support" icon="photo-film" href="/platform/features/multimodal-support">
|
||||
Process and analyze various types of content including images.
|
||||
</Card>
|
||||
<Card title="Memory Customization" icon="filter" href="selective-memory">
|
||||
<Card title="Memory Customization" icon="filter" href="/platform/features/selective-memory">
|
||||
Customize and curate stored memories to focus on relevant information while excluding unnecessary data, enabling improved accuracy, privacy control, and resource efficiency.
|
||||
</Card>
|
||||
<Card title="Custom Categories" icon="tags" href="custom-categories">
|
||||
<Card title="Custom Categories" icon="tags" href="/platform/features/custom-categories">
|
||||
Create and manage custom categories to organize memories based on your specific needs and requirements.
|
||||
</Card>
|
||||
<Card title="Custom Instructions" icon="list-check" href="custom-instructions">
|
||||
<Card title="Custom Instructions" icon="list-check" href="/platform/features/custom-instructions">
|
||||
Define specific guidelines for your project to ensure consistent handling of information and requirements.
|
||||
</Card>
|
||||
<Card title="Direct Import" icon="message-bot" href="direct-import">
|
||||
<Card title="Direct Import" icon="message-bot" href="/platform/features/direct-import">
|
||||
Tailor the behavior of your Mem0 instance with custom prompts for specific use cases or domains.
|
||||
</Card>
|
||||
<Card title="Async Client" icon="bolt" href="async-client">
|
||||
<Card title="Async Client" icon="bolt" href="/platform/features/async-client">
|
||||
Asynchronous client for non-blocking operations and high concurrency applications.
|
||||
</Card>
|
||||
<Card title="Memory Export" icon="file-export" href="memory-export">
|
||||
<Card title="Memory Export" icon="file-export" href="/platform/features/memory-export">
|
||||
Export memories in structured formats using customizable Pydantic schemas.
|
||||
</Card>
|
||||
<Card title="Graph Memory" icon="circle-nodes" href="graph-memory">
|
||||
<Card title="Graph Memory" icon="circle-nodes" href="/platform/features/graph-memory">
|
||||
Add memories in the form of nodes and edges in a graph database and search for related memories.
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
title: Configurations
|
||||
icon: "gear"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
|
||||
Config in mem0 is a dictionary that specifies the settings for your embedding models. It allows you to customize the behavior and connection details of your chosen embedder.
|
||||
|
||||
## How to define configurations?
|
||||
|
||||
The config is defined as an object (or dictionary) with two main keys:
|
||||
- `embedder`: Specifies the embedder provider and its configuration
|
||||
- `provider`: The name of the embedder (e.g., "openai", "ollama")
|
||||
- `config`: A nested object or dictionary containing provider-specific settings
|
||||
|
||||
|
||||
## How to use configurations?
|
||||
|
||||
Here's a general example of how to use the config with mem0:
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "your_chosen_provider",
|
||||
"config": {
|
||||
# Provider-specific settings go here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("Your text here", user_id="user", metadata={"category": "example"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'openai',
|
||||
config: {
|
||||
apiKey: process.env.OPENAI_API_KEY || '',
|
||||
model: 'text-embedding-3-small',
|
||||
// Provider-specific settings go here
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
await memory.add("Your text here", { userId: "user", metadata: { category: "example" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Why is Config Needed?
|
||||
|
||||
Config is essential for:
|
||||
1. Specifying which embedding model to use.
|
||||
2. Providing necessary connection details (e.g., model, api_key, embedding_dims).
|
||||
3. Ensuring proper initialization and connection to your chosen embedder.
|
||||
|
||||
## Master List of All Params in Config
|
||||
|
||||
Here's a comprehensive list of all parameters that can be used across different embedders:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Provider |
|
||||
|-----------|-------------|----------|
|
||||
| `model` | Embedding model to use | All |
|
||||
| `api_key` | API key of the provider | All |
|
||||
| `embedding_dims` | Dimensions of the embedding model | All |
|
||||
| `http_client_proxies` | Allow proxy server settings | All |
|
||||
| `ollama_base_url` | Base URL for the Ollama embedding model | Ollama |
|
||||
| `model_kwargs` | Key-Value arguments for the Huggingface embedding model | Huggingface |
|
||||
| `azure_kwargs` | Key-Value arguments for the AzureOpenAI embedding model | Azure OpenAI |
|
||||
| `openai_base_url` | Base URL for OpenAI API | OpenAI |
|
||||
| `vertex_credentials_json` | Path to the Google Cloud credentials JSON file for VertexAI | VertexAI |
|
||||
| `memory_add_embedding_type` | The type of embedding to use for the add memory action | VertexAI |
|
||||
| `memory_update_embedding_type` | The type of embedding to use for the update memory action | VertexAI |
|
||||
| `memory_search_embedding_type` | The type of embedding to use for the search memory action | VertexAI |
|
||||
| `lmstudio_base_url` | Base URL for LM Studio API | LM Studio |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Provider |
|
||||
|-----------|-------------|----------|
|
||||
| `model` | Embedding model to use | All |
|
||||
| `apiKey` | API key of the provider | All |
|
||||
| `embeddingDims` | Dimensions of the embedding model | All |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Supported Embedding Models
|
||||
|
||||
For detailed information on configuring specific embedders, please visit the [Embedding Models](./models) section. There you'll find information for each supported embedder with provider-specific usage examples and configuration details.
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: AWS Bedrock
|
||||
---
|
||||
|
||||
To use AWS Bedrock embedding models, you need to have the appropriate AWS credentials and permissions. The embeddings implementation relies on the `boto3` library.
|
||||
|
||||
### Setup
|
||||
- Ensure you have model access from the [AWS Bedrock Console](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess)
|
||||
- Authenticate the boto3 client using a method described in the [AWS documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html)
|
||||
- Set up environment variables for authentication:
|
||||
```bash
|
||||
export AWS_REGION=us-east-1
|
||||
export AWS_ACCESS_KEY_ID=your-access-key
|
||||
export AWS_SECRET_ACCESS_KEY=your-secret-key
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
# For LLM if needed
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
|
||||
# AWS credentials
|
||||
os.environ["AWS_REGION"] = "us-west-2"
|
||||
os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key"
|
||||
os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-key"
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "amazon.titan-embed-text-v2:0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice")
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring AWS Bedrock embedder:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `amazon.titan-embed-text-v1` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,125 @@
|
||||
---
|
||||
title: Azure OpenAI
|
||||
---
|
||||
|
||||
To use Azure OpenAI embedding models, set the `EMBEDDING_AZURE_OPENAI_API_KEY`, `EMBEDDING_AZURE_DEPLOYMENT`, `EMBEDDING_AZURE_ENDPOINT` and `EMBEDDING_AZURE_API_VERSION` environment variables. You can obtain the Azure OpenAI API key from the Azure.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["EMBEDDING_AZURE_OPENAI_API_KEY"] = "your-api-key"
|
||||
os.environ["EMBEDDING_AZURE_DEPLOYMENT"] = "your-deployment-name"
|
||||
os.environ["EMBEDDING_AZURE_ENDPOINT"] = "your-api-base-url"
|
||||
os.environ["EMBEDDING_AZURE_API_VERSION"] = "version-to-use"
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "azure_openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-large"
|
||||
"azure_kwargs": {
|
||||
"api_version": "",
|
||||
"azure_deployment": "",
|
||||
"azure_endpoint": "",
|
||||
"api_key": "",
|
||||
"default_headers": {
|
||||
"CustomHeader": "your-custom-header",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: "azure_openai",
|
||||
config: {
|
||||
model: "text-embedding-3-large",
|
||||
modelProperties: {
|
||||
endpoint: "your-api-base-url",
|
||||
deployment: "your-deployment-name",
|
||||
apiVersion: "version-to-use",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const memory = new Memory(config);
|
||||
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
|
||||
await memory.add(messages, { userId: "john" });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with [Azure OpenAI role-based security](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/role-based-access-control).
|
||||
|
||||
<Note> If an API key is provided, it will be used for authentication over an Azure Identity </Note>
|
||||
|
||||
Below is a sample configuration for using Mem0 with Azure OpenAI and Azure Identity:
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
# You can set the values directly in the config dictionary or use environment variables
|
||||
|
||||
os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name"
|
||||
os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url"
|
||||
os.environ["LLM_AZURE_API_VERSION"] = "version-to-use"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "azure_openai_structured",
|
||||
"config": {
|
||||
"model": "your-deployment-name",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
"azure_kwargs": {
|
||||
"azure_deployment": "<your-deployment-name>",
|
||||
"api_version": "<version-to-use>",
|
||||
"azure_endpoint": "<your-api-base-url>",
|
||||
"default_headers": {
|
||||
"CustomHeader": "your-custom-header",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Refer to [Azure Identity troubleshooting tips](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues) for setting up an Azure Identity credential.
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Azure OpenAI embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `azure_kwargs` | The Azure OpenAI configs | `config_keys` |
|
||||
@@ -0,0 +1,69 @@
|
||||
---
|
||||
title: Google AI
|
||||
---
|
||||
|
||||
To use Google AI embedding models, set the `GOOGLE_API_KEY` environment variables. You can obtain the Gemini API key from [here](https://aistudio.google.com/app/apikey).
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["GOOGLE_API_KEY"] = "key"
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "gemini",
|
||||
"config": {
|
||||
"model": "models/text-embedding-004",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'google',
|
||||
config: {
|
||||
apiKey: process.env.GOOGLE_API_KEY || '',
|
||||
model: 'text-embedding-004',
|
||||
// The output dimensionality is fixed at 768 for Google AI embeddings
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "john" });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Gemini embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `models/text-embedding-004` |
|
||||
| `embedding_dims` | Dimensions of the embedding model (output_dimensionality will be considered as embedding_dims, so please set embedding_dims accordingly) | `768` |
|
||||
| `api_key` | The Google API key | `None` |
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
title: Hugging Face
|
||||
---
|
||||
|
||||
You can use embedding models from Huggingface to run Mem0 locally.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "multi-qa-MiniLM-L6-cos-v1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
### Using Text Embeddings Inference (TEI)
|
||||
|
||||
You can also use Hugging Face's Text Embeddings Inference service for faster and more efficient embeddings:
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
# Using HuggingFace Text Embeddings Inference API
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"huggingface_base_url": "http://localhost:3000/v1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("This text will be embedded using the TEI service.", user_id="john")
|
||||
```
|
||||
|
||||
To run the TEI service, you can use Docker:
|
||||
|
||||
```bash
|
||||
docker run -d -p 3000:80 -v huggingfacetei:/data --platform linux/amd64 \
|
||||
ghcr.io/huggingface/text-embeddings-inference:cpu-1.6 \
|
||||
--model-id BAAI/bge-small-en-v1.5
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Huggingface embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the model to use | `multi-qa-MiniLM-L6-cos-v1` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `selected_model_dimensions` |
|
||||
| `model_kwargs` | Additional arguments for the model | `None` |
|
||||
| `huggingface_base_url` | URL to connect to Text Embeddings Inference (TEI) API | `None` |
|
||||
@@ -0,0 +1,196 @@
|
||||
---
|
||||
title: LangChain
|
||||
---
|
||||
|
||||
Mem0 supports LangChain as a provider to access a wide range of embedding models. LangChain is a framework for developing applications powered by language models, making it easy to integrate various embedding providers through a consistent interface.
|
||||
|
||||
For a complete list of available embedding models supported by LangChain, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/).
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
|
||||
# Set necessary environment variables for your chosen LangChain provider
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
# Initialize a LangChain embeddings model directly
|
||||
openai_embeddings = OpenAIEmbeddings(
|
||||
model="text-embedding-3-small",
|
||||
dimensions=1536
|
||||
)
|
||||
|
||||
# Pass the initialized model to the config
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "langchain",
|
||||
"config": {
|
||||
"model": openai_embeddings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
import { OpenAIEmbeddings } from "@langchain/openai";
|
||||
|
||||
// Initialize a LangChain embeddings model directly
|
||||
const openaiEmbeddings = new OpenAIEmbeddings({
|
||||
modelName: "text-embedding-3-small",
|
||||
dimensions: 1536,
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'langchain',
|
||||
config: {
|
||||
model: openaiEmbeddings,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Supported LangChain Embedding Providers
|
||||
|
||||
LangChain supports a wide range of embedding providers, including:
|
||||
|
||||
- OpenAI (`OpenAIEmbeddings`)
|
||||
- Cohere (`CohereEmbeddings`)
|
||||
- Google (`VertexAIEmbeddings`)
|
||||
- Hugging Face (`HuggingFaceEmbeddings`)
|
||||
- Sentence Transformers (`HuggingFaceEmbeddings`)
|
||||
- Azure OpenAI (`AzureOpenAIEmbeddings`)
|
||||
- Ollama (`OllamaEmbeddings`)
|
||||
- Together (`TogetherEmbeddings`)
|
||||
- And many more
|
||||
|
||||
You can use any of these model instances directly in your configuration. For a complete and up-to-date list of available embedding providers, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/).
|
||||
|
||||
## Provider-Specific Configuration
|
||||
|
||||
When using LangChain as an embedder provider, you'll need to:
|
||||
|
||||
1. Set the appropriate environment variables for your chosen embedding provider
|
||||
2. Import and initialize the specific model class you want to use
|
||||
3. Pass the initialized model instance to the config
|
||||
|
||||
### Examples with Different Providers
|
||||
|
||||
<CodeGroup>
|
||||
#### HuggingFace Embeddings
|
||||
|
||||
```python Python
|
||||
from langchain_huggingface import HuggingFaceEmbeddings
|
||||
|
||||
# Initialize a HuggingFace embeddings model
|
||||
hf_embeddings = HuggingFaceEmbeddings(
|
||||
model_name="BAAI/bge-small-en-v1.5",
|
||||
encode_kwargs={"normalize_embeddings": True}
|
||||
)
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "langchain",
|
||||
"config": {
|
||||
"model": hf_embeddings
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
import { HuggingFaceEmbeddings } from "@langchain/community/embeddings/hf";
|
||||
|
||||
// Initialize a HuggingFace embeddings model
|
||||
const hfEmbeddings = new HuggingFaceEmbeddings({
|
||||
modelName: "BAAI/bge-small-en-v1.5",
|
||||
encode: {
|
||||
normalize_embeddings: true,
|
||||
},
|
||||
});
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'langchain',
|
||||
config: {
|
||||
model: hfEmbeddings,
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
<CodeGroup>
|
||||
#### Ollama Embeddings
|
||||
|
||||
```python Python
|
||||
from langchain_ollama import OllamaEmbeddings
|
||||
|
||||
# Initialize an Ollama embeddings model
|
||||
ollama_embeddings = OllamaEmbeddings(
|
||||
model="nomic-embed-text"
|
||||
)
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "langchain",
|
||||
"config": {
|
||||
"model": ollama_embeddings
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
|
||||
|
||||
// Initialize an Ollama embeddings model
|
||||
const ollamaEmbeddings = new OllamaEmbeddings({
|
||||
model: "nomic-embed-text",
|
||||
baseUrl: "http://localhost:11434", // Ollama server URL
|
||||
});
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'langchain',
|
||||
config: {
|
||||
model: ollamaEmbeddings,
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
<Note>
|
||||
Make sure to install the necessary LangChain packages and any provider-specific dependencies.
|
||||
</Note>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `langchain` embedder config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,38 @@
|
||||
You can use embedding models from LM Studio to run Mem0 locally.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "lmstudio",
|
||||
"config": {
|
||||
"model": "nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Ollama embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the OpenAI model to use | `nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `lmstudio_base_url` | Base URL for LM Studio connection | `http://localhost:1234/v1` |
|
||||
@@ -0,0 +1,73 @@
|
||||
You can use embedding models from Ollama to run Mem0 locally.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "ollama",
|
||||
"config": {
|
||||
"model": "mxbai-embed-large"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'ollama',
|
||||
config: {
|
||||
model: 'nomic-embed-text:latest', // or any other Ollama embedding model
|
||||
url: 'http://localhost:11434', // Ollama server URL
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "john" });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Ollama embedder:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the Ollama model to use | `nomic-embed-text` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `512` |
|
||||
| `ollama_base_url` | Base URL for ollama connection | `None` |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the Ollama model to use | `nomic-embed-text:latest` |
|
||||
| `url` | Base URL for Ollama server | `http://localhost:11434` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
title: OpenAI
|
||||
---
|
||||
|
||||
To use OpenAI embedding models, set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys).
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key"
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-large"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
embedder: {
|
||||
provider: 'openai',
|
||||
config: {
|
||||
apiKey: 'your-openai-api-key',
|
||||
model: 'text-embedding-3-large',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
await memory.add("I'm visiting Paris", { userId: "john" });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring OpenAI embedder:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `api_key` | The OpenAI API key | `None` |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
|
||||
| `embeddingDims` | Dimensions of the embedding model | `1536` |
|
||||
| `apiKey` | The OpenAI API key | `None` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: Together
|
||||
---
|
||||
|
||||
To use Together embedding models, set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from the [Together Platform](https://api.together.xyz/settings/api-keys).
|
||||
|
||||
### Usage
|
||||
|
||||
<Note> The `embedding_model_dims` parameter for `vector_store` should be set to `768` for Together embedder. </Note>
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["TOGETHER_API_KEY"] = "your_api_key"
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "together",
|
||||
"config": {
|
||||
"model": "togethercomputer/m2-bert-80M-8k-retrieval"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Together embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `model` | The name of the embedding model to use | `togethercomputer/m2-bert-80M-8k-retrieval` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `768` |
|
||||
| `api_key` | The Together API key | `None` |
|
||||
@@ -0,0 +1,55 @@
|
||||
### Vertex AI
|
||||
|
||||
To use Google Cloud's Vertex AI for text embedding models, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to the path of your service account's credentials JSON file. These credentials can be created in the [Google Cloud Console](https://console.cloud.google.com/).
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
# Set the path to your Google Cloud credentials JSON file
|
||||
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your/credentials.json"
|
||||
os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "vertexai",
|
||||
"config": {
|
||||
"model": "text-embedding-004",
|
||||
"memory_add_embedding_type": "RETRIEVAL_DOCUMENT",
|
||||
"memory_update_embedding_type": "RETRIEVAL_DOCUMENT",
|
||||
"memory_search_embedding_type": "RETRIEVAL_QUERY"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="john")
|
||||
```
|
||||
The embedding types can be one of the following:
|
||||
- SEMANTIC_SIMILARITY
|
||||
- CLASSIFICATION
|
||||
- CLUSTERING
|
||||
- RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, QUESTION_ANSWERING, FACT_VERIFICATION
|
||||
- CODE_RETRIEVAL_QUERY
|
||||
Check out the [Vertex AI documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/task-types#supported_task_types) for more information.
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring the Vertex AI embedder:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| ------------------------- | ------------------------------------------------ | -------------------- |
|
||||
| `model` | The name of the Vertex AI embedding model to use | `text-embedding-004` |
|
||||
| `vertex_credentials_json` | Path to the Google Cloud credentials JSON file | `None` |
|
||||
| `embedding_dims` | Dimensions of the embedding model | `256` |
|
||||
| `memory_add_embedding_type` | The type of embedding to use for the add memory action | `RETRIEVAL_DOCUMENT` |
|
||||
| `memory_update_embedding_type` | The type of embedding to use for the update memory action | `RETRIEVAL_DOCUMENT` |
|
||||
| `memory_search_embedding_type` | The type of embedding to use for the search memory action | `RETRIEVAL_QUERY` |
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: Overview
|
||||
icon: "info"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
Mem0 offers support for various embedding models, allowing users to choose the one that best suits their needs.
|
||||
|
||||
## Supported Embedders
|
||||
|
||||
See the list of supported embedders below.
|
||||
|
||||
<Note>
|
||||
The following embedders are supported in the Python implementation. The TypeScript implementation currently only supports OpenAI.
|
||||
</Note>
|
||||
|
||||
<CardGroup cols={4}>
|
||||
<Card title="OpenAI" href="/components/embedders/models/openai"></Card>
|
||||
<Card title="Azure OpenAI" href="/components/embedders/models/azure_openai"></Card>
|
||||
<Card title="Ollama" href="/components/embedders/models/ollama"></Card>
|
||||
<Card title="Hugging Face" href="/components/embedders/models/huggingface"></Card>
|
||||
<Card title="Google AI" href="/components/embedders/models/google_AI"></Card>
|
||||
<Card title="Vertex AI" href="/components/embedders/models/vertexai"></Card>
|
||||
<Card title="Together" href="/components/embedders/models/together"></Card>
|
||||
<Card title="LM Studio" href="/components/embedders/models/lmstudio"></Card>
|
||||
<Card title="Langchain" href="/components/embedders/models/langchain"></Card>
|
||||
<Card title="AWS Bedrock" href="/components/embedders/models/aws_bedrock"></Card>
|
||||
</CardGroup>
|
||||
|
||||
## Usage
|
||||
|
||||
To utilize a embedder, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the embedder.
|
||||
|
||||
For a comprehensive list of available parameters for embedder configuration, please refer to [Config](./config).
|
||||
@@ -0,0 +1,137 @@
|
||||
---
|
||||
title: Configurations
|
||||
icon: "gear"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## How to define configurations?
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
The `config` is defined as a Python dictionary with two main keys:
|
||||
- `llm`: Specifies the llm provider and its configuration
|
||||
- `provider`: The name of the llm (e.g., "openai", "groq")
|
||||
- `config`: A nested dictionary containing provider-specific settings
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
The `config` is defined as a TypeScript object with these keys:
|
||||
- `llm`: Specifies the LLM provider and its configuration (required)
|
||||
- `provider`: The name of the LLM (e.g., "openai", "groq")
|
||||
- `config`: A nested object containing provider-specific settings
|
||||
- `embedder`: Specifies the embedder provider and its configuration (optional)
|
||||
- `vectorStore`: Specifies the vector store provider and its configuration (optional)
|
||||
- `historyDbPath`: Path to the history database file (optional)
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
### Config Values Precedence
|
||||
|
||||
Config values are applied in the following order of precedence (from highest to lowest):
|
||||
|
||||
1. Values explicitly set in the `config` object/dictionary
|
||||
2. Environment variables (e.g., `OPENAI_API_KEY`, `OPENAI_BASE_URL`)
|
||||
3. Default values defined in the LLM implementation
|
||||
|
||||
This means that values specified in the `config` will override corresponding environment variables, which in turn override default values.
|
||||
|
||||
## How to Use Config
|
||||
|
||||
Here's a general example of how to use the config with Mem0:
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx" # for embedder
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "your_chosen_provider",
|
||||
"config": {
|
||||
# Provider-specific settings go here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("Your text here", user_id="user", metadata={"category": "example"})
|
||||
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
// Minimal configuration with just the LLM settings
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'your_chosen_provider',
|
||||
config: {
|
||||
// Provider-specific settings go here
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
await memory.add("Your text here", { userId: "user123", metadata: { category: "example" } });
|
||||
```
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
## Why is Config Needed?
|
||||
|
||||
Config is essential for:
|
||||
1. Specifying which LLM to use.
|
||||
2. Providing necessary connection details (e.g., model, api_key, temperature).
|
||||
3. Ensuring proper initialization and connection to your chosen LLM.
|
||||
|
||||
## Master List of All Params in Config
|
||||
|
||||
Here's a comprehensive list of all parameters that can be used across different LLMs:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Provider |
|
||||
|----------------------|-----------------------------------------------|-------------------|
|
||||
| `model` | Embedding model to use | All |
|
||||
| `temperature` | Temperature of the model | All |
|
||||
| `api_key` | API key to use | All |
|
||||
| `max_tokens` | Tokens to generate | All |
|
||||
| `top_p` | Probability threshold for nucleus sampling | All |
|
||||
| `top_k` | Number of highest probability tokens to keep | All |
|
||||
| `http_client_proxies`| Allow proxy server settings | AzureOpenAI |
|
||||
| `models` | List of models | Openrouter |
|
||||
| `route` | Routing strategy | Openrouter |
|
||||
| `openrouter_base_url`| Base URL for Openrouter API | Openrouter |
|
||||
| `site_url` | Site URL | Openrouter |
|
||||
| `app_name` | Application name | Openrouter |
|
||||
| `ollama_base_url` | Base URL for Ollama API | Ollama |
|
||||
| `openai_base_url` | Base URL for OpenAI API | OpenAI |
|
||||
| `azure_kwargs` | Azure LLM args for initialization | AzureOpenAI |
|
||||
| `deepseek_base_url` | Base URL for DeepSeek API | DeepSeek |
|
||||
| `xai_base_url` | Base URL for XAI API | XAI |
|
||||
| `sarvam_base_url` | Base URL for Sarvam API | Sarvam |
|
||||
| `reasoning_effort` | Reasoning level (low, medium, high) | Sarvam |
|
||||
| `frequency_penalty` | Penalize frequent tokens (-2.0 to 2.0) | Sarvam |
|
||||
| `presence_penalty` | Penalize existing tokens (-2.0 to 2.0) | Sarvam |
|
||||
| `seed` | Seed for deterministic sampling | Sarvam |
|
||||
| `stop` | Stop sequences (max 4) | Sarvam |
|
||||
| `lmstudio_base_url` | Base URL for LM Studio API | LM Studio |
|
||||
| `response_callback` | LLM response callback function | OpenAI |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Provider |
|
||||
|----------------------|-----------------------------------------------|-------------------|
|
||||
| `model` | Embedding model to use | All |
|
||||
| `temperature` | Temperature of the model | All |
|
||||
| `apiKey` | API key to use | All |
|
||||
| `maxTokens` | Tokens to generate | All |
|
||||
| `topP` | Probability threshold for nucleus sampling | All |
|
||||
| `topK` | Number of highest probability tokens to keep | All |
|
||||
| `openaiBaseUrl` | Base URL for OpenAI API | OpenAI |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Supported LLMs
|
||||
|
||||
For detailed information on configuring specific LLMs, please visit the [LLMs](./models) section. There you'll find information for each supported LLM with provider-specific usage examples and configuration details.
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
title: Anthropic
|
||||
---
|
||||
|
||||
|
||||
To use Anthropic's models, please set the `ANTHROPIC_API_KEY` which you find on their [Account Settings Page](https://console.anthropic.com/account/keys).
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "anthropic",
|
||||
"config": {
|
||||
"model": "claude-sonnet-4-20250514",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'anthropic',
|
||||
config: {
|
||||
apiKey: process.env.ANTHROPIC_API_KEY || '',
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
temperature: 0.1,
|
||||
maxTokens: 2000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `anthropic` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
title: AWS Bedrock
|
||||
---
|
||||
|
||||
### Setup
|
||||
- Before using the AWS Bedrock LLM, make sure you have the appropriate model access from [Bedrock Console](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess).
|
||||
- You will also need to authenticate the `boto3` client by using a method in the [AWS documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#configuring-credentials)
|
||||
- You will have to export `AWS_REGION`, `AWS_ACCESS_KEY`, and `AWS_SECRET_ACCESS_KEY` to set environment variables.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ['AWS_REGION'] = 'us-west-2'
|
||||
os.environ["AWS_ACCESS_KEY_ID"] = "xx"
|
||||
os.environ["AWS_SECRET_ACCESS_KEY"] = "xx"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "anthropic.claude-3-5-haiku-20241022-v1:0",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
All available parameters for the `aws_bedrock` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,161 @@
|
||||
---
|
||||
title: Azure OpenAI
|
||||
---
|
||||
|
||||
<Note> Mem0 Now Supports Azure OpenAI Models in TypeScript SDK </Note>
|
||||
|
||||
To use Azure OpenAI models, you have to set the `LLM_AZURE_OPENAI_API_KEY`, `LLM_AZURE_ENDPOINT`, `LLM_AZURE_DEPLOYMENT` and `LLM_AZURE_API_VERSION` environment variables. You can obtain the Azure API key from the [Azure](https://azure.microsoft.com/).
|
||||
|
||||
Optionally, you can use Azure Identity to authenticate with Azure OpenAI, which allows you to use managed identities or service principals for production and Azure CLI login for development instead of an API key. If an Azure Identity is to be used, ***do not*** set the `LLM_AZURE_OPENAI_API_KEY` environment variable or the api_key in the config dictionary.
|
||||
|
||||
> **Note**: The following are currently unsupported with reasoning models `Parallel tool calling`,`temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `logprobs`, `top_logprobs`, `logit_bias`, `max_tokens`
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
|
||||
os.environ["LLM_AZURE_OPENAI_API_KEY"] = "your-api-key"
|
||||
os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name"
|
||||
os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url"
|
||||
os.environ["LLM_AZURE_API_VERSION"] = "version-to-use"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "azure_openai",
|
||||
"config": {
|
||||
"model": "your-deployment-name",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
"azure_kwargs": {
|
||||
"azure_deployment": "",
|
||||
"api_version": "",
|
||||
"azure_endpoint": "",
|
||||
"api_key": "",
|
||||
"default_headers": {
|
||||
"CustomHeader": "your-custom-header",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'azure_openai',
|
||||
config: {
|
||||
apiKey: process.env.AZURE_OPENAI_API_KEY || '',
|
||||
modelProperties: {
|
||||
endpoint: 'https://your-api-base-url',
|
||||
deployment: 'your-deployment-name',
|
||||
modelName: 'your-model-name',
|
||||
apiVersion: 'version-to-use',
|
||||
// Any other parameters you want to pass to the Azure OpenAI API
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
|
||||
We also support the new [OpenAI structured-outputs](https://platform.openai.com/docs/guides/structured-outputs/introduction) model. Typescript SDK does not support the `azure_openai_structured` model yet.
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["LLM_AZURE_OPENAI_API_KEY"] = "your-api-key"
|
||||
os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name"
|
||||
os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url"
|
||||
os.environ["LLM_AZURE_API_VERSION"] = "version-to-use"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "azure_openai_structured",
|
||||
"config": {
|
||||
"model": "your-deployment-name",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
"azure_kwargs": {
|
||||
"azure_deployment": "",
|
||||
"api_version": "",
|
||||
"azure_endpoint": "",
|
||||
"api_key": "",
|
||||
"default_headers": {
|
||||
"CustomHeader": "your-custom-header",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with [Azure OpenAI role-based security](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/role-based-access-control).
|
||||
|
||||
<Note> If an API key is provided, it will be used for authentication over an Azure Identity </Note>
|
||||
|
||||
Below is a sample configuration for using Mem0 with Azure OpenAI and Azure Identity:
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
# You can set the values directly in the config dictionary or use environment variables
|
||||
|
||||
os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name"
|
||||
os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url"
|
||||
os.environ["LLM_AZURE_API_VERSION"] = "version-to-use"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "azure_openai_structured",
|
||||
"config": {
|
||||
"model": "your-deployment-name",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
"azure_kwargs": {
|
||||
"azure_deployment": "<your-deployment-name>",
|
||||
"api_version": "<version-to-use>",
|
||||
"azure_endpoint": "<your-api-base-url>",
|
||||
"default_headers": {
|
||||
"CustomHeader": "your-custom-header",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Refer to [Azure Identity troubleshooting tips](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues) for setting up an Azure Identity credential.
|
||||
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `azure_openai` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: DeepSeek
|
||||
---
|
||||
|
||||
To use DeepSeek LLM models, you have to set the `DEEPSEEK_API_KEY` environment variable. You can also optionally set `DEEPSEEK_API_BASE` if you need to use a different API endpoint (defaults to "https://api.deepseek.com").
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["DEEPSEEK_API_KEY"] = "your-api-key"
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # for embedder model
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "deepseek",
|
||||
"config": {
|
||||
"model": "deepseek-chat", # default model
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
"top_p": 1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
You can also configure the API base URL in the config:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "deepseek",
|
||||
"config": {
|
||||
"model": "deepseek-chat",
|
||||
"deepseek_base_url": "https://your-custom-endpoint.com",
|
||||
"api_key": "your-api-key" # alternatively to using environment variable
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `deepseek` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
title: Google AI
|
||||
---
|
||||
|
||||
To use the Gemini model, set the `GOOGLE_API_KEY` environment variable. You can obtain the Google/Gemini API key from [Google AI Studio](https://aistudio.google.com/app/apikey).
|
||||
|
||||
> **Note:** As of the latest release, Mem0 uses the new `google.genai` SDK instead of the deprecated `google.generativeai`. All message formatting and model interaction now use the updated `types` module from `google.genai`.
|
||||
|
||||
> **Note:** Some Gemini models are being deprecated and will retire soon. It is recommended to migrate to the latest stable models like `"gemini-2.0-flash-001"` or `"gemini-2.0-flash-lite-001"` to ensure ongoing support and improvements.
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # Used for embedding model
|
||||
os.environ["GOOGLE_API_KEY"] = "your-gemini-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "gemini",
|
||||
"config": {
|
||||
"model": "gemini-2.0-flash-001",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
"top_p": 1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thrillers, but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thrillers and suggest sci-fi movies instead."}
|
||||
]
|
||||
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
|
||||
```
|
||||
```typescript TypeScript
|
||||
import { Memory } from "mem0ai/oss";
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
// You can also use "google" as provider ( for backward compatibility )
|
||||
provider: "gemini",
|
||||
config: {
|
||||
model: "gemini-2.0-flash-001",
|
||||
temperature: 0.1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const memory = new Memory(config);
|
||||
|
||||
const messages = [
|
||||
{ role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
|
||||
{ role: "assistant", content: "How about thriller movies? They can be quite engaging." },
|
||||
{ role: "user", content: "I’m not a big fan of thrillers, but I love sci-fi movies." },
|
||||
{ role: "assistant", content: "Got it! I'll avoid thrillers and suggest sci-fi movies instead." }
|
||||
]
|
||||
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `Gemini` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: Groq
|
||||
---
|
||||
|
||||
[Groq](https://groq.com/) is the creator of the world's first Language Processing Unit (LPU), providing exceptional speed performance for AI workloads running on their LPU Inference Engine.
|
||||
|
||||
In order to use LLMs from Groq, go to their [platform](https://console.groq.com/keys) and get the API key. Set the API key as `GROQ_API_KEY` environment variable to use the model as given below in the example.
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["GROQ_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "groq",
|
||||
"config": {
|
||||
"model": "mixtral-8x7b-32768",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'groq',
|
||||
config: {
|
||||
apiKey: process.env.GROQ_API_KEY || '',
|
||||
model: 'mixtral-8x7b-32768',
|
||||
temperature: 0.1,
|
||||
maxTokens: 1000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `groq` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
title: LangChain
|
||||
---
|
||||
|
||||
|
||||
Mem0 supports LangChain as a provider to access a wide range of LLM models. LangChain is a framework for developing applications powered by language models, making it easy to integrate various LLM providers through a consistent interface.
|
||||
|
||||
For a complete list of available chat models supported by LangChain, refer to the [LangChain Chat Models documentation](https://python.langchain.com/docs/integrations/chat).
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
# Set necessary environment variables for your chosen LangChain provider
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
# Initialize a LangChain model directly
|
||||
openai_model = ChatOpenAI(
|
||||
model="gpt-4o",
|
||||
temperature=0.2,
|
||||
max_tokens=2000
|
||||
)
|
||||
|
||||
# Pass the initialized model to the config
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "langchain",
|
||||
"config": {
|
||||
"model": openai_model
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
import { ChatOpenAI } from "@langchain/openai";
|
||||
|
||||
// Initialize a LangChain model directly
|
||||
const openaiModel = new ChatOpenAI({
|
||||
modelName: "gpt-4",
|
||||
temperature: 0.2,
|
||||
maxTokens: 2000,
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'langchain',
|
||||
config: {
|
||||
model: openaiModel,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Supported LangChain Providers
|
||||
|
||||
LangChain supports a wide range of LLM providers, including:
|
||||
|
||||
- OpenAI (`ChatOpenAI`)
|
||||
- Anthropic (`ChatAnthropic`)
|
||||
- Google (`ChatGoogleGenerativeAI`, `ChatGooglePalm`)
|
||||
- Mistral (`ChatMistralAI`)
|
||||
- Ollama (`ChatOllama`)
|
||||
- Azure OpenAI (`AzureChatOpenAI`)
|
||||
- HuggingFace (`HuggingFaceChatEndpoint`)
|
||||
- And many more
|
||||
|
||||
You can use any of these model instances directly in your configuration. For a complete and up-to-date list of available providers, refer to the [LangChain Chat Models documentation](https://python.langchain.com/docs/integrations/chat).
|
||||
|
||||
## Provider-Specific Configuration
|
||||
|
||||
When using LangChain as a provider, you'll need to:
|
||||
|
||||
1. Set the appropriate environment variables for your chosen LLM provider
|
||||
2. Import and initialize the specific model class you want to use
|
||||
3. Pass the initialized model instance to the config
|
||||
|
||||
<Note>
|
||||
Make sure to install the necessary LangChain packages and any provider-specific dependencies.
|
||||
</Note>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `langchain` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,34 @@
|
||||
[Litellm](https://litellm.vercel.app/docs/) is compatible with over 100 large language models (LLMs), all using a standardized input/output format. You can explore the [available models](https://litellm.vercel.app/docs/providers) to use with Litellm. Ensure you set the `API_KEY` for the model you choose to use.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "litellm",
|
||||
"config": {
|
||||
"model": "gpt-4o-mini",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `litellm` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
title: LM Studio
|
||||
---
|
||||
|
||||
To use LM Studio with Mem0, you'll need to have LM Studio running locally with its server enabled. LM Studio provides a way to run local LLMs with an OpenAI-compatible API.
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "lmstudio",
|
||||
"config": {
|
||||
"model": "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
"lmstudio_base_url": "http://localhost:1234/v1", # default LM Studio API URL
|
||||
"lmstudio_response_format": {"type": "json_schema", "json_schema": {"type": "object", "schema": {}}},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Running Completely Locally
|
||||
|
||||
You can also use LM Studio for both LLM and embedding to run Mem0 entirely locally:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
# No external API keys needed!
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "lmstudio"
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "lmstudio"
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice123", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
<Note>
|
||||
When using LM Studio for both LLM and embedding, make sure you have:
|
||||
1. An LLM model loaded for generating responses
|
||||
2. An embedding model loaded for vector embeddings
|
||||
3. The server enabled with the correct endpoints accessible
|
||||
</Note>
|
||||
|
||||
<Note>
|
||||
To use LM Studio, you need to:
|
||||
1. Download and install [LM Studio](https://lmstudio.ai/)
|
||||
2. Start a local server from the "Server" tab
|
||||
3. Set the appropriate `lmstudio_base_url` in your configuration (default is usually http://localhost:1234/v1)
|
||||
</Note>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `lmstudio` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
title: Mistral AI
|
||||
---
|
||||
|
||||
To use mistral's models, please obtain the Mistral AI api key from their [console](https://console.mistral.ai/). Set the `MISTRAL_API_KEY` environment variable to use the model as given below in the example.
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["MISTRAL_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "litellm",
|
||||
"config": {
|
||||
"model": "open-mixtral-8x7b",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'mistral',
|
||||
config: {
|
||||
apiKey: process.env.MISTRAL_API_KEY || '',
|
||||
model: 'mistral-tiny-latest', // Or 'mistral-small-latest', 'mistral-medium-latest', etc.
|
||||
temperature: 0.1,
|
||||
maxTokens: 2000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `litellm` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,60 @@
|
||||
You can use LLMs from Ollama to run Mem0 locally. These [models](https://ollama.com/search?c=tools) support tool support.
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # for embedder
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "ollama",
|
||||
"config": {
|
||||
"model": "mixtral:8x7b",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'ollama',
|
||||
config: {
|
||||
model: 'llama3.1:8b', // or any other Ollama model
|
||||
url: 'http://localhost:11434', // Ollama server URL
|
||||
temperature: 0.1,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `ollama` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: OpenAI
|
||||
---
|
||||
|
||||
To use OpenAI LLM models, you have to set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys).
|
||||
|
||||
> **Note**: The following are currently unsupported with reasoning models `Parallel tool calling`,`temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `logprobs`, `top_logprobs`, `logit_bias`, `max_tokens`
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "gpt-4o",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Use Openrouter by passing it's api key
|
||||
# os.environ["OPENROUTER_API_KEY"] = "your-api-key"
|
||||
# config = {
|
||||
# "llm": {
|
||||
# "provider": "openai",
|
||||
# "config": {
|
||||
# "model": "meta-llama/llama-3.1-70b-instruct",
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
llm: {
|
||||
provider: 'openai',
|
||||
config: {
|
||||
apiKey: process.env.OPENAI_API_KEY || '',
|
||||
model: 'gpt-4-turbo-preview',
|
||||
temperature: 0.2,
|
||||
maxTokens: 1500,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
We also support the new [OpenAI structured-outputs](https://platform.openai.com/docs/guides/structured-outputs/introduction) model.
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "openai_structured",
|
||||
"config": {
|
||||
"model": "gpt-4o-2024-08-06",
|
||||
"temperature": 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `openai` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Sarvam AI
|
||||
---
|
||||
|
||||
**Sarvam AI** is an Indian AI company developing language models with a focus on Indian languages and cultural context. Their latest model **Sarvam-M** is designed to understand and generate content in multiple Indian languages while maintaining high performance in English.
|
||||
|
||||
To use Sarvam AI's models, please set the `SARVAM_API_KEY` which you can get from their [platform](https://dashboard.sarvam.ai/).
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["SARVAM_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "sarvam",
|
||||
"config": {
|
||||
"model": "sarvam-m",
|
||||
"temperature": 0.7,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alex")
|
||||
```
|
||||
|
||||
## Advanced Usage with Sarvam-Specific Features
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "sarvam",
|
||||
"config": {
|
||||
"model": {
|
||||
"name": "sarvam-m",
|
||||
"reasoning_effort": "high", # Enable advanced reasoning
|
||||
"frequency_penalty": 0.1, # Reduce repetition
|
||||
"seed": 42 # For deterministic outputs
|
||||
},
|
||||
"temperature": 0.3,
|
||||
"max_tokens": 2000,
|
||||
"api_key": "your-sarvam-api-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Example with Hindi conversation
|
||||
messages = [
|
||||
{"role": "user", "content": "मैं SBI में joint account खोलना चाहता हूँ।"},
|
||||
{"role": "assistant", "content": "SBI में joint account खोलने के लिए आपको कुछ documents की जरूरत होगी। क्या आप जानना चाहते हैं कि कौन से documents चाहिए?"}
|
||||
]
|
||||
m.add(messages, user_id="rajesh", metadata={"language": "hindi", "topic": "banking"})
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `sarvam` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,35 @@
|
||||
To use TogetherAI LLM models, you have to set the `TOGETHER_API_KEY` environment variable. You can obtain the TogetherAI API key from their [Account settings page](https://api.together.xyz/settings/api-keys).
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["TOGETHER_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "together",
|
||||
"config": {
|
||||
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `togetherai` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,107 @@
|
||||
---
|
||||
title: vLLM
|
||||
---
|
||||
|
||||
[vLLM](https://docs.vllm.ai/) is a high-performance inference engine for large language models that provides significant performance improvements for local inference. It's designed to maximize throughput and memory efficiency for serving LLMs.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **Install vLLM**:
|
||||
|
||||
```bash
|
||||
pip install vllm
|
||||
```
|
||||
|
||||
2. **Start vLLM server**:
|
||||
|
||||
```bash
|
||||
# For testing with a small model
|
||||
vllm serve microsoft/DialoGPT-medium --port 8000
|
||||
|
||||
# For production with a larger model (requires GPU)
|
||||
vllm serve Qwen/Qwen2.5-32B-Instruct --port 8000
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "vllm",
|
||||
"config": {
|
||||
"model": "Qwen/Qwen2.5-32B-Instruct",
|
||||
"vllm_base_url": "http://localhost:8000/v1",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thrillers, but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thrillers and suggest sci-fi movies instead."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Configuration Parameters
|
||||
|
||||
| Parameter | Description | Default | Environment Variable |
|
||||
| --------------- | --------------------------------- | ----------------------------- | -------------------- |
|
||||
| `model` | Model name running on vLLM server | `"Qwen/Qwen2.5-32B-Instruct"` | - |
|
||||
| `vllm_base_url` | vLLM server URL | `"http://localhost:8000/v1"` | `VLLM_BASE_URL` |
|
||||
| `api_key` | API key (dummy for local) | `"vllm-api-key"` | `VLLM_API_KEY` |
|
||||
| `temperature` | Sampling temperature | `0.1` | - |
|
||||
| `max_tokens` | Maximum tokens to generate | `2000` | - |
|
||||
|
||||
## Environment Variables
|
||||
|
||||
You can set these environment variables instead of specifying them in config:
|
||||
|
||||
```bash
|
||||
export VLLM_BASE_URL="http://localhost:8000/v1"
|
||||
export VLLM_API_KEY="your-vllm-api-key"
|
||||
export OPENAI_API_KEY="your-openai-api-key" # for embeddings
|
||||
```
|
||||
|
||||
## Benefits
|
||||
|
||||
- **High Performance**: 2-24x faster inference than standard implementations
|
||||
- **Memory Efficient**: Optimized memory usage with PagedAttention
|
||||
- **Local Deployment**: Keep your data private and reduce API costs
|
||||
- **Easy Integration**: Drop-in replacement for other LLM providers
|
||||
- **Flexible**: Works with any model supported by vLLM
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
1. **Server not responding**: Make sure vLLM server is running
|
||||
|
||||
```bash
|
||||
curl http://localhost:8000/health
|
||||
```
|
||||
|
||||
2. **404 errors**: Ensure correct base URL format
|
||||
|
||||
```python
|
||||
"vllm_base_url": "http://localhost:8000/v1" # Note the /v1
|
||||
```
|
||||
|
||||
3. **Model not found**: Check model name matches server
|
||||
|
||||
4. **Out of memory**: Try smaller models or reduce `max_model_len`
|
||||
|
||||
```bash
|
||||
vllm serve Qwen/Qwen2.5-32B-Instruct --max-model-len 4096
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `vllm` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
title: xAI
|
||||
---
|
||||
|
||||
[xAI](https://x.ai/) is a new AI company founded by Elon Musk that develops large language models, including Grok. Grok is trained on real-time data from X (formerly Twitter) and aims to provide accurate, up-to-date responses with a touch of wit and humor.
|
||||
|
||||
In order to use LLMs from xAI, go to their [platform](https://console.x.ai) and get the API key. Set the API key as `XAI_API_KEY` environment variable to use the model as given below in the example.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model
|
||||
os.environ["XAI_API_KEY"] = "your-api-key"
|
||||
|
||||
config = {
|
||||
"llm": {
|
||||
"provider": "xai",
|
||||
"config": {
|
||||
"model": "grok-3-beta",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `xai` config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
title: Overview
|
||||
icon: "info"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
Mem0 includes built-in support for various popular large language models. Memory can utilize the LLM provided by the user, ensuring efficient use for specific needs.
|
||||
|
||||
## Usage
|
||||
|
||||
To use a llm, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the llm.
|
||||
|
||||
For a comprehensive list of available parameters for llm configuration, please refer to [Config](./config).
|
||||
|
||||
## Supported LLMs
|
||||
|
||||
See the list of supported LLMs below.
|
||||
|
||||
<Note>
|
||||
All LLMs are supported in Python. The following LLMs are also supported in TypeScript: **OpenAI**, **Anthropic**, and **Groq**.
|
||||
</Note>
|
||||
|
||||
<CardGroup cols={4}>
|
||||
<Card title="OpenAI" href="/components/llms/models/openai" />
|
||||
<Card title="Ollama" href="/components/llms/models/ollama" />
|
||||
<Card title="Azure OpenAI" href="/components/llms/models/azure_openai" />
|
||||
<Card title="Anthropic" href="/components/llms/models/anthropic" />
|
||||
<Card title="Together" href="/components/llms/models/together" />
|
||||
<Card title="Groq" href="/components/llms/models/groq" />
|
||||
<Card title="Litellm" href="/components/llms/models/litellm" />
|
||||
<Card title="Mistral AI" href="/components/llms/models/mistral_AI" />
|
||||
<Card title="Google AI" href="/components/llms/models/google_AI" />
|
||||
<Card title="AWS bedrock" href="/components/llms/models/aws_bedrock" />
|
||||
<Card title="DeepSeek" href="/components/llms/models/deepseek" />
|
||||
<Card title="xAI" href="/components/llms/models/xAI" />
|
||||
<Card title="Sarvam AI" href="/components/llms/models/sarvam" />
|
||||
<Card title="LM Studio" href="/components/llms/models/lmstudio" />
|
||||
<Card title="Langchain" href="/components/llms/models/langchain" />
|
||||
</CardGroup>
|
||||
|
||||
## Structured vs Unstructured Outputs
|
||||
|
||||
Mem0 supports two types of OpenAI LLM formats, each with its own strengths and use cases:
|
||||
|
||||
### Structured Outputs
|
||||
|
||||
Structured outputs are LLMs that align with OpenAI's structured outputs model:
|
||||
|
||||
- **Optimized for:** Returning structured responses (e.g., JSON objects)
|
||||
- **Benefits:** Precise, easily parseable data
|
||||
- **Ideal for:** Data extraction, form filling, API responses
|
||||
- **Learn more:** [OpenAI Structured Outputs Guide](https://platform.openai.com/docs/guides/structured-outputs/introduction)
|
||||
|
||||
### Unstructured Outputs
|
||||
|
||||
Unstructured outputs correspond to OpenAI's standard, free-form text model:
|
||||
|
||||
- **Flexibility:** Returns open-ended, natural language responses
|
||||
- **Customization:** Use the `response_format` parameter to guide output
|
||||
- **Trade-off:** Less efficient than structured outputs for specific data needs
|
||||
- **Best for:** Creative writing, explanations, general conversation
|
||||
|
||||
Choose the format that best suits your application's requirements for optimal performance and usability.
|
||||
@@ -0,0 +1,128 @@
|
||||
---
|
||||
title: Configurations
|
||||
icon: "gear"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## How to define configurations?
|
||||
|
||||
The `config` is defined as an object with two main keys:
|
||||
- `vector_store`: Specifies the vector database provider and its configuration
|
||||
- `provider`: The name of the vector database (e.g., "chroma", "pgvector", "qdrant", "milvus", "upstash_vector", "azure_ai_search", "vertex_ai_vector_search", "valkey")
|
||||
- `config`: A nested dictionary containing provider-specific settings
|
||||
|
||||
|
||||
## How to Use Config
|
||||
|
||||
Here's a general example of how to use the config with mem0:
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "your_chosen_provider",
|
||||
"config": {
|
||||
# Provider-specific settings go here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("Your text here", user_id="user", metadata={"category": "example"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
// Example for in-memory vector database (Only supported in TypeScript)
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const configMemory = {
|
||||
vector_store: {
|
||||
provider: 'memory',
|
||||
config: {
|
||||
collectionName: 'memories',
|
||||
dimension: 1536,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(configMemory);
|
||||
await memory.add("Your text here", { userId: "user", metadata: { category: "example" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
<Note>
|
||||
The in-memory vector database is only supported in the TypeScript implementation.
|
||||
</Note>
|
||||
|
||||
## Why is Config Needed?
|
||||
|
||||
Config is essential for:
|
||||
1. Specifying which vector database to use.
|
||||
2. Providing necessary connection details (e.g., host, port, credentials).
|
||||
3. Customizing database-specific settings (e.g., collection name, path).
|
||||
4. Ensuring proper initialization and connection to your chosen vector store.
|
||||
|
||||
## Master List of All Params in Config
|
||||
|
||||
Here's a comprehensive list of all parameters that can be used across different vector databases:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `collection_name` | Name of the collection |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model |
|
||||
| `client` | Custom client for the database |
|
||||
| `path` | Path for the database |
|
||||
| `host` | Host where the server is running |
|
||||
| `port` | Port where the server is running |
|
||||
| `user` | Username for database connection |
|
||||
| `password` | Password for database connection |
|
||||
| `dbname` | Name of the database |
|
||||
| `url` | Full URL for the server |
|
||||
| `api_key` | API key for the server |
|
||||
| `on_disk` | Enable persistent storage |
|
||||
| `endpoint_id` | Endpoint ID (vertex_ai_vector_search) |
|
||||
| `index_id` | Index ID (vertex_ai_vector_search) |
|
||||
| `deployment_index_id` | Deployment index ID (vertex_ai_vector_search) |
|
||||
| `project_id` | Project ID (vertex_ai_vector_search) |
|
||||
| `project_number` | Project number (vertex_ai_vector_search) |
|
||||
| `vector_search_api_endpoint` | Vector search API endpoint (vertex_ai_vector_search) |
|
||||
| `connection_string` | PostgreSQL connection string (for Supabase/PGVector) |
|
||||
| `index_method` | Vector index method (for Supabase) |
|
||||
| `index_measure` | Distance measure for similarity search (for Supabase) |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `collectionName` | Name of the collection |
|
||||
| `embeddingModelDims` | Dimensions of the embedding model |
|
||||
| `dimension` | Dimensions of the embedding model (for memory provider) |
|
||||
| `host` | Host where the server is running |
|
||||
| `port` | Port where the server is running |
|
||||
| `url` | URL for the server |
|
||||
| `apiKey` | API key for the server |
|
||||
| `path` | Path for the database |
|
||||
| `onDisk` | Enable persistent storage |
|
||||
| `redisUrl` | URL for the Redis server |
|
||||
| `username` | Username for database connection |
|
||||
| `password` | Password for database connection |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Customizing Config
|
||||
|
||||
Each vector database has its own specific configuration requirements. To customize the config for your chosen vector store:
|
||||
|
||||
1. Identify the vector database you want to use from [supported vector databases](./dbs).
|
||||
2. Refer to the `Config` section in the respective vector database's documentation.
|
||||
3. Include only the relevant parameters for your chosen database in the `config` dictionary.
|
||||
|
||||
## Supported Vector Databases
|
||||
|
||||
For detailed information on configuring specific vector databases, please visit the [Supported Vector Databases](./dbs) section. There you'll find individual pages for each supported vector store with provider-specific usage examples and configuration details.
|
||||
@@ -0,0 +1,179 @@
|
||||
---
|
||||
title: Azure AI Search
|
||||
---
|
||||
|
||||
[Azure AI Search](https://learn.microsoft.com/azure/search/search-what-is-azure-search/) (formerly known as "Azure Cognitive Search") provides secure information retrieval at scale over user-owned content in traditional and generative AI search applications.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx" # This key is used for embedding purpose
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "azure_ai_search",
|
||||
"config": {
|
||||
"service_name": "<your-azure-ai-search-service-name>",
|
||||
"api_key": "<your-api-key>",
|
||||
"collection_name": "mem0",
|
||||
"embedding_model_dims": 1536
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Using binary compression for large vector collections
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "azure_ai_search",
|
||||
"config": {
|
||||
"service_name": "<your-azure-ai-search-service-name>",
|
||||
"api_key": "<your-api-key>",
|
||||
"collection_name": "mem0",
|
||||
"embedding_model_dims": 1536,
|
||||
"compression_type": "binary",
|
||||
"use_float16": True # Use half precision for storage efficiency
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Using hybrid search
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "azure_ai_search",
|
||||
"config": {
|
||||
"service_name": "<your-azure-ai-search-service-name>",
|
||||
"api_key": "<your-api-key>",
|
||||
"collection_name": "mem0",
|
||||
"embedding_model_dims": 1536,
|
||||
"hybrid_search": True,
|
||||
"vector_filter_mode": "postFilter"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Using Azure Identity for Authentication
|
||||
As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with Azure OpenAI. The list below shows the order of precedence for credential application:
|
||||
|
||||
1. **Environment Credential:**
|
||||
Azure client ID, secret, tenant ID, or certificate in environment variables for service principal authentication.
|
||||
|
||||
2. **Workload Identity Credential:**
|
||||
Utilizes Azure Workload Identity (relevant for Kubernetes and Azure workloads).
|
||||
|
||||
3. **Managed Identity Credential:**
|
||||
Authenticates as a Managed Identity (for apps/services hosted in Azure with Managed Identity enabled), this is the most secure production credential.
|
||||
|
||||
4. **Shared Token Cache Credential / Visual Studio Credential (Windows only):**
|
||||
Uses cached credentials from Visual Studio sign-ins (and sometimes VS Code if SSO is enabled).
|
||||
|
||||
5. **Azure CLI Credential:**
|
||||
Uses the currently logged-in user from the Azure CLI (`az login`), this is the most common development credential.
|
||||
|
||||
6. **Azure PowerShell Credential:**
|
||||
Uses the identity from Azure PowerShell (`Connect-AzAccount`).
|
||||
|
||||
7. **Azure Developer CLI Credential:**
|
||||
Uses the session from Azure Developer CLI (`azd auth login`).
|
||||
|
||||
<Note> If an API is provided, it will be used for authentication over an Azure Identity </Note>
|
||||
To enable Role-Based Access Control (RBAC) for Azure AI Search, follow these steps:
|
||||
|
||||
1. In the Azure Portal, navigate to your **Azure AI Search** service.
|
||||
2. In the left menu, select **Settings** > **Keys**.
|
||||
3. Change the authentication setting to **Role-based access control**, or **Both** if you need API key compatibility. The default is “Key-based authentication”—you must switch it to use Azure roles.
|
||||
4. **Go to Access Control (IAM):**
|
||||
- In the Azure Portal, select your Search service.
|
||||
- Click **Access Control (IAM)** on the left.
|
||||
5. **Add a Role Assignment:**
|
||||
- Click **Add** > **Add role assignment**.
|
||||
6. **Choose Role:**
|
||||
- Mem0 requires the **Search Index Data Contributor** and **Search Service Contributor** role.
|
||||
7. **Choose Member**
|
||||
- To assign to a User, Group, Service Principle or Managed Identity:
|
||||
- For production it is recommended to use a service principal or managed identity.
|
||||
- For a service principal: select **User, group, or service principal** and search for the service principal.
|
||||
- For a managed identity: select **Managed identity** and choose the managed identity.
|
||||
- For development, you can assign the role to a user account.
|
||||
- For development: select ***User, group, or service principal** and pick a Azure Entra ID account (the same used with `az login`).
|
||||
8. **Complete the Assignment:**
|
||||
- Click **Review + Assign**.
|
||||
|
||||
If you are using Azure Identity, do not set the `api_key` in the configuration.
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "azure_ai_search",
|
||||
"config": {
|
||||
"service_name": "<your-azure-ai-search-service-name>",
|
||||
"collection_name": "mem0",
|
||||
"embedding_model_dims": 1536,
|
||||
"compression_type": "binary",
|
||||
"use_float16": True # Use half precision for storage efficiency
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Environment Variables to set to use Azure Identity Credential:
|
||||
* For an Environment Credential, you will need to setup a Service Principal and set the following environment variables:
|
||||
- `AZURE_TENANT_ID`: Your Azure Active Directory tenant ID.
|
||||
- `AZURE_CLIENT_ID`: The client ID of your service principal or managed identity.
|
||||
- `AZURE_CLIENT_SECRET`: The client secret of your service principal.
|
||||
* For a User-Assigned Managed Identity, you will need to set the following environment variable:
|
||||
- `AZURE_CLIENT_ID`: The client ID of the user-assigned managed identity.
|
||||
* For a System-Assigned Managed Identity, no additional environment variables are needed.
|
||||
|
||||
### Developer logins to use for a Azure Identity Credential:
|
||||
* For an Azure CLI Credential, you need to have the Azure CLI installed and logged in with `az login`.
|
||||
* For an Azure PowerShell Credential, you need to have the Azure PowerShell module installed and logged in with `Connect-AzAccount`.
|
||||
* For an Azure Developer CLI Credential, you need to have the Azure Developer CLI installed and logged in with `azd auth login`.
|
||||
|
||||
Troubleshooting tips for [Azure Identity](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues).
|
||||
|
||||
|
||||
## Configuration Parameters
|
||||
|
||||
| Parameter | Description | Default Value | Options |
|
||||
| --- | --- | --- | --- |
|
||||
| `service_name` | Azure AI Search service name | Required | - |
|
||||
| `api_key` | API key of the Azure AI Search service | Optional | If not present, the [Azure Identity](#using-azure-identity-for-authentication) credential chain will be used |
|
||||
| `collection_name` | The name of the collection/index to store vectors | `mem0` | Any valid index name |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` | Any integer value |
|
||||
| `compression_type` | Type of vector compression to use | `none` | `none`, `scalar`, `binary` |
|
||||
| `use_float16` | Store vectors in half precision (Edm.Half) | `False` | `True`, `False` |
|
||||
| `vector_filter_mode` | Vector filter mode to use | `preFilter` | `postFilter`, `preFilter` |
|
||||
| `hybrid_search` | Use hybrid search | `False` | `True`, `False` |
|
||||
|
||||
## Notes on Configuration Options
|
||||
|
||||
- **compression_type**:
|
||||
- `none`: No compression, uses full vector precision
|
||||
- `scalar`: Scalar quantization with reasonable balance of speed and accuracy
|
||||
- `binary`: Binary quantization for maximum compression with some accuracy trade-off
|
||||
|
||||
- **vector_filter_mode**:
|
||||
- `preFilter`: Applies filters before vector search (faster)
|
||||
- `postFilter`: Applies filters after vector search (may provide better relevance)
|
||||
|
||||
- **use_float16**: Using half precision (float16) reduces storage requirements but may slightly impact accuracy. Useful for very large vector collections.
|
||||
|
||||
- **Filterable Fields**: The implementation automatically extracts `user_id`, `run_id`, and `agent_id` fields from payloads for filtering.
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
title: Baidu VectorDB (Mochow)
|
||||
---
|
||||
|
||||
[Baidu VectorDB](https://cloud.baidu.com/doc/VDB/index.html) is an enterprise-level distributed vector database service developed by Baidu Intelligent Cloud. It is powered by Baidu's proprietary "Mochow" vector database kernel, providing high performance, availability, and security for vector search.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "baidu",
|
||||
"config": {
|
||||
"endpoint": "http://your-mochow-endpoint:8287",
|
||||
"account": "root",
|
||||
"api_key": "your-api-key",
|
||||
"database_name": "mem0",
|
||||
"table_name": "mem0_table",
|
||||
"embedding_model_dims": 1536,
|
||||
"metric_type": "COSINE"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the available parameters for the `mochow` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `endpoint` | Endpoint URL for your Baidu VectorDB instance | Required |
|
||||
| `account` | Baidu VectorDB account name | `root` |
|
||||
| `api_key` | API key for accessing Baidu VectorDB | Required |
|
||||
| `database_name` | Name of the database | `mem0` |
|
||||
| `table_name` | Name of the table | `mem0_table` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `metric_type` | Distance metric for similarity search | `L2` |
|
||||
|
||||
### Distance Metrics
|
||||
|
||||
The following distance metrics are supported:
|
||||
|
||||
- `L2`: Euclidean distance (default)
|
||||
- `IP`: Inner product
|
||||
- `COSINE`: Cosine similarity
|
||||
|
||||
### Index Configuration
|
||||
|
||||
The vector index is automatically configured with the following HNSW parameters:
|
||||
|
||||
- `m`: 16 (number of connections per element)
|
||||
- `efconstruction`: 200 (size of the dynamic candidate list)
|
||||
- `auto_build`: true (automatically build index)
|
||||
- `auto_build_index_policy`: Incremental build with 10000 rows increment
|
||||
@@ -0,0 +1,48 @@
|
||||
[Chroma](https://www.trychroma.com/) is an AI-native open-source vector database that simplifies building LLM apps by providing tools for storing, embedding, and searching embeddings with a focus on simplicity and speed. It supports both local deployment and cloud hosting through ChromaDB Cloud.
|
||||
|
||||
### Usage
|
||||
|
||||
#### Local Installation
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "chroma",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"path": "db",
|
||||
# Optional: ChromaDB Cloud configuration
|
||||
# "api_key": "your-chroma-cloud-api-key",
|
||||
# "tenant": "your-chroma-cloud-tenant-id",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Chroma:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection | `mem0` |
|
||||
| `client` | Custom client for Chroma | `None` |
|
||||
| `path` | Path for the Chroma database | `db` |
|
||||
| `host` | The host where the Chroma server is running | `None` |
|
||||
| `port` | The port where the Chroma server is running | `None` |
|
||||
| `api_key` | ChromaDB Cloud API key (for cloud usage) | `None` |
|
||||
| `tenant` | ChromaDB Cloud tenant ID (for cloud usage) | `None` |
|
||||
@@ -0,0 +1,130 @@
|
||||
[Databricks Vector Search](https://docs.databricks.com/en/generative-ai/vector-search.html) is a serverless similarity search engine that allows you to store a vector representation of your data, including metadata, in a vector database. With Vector Search, you can create auto-updating vector search indexes from Delta tables managed by Unity Catalog and query them with a simple API to return the most similar vectors.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "databricks",
|
||||
"config": {
|
||||
"workspace_url": "https://your-workspace.databricks.com",
|
||||
"access_token": "your-access-token",
|
||||
"endpoint_name": "your-vector-search-endpoint",
|
||||
"index_name": "catalog.schema.index_name",
|
||||
"source_table_name": "catalog.schema.source_table",
|
||||
"embedding_dimension": 1536
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Databricks Vector Search:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `workspace_url` | The URL of your Databricks workspace | **Required** |
|
||||
| `access_token` | Personal Access Token for authentication | `None` |
|
||||
| `service_principal_client_id` | Service principal client ID (alternative to access_token) | `None` |
|
||||
| `service_principal_client_secret` | Service principal client secret (required with client_id) | `None` |
|
||||
| `endpoint_name` | Name of the Vector Search endpoint | **Required** |
|
||||
| `index_name` | Name of the vector index (Unity Catalog format: catalog.schema.index) | **Required** |
|
||||
| `source_table_name` | Name of the source Delta table (Unity Catalog format: catalog.schema.table) | **Required** |
|
||||
| `embedding_dimension` | Dimension of self-managed embeddings | `1536` |
|
||||
| `embedding_source_column` | Column name for text when using Databricks-computed embeddings | `None` |
|
||||
| `embedding_model_endpoint_name` | Databricks serving endpoint for embeddings | `None` |
|
||||
| `embedding_vector_column` | Column name for self-managed embedding vectors | `embedding` |
|
||||
| `endpoint_type` | Type of endpoint (`STANDARD` or `STORAGE_OPTIMIZED`) | `STANDARD` |
|
||||
| `sync_computed_embeddings` | Whether to sync computed embeddings automatically | `True` |
|
||||
|
||||
### Authentication
|
||||
|
||||
Databricks Vector Search supports two authentication methods:
|
||||
|
||||
#### Service Principal (Recommended for Production)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "databricks",
|
||||
"config": {
|
||||
"workspace_url": "https://your-workspace.databricks.com",
|
||||
"service_principal_client_id": "your-service-principal-id",
|
||||
"service_principal_client_secret": "your-service-principal-secret",
|
||||
"endpoint_name": "your-endpoint",
|
||||
"index_name": "catalog.schema.index_name",
|
||||
"source_table_name": "catalog.schema.source_table"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Personal Access Token (for Development)
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "databricks",
|
||||
"config": {
|
||||
"workspace_url": "https://your-workspace.databricks.com",
|
||||
"access_token": "your-personal-access-token",
|
||||
"endpoint_name": "your-endpoint",
|
||||
"index_name": "catalog.schema.index_name",
|
||||
"source_table_name": "catalog.schema.source_table"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Embedding Options
|
||||
|
||||
#### Self-Managed Embeddings (Default)
|
||||
Use your own embedding model and provide vectors directly:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "databricks",
|
||||
"config": {
|
||||
# ... authentication config ...
|
||||
"embedding_dimension": 768, # Match your embedding model
|
||||
"embedding_vector_column": "embedding"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Databricks-Computed Embeddings
|
||||
Let Databricks compute embeddings from text using a serving endpoint:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "databricks",
|
||||
"config": {
|
||||
# ... authentication config ...
|
||||
"embedding_source_column": "text",
|
||||
"embedding_model_endpoint_name": "e5-small-v2"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Important Notes
|
||||
|
||||
- **Delta Sync Index**: This implementation uses Delta Sync Index, which automatically syncs with your source Delta table. Direct vector insertion/deletion/update operations will log warnings as they're not supported with Delta Sync.
|
||||
- **Unity Catalog**: Both the source table and index must be in Unity Catalog format (`catalog.schema.table_name`).
|
||||
- **Endpoint Auto-Creation**: If the specified endpoint doesn't exist, it will be created automatically.
|
||||
- **Index Auto-Creation**: If the specified index doesn't exist, it will be created automatically with the provided configuration.
|
||||
- **Filter Support**: Supports filtering by metadata fields, with different syntax for STANDARD vs STORAGE_OPTIMIZED endpoints.
|
||||
@@ -0,0 +1,109 @@
|
||||
[Elasticsearch](https://www.elastic.co/) is a distributed, RESTful search and analytics engine that can efficiently store and search vector data using dense vectors and k-NN search.
|
||||
|
||||
### Installation
|
||||
|
||||
Elasticsearch support requires additional dependencies. Install them with:
|
||||
|
||||
```bash
|
||||
pip install elasticsearch>=8.0.0
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "elasticsearch",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"host": "localhost",
|
||||
"port": 9200,
|
||||
"embedding_model_dims": 1536
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Let's see the available parameters for the `elasticsearch` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| ---------------------- | -------------------------------------------------- | ------------- |
|
||||
| `collection_name` | The name of the index to store the vectors | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `host` | The host where the Elasticsearch server is running | `localhost` |
|
||||
| `port` | The port where the Elasticsearch server is running | `9200` |
|
||||
| `cloud_id` | Cloud ID for Elastic Cloud deployment | `None` |
|
||||
| `api_key` | API key for authentication | `None` |
|
||||
| `user` | Username for basic authentication | `None` |
|
||||
| `password` | Password for basic authentication | `None` |
|
||||
| `verify_certs` | Whether to verify SSL certificates | `True` |
|
||||
| `auto_create_index` | Whether to automatically create the index | `True` |
|
||||
| `custom_search_query` | Function returning a custom search query | `None` |
|
||||
| `headers` | Custom headers to include in requests | `None` |
|
||||
|
||||
### Features
|
||||
|
||||
- Efficient vector search using Elasticsearch's native k-NN search
|
||||
- Support for both local and cloud deployments (Elastic Cloud)
|
||||
- Multiple authentication methods (Basic Auth, API Key)
|
||||
- Automatic index creation with optimized mappings for vector search
|
||||
- Memory isolation through payload filtering
|
||||
- Custom search query function to customize the search query
|
||||
|
||||
### Custom Search Query
|
||||
|
||||
The `custom_search_query` parameter allows you to customize the search query when `Memory.search` is called.
|
||||
|
||||
__Example__
|
||||
```python
|
||||
import os
|
||||
from typing import List, Optional, Dict
|
||||
from mem0 import Memory
|
||||
|
||||
def custom_search_query(query: List[float], limit: int, filters: Optional[Dict]) -> Dict:
|
||||
return {
|
||||
"knn": {
|
||||
"field": "vector",
|
||||
"query_vector": query,
|
||||
"k": limit,
|
||||
"num_candidates": limit * 2
|
||||
}
|
||||
}
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "elasticsearch",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"host": "localhost",
|
||||
"port": 9200,
|
||||
"embedding_model_dims": 1536,
|
||||
"custom_search_query": custom_search_query
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
It should be a function that takes the following parameters:
|
||||
- `query`: a query vector used in `Memory.search`
|
||||
- `limit`: a number of results used in `Memory.search`
|
||||
- `filters`: a dictionary of key-value pairs used in `Memory.search`. You can add custom pairs for the custom search query.
|
||||
|
||||
The function should return a query body for the Elasticsearch search API.
|
||||
@@ -0,0 +1,72 @@
|
||||
[FAISS](https://github.com/facebookresearch/faiss) is a library for efficient similarity search and clustering of dense vectors. It is designed to work with large-scale datasets and provides a high-performance search engine for vector data. FAISS is optimized for memory usage and search speed, making it an excellent choice for production environments.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "faiss",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"path": "/tmp/faiss_memories",
|
||||
"distance_strategy": "euclidean"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Installation
|
||||
|
||||
To use FAISS in your mem0 project, you need to install the appropriate FAISS package for your environment:
|
||||
|
||||
```bash
|
||||
# For CPU version
|
||||
pip install faiss-cpu
|
||||
|
||||
# For GPU version (requires CUDA)
|
||||
pip install faiss-gpu
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring FAISS:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection | `mem0` |
|
||||
| `path` | Path to store FAISS index and metadata | `/tmp/faiss/<collection_name>` |
|
||||
| `distance_strategy` | Distance metric strategy to use (options: 'euclidean', 'inner_product', 'cosine') | `euclidean` |
|
||||
| `normalize_L2` | Whether to normalize L2 vectors (only applicable for euclidean distance) | `False` |
|
||||
|
||||
### Performance Considerations
|
||||
|
||||
FAISS offers several advantages for vector search:
|
||||
|
||||
1. **Efficiency**: FAISS is optimized for memory usage and speed, making it suitable for large-scale applications.
|
||||
2. **Offline Support**: FAISS works entirely locally, with no need for external servers or API calls.
|
||||
3. **Storage Options**: Vectors can be stored in-memory for maximum speed or persisted to disk.
|
||||
4. **Multiple Index Types**: FAISS supports different index types optimized for various use cases (though mem0 currently uses the basic flat index).
|
||||
|
||||
### Distance Strategies
|
||||
|
||||
FAISS in mem0 supports three distance strategies:
|
||||
|
||||
- **euclidean**: L2 distance, suitable for most embedding models
|
||||
- **inner_product**: Dot product similarity, useful for some specialized embeddings
|
||||
- **cosine**: Cosine similarity, best for comparing semantic similarity regardless of vector magnitude
|
||||
|
||||
When using `cosine` or `inner_product` with normalized vectors, you may want to set `normalize_L2=True` for better results.
|
||||
@@ -0,0 +1,112 @@
|
||||
---
|
||||
title: LangChain
|
||||
---
|
||||
|
||||
Mem0 supports LangChain as a provider for vector store integration. LangChain provides a unified interface to various vector databases, making it easy to integrate different vector store providers through a consistent API.
|
||||
|
||||
<Note>
|
||||
When using LangChain as your vector store provider, you must set the collection name to "mem0". This is a required configuration for proper integration with Mem0.
|
||||
</Note>
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
from langchain_community.vectorstores import Chroma
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
|
||||
# Initialize a LangChain vector store
|
||||
embeddings = OpenAIEmbeddings()
|
||||
vector_store = Chroma(
|
||||
persist_directory="./chroma_db",
|
||||
embedding_function=embeddings,
|
||||
collection_name="mem0" # Required collection name
|
||||
)
|
||||
|
||||
# Pass the initialized vector store to the config
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "langchain",
|
||||
"config": {
|
||||
"client": vector_store
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from "mem0ai";
|
||||
import { OpenAIEmbeddings } from "@langchain/openai";
|
||||
import { MemoryVectorStore as LangchainMemoryStore } from "langchain/vectorstores/memory";
|
||||
|
||||
const embeddings = new OpenAIEmbeddings();
|
||||
const vectorStore = new LangchainVectorStore(embeddings);
|
||||
|
||||
const config = {
|
||||
"vector_store": {
|
||||
"provider": "langchain",
|
||||
"config": { "client": vectorStore }
|
||||
}
|
||||
}
|
||||
|
||||
const memory = new Memory(config);
|
||||
|
||||
const messages = [
|
||||
{ role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
|
||||
{ role: "assistant", content: "How about a thriller movies? They can be quite engaging." },
|
||||
{ role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." },
|
||||
{ role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." }
|
||||
]
|
||||
|
||||
memory.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Supported LangChain Vector Stores
|
||||
|
||||
LangChain supports a wide range of vector store providers, including:
|
||||
|
||||
- Chroma
|
||||
- FAISS
|
||||
- Pinecone
|
||||
- Weaviate
|
||||
- Milvus
|
||||
- Qdrant
|
||||
- And many more
|
||||
|
||||
You can use any of these vector store instances directly in your configuration. For a complete and up-to-date list of available providers, refer to the [LangChain Vector Stores documentation](https://python.langchain.com/docs/integrations/vectorstores).
|
||||
|
||||
## Limitations
|
||||
|
||||
When using LangChain as a vector store provider, there are some limitations to be aware of:
|
||||
|
||||
1. **Bulk Operations**: The `get_all` and `delete_all` operations are not supported when using LangChain as the vector store provider. This is because LangChain's vector store interface doesn't provide standardized methods for these bulk operations across all providers.
|
||||
|
||||
2. **Provider-Specific Features**: Some advanced features may not be available depending on the specific vector store implementation you're using through LangChain.
|
||||
|
||||
## Provider-Specific Configuration
|
||||
|
||||
When using LangChain as a vector store provider, you'll need to:
|
||||
|
||||
1. Set the appropriate environment variables for your chosen vector store provider
|
||||
2. Import and initialize the specific vector store class you want to use
|
||||
3. Pass the initialized vector store instance to the config
|
||||
|
||||
<Note>
|
||||
Make sure to install the necessary LangChain packages and any provider-specific dependencies.
|
||||
</Note>
|
||||
|
||||
## Config
|
||||
|
||||
All available parameters for the `langchain` vector store config are present in [Master List of All Params in Config](../config).
|
||||
@@ -0,0 +1,43 @@
|
||||
[Milvus](https://milvus.io/) Milvus is an open-source vector database that suits AI applications of every size from running a demo chatbot in Jupyter notebook to building web-scale search that serves billions of users.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "milvus",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"embedding_model_dims": "123",
|
||||
"url": "127.0.0.1",
|
||||
"token": "8e4b8ca8cf2c67",
|
||||
"db_name": "my_database",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here's the parameters available for configuring Milvus Database:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `url` | Full URL/Uri for Milvus/Zilliz server | `http://localhost:19530` |
|
||||
| `token` | Token for Zilliz server / for local setup defaults to None. | `None` |
|
||||
| `collection_name` | The name of the collection | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `metric_type` | Metric type for similarity search | `L2` |
|
||||
| `db_name` | Name of the database | `""` |
|
||||
@@ -0,0 +1,45 @@
|
||||
# MongoDB
|
||||
|
||||
[MongoDB](https://www.mongodb.com/) is a versatile document database that supports vector search capabilities, allowing for efficient high-dimensional similarity searches over large datasets with robust scalability and performance.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"db_name": "mem0-db",
|
||||
"collection_name": "mem0-collection",
|
||||
"mongo_uri":"mongodb://username:password@localhost:27017"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
Here are the parameters available for configuring MongoDB:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| db_name | Name of the MongoDB database | `"mem0_db"` |
|
||||
| collection_name | Name of the MongoDB collection | `"mem0_collection"` |
|
||||
| embedding_model_dims | Dimensions of the embedding vectors | `1536` |
|
||||
| mongo_uri | The mongo URI connection string | mongodb://username:password@localhost:27017 |
|
||||
|
||||
> **Note**: If Mongo_uri is not provided it will default to mongodb://username:password@localhost:27017.
|
||||
@@ -0,0 +1,42 @@
|
||||
# Neptune Analytics Vector Store
|
||||
|
||||
[Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html/) is a memory-optimized graph database engine for analytics. With Neptune Analytics, you can get insights and find trends by processing large amounts of graph data in seconds, including vector search.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install mem0ai[vector_stores]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "neptune",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"endpoint": f"neptune-graph://my-graph-identifier",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
Let's see the available parameters for the `neptune` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection to store the vectors | `mem0` |
|
||||
| `endpoint` | Connection URL for the Neptune Analytics service | `neptune-graph://my-graph-identifier` |
|
||||
@@ -0,0 +1,81 @@
|
||||
[OpenSearch](https://opensearch.org/) is an enterprise-grade search and observability suite that brings order to unstructured data at scale. OpenSearch supports k-NN (k-Nearest Neighbors) and allows you to store and retrieve high-dimensional vector embeddings efficiently.
|
||||
|
||||
### Installation
|
||||
|
||||
OpenSearch support requires additional dependencies. Install them with:
|
||||
|
||||
```bash
|
||||
pip install opensearch-py
|
||||
```
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Before using OpenSearch with Mem0, you need to set up a collection in AWS OpenSearch Service.
|
||||
|
||||
#### AWS OpenSearch Service
|
||||
You can create a collection through the AWS Console:
|
||||
- Navigate to [OpenSearch Service Console](https://console.aws.amazon.com/aos/home)
|
||||
- Click "Create collection"
|
||||
- Select "Serverless collection" and then enable "Vector search" capabilities
|
||||
- Once created, note the endpoint URL (host) for your configuration
|
||||
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
import boto3
|
||||
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
|
||||
|
||||
# For AWS OpenSearch Service with IAM authentication
|
||||
region = 'us-west-2'
|
||||
service = 'aoss'
|
||||
credentials = boto3.Session().get_credentials()
|
||||
auth = AWSV4SignerAuth(credentials, region, service)
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "opensearch",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"host": "your-domain.us-west-2.aoss.amazonaws.com",
|
||||
"port": 443,
|
||||
"http_auth": auth,
|
||||
"embedding_model_dims": 1024,
|
||||
"connection_class": RequestsHttpConnection,
|
||||
"pool_maxsize": 20,
|
||||
"use_ssl": True,
|
||||
"verify_certs": True
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Add Memories
|
||||
|
||||
```python
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Search Memories
|
||||
|
||||
```python
|
||||
results = m.search("What kind of movies does Alice like?", user_id="alice")
|
||||
```
|
||||
|
||||
### Features
|
||||
|
||||
- Fast and Efficient Vector Search
|
||||
- Can be deployed on-premises, in containers, or on cloud platforms like AWS OpenSearch Service.
|
||||
- Multiple Authentication and Security Methods (Basic Authentication, API Keys, LDAP, SAML, and OpenID Connect)
|
||||
- Automatic index creation with optimized mappings for vector search
|
||||
- Memory Optimization through Disk-Based Vector Search and Quantization
|
||||
- Real-Time Analytics and Observability
|
||||
@@ -0,0 +1,87 @@
|
||||
[pgvector](https://github.com/pgvector/pgvector) is open-source vector similarity search for Postgres. After connecting with postgres run `CREATE EXTENSION IF NOT EXISTS vector;` to create the vector extension.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "pgvector",
|
||||
"config": {
|
||||
"user": "test",
|
||||
"password": "123",
|
||||
"host": "127.0.0.1",
|
||||
"port": "5432",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
vectorStore: {
|
||||
provider: 'pgvector',
|
||||
config: {
|
||||
collectionName: 'memories',
|
||||
embeddingModelDims: 1536,
|
||||
user: 'test',
|
||||
password: '123',
|
||||
host: '127.0.0.1',
|
||||
port: 5432,
|
||||
dbname: 'vector_store', // Optional, defaults to 'postgres'
|
||||
diskann: false, // Optional, requires pgvectorscale extension
|
||||
hnsw: false, // Optional, for HNSW indexing
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Here's the parameters available for configuring pgvector:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `dbname` | The name of the database | `postgres` |
|
||||
| `collection_name` | The name of the collection | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `user` | User name to connect to the database | `None` |
|
||||
| `password` | Password to connect to the database | `None` |
|
||||
| `host` | The host where the Postgres server is running | `None` |
|
||||
| `port` | The port where the Postgres server is running | `None` |
|
||||
| `diskann` | Whether to use diskann for vector similarity search (requires pgvectorscale) | `True` |
|
||||
| `hnsw` | Whether to use hnsw for vector similarity search | `False` |
|
||||
| `sslmode` | SSL mode for PostgreSQL connection (e.g., 'require', 'prefer', 'disable') | `None` |
|
||||
| `connection_string` | PostgreSQL connection string (overrides individual connection parameters) | `None` |
|
||||
| `connection_pool` | psycopg2 connection pool object (overrides connection string and individual parameters) | `None` |
|
||||
|
||||
**Note**: The connection parameters have the following priority:
|
||||
1. `connection_pool` (highest priority)
|
||||
2. `connection_string`
|
||||
3. Individual connection parameters (`user`, `password`, `host`, `port`, `sslmode`)
|
||||
@@ -0,0 +1,98 @@
|
||||
[Pinecone](https://www.pinecone.io/) is a fully managed vector database designed for machine learning applications, offering high performance vector search with low latency at scale. It's particularly well-suited for semantic search, recommendation systems, and other AI-powered applications.
|
||||
|
||||
> **New**: Pinecone integration now supports custom namespaces! Use the `namespace` parameter to logically separate data within the same index. This is especially useful for multi-tenant or multi-user applications.
|
||||
|
||||
> **Note**: Before configuring Pinecone, you need to select an embedding model (e.g., OpenAI, Cohere, or custom models) and ensure the `embedding_model_dims` in your config matches your chosen model's dimensions. For example, OpenAI's text-embedding-3-small uses 1536 dimensions.
|
||||
|
||||
### Usage
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
os.environ["PINECONE_API_KEY"] = "your-api-key"
|
||||
|
||||
# Example using serverless configuration
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "pinecone",
|
||||
"config": {
|
||||
"collection_name": "testing",
|
||||
"embedding_model_dims": 1536, # Matches OpenAI's text-embedding-3-small
|
||||
"namespace": "my-namespace", # Optional: specify a namespace for multi-tenancy
|
||||
"serverless_config": {
|
||||
"cloud": "aws", # Choose between 'aws' or 'gcp' or 'azure'
|
||||
"region": "us-east-1"
|
||||
},
|
||||
"metric": "cosine"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Pinecone:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | Name of the index/collection | Required |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model (must match your chosen embedding model) | Required |
|
||||
| `client` | Existing Pinecone client instance | `None` |
|
||||
| `api_key` | API key for Pinecone | Environment variable: `PINECONE_API_KEY` |
|
||||
| `environment` | Pinecone environment | `None` |
|
||||
| `serverless_config` | Configuration for serverless deployment (AWS or GCP or Azure) | `None` |
|
||||
| `pod_config` | Configuration for pod-based deployment | `None` |
|
||||
| `hybrid_search` | Whether to enable hybrid search | `False` |
|
||||
| `metric` | Distance metric for vector similarity | `"cosine"` |
|
||||
| `batch_size` | Batch size for operations | `100` |
|
||||
| `namespace` | Namespace for the collection, useful for multi-tenancy. | `None` |
|
||||
|
||||
> **Important**: You must choose either `serverless_config` or `pod_config` for your deployment, but not both.
|
||||
|
||||
#### Serverless Config Example
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "pinecone",
|
||||
"config": {
|
||||
"collection_name": "memory_index",
|
||||
"embedding_model_dims": 1536, # For OpenAI's text-embedding-3-small
|
||||
"namespace": "my-namespace", # Optional: custom namespace
|
||||
"serverless_config": {
|
||||
"cloud": "aws", # or "gcp" or "azure"
|
||||
"region": "us-east-1" # Choose appropriate region
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Pod Config Example
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "pinecone",
|
||||
"config": {
|
||||
"collection_name": "memory_index",
|
||||
"embedding_model_dims": 1536, # For OpenAI's text-embedding-ada-002
|
||||
"namespace": "my-namespace", # Optional: custom namespace
|
||||
"pod_config": {
|
||||
"environment": "gcp-starter",
|
||||
"replicas": 1,
|
||||
"pod_type": "starter"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,89 @@
|
||||
[Qdrant](https://qdrant.tech/) is an open-source vector search engine. It is designed to work with large-scale datasets and provides a high-performance search engine for vector data.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"host": "localhost",
|
||||
"port": 6333,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
vectorStore: {
|
||||
provider: 'qdrant',
|
||||
config: {
|
||||
collectionName: 'memories',
|
||||
embeddingModelDims: 1536,
|
||||
host: 'localhost',
|
||||
port: 6333,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Let's see the available parameters for the `qdrant` config:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection to store the vectors | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `client` | Custom client for qdrant | `None` |
|
||||
| `host` | The host where the qdrant server is running | `None` |
|
||||
| `port` | The port where the qdrant server is running | `None` |
|
||||
| `path` | Path for the qdrant database | `/tmp/qdrant` |
|
||||
| `url` | Full URL for the qdrant server | `None` |
|
||||
| `api_key` | API key for the qdrant server | `None` |
|
||||
| `on_disk` | For enabling persistent storage | `False` |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collectionName` | The name of the collection to store the vectors | `mem0` |
|
||||
| `embeddingModelDims` | Dimensions of the embedding model | `1536` |
|
||||
| `host` | The host where the Qdrant server is running | `None` |
|
||||
| `port` | The port where the Qdrant server is running | `None` |
|
||||
| `path` | Path for the Qdrant database | `/tmp/qdrant` |
|
||||
| `url` | Full URL for the Qdrant server | `None` |
|
||||
| `apiKey` | API key for the Qdrant server | `None` |
|
||||
| `onDisk` | For enabling persistent storage | `False` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,92 @@
|
||||
[Redis](https://redis.io/) is a scalable, real-time database that can store, search, and analyze vector data.
|
||||
|
||||
### Installation
|
||||
```bash
|
||||
pip install redis redisvl
|
||||
```
|
||||
|
||||
Redis Stack using Docker:
|
||||
```bash
|
||||
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "redis",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"embedding_model_dims": 1536,
|
||||
"redis_url": "redis://localhost:6379"
|
||||
}
|
||||
},
|
||||
"version": "v1.1"
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
vectorStore: {
|
||||
provider: 'redis',
|
||||
config: {
|
||||
collectionName: 'memories',
|
||||
embeddingModelDims: 1536,
|
||||
redisUrl: 'redis://localhost:6379',
|
||||
username: 'your-redis-username',
|
||||
password: 'your-redis-password',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Let's see the available parameters for the `redis` config:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection to store the vectors | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `redis_url` | The URL of the Redis server | `None` |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collectionName` | The name of the collection to store the vectors | `mem0` |
|
||||
| `embeddingModelDims` | Dimensions of the embedding model | `1536` |
|
||||
| `redisUrl` | The URL of the Redis server | `None` |
|
||||
| `username` | Username for Redis connection | `None` |
|
||||
| `password` | Password for Redis connection | `None` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,78 @@
|
||||
---
|
||||
title: Amazon S3 Vectors
|
||||
---
|
||||
|
||||
[Amazon S3 Vectors](https://aws.amazon.com/s3/features/vectors/) is a purpose-built, cost-optimized vector storage and query service for semantic search and AI applications. It provides S3-level elasticity and durability with sub-second query performance.
|
||||
|
||||
### Installation
|
||||
|
||||
S3 Vectors support requires additional dependencies. Install them with:
|
||||
|
||||
```bash
|
||||
pip install boto3
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
To use Amazon S3 Vectors with Mem0, you need to have an AWS account and the necessary IAM permissions (`s3vectors:*`). Ensure your environment is configured with AWS credentials (e.g., via `~/.aws/credentials` or environment variables).
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
# Ensure your AWS credentials are configured in your environment
|
||||
# e.g., by setting AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "s3_vectors",
|
||||
"config": {
|
||||
"vector_bucket_name": "my-mem0-vector-bucket",
|
||||
"index_name": "my-memories-index",
|
||||
"embedding_model_dims": 1536,
|
||||
"distance_metric": "cosine",
|
||||
"region_name": "us-east-1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the available parameters for the `s3_vectors` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| ---------------------- | -------------------------------------------------------------------- | ------------- |
|
||||
| `vector_bucket_name` | The name of the S3 Vector bucket to use. It will be created if it doesn't exist. | Required |
|
||||
| `index_name` | The name of the vector index within the bucket. | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model. Must match your embedder. | `1536` |
|
||||
| `distance_metric` | Distance metric for similarity search. Options: `cosine`, `euclidean`. | `cosine` |
|
||||
| `region_name` | The AWS region where the bucket and index reside. | `None` (uses default from AWS config) |
|
||||
|
||||
### IAM Permissions
|
||||
|
||||
Your AWS identity (user or role) needs permissions to perform actions on S3 Vectors. A minimal policy would look like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": "s3vectors:*",
|
||||
"Resource": "*"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
For production, it is recommended to scope down the resource ARN to your specific buckets and indexes.
|
||||
@@ -0,0 +1,170 @@
|
||||
[Supabase](https://supabase.com/) is an open-source Firebase alternative that provides a PostgreSQL database with pgvector extension for vector similarity search. It offers a powerful and scalable solution for storing and querying vector embeddings.
|
||||
|
||||
Create a [Supabase](https://supabase.com/dashboard/projects) account and project, then get your connection string from Project Settings > Database. See the [docs](https://supabase.github.io/vecs/hosting/) for details.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "supabase",
|
||||
"config": {
|
||||
"connection_string": "postgresql://user:password@host:port/database",
|
||||
"collection_name": "memories",
|
||||
"index_method": "hnsw", # Optional: defaults to "auto"
|
||||
"index_measure": "cosine_distance" # Optional: defaults to "cosine_distance"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
```typescript Typescript
|
||||
import { Memory } from "mem0ai/oss";
|
||||
|
||||
const config = {
|
||||
vectorStore: {
|
||||
provider: "supabase",
|
||||
config: {
|
||||
collectionName: "memories",
|
||||
embeddingModelDims: 1536,
|
||||
supabaseUrl: process.env.SUPABASE_URL || "",
|
||||
supabaseKey: process.env.SUPABASE_KEY || "",
|
||||
tableName: "memories",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const memory = new Memory(config);
|
||||
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
|
||||
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### SQL Migrations for TypeScript Implementation
|
||||
|
||||
The following SQL migrations are required to enable the vector extension and create the memories table:
|
||||
|
||||
```sql
|
||||
-- Enable the vector extension
|
||||
create extension if not exists vector;
|
||||
|
||||
-- Create the memories table
|
||||
create table if not exists memories (
|
||||
id text primary key,
|
||||
embedding vector(1536),
|
||||
metadata jsonb,
|
||||
created_at timestamp with time zone default timezone('utc', now()),
|
||||
updated_at timestamp with time zone default timezone('utc', now())
|
||||
);
|
||||
|
||||
-- Create the vector similarity search function
|
||||
create or replace function match_vectors(
|
||||
query_embedding vector(1536),
|
||||
match_count int,
|
||||
filter jsonb default '{}'::jsonb
|
||||
)
|
||||
returns table (
|
||||
id text,
|
||||
similarity float,
|
||||
metadata jsonb
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
return query
|
||||
select
|
||||
t.id::text,
|
||||
1 - (t.embedding <=> query_embedding) as similarity,
|
||||
t.metadata
|
||||
from memories t
|
||||
where case
|
||||
when filter::text = '{}'::text then true
|
||||
else t.metadata @> filter
|
||||
end
|
||||
order by t.embedding <=> query_embedding
|
||||
limit match_count;
|
||||
end;
|
||||
$$;
|
||||
```
|
||||
|
||||
Goto [Supabase](https://supabase.com/dashboard/projects) and run the above SQL migrations inside the SQL Editor.
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Supabase:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Python">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `connection_string` | PostgreSQL connection string (required) | None |
|
||||
| `collection_name` | Name for the vector collection | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `index_method` | Vector index method to use | `auto` |
|
||||
| `index_measure` | Distance measure for similarity search | `cosine_distance` |
|
||||
</Tab>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collectionName` | Name for the vector collection | `mem0` |
|
||||
| `embeddingModelDims` | Dimensions of the embedding model | `1536` |
|
||||
| `supabaseUrl` | Supabase URL | None |
|
||||
| `supabaseKey` | Supabase key | None |
|
||||
| `tableName` | Name for the vector table | `memories` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
### Index Methods
|
||||
|
||||
The following index methods are supported:
|
||||
|
||||
- `auto`: Automatically selects the best available index method
|
||||
- `hnsw`: Hierarchical Navigable Small World graph index (faster search, more memory usage)
|
||||
- `ivfflat`: Inverted File Flat index (good balance of speed and memory)
|
||||
|
||||
### Distance Measures
|
||||
|
||||
Available distance measures for similarity search:
|
||||
|
||||
- `cosine_distance`: Cosine similarity (recommended for most embedding models)
|
||||
- `l2_distance`: Euclidean distance
|
||||
- `l1_distance`: Manhattan distance
|
||||
- `max_inner_product`: Maximum inner product similarity
|
||||
|
||||
### Best Practices
|
||||
|
||||
1. **Index Method Selection**:
|
||||
- Use `hnsw` for fastest search performance when memory is not a constraint
|
||||
- Use `ivfflat` for a good balance of search speed and memory usage
|
||||
- Use `auto` if unsure, it will select the best method based on your data
|
||||
|
||||
2. **Distance Measure Selection**:
|
||||
- Use `cosine_distance` for most embedding models (OpenAI, Hugging Face, etc.)
|
||||
- Use `max_inner_product` if your vectors are normalized
|
||||
- Use `l2_distance` or `l1_distance` if working with raw feature vectors
|
||||
|
||||
3. **Connection String**:
|
||||
- Always use environment variables for sensitive information in the connection string
|
||||
- Format: `postgresql://user:password@host:port/database`
|
||||
@@ -0,0 +1,70 @@
|
||||
[Upstash Vector](https://upstash.com/docs/vector) is a serverless vector database with built-in embedding models.
|
||||
|
||||
### Usage with Upstash embeddings
|
||||
|
||||
You can enable the built-in embedding models by setting `enable_embeddings` to `True`. This allows you to use Upstash's embedding models for vectorization.
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["UPSTASH_VECTOR_REST_URL"] = "..."
|
||||
os.environ["UPSTASH_VECTOR_REST_TOKEN"] = "..."
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "upstash_vector",
|
||||
"enable_embeddings": True,
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
|
||||
```
|
||||
|
||||
<Note>
|
||||
Setting `enable_embeddings` to `True` will bypass any external embedding provider you have configured.
|
||||
</Note>
|
||||
|
||||
### Usage with external embedding providers
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "..."
|
||||
os.environ["UPSTASH_VECTOR_REST_URL"] = "..."
|
||||
os.environ["UPSTASH_VECTOR_REST_TOKEN"] = "..."
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "upstash_vector",
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-large"
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Here are the parameters available for configuring Upstash Vector:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| ------------------- | ---------------------------------- | ------------- |
|
||||
| `url` | URL for the Upstash Vector index | `None` |
|
||||
| `token` | Token for the Upstash Vector index | `None` |
|
||||
| `client` | An `upstash_vector.Index` instance | `None` |
|
||||
| `collection_name` | The default namespace used | `""` |
|
||||
| `enable_embeddings` | Whether to use Upstash embeddings | `False` |
|
||||
|
||||
<Note>
|
||||
When `url` and `token` are not provided, the `UPSTASH_VECTOR_REST_URL` and
|
||||
`UPSTASH_VECTOR_REST_TOKEN` environment variables are used.
|
||||
</Note>
|
||||
@@ -0,0 +1,49 @@
|
||||
# Valkey Vector Store
|
||||
|
||||
[Valkey](https://valkey.io/) is an open source (BSD) high-performance key/value datastore that supports a variety of workloads and rich datastructures including vector search.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install mem0ai[vector_stores]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "valkey",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"valkey_url": "valkey://localhost:6379",
|
||||
"embedding_model_dims": 1536,
|
||||
"index_type": "flat"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
Let's see the available parameters for the `valkey` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection to store the vectors | `mem0` |
|
||||
| `valkey_url` | Connection URL for the Valkey server | `valkey://localhost:6379` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `index_type` | Vector index algorithm (`hnsw` or `flat`) | `hnsw` |
|
||||
| `hnsw_m` | Number of bi-directional links for HNSW | `16` |
|
||||
| `hnsw_ef_construction` | Size of dynamic candidate list for HNSW | `200` |
|
||||
| `hnsw_ef_runtime` | Size of dynamic candidate list for search | `10` |
|
||||
| `distance_metric` | Distance metric for vector similarity | `cosine` |
|
||||
@@ -0,0 +1,45 @@
|
||||
[Cloudflare Vectorize](https://developers.cloudflare.com/vectorize/) is a vector database offering from Cloudflare, allowing you to build AI-powered applications with vector embeddings.
|
||||
|
||||
### Usage
|
||||
|
||||
<CodeGroup>
|
||||
```typescript TypeScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const config = {
|
||||
vectorStore: {
|
||||
provider: 'vectorize',
|
||||
config: {
|
||||
indexName: 'my-memory-index',
|
||||
accountId: 'your-cloudflare-account-id',
|
||||
apiKey: 'your-cloudflare-api-key',
|
||||
dimension: 1536, // Optional: defaults to 1536
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const memory = new Memory(config);
|
||||
const messages = [
|
||||
{"role": "user", "content": "I'm looking for a good book to read."},
|
||||
{"role": "assistant", "content": "Sure, what genre are you interested in?"},
|
||||
{"role": "user", "content": "I enjoy fantasy novels with strong world-building."},
|
||||
{"role": "assistant", "content": "Great! I'll keep that in mind for future recommendations."}
|
||||
]
|
||||
await memory.add(messages, { userId: "bob", metadata: { interest: "books" } });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Config
|
||||
|
||||
Let's see the available parameters for the `vectorize` config:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="TypeScript">
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `indexName` | The name of the Vectorize index | `None` (Required) |
|
||||
| `accountId` | Your Cloudflare account ID | `None` (Required) |
|
||||
| `apiKey` | Your Cloudflare API token | `None` (Required) |
|
||||
| `dimension` | Dimensions of the embedding model | `1536` |
|
||||
</Tab>
|
||||
</Tabs>
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
title: Vertex AI Vector Search
|
||||
---
|
||||
|
||||
|
||||
### Usage
|
||||
|
||||
To use Google Cloud Vertex AI Vector Search with `mem0`, you need to configure the `vector_store` in your `mem0` config:
|
||||
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["GOOGLE_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "vertex_ai_vector_search",
|
||||
"config": {
|
||||
"endpoint_id": "YOUR_ENDPOINT_ID", # Required: Vector Search endpoint ID
|
||||
"index_id": "YOUR_INDEX_ID", # Required: Vector Search index ID
|
||||
"deployment_index_id": "YOUR_DEPLOYMENT_INDEX_ID", # Required: Deployment-specific ID
|
||||
"project_id": "YOUR_PROJECT_ID", # Required: Google Cloud project ID
|
||||
"project_number": "YOUR_PROJECT_NUMBER", # Required: Google Cloud project number
|
||||
"region": "YOUR_REGION", # Optional: Defaults to GOOGLE_CLOUD_REGION
|
||||
"credentials_path": "path/to/credentials.json", # Optional: Defaults to GOOGLE_APPLICATION_CREDENTIALS
|
||||
"vector_search_api_endpoint": "YOUR_API_ENDPOINT" # Required for get operations
|
||||
}
|
||||
}
|
||||
}
|
||||
m = Memory.from_config(config)
|
||||
m.add("Your text here", user_id="user", metadata={"category": "example"})
|
||||
```
|
||||
|
||||
|
||||
### Required Parameters
|
||||
|
||||
| Parameter | Description | Required |
|
||||
|-----------|-------------|----------|
|
||||
| `endpoint_id` | Vector Search endpoint ID | Yes |
|
||||
| `index_id` | Vector Search index ID | Yes |
|
||||
| `deployment_index_id` | Deployment-specific index ID | Yes |
|
||||
| `project_id` | Google Cloud project ID | Yes |
|
||||
| `project_number` | Google Cloud project number | Yes |
|
||||
| `vector_search_api_endpoint` | Vector search API endpoint | Yes (for get operations) |
|
||||
| `region` | Google Cloud region | No (defaults to GOOGLE_CLOUD_REGION) |
|
||||
| `credentials_path` | Path to service account credentials | No (defaults to GOOGLE_APPLICATION_CREDENTIALS) |
|
||||
@@ -0,0 +1,47 @@
|
||||
[Weaviate](https://weaviate.io/) is an open-source vector search engine. It allows efficient storage and retrieval of high-dimensional vector embeddings, enabling powerful search and retrieval capabilities.
|
||||
|
||||
|
||||
### Installation
|
||||
```bash
|
||||
pip install weaviate weaviate-client
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "sk-xx"
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "weaviate",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"cluster_url": "http://localhost:8080",
|
||||
"auth_client_secret": None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m = Memory.from_config(config)
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."},
|
||||
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
m.add(messages, user_id="alice", metadata={"category": "movies"})
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
Let's see the available parameters for the `weaviate` config:
|
||||
|
||||
| Parameter | Description | Default Value |
|
||||
| --- | --- | --- |
|
||||
| `collection_name` | The name of the collection to store the vectors | `mem0` |
|
||||
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
|
||||
| `cluster_url` | URL for the Weaviate server | `None` |
|
||||
| `auth_client_secret` | API key for Weaviate authentication | `None` |
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: Overview
|
||||
icon: "info"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
Mem0 includes built-in support for various popular databases. Memory can utilize the database provided by the user, ensuring efficient use for specific needs.
|
||||
|
||||
## Supported Vector Databases
|
||||
|
||||
See the list of supported vector databases below.
|
||||
|
||||
<Note>
|
||||
The following vector databases are supported in the Python implementation. The TypeScript implementation currently only supports Qdrant, Redis, Valkey, Vectorize and in-memory vector database.
|
||||
</Note>
|
||||
|
||||
<CardGroup cols={3}>
|
||||
<Card title="Qdrant" href="/components/vectordbs/dbs/qdrant"></Card>
|
||||
<Card title="Chroma" href="/components/vectordbs/dbs/chroma"></Card>
|
||||
<Card title="Pgvector" href="/components/vectordbs/dbs/pgvector"></Card>
|
||||
<Card title="Upstash Vector" href="/components/vectordbs/dbs/upstash-vector"></Card>
|
||||
<Card title="Milvus" href="/components/vectordbs/dbs/milvus"></Card>
|
||||
<Card title="Pinecone" href="/components/vectordbs/dbs/pinecone"></Card>
|
||||
<Card title="MongoDB" href="/components/vectordbs/dbs/mongodb"></Card>
|
||||
<Card title="Azure" href="/components/vectordbs/dbs/azure"></Card>
|
||||
<Card title="Redis" href="/components/vectordbs/dbs/redis"></Card>
|
||||
<Card title="Valkey" href="/components/vectordbs/dbs/valkey"></Card>
|
||||
<Card title="Elasticsearch" href="/components/vectordbs/dbs/elasticsearch"></Card>
|
||||
<Card title="OpenSearch" href="/components/vectordbs/dbs/opensearch"></Card>
|
||||
<Card title="Supabase" href="/components/vectordbs/dbs/supabase"></Card>
|
||||
<Card title="Vertex AI" href="/components/vectordbs/dbs/vertex_ai"></Card>
|
||||
<Card title="Weaviate" href="/components/vectordbs/dbs/weaviate"></Card>
|
||||
<Card title="FAISS" href="/components/vectordbs/dbs/faiss"></Card>
|
||||
<Card title="LangChain" href="/components/vectordbs/dbs/langchain"></Card>
|
||||
<Card title="Amazon S3 Vectors" href="/components/vectordbs/dbs/s3_vectors"></Card>
|
||||
<Card title="Databricks" href="/components/vectordbs/dbs/databricks"></Card>
|
||||
</CardGroup>
|
||||
|
||||
## Usage
|
||||
|
||||
To utilize a vector database, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `Qdrant` will be used as the vector database.
|
||||
|
||||
For a comprehensive list of available parameters for vector database configuration, please refer to [Config](./config).
|
||||
|
||||
## Common issues
|
||||
|
||||
### Using model with different dimensions
|
||||
|
||||
If you are using customized model, which is having different dimensions other than 1536
|
||||
for example 768, you may encounter below error:
|
||||
|
||||
`ValueError: shapes (0,1536) and (768,) not aligned: 1536 (dim 1) != 768 (dim 0)`
|
||||
|
||||
you could add `"embedding_model_dims": 768,` to the config of the vector_store to overcome this issue.
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
---
|
||||
title: Add Memory
|
||||
description: Add memory into the Mem0 platform by storing user-assistant interactions and facts for later retrieval.
|
||||
icon: "plus"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
The `add` operation is how you store memory into Mem0. Whether you're working with a chatbot, a voice assistant, or a multi-agent system, this is the entry point to create long-term memory.
|
||||
|
||||
Memories typically come from a **user-assistant interaction** and Mem0 handles the extraction, transformation, and storage for you.
|
||||
|
||||
Mem0 offers two implementation flows:
|
||||
|
||||
- **Mem0 Platform** (Managed, scalable, with dashboard + API)
|
||||
- **Mem0 Open Source** (Lightweight, fully local, flexible SDKs)
|
||||
|
||||
Each supports the same core memory operations, but with slightly different setup. Below, we walk through examples for both.
|
||||
|
||||
|
||||
## Architecture
|
||||
|
||||
<Frame caption="Architecture diagram illustrating the process of adding memories.">
|
||||
<img src="../../images/add_architecture.png" />
|
||||
</Frame>
|
||||
|
||||
When you call `add`, Mem0 performs the following steps under the hood:
|
||||
|
||||
1. **Information Extraction**
|
||||
The input messages are passed through an LLM that extracts key facts, decisions, preferences, or events worth remembering.
|
||||
|
||||
2. **Conflict Resolution**
|
||||
Mem0 compares the new memory against existing ones to detect duplication or contradiction and handles updates accordingly.
|
||||
|
||||
3. **Memory Storage**
|
||||
The result is stored in a vector database (for semantic search) and optionally in a graph structure (for relationship mapping).
|
||||
|
||||
You don’t need to handle any of this manually, Mem0 takes care of it with a single API call or SDK method.
|
||||
|
||||
---
|
||||
|
||||
## Example: Mem0 Platform
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning a trip to Tokyo next month."},
|
||||
{"role": "assistant", "content": "Great! I’ll remember that for future suggestions."}
|
||||
]
|
||||
|
||||
client.add(
|
||||
messages=messages,
|
||||
user_id="alice",
|
||||
version="v2"
|
||||
)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import { MemoryClient } from "mem0ai";
|
||||
|
||||
const client = new MemoryClient({apiKey: "your-api-key"});
|
||||
|
||||
const messages = [
|
||||
{ role: "user", content: "I'm planning a trip to Tokyo next month." },
|
||||
{ role: "assistant", content: "Great! I’ll remember that for future suggestions." }
|
||||
];
|
||||
|
||||
await client.add({
|
||||
messages,
|
||||
user_id: "alice",
|
||||
version: "v2"
|
||||
});
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## Example: Mem0 Open Source
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
import os
|
||||
from mem0 import Memory
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = "your-api-key"
|
||||
|
||||
m = Memory()
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
|
||||
# Store inferred memories (default behavior)
|
||||
result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"})
|
||||
|
||||
# Optionally store raw messages without inference
|
||||
result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}, infer=False)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const memory = new Memory();
|
||||
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: "I like to drink coffee in the morning and go for a walk"
|
||||
}
|
||||
];
|
||||
|
||||
const result = memory.add(messages, {
|
||||
userId: "alice",
|
||||
metadata: { category: "preferences" }
|
||||
});
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## When Should You Add Memory?
|
||||
|
||||
Add memory whenever your agent learns something useful:
|
||||
|
||||
- A new user preference is shared
|
||||
- A decision or suggestion is made
|
||||
- A goal or task is completed
|
||||
- A new entity is introduced
|
||||
- A user gives feedback or clarification
|
||||
|
||||
Storing this context allows the agent to reason better in future interactions.
|
||||
|
||||
|
||||
### More Details
|
||||
|
||||
For full list of supported fields, required formats, and advanced options, see the
|
||||
[Add Memory API Reference](/api-reference/memory/add-memories).
|
||||
|
||||
---
|
||||
|
||||
## Need help?
|
||||
If you have any questions, please feel free to reach out to us using one of the following methods:
|
||||
|
||||
<Snippet file="get-help.mdx"/>
|
||||
@@ -0,0 +1,141 @@
|
||||
---
|
||||
title: Delete Memory
|
||||
description: Remove memories from Mem0 either individually, in bulk, or via filters.
|
||||
icon: "trash"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Memories can become outdated, irrelevant, or need to be removed for privacy or compliance reasons. Mem0 offers flexible ways to delete memory:
|
||||
|
||||
1. **Delete a Single Memory**: Using a specific memory ID
|
||||
2. **Batch Delete**: Delete multiple known memory IDs (up to 1000)
|
||||
3. **Filtered Delete**: Delete memories matching a filter (e.g., `user_id`, `metadata`, `run_id`)
|
||||
|
||||
This page walks through code example for each method.
|
||||
|
||||
|
||||
## Use Cases
|
||||
|
||||
- Forget a user’s past preferences by request
|
||||
- Remove outdated or incorrect memory entries
|
||||
- Clean up memory after session expiration
|
||||
- Comply with data deletion requests (e.g., GDPR)
|
||||
|
||||
---
|
||||
|
||||
## 1. Delete a Single Memory by ID
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
memory_id = "your_memory_id"
|
||||
client.delete(memory_id=memory_id)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import MemoryClient from 'mem0ai';
|
||||
|
||||
const client = new MemoryClient({ apiKey: "your-api-key" });
|
||||
|
||||
client.delete("your_memory_id")
|
||||
.then(result => console.log(result))
|
||||
.catch(error => console.error(error));
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## 2. Batch Delete Multiple Memories
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
delete_memories = [
|
||||
{"memory_id": "id1"},
|
||||
{"memory_id": "id2"}
|
||||
]
|
||||
|
||||
response = client.batch_delete(delete_memories)
|
||||
print(response)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import MemoryClient from 'mem0ai';
|
||||
|
||||
const client = new MemoryClient({ apiKey: "your-api-key" });
|
||||
|
||||
const deleteMemories = [
|
||||
{ memory_id: "id1" },
|
||||
{ memory_id: "id2" }
|
||||
];
|
||||
|
||||
client.batchDelete(deleteMemories)
|
||||
.then(response => console.log('Batch delete response:', response))
|
||||
.catch(error => console.error(error));
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## 3. Delete Memories by Filter (e.g., user_id)
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
# Delete all memories for a specific user
|
||||
client.delete_all(user_id="alice")
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import MemoryClient from 'mem0ai';
|
||||
|
||||
const client = new MemoryClient({ apiKey: "your-api-key" });
|
||||
|
||||
client.deleteAll({ user_id: "alice" })
|
||||
.then(result => console.log(result))
|
||||
.catch(error => console.error(error));
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
You can also filter by other parameters such as:
|
||||
- `agent_id`
|
||||
- `run_id`
|
||||
- `metadata` (as JSON string)
|
||||
|
||||
---
|
||||
|
||||
## Key Differences
|
||||
|
||||
| Method | Use When | IDs Needed | Filters |
|
||||
|----------------------|-------------------------------------------|------------|----------|
|
||||
| `delete(memory_id)` | You know exactly which memory to remove | ✔ | ✘ |
|
||||
| `batch_delete([...])`| You have a known list of memory IDs | ✔ | ✘ |
|
||||
| `delete_all(...)` | You want to delete by user/agent/run/etc | ✘ | ✔ |
|
||||
|
||||
|
||||
### More Details
|
||||
|
||||
For request/response schema and additional filtering options, see:
|
||||
- [Delete Memory API Reference](/api-reference/memory/delete-memory)
|
||||
- [Batch Delete API Reference](/api-reference/memory/batch-delete)
|
||||
- [Delete Memories by Filter Reference](/api-reference/memory/delete-memories)
|
||||
|
||||
You’ve now seen how to add, search, update, and delete memories in Mem0.
|
||||
|
||||
---
|
||||
|
||||
## Need help?
|
||||
If you have any questions, please feel free to reach out to us using one of the following methods:
|
||||
|
||||
<Snippet file="get-help.mdx"/>
|
||||
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: Search Memory
|
||||
description: Retrieve relevant memories from Mem0 using powerful semantic and filtered search capabilities.
|
||||
icon: "magnifying-glass"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The `search` operation allows you to retrieve relevant memories based on a natural language query and optional filters like user ID, agent ID, categories, and more. This is the foundation of giving your agents memory-aware behavior.
|
||||
|
||||
Mem0 supports:
|
||||
- Semantic similarity search
|
||||
- Metadata filtering (with advanced logic)
|
||||
- Reranking and thresholds
|
||||
- Cross-agent, multi-session context resolution
|
||||
|
||||
This applies to both:
|
||||
- **Mem0 Platform** (hosted API with full-scale features)
|
||||
- **Mem0 Open Source** (local-first with LLM inference and local vector DB)
|
||||
|
||||
|
||||
## Architecture
|
||||
|
||||
<Frame caption="Architecture diagram illustrating the memory search process.">
|
||||
<img src="../../images/search_architecture.png" />
|
||||
</Frame>
|
||||
|
||||
The search flow follows these steps:
|
||||
|
||||
1. **Query Processing**
|
||||
An LLM refines and optimizes your natural language query.
|
||||
|
||||
2. **Vector Search**
|
||||
Semantic embeddings are used to find the most relevant memories using cosine similarity.
|
||||
|
||||
3. **Filtering & Ranking**
|
||||
Logical and comparison-based filters are applied. Memories are scored, filtered, and optionally reranked.
|
||||
|
||||
4. **Results Delivery**
|
||||
Relevant memories are returned with associated metadata and timestamps.
|
||||
|
||||
---
|
||||
|
||||
## Example: Mem0 Platform
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
query = "What do you know about me?"
|
||||
filters = {
|
||||
"OR": [
|
||||
{"user_id": "alice"},
|
||||
{"agent_id": {"in": ["travel-assistant", "customer-support"]}}
|
||||
]
|
||||
}
|
||||
|
||||
results = client.search(query, version="v2", filters=filters)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import { MemoryClient } from "mem0ai";
|
||||
|
||||
const client = new MemoryClient({apiKey: "your-api-key"});
|
||||
|
||||
const query = "I'm craving some pizza. Any recommendations?";
|
||||
const filters = {
|
||||
AND: [
|
||||
{ user_id: "alice" }
|
||||
]
|
||||
};
|
||||
|
||||
const results = await client.search(query, {
|
||||
version: "v2",
|
||||
filters
|
||||
});
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## Example: Mem0 Open Source
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import Memory
|
||||
|
||||
m = Memory()
|
||||
related_memories = m.search("Should I drink coffee or tea?", user_id="alice")
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
|
||||
const memory = new Memory();
|
||||
const relatedMemories = memory.search("Should I drink coffee or tea?", { userId: "alice" });
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## Tips for Better Search
|
||||
|
||||
- Use descriptive natural queries (Mem0 can interpret intent)
|
||||
- Apply filters for scoped, faster lookup
|
||||
- Use `version: "v2"` for enhanced results
|
||||
- Consider wildcard filters (e.g., `run_id: "*"`) for broader matches
|
||||
- Tune with `top_k`, `threshold`, or `rerank` if needed
|
||||
|
||||
|
||||
### More Details
|
||||
|
||||
For the full list of filter logic, comparison operators, and optional search parameters, see the
|
||||
[Search Memory API Reference](/api-reference/memory/v2-search-memories).
|
||||
|
||||
---
|
||||
|
||||
## Need help?
|
||||
If you have any questions, please feel free to reach out to us using one of the following methods:
|
||||
|
||||
<Snippet file="get-help.mdx"/>
|
||||
@@ -0,0 +1,117 @@
|
||||
---
|
||||
title: Update Memory
|
||||
description: Modify an existing memory by updating its content or metadata.
|
||||
icon: "pencil"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
User preferences, interests, and behaviors often evolve over time. The `update` operation lets you revise a stored memory, whether it's updating facts and memories, rephrasing a message, or enriching metadata.
|
||||
|
||||
Mem0 supports both:
|
||||
- **Single Memory Update** for one specific memory using its ID
|
||||
- **Batch Update** for updating many memories at once (up to 1000)
|
||||
|
||||
This guide includes usage for both single update and batch update of memories through **Mem0 Platform**
|
||||
|
||||
|
||||
## Use Cases
|
||||
|
||||
- Refine a vague or incorrect memory after a correction
|
||||
- Add or edit memory with new metadata (e.g., categories, tags)
|
||||
- Evolve factual knowledge as the user’s profile changes
|
||||
- A user profile evolves: “I love spicy food” → later says “Actually, I can’t handle spicy food.”
|
||||
|
||||
Updating memory ensures your agents remain accurate, adaptive, and personalized.
|
||||
|
||||
---
|
||||
|
||||
## Update Memory
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
memory_id = "your_memory_id"
|
||||
client.update(
|
||||
memory_id=memory_id,
|
||||
text="Updated memory content about the user",
|
||||
metadata={"category": "profile-update"}
|
||||
)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import MemoryClient from 'mem0ai';
|
||||
|
||||
const client = new MemoryClient({ apiKey: "your-api-key" });
|
||||
const memory_id = "your_memory_id";
|
||||
|
||||
client.update(memory_id, {
|
||||
text: "Updated memory content about the user",
|
||||
metadata: { category: "profile-update" }
|
||||
})
|
||||
.then(result => console.log(result))
|
||||
.catch(error => console.error(error));
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## Batch Update
|
||||
|
||||
Update up to 1000 memories in one call.
|
||||
|
||||
<CodeGroup>
|
||||
```python Python
|
||||
from mem0 import MemoryClient
|
||||
|
||||
client = MemoryClient(api_key="your-api-key")
|
||||
|
||||
update_memories = [
|
||||
{"memory_id": "id1", "text": "Watches football"},
|
||||
{"memory_id": "id2", "text": "Likes to travel"}
|
||||
]
|
||||
|
||||
response = client.batch_update(update_memories)
|
||||
print(response)
|
||||
```
|
||||
|
||||
```javascript JavaScript
|
||||
import MemoryClient from 'mem0ai';
|
||||
|
||||
const client = new MemoryClient({ apiKey: "your-api-key" });
|
||||
|
||||
const updateMemories = [
|
||||
{ memoryId: "id1", text: "Watches football" },
|
||||
{ memoryId: "id2", text: "Likes to travel" }
|
||||
];
|
||||
|
||||
client.batchUpdate(updateMemories)
|
||||
.then(response => console.log('Batch update response:', response))
|
||||
.catch(error => console.error(error));
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
---
|
||||
|
||||
## Tips
|
||||
|
||||
- You can update both `text` and `metadata` in the same call.
|
||||
- Use `batchUpdate` when you're applying similar corrections at scale.
|
||||
- If memory is marked `immutable`, it must first be deleted and re-added.
|
||||
- Combine this with feedback mechanisms (e.g., user thumbs-up/down) to self-improve memory.
|
||||
|
||||
|
||||
### More Details
|
||||
|
||||
Refer to the full [Update Memory API Reference](/api-reference/memory/update-memory) and [Batch Update Reference](/api-reference/memory/batch-update) for schema and advanced fields.
|
||||
|
||||
---
|
||||
|
||||
## Need help?
|
||||
If you have any questions, please feel free to reach out to us using one of the following methods:
|
||||
|
||||
<Snippet file="get-help.mdx"/>
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: Memory Types
|
||||
description: Understanding different types of memory in AI Applications
|
||||
icon: "memory"
|
||||
iconType: "solid"
|
||||
---
|
||||
|
||||
To build useful AI applications, we need to understand how different memory systems work together. This guide explores the fundamental types of memory in AI systems and shows how Mem0 implements these concepts.
|
||||
|
||||
## Why Memory Matters
|
||||
|
||||
AI systems need memory for three key purposes:
|
||||
1. Maintaining context during conversations
|
||||
2. Learning from past interactions
|
||||
3. Building personalized experiences over time
|
||||
|
||||
Without proper memory systems, AI applications would treat each interaction as completely new, losing valuable context and personalization opportunities.
|
||||
|
||||
## Short-Term Memory
|
||||
|
||||
The most basic form of memory in AI systems holds immediate context - like a person remembering what was just said in a conversation. This includes:
|
||||
|
||||
- **Conversation History**: Recent messages and their order
|
||||
- **Working Memory**: Temporary variables and state
|
||||
- **Attention Context**: Current focus of the conversation
|
||||
|
||||
## Long-Term Memory
|
||||
|
||||
More sophisticated AI applications implement long-term memory to retain information across conversations. This includes:
|
||||
|
||||
- **Factual Memory**: Stored knowledge about users, preferences, and domain-specific information
|
||||
- **Episodic Memory**: Past interactions and experiences
|
||||
- **Semantic Memory**: Understanding of concepts and their relationships
|
||||
|
||||
## Memory Characteristics
|
||||
|
||||
Each memory type has distinct characteristics:
|
||||
|
||||
| Type | Persistence | Access Speed | Use Case |
|
||||
|------|-------------|--------------|-----------|
|
||||
| Short-Term | Temporary | Instant | Active conversations |
|
||||
| Long-Term | Persistent | Fast | User preferences and history |
|
||||
|
||||
## How Mem0 Implements Long-Term Memory
|
||||
Mem0's long-term memory system builds on these foundations by:
|
||||
|
||||
1. Using vector embeddings to store and retrieve semantic information
|
||||
2. Maintaining user-specific context across sessions
|
||||
3. Implementing efficient retrieval mechanisms for relevant past interactions
|
||||
@@ -0,0 +1,126 @@
|
||||
---
|
||||
title: AI Companion in Node.js
|
||||
---
|
||||
|
||||
You can create a personalised AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
|
||||
|
||||
## Overview
|
||||
|
||||
The Personalized AI Companion leverages Mem0 to retain information across interactions, enabling a tailored learning experience. It creates memories for each user interaction and integrates with OpenAI's GPT models to provide detailed and context-aware responses to user queries.
|
||||
|
||||
## Setup
|
||||
|
||||
Before you begin, ensure you have Node.js installed and create a new project. Install the required dependencies using npm:
|
||||
|
||||
```bash
|
||||
npm install openai mem0ai
|
||||
```
|
||||
|
||||
## Full Code Example
|
||||
|
||||
Below is the complete code to create and interact with an AI Companion using Mem0:
|
||||
|
||||
```javascript
|
||||
import { OpenAI } from 'openai';
|
||||
import { Memory } from 'mem0ai/oss';
|
||||
import * as readline from 'readline';
|
||||
|
||||
const openaiClient = new OpenAI();
|
||||
const memory = new Memory();
|
||||
|
||||
async function chatWithMemories(message, userId = "default_user") {
|
||||
const relevantMemories = await memory.search(message, { userId: userId });
|
||||
|
||||
const memoriesStr = relevantMemories.results
|
||||
.map(entry => `- ${entry.memory}`)
|
||||
.join('\n');
|
||||
|
||||
const systemPrompt = `You are a helpful AI. Answer the question based on query and memories.
|
||||
User Memories:
|
||||
${memoriesStr}`;
|
||||
|
||||
const messages = [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: message }
|
||||
];
|
||||
|
||||
const response = await openaiClient.chat.completions.create({
|
||||
model: "gpt-4o-mini",
|
||||
messages: messages
|
||||
});
|
||||
|
||||
const assistantResponse = response.choices[0].message.content || "";
|
||||
|
||||
messages.push({ role: "assistant", content: assistantResponse });
|
||||
await memory.add(messages, { userId: userId });
|
||||
|
||||
return assistantResponse;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
console.log("Chat with AI (type 'exit' to quit)");
|
||||
|
||||
const askQuestion = () => {
|
||||
return new Promise((resolve) => {
|
||||
rl.question("You: ", (input) => {
|
||||
resolve(input.trim());
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const userInput = await askQuestion();
|
||||
|
||||
if (userInput.toLowerCase() === 'exit') {
|
||||
console.log("Goodbye!");
|
||||
rl.close();
|
||||
break;
|
||||
}
|
||||
|
||||
const response = await chatWithMemories(userInput, "sample_user");
|
||||
console.log(`AI: ${response}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("An error occurred:", error);
|
||||
rl.close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
```
|
||||
|
||||
### Key Components
|
||||
|
||||
1. **Initialization**
|
||||
- The code initializes both OpenAI and Mem0 Memory clients
|
||||
- Uses Node.js's built-in readline module for command-line interaction
|
||||
|
||||
2. **Memory Management (chatWithMemories function)**
|
||||
- Retrieves relevant memories using Mem0's search functionality
|
||||
- Constructs a system prompt that includes past memories
|
||||
- Makes API calls to OpenAI for generating responses
|
||||
- Stores new interactions in memory
|
||||
|
||||
3. **Interactive Chat Interface (main function)**
|
||||
- Creates a command-line interface for user interaction
|
||||
- Handles user input and displays AI responses
|
||||
- Includes graceful exit functionality
|
||||
|
||||
### Environment Setup
|
||||
|
||||
Make sure to set up your environment variables:
|
||||
```bash
|
||||
export OPENAI_API_KEY=your_api_key
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
This implementation demonstrates how to create an AI Companion that maintains context across conversations using Mem0's memory capabilities. The system automatically stores and retrieves relevant information, creating a more personalized and context-aware interaction experience.
|
||||
|
||||
As users interact with the system, Mem0's memory system continuously learns and adapts, making future responses more relevant and personalized. This setup is ideal for creating long-term learning AI assistants that can maintain context and provide increasingly personalized responses over time.
|
||||
@@ -0,0 +1,130 @@
|
||||
---
|
||||
title: "Amazon Stack: AWS Bedrock, AOSS, and Neptune Analytics"
|
||||
---
|
||||
|
||||
This example demonstrates how to configure and use the `mem0ai` SDK with **AWS Bedrock**, **OpenSearch Service (AOSS)**, and **AWS Neptune Analytics** for persistent memory capabilities in Python.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the required dependencies to include the Amazon data stack, including **boto3**, **opensearch-py**, and **langchain-aws**:
|
||||
|
||||
```bash
|
||||
pip install "mem0ai[graph,extras]"
|
||||
```
|
||||
|
||||
## Environment Setup
|
||||
|
||||
Set your AWS environment variables:
|
||||
|
||||
```python
|
||||
import os
|
||||
|
||||
# Set these in your environment or notebook
|
||||
os.environ['AWS_REGION'] = 'us-west-2'
|
||||
os.environ['AWS_ACCESS_KEY_ID'] = 'AK00000000000000000'
|
||||
os.environ['AWS_SECRET_ACCESS_KEY'] = 'AS00000000000000000'
|
||||
|
||||
# Confirm they are set
|
||||
print(os.environ['AWS_REGION'])
|
||||
print(os.environ['AWS_ACCESS_KEY_ID'])
|
||||
print(os.environ['AWS_SECRET_ACCESS_KEY'])
|
||||
```
|
||||
|
||||
## Configuration and Usage
|
||||
|
||||
This sets up Mem0 with:
|
||||
- [AWS Bedrock for LLM](https://docs.mem0.ai/components/llms/models/aws_bedrock)
|
||||
- [AWS Bedrock for embeddings](https://docs.mem0.ai/components/embedders/models/aws_bedrock#aws-bedrock)
|
||||
- [OpenSearch as the vector store](https://docs.mem0.ai/components/vectordbs/dbs/opensearch)
|
||||
- [Neptune Analytics as your graph store](https://docs.mem0.ai/open-source/graph_memory/overview#initialize-neptune-analytics).
|
||||
|
||||
```python
|
||||
import boto3
|
||||
from opensearchpy import RequestsHttpConnection, AWSV4SignerAuth
|
||||
from mem0.memory.main import Memory
|
||||
|
||||
region = 'us-west-2'
|
||||
service = 'aoss'
|
||||
credentials = boto3.Session().get_credentials()
|
||||
auth = AWSV4SignerAuth(credentials, region, service)
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "amazon.titan-embed-text-v2:0"
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000
|
||||
}
|
||||
},
|
||||
"vector_store": {
|
||||
"provider": "opensearch",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"host": "your-opensearch-domain.us-west-2.es.amazonaws.com",
|
||||
"port": 443,
|
||||
"http_auth": auth,
|
||||
"connection_class": RequestsHttpConnection,
|
||||
"pool_maxsize": 20,
|
||||
"use_ssl": True,
|
||||
"verify_certs": True,
|
||||
"embedding_model_dims": 1024,
|
||||
}
|
||||
},
|
||||
"graph_store": {
|
||||
"provider": "neptune",
|
||||
"config": {
|
||||
"endpoint": f"neptune-graph://my-graph-identifier",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Initialize the memory system
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Reference [Notebook example](https://github.com/mem0ai/mem0/blob/main/examples/graph-db-demo/neptune-example.ipynb)
|
||||
|
||||
#### Add a memory:
|
||||
|
||||
```python
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
|
||||
# Store inferred memories (default behavior)
|
||||
result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"})
|
||||
```
|
||||
|
||||
#### Search a memory:
|
||||
```python
|
||||
relevant_memories = m.search(query, user_id="alice")
|
||||
```
|
||||
|
||||
#### Get all memories:
|
||||
```python
|
||||
all_memories = m.get_all(user_id="alice")
|
||||
```
|
||||
|
||||
#### Get a specific memory:
|
||||
```python
|
||||
memory = m.get(memory_id)
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Conclusion
|
||||
|
||||
With Mem0 and AWS services like Bedrock, OpenSearch, and Neptune Analytics, you can build intelligent AI companions that remember, adapt, and personalize their responses over time. This makes them ideal for long-term assistants, tutors, or support bots with persistent memory and natural conversation abilities.
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: "Amazon Stack - Neptune Analytics Hybrid Store: AWS Bedrock and Neptune Analytics"
|
||||
---
|
||||
|
||||
This example demonstrates how to configure and use the `mem0ai` SDK with **AWS Bedrock** and **AWS Neptune Analytics** for persistent memory capabilities in Python.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the required dependencies to include the Amazon data stack, including **boto3** and **langchain-aws**:
|
||||
|
||||
```bash
|
||||
pip install "mem0ai[graph,extras]"
|
||||
```
|
||||
|
||||
## Environment Setup
|
||||
|
||||
Set your AWS environment variables:
|
||||
|
||||
```python
|
||||
import os
|
||||
|
||||
# Set these in your environment or notebook
|
||||
os.environ['AWS_REGION'] = 'us-west-2'
|
||||
os.environ['AWS_ACCESS_KEY_ID'] = 'AK00000000000000000'
|
||||
os.environ['AWS_SECRET_ACCESS_KEY'] = 'AS00000000000000000'
|
||||
|
||||
# Confirm they are set
|
||||
print(os.environ['AWS_REGION'])
|
||||
print(os.environ['AWS_ACCESS_KEY_ID'])
|
||||
print(os.environ['AWS_SECRET_ACCESS_KEY'])
|
||||
```
|
||||
|
||||
## Configuration and Usage
|
||||
|
||||
This sets up Mem0 with:
|
||||
- [AWS Bedrock for LLM](https://docs.mem0.ai/components/llms/models/aws_bedrock)
|
||||
- [AWS Bedrock for embeddings](https://docs.mem0.ai/components/embedders/models/aws_bedrock#aws-bedrock)
|
||||
- [Neptune Analytics as the vector store](https://docs.mem0.ai/components/vectordbs/dbs/neptune_analytics)
|
||||
- [Neptune Analytics as the graph store](https://docs.mem0.ai/open-source/graph_memory/overview#initialize-neptune-analytics).
|
||||
|
||||
```python
|
||||
import boto3
|
||||
from mem0.memory.main import Memory
|
||||
|
||||
region = 'us-west-2'
|
||||
neptune_analytics_endpoint = 'neptune-graph://my-graph-identifier'
|
||||
|
||||
config = {
|
||||
"embedder": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "amazon.titan-embed-text-v2:0"
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "aws_bedrock",
|
||||
"config": {
|
||||
"model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000
|
||||
}
|
||||
},
|
||||
"vector_store": {
|
||||
"provider": "neptune",
|
||||
"config": {
|
||||
"collection_name": "mem0",
|
||||
"endpoint": neptune_analytics_endpoint,
|
||||
},
|
||||
},
|
||||
"graph_store": {
|
||||
"provider": "neptune",
|
||||
"config": {
|
||||
"endpoint": neptune_analytics_endpoint,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Initialize the memory system
|
||||
m = Memory.from_config(config)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Reference [Notebook example](https://github.com/mem0ai/mem0/blob/main/examples/graph-db-demo/neptune-example.ipynb)
|
||||
|
||||
#### Add a memory:
|
||||
|
||||
```python
|
||||
messages = [
|
||||
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
|
||||
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
|
||||
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
|
||||
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
|
||||
]
|
||||
|
||||
# Store inferred memories (default behavior)
|
||||
result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"})
|
||||
```
|
||||
|
||||
#### Search a memory:
|
||||
```python
|
||||
relevant_memories = m.search(query, user_id="alice")
|
||||
```
|
||||
|
||||
#### Get all memories:
|
||||
```python
|
||||
all_memories = m.get_all(user_id="alice")
|
||||
```
|
||||
|
||||
#### Get a specific memory:
|
||||
```python
|
||||
memory = m.get(memory_id)
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Conclusion
|
||||
|
||||
With Mem0 and AWS services like Bedrock and Neptune Analytics, you can build intelligent AI companions that remember, adapt, and personalize their responses over time. This makes them ideal for long-term assistants, tutors, or support bots with persistent memory and natural conversation abilities.
|
||||
@@ -0,0 +1,55 @@
|
||||
# Mem0 Chrome Extension
|
||||
|
||||
Enhance your AI interactions with **Mem0**, a Chrome extension that introduces a universal memory layer across platforms like `ChatGPT`, `Claude`, and `Perplexity`. Mem0 ensures seamless context sharing, making your AI experiences more personalized and efficient.
|
||||
|
||||
<Note>
|
||||
🎉 We now support Grok! The Mem0 Chrome Extension has been updated to work with Grok, bringing the same powerful memory capabilities to your Grok conversations.
|
||||
</Note>
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
- **Universal Memory Layer**: Share context seamlessly across ChatGPT, Claude, Perplexity, and Grok.
|
||||
- **Smart Context Detection**: Automatically captures relevant information from your conversations.
|
||||
- **Intelligent Memory Retrieval**: Surfaces pertinent memories at the right time.
|
||||
- **One-Click Sync**: Easily synchronize with existing ChatGPT memories.
|
||||
- **Memory Dashboard**: Manage all your memories in one centralized location.
|
||||
|
||||
## Installation
|
||||
|
||||
You can install the Mem0 Chrome Extension using one of the following methods:
|
||||
|
||||
### Method 1: Chrome Web Store Installation
|
||||
|
||||
1. **Download the Extension**: Open Google Chrome and navigate to the [Mem0 Chrome Extension page](https://chromewebstore.google.com/detail/mem0/onihkkbipkfeijkadecaafbgagkhglop?hl=en).
|
||||
2. **Add to Chrome**: Click on the "Add to Chrome" button.
|
||||
3. **Confirm Installation**: In the pop-up dialog, click "Add extension" to confirm. The Mem0 icon should now appear in your Chrome toolbar.
|
||||
|
||||
### Method 2: Manual Installation
|
||||
|
||||
1. **Download the Extension**: Clone or download the extension files from the [Mem0 Chrome Extension GitHub repository](https://github.com/mem0ai/mem0-chrome-extension).
|
||||
2. **Access Chrome Extensions**: Open Google Chrome and navigate to `chrome://extensions`.
|
||||
3. **Enable Developer Mode**: Toggle the "Developer mode" switch in the top right corner.
|
||||
4. **Load Unpacked Extension**: Click "Load unpacked" and select the directory containing the extension files.
|
||||
5. **Confirm Installation**: The Mem0 Chrome Extension should now appear in your Chrome toolbar.
|
||||
|
||||
## Usage
|
||||
|
||||
1. **Locate the Mem0 Icon**: After installation, find the Mem0 icon in your Chrome toolbar.
|
||||
2. **Sign In**: Click the icon and sign in with your Google account.
|
||||
3. **Interact with AI Assistants**:
|
||||
- **ChatGPT and Perplexity**: Continue your conversations as usual; Mem0 operates seamlessly in the background.
|
||||
- **Claude**: Click the Mem0 button or use the shortcut `Ctrl + M` to activate memory functions.
|
||||
|
||||
## Configuration
|
||||
|
||||
- **API Key**: Obtain your API key from the Mem0 Dashboard to connect the extension to the Mem0 API.
|
||||
- **User ID**: This is your unique identifier in the Mem0 system. If not provided, it defaults to 'chrome-extension-user'.
|
||||
|
||||
## Demo Video
|
||||
|
||||
<iframe width="700" height="400" src="https://www.youtube.com/embed/dqenCMMlfwQ?si=zhGVrkq6IS_0Jwyj" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
|
||||
|
||||
## Privacy and Data Security
|
||||
|
||||
Your messages are sent to the Mem0 API for extracting and retrieving memories. Mem0 is committed to ensuring your data's privacy and security.
|
||||
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: Multi-User Collaboration with Mem0
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Build a multi-user collaborative chat or task management system with Mem0. Each message is attributed to its author, and all messages are stored in a shared project space. Mem0 makes it easy to track contributions, sort and group messages, and collaborate in real time.
|
||||
|
||||
## Setup
|
||||
|
||||
Install the required packages:
|
||||
|
||||
```bash
|
||||
pip install openai mem0ai
|
||||
```
|
||||
|
||||
## Full Code Example
|
||||
|
||||
```python
|
||||
from openai import OpenAI
|
||||
from mem0 import Memory
|
||||
import os
|
||||
from datetime import datetime
|
||||
from collections import defaultdict
|
||||
|
||||
# Set your OpenAI API key
|
||||
os.environ["OPENAI_API_KEY"] = "sk-your-key"
|
||||
|
||||
# Shared project context
|
||||
RUN_ID = "project-demo"
|
||||
|
||||
# Initialize Mem0
|
||||
mem = Memory()
|
||||
|
||||
class CollaborativeAgent:
|
||||
def __init__(self, run_id):
|
||||
self.run_id = run_id
|
||||
self.mem = mem
|
||||
|
||||
def add_message(self, role, name, content):
|
||||
msg = {"role": role, "name": name, "content": content}
|
||||
self.mem.add([msg], run_id=self.run_id, infer=False)
|
||||
|
||||
def brainstorm(self, prompt):
|
||||
# Get recent messages for context
|
||||
memories = self.mem.search(prompt, run_id=self.run_id, limit=5)["results"]
|
||||
context = "\n".join(f"- {m['memory']} (by {m.get('actor_id', 'Unknown')})" for m in memories)
|
||||
client = OpenAI()
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful project assistant."},
|
||||
{"role": "user", "content": f"Prompt: {prompt}\nContext:\n{context}"}
|
||||
]
|
||||
reply = client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=messages
|
||||
).choices[0].message.content.strip()
|
||||
self.add_message("assistant", "assistant", reply)
|
||||
return reply
|
||||
|
||||
def get_all_messages(self):
|
||||
return self.mem.get_all(run_id=self.run_id)["results"]
|
||||
|
||||
def print_sorted_by_time(self):
|
||||
messages = self.get_all_messages()
|
||||
messages.sort(key=lambda m: m.get('created_at', ''))
|
||||
print("\n--- Messages (sorted by time) ---")
|
||||
for m in messages:
|
||||
who = m.get("actor_id") or "Unknown"
|
||||
ts = m.get('created_at', 'Timestamp N/A')
|
||||
try:
|
||||
dt = datetime.fromisoformat(ts.replace('Z', '+00:00'))
|
||||
ts_fmt = dt.strftime('%Y-%m-%d %H:%M:%S')
|
||||
except Exception:
|
||||
ts_fmt = ts
|
||||
print(f"[{ts_fmt}] [{who}] {m['memory']}")
|
||||
|
||||
def print_grouped_by_actor(self):
|
||||
messages = self.get_all_messages()
|
||||
grouped = defaultdict(list)
|
||||
for m in messages:
|
||||
grouped[m.get("actor_id") or "Unknown"].append(m)
|
||||
print("\n--- Messages (grouped by actor) ---")
|
||||
for actor, mems in grouped.items():
|
||||
print(f"\n=== {actor} ===")
|
||||
for m in mems:
|
||||
ts = m.get('created_at', 'Timestamp N/A')
|
||||
try:
|
||||
dt = datetime.fromisoformat(ts.replace('Z', '+00:00'))
|
||||
ts_fmt = dt.strftime('%Y-%m-%d %H:%M:%S')
|
||||
except Exception:
|
||||
ts_fmt = ts
|
||||
print(f"[{ts_fmt}] {m['memory']}")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
# Example usage
|
||||
agent = CollaborativeAgent(RUN_ID)
|
||||
agent.add_message("user", "alice", "Let's list tasks for the new landing page.")
|
||||
agent.add_message("user", "bob", "I'll own the hero section copy.")
|
||||
agent.add_message("user", "carol", "I'll choose product screenshots.")
|
||||
|
||||
# Brainstorm with context
|
||||
print("\nAssistant reply:\n", agent.brainstorm("What are the current open tasks?"))
|
||||
|
||||
# Print all messages sorted by time
|
||||
agent.print_sorted_by_time()
|
||||
|
||||
# Print all messages grouped by actor
|
||||
agent.print_grouped_by_actor()
|
||||
```
|
||||
|
||||
## Key Points
|
||||
|
||||
- Each message is attributed to a user or agent (actor)
|
||||
- All messages are stored in a shared project space (`run_id`)
|
||||
- You can sort messages by time, group by actor, and format timestamps for clarity
|
||||
- Mem0 makes it easy to build collaborative, attributed chat/task systems
|
||||
|
||||
## Conclusion
|
||||
|
||||
Mem0 enables fast, transparent collaboration for teams and agents, with full attribution, flexible memory search, and easy message organization.
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
title: Customer Support AI Agent
|
||||
---
|
||||
|
||||
|
||||
You can create a personalized Customer Support AI Agent using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
|
||||
|
||||
## Overview
|
||||
|
||||
The Customer Support AI Agent leverages Mem0 to retain information across interactions, enabling a personalized and efficient support experience.
|
||||
|
||||
## Setup
|
||||
|
||||
Install the necessary packages using pip:
|
||||
|
||||
```bash
|
||||
pip install openai mem0ai
|
||||
```
|
||||
|
||||
## Full Code Example
|
||||
|
||||
Below is the simplified code to create and interact with a Customer Support AI Agent using Mem0:
|
||||
|
||||
```python
|
||||
import os
|
||||
from openai import OpenAI
|
||||
from mem0 import Memory
|
||||
|
||||
# Set the OpenAI API key
|
||||
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
|
||||
|
||||
class CustomerSupportAIAgent:
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize the CustomerSupportAIAgent with memory configuration and OpenAI client.
|
||||
"""
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"host": "localhost",
|
||||
"port": 6333,
|
||||
}
|
||||
},
|
||||
}
|
||||
self.memory = Memory.from_config(config)
|
||||
self.client = OpenAI()
|
||||
self.app_id = "customer-support"
|
||||
|
||||
def handle_query(self, query, user_id=None):
|
||||
"""
|
||||
Handle a customer query and store the relevant information in memory.
|
||||
|
||||
:param query: The customer query to handle.
|
||||
:param user_id: Optional user ID to associate with the memory.
|
||||
"""
|
||||
# Start a streaming chat completion request to the AI
|
||||
stream = self.client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
stream=True,
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a customer support AI agent."},
|
||||
{"role": "user", "content": query}
|
||||
]
|
||||
)
|
||||
# Store the query in memory
|
||||
self.memory.add(query, user_id=user_id, metadata={"app_id": self.app_id})
|
||||
|
||||
# Print the response from the AI in real-time
|
||||
for chunk in stream:
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
print(chunk.choices[0].delta.content, end="")
|
||||
|
||||
def get_memories(self, user_id=None):
|
||||
"""
|
||||
Retrieve all memories associated with the given customer ID.
|
||||
|
||||
:param user_id: Optional user ID to filter memories.
|
||||
:return: List of memories.
|
||||
"""
|
||||
return self.memory.get_all(user_id=user_id)
|
||||
|
||||
# Instantiate the CustomerSupportAIAgent
|
||||
support_agent = CustomerSupportAIAgent()
|
||||
|
||||
# Define a customer ID
|
||||
customer_id = "jane_doe"
|
||||
|
||||
# Handle a customer query
|
||||
support_agent.handle_query("I need help with my recent order. It hasn't arrived yet.", user_id=customer_id)
|
||||
```
|
||||
|
||||
### Fetching Memories
|
||||
|
||||
You can fetch all the memories at any point in time using the following code:
|
||||
|
||||
```python
|
||||
memories = support_agent.get_memories(user_id=customer_id)
|
||||
for m in memories['results']:
|
||||
print(m['memory'])
|
||||
```
|
||||
|
||||
### Key Points
|
||||
|
||||
- **Initialization**: The CustomerSupportAIAgent class is initialized with the necessary memory configuration and OpenAI client setup.
|
||||
- **Handling Queries**: The handle_query method sends a query to the AI and stores the relevant information in memory.
|
||||
- **Retrieving Memories**: The get_memories method fetches all stored memories associated with a customer.
|
||||
|
||||
### Conclusion
|
||||
|
||||
As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized support experience.
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Eliza OS Character
|
||||
---
|
||||
|
||||
You can create a personalised Eliza OS Character using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
|
||||
|
||||
## Overview
|
||||
|
||||
ElizaOS is a powerful AI agent framework for autonomy & personality. It is a collection of tools that help you create a personalised AI agent.
|
||||
|
||||
## Setup
|
||||
You can start by cloning the eliza-os repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/elizaOS/eliza.git
|
||||
```
|
||||
|
||||
Change the directory to the eliza-os repository:
|
||||
|
||||
```bash
|
||||
cd eliza
|
||||
```
|
||||
|
||||
Install the dependencies:
|
||||
|
||||
```bash
|
||||
pnpm install
|
||||
```
|
||||
|
||||
Build the project:
|
||||
|
||||
```bash
|
||||
pnpm build
|
||||
```
|
||||
|
||||
## Setup ENVs
|
||||
|
||||
Create a `.env` file in the root of the project and add the following ( You can use the `.env.example` file as a reference):
|
||||
|
||||
```bash
|
||||
# Mem0 Configuration
|
||||
MEM0_API_KEY= # Mem0 API Key ( Get from https://app.mem0.ai/dashboard/api-keys )
|
||||
MEM0_USER_ID= # Default: eliza-os-user
|
||||
MEM0_PROVIDER= # Default: openai
|
||||
MEM0_PROVIDER_API_KEY= # API Key for the provider (openai, anthropic, etc.)
|
||||
SMALL_MEM0_MODEL= # Default: gpt-4o-mini
|
||||
MEDIUM_MEM0_MODEL= # Default: gpt-4o
|
||||
LARGE_MEM0_MODEL= # Default: gpt-4o
|
||||
```
|
||||
|
||||
## Make the default character use Mem0
|
||||
|
||||
By default, there is a character called `eliza` that uses the `ollama` model. You can make this character use Mem0 by changing the config in the `agent/src/defaultCharacter.ts` file.
|
||||
|
||||
```ts
|
||||
modelProvider: ModelProviderName.MEM0,
|
||||
```
|
||||
|
||||
This will make the character use Mem0 to generate responses.
|
||||
|
||||
## Run the project
|
||||
|
||||
```bash
|
||||
pnpm start
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
You have now created a personalised Eliza OS Character using Mem0. You can now start interacting with the character by running the project and talking to the character.
|
||||
|
||||
This is a simple example of how to use Mem0 to create a personalised AI agent. You can use this as a starting point to create your own AI agent.
|
||||
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
---
|
||||
title: Email Processing with Mem0
|
||||
---
|
||||
|
||||
This guide demonstrates how to build an intelligent email processing system using Mem0's memory capabilities. You'll learn how to store, categorize, retrieve, and analyze emails to create a smart email management solution.
|
||||
|
||||
## Overview
|
||||
|
||||
Email overload is a common challenge for many professionals. By leveraging Mem0's memory capabilities, you can build an intelligent system that:
|
||||
|
||||
- Stores emails as searchable memories
|
||||
- Categorizes emails automatically
|
||||
- Retrieves relevant past conversations
|
||||
- Prioritizes messages based on importance
|
||||
- Generates summaries and action items
|
||||
|
||||
## Setup
|
||||
|
||||
Before you begin, ensure you have the required dependencies installed:
|
||||
|
||||
```bash
|
||||
pip install mem0ai openai
|
||||
```
|
||||
|
||||
## Implementation
|
||||
|
||||
### Basic Email Memory System
|
||||
|
||||
The following example shows how to create a basic email processing system with Mem0:
|
||||
|
||||
```python
|
||||
import os
|
||||
from mem0 import MemoryClient
|
||||
from email.parser import Parser
|
||||
|
||||
# Configure API keys
|
||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
|
||||
# Initialize Mem0 client
|
||||
client = MemoryClient()
|
||||
|
||||
class EmailProcessor:
|
||||
def __init__(self):
|
||||
"""Initialize the Email Processor with Mem0 memory client"""
|
||||
self.client = client
|
||||
|
||||
def process_email(self, email_content, user_id):
|
||||
"""
|
||||
Process an email and store it in Mem0 memory
|
||||
|
||||
Args:
|
||||
email_content (str): Raw email content
|
||||
user_id (str): User identifier for memory association
|
||||
"""
|
||||
# Parse email
|
||||
parser = Parser()
|
||||
email = parser.parsestr(email_content)
|
||||
|
||||
# Extract email details
|
||||
sender = email['from']
|
||||
recipient = email['to']
|
||||
subject = email['subject']
|
||||
date = email['date']
|
||||
body = self._get_email_body(email)
|
||||
|
||||
# Create message object for Mem0
|
||||
message = {
|
||||
"role": "user",
|
||||
"content": f"Email from {sender}: {subject}\n\n{body}"
|
||||
}
|
||||
|
||||
# Create metadata for better retrieval
|
||||
metadata = {
|
||||
"email_type": "incoming",
|
||||
"sender": sender,
|
||||
"recipient": recipient,
|
||||
"subject": subject,
|
||||
"date": date
|
||||
}
|
||||
|
||||
# Store in Mem0 with appropriate categories
|
||||
response = self.client.add(
|
||||
messages=[message],
|
||||
user_id=user_id,
|
||||
metadata=metadata,
|
||||
categories=["email", "correspondence"],
|
||||
version="v2"
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
def _get_email_body(self, email):
|
||||
"""Extract the body content from an email"""
|
||||
# Simplified extraction - in real-world, handle multipart emails
|
||||
if email.is_multipart():
|
||||
for part in email.walk():
|
||||
if part.get_content_type() == "text/plain":
|
||||
return part.get_payload(decode=True).decode()
|
||||
else:
|
||||
return email.get_payload(decode=True).decode()
|
||||
|
||||
def search_emails(self, query, user_id):
|
||||
"""
|
||||
Search through stored emails
|
||||
|
||||
Args:
|
||||
query (str): Search query
|
||||
user_id (str): User identifier
|
||||
"""
|
||||
# Search Mem0 for relevant emails
|
||||
results = self.client.search(
|
||||
query=query,
|
||||
user_id=user_id,
|
||||
categories=["email"],
|
||||
output_format="v1.1",
|
||||
version="v2"
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
def get_email_thread(self, subject, user_id):
|
||||
"""
|
||||
Retrieve all emails in a thread based on subject
|
||||
|
||||
Args:
|
||||
subject (str): Email subject to match
|
||||
user_id (str): User identifier
|
||||
"""
|
||||
filters = {
|
||||
"AND": [
|
||||
{"user_id": user_id},
|
||||
{"categories": {"contains": "email"}},
|
||||
{"metadata": {"subject": {"contains": subject}}}
|
||||
]
|
||||
}
|
||||
|
||||
thread = self.client.get_all(
|
||||
version="v2",
|
||||
filters=filters,
|
||||
output_format="v1.1"
|
||||
)
|
||||
|
||||
return thread
|
||||
|
||||
# Initialize the processor
|
||||
processor = EmailProcessor()
|
||||
|
||||
# Example raw email
|
||||
sample_email = """From: alice@example.com
|
||||
To: bob@example.com
|
||||
Subject: Meeting Schedule Update
|
||||
Date: Mon, 15 Jul 2024 14:22:05 -0700
|
||||
|
||||
Hi Bob,
|
||||
|
||||
I wanted to update you on the schedule for our upcoming project meeting.
|
||||
We'll be meeting this Thursday at 2pm instead of Friday.
|
||||
|
||||
Could you please prepare your section of the presentation?
|
||||
|
||||
Thanks,
|
||||
Alice
|
||||
"""
|
||||
|
||||
# Process and store the email
|
||||
user_id = "bob@example.com"
|
||||
processor.process_email(sample_email, user_id)
|
||||
|
||||
# Later, search for emails about meetings
|
||||
meeting_emails = processor.search_emails("meeting schedule", user_id)
|
||||
print(f"Found {len(meeting_emails['results'])} relevant emails")
|
||||
```
|
||||
|
||||
## Key Features and Benefits
|
||||
|
||||
- **Long-term Email Memory**: Store and retrieve email conversations across long periods
|
||||
- **Semantic Search**: Find relevant emails even if they don't contain exact keywords
|
||||
- **Intelligent Categorization**: Automatically sort emails into meaningful categories
|
||||
- **Action Item Extraction**: Identify and track tasks mentioned in emails
|
||||
- **Priority Management**: Focus on important emails based on AI-determined priority
|
||||
- **Context Awareness**: Maintain thread context for more relevant interactions
|
||||
|
||||
## Conclusion
|
||||
|
||||
By combining Mem0's memory capabilities with email processing, you can create intelligent email management systems that help users organize, prioritize, and act on their inbox effectively. The advanced capabilities like automatic categorization, action item extraction, and priority management can significantly reduce the time spent on email management, allowing users to focus on more important tasks.
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
---
|
||||
title: LlamaIndex ReAct Agent
|
||||
---
|
||||
|
||||
Create a ReAct Agent with LlamaIndex which uses Mem0 as the memory store.
|
||||
|
||||
### 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.
|
||||
|
||||
### Setup
|
||||
```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 the Mem0 client. 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 the 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}"
|
||||
|
||||
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 tools and 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, # or memory_from_config
|
||||
verbose=True,
|
||||
)
|
||||
```
|
||||
|
||||
Start the chat.
|
||||
<Note> The agent will use the Mem0 to store the relevant memories from the chat. </Note>
|
||||
|
||||
Input
|
||||
```python
|
||||
response = agent.chat("Hi, My name is David")
|
||||
print(response)
|
||||
```
|
||||
Output
|
||||
```text
|
||||
> 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?
|
||||
```
|
||||
|
||||
Input
|
||||
```python
|
||||
response = agent.chat("I love to eat pizza on weekends")
|
||||
print(response)
|
||||
```
|
||||
Output
|
||||
```text
|
||||
> 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!
|
||||
```
|
||||
Input
|
||||
```python
|
||||
response = agent.chat("My preferred way of communication is email")
|
||||
print(response)
|
||||
```
|
||||
Output
|
||||
```text
|
||||
> 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.
|
||||
```
|
||||
|
||||
### Using the agent WITHOUT memory
|
||||
Input
|
||||
```python
|
||||
agent = FunctionCallingAgent.from_tools(
|
||||
[call_tool, email_tool, order_food_tool],
|
||||
# memory is not provided
|
||||
llm=llm,
|
||||
verbose=True,
|
||||
)
|
||||
response = agent.chat("I am feeling hungry, order me something and send me the bill")
|
||||
print(response)
|
||||
```
|
||||
Output
|
||||
```text
|
||||
> 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!
|
||||
```
|
||||
<Note> The agent is not able to remember the past preferences that user shared in previous chats. </Note>
|
||||
|
||||
### Using the agent WITH memory
|
||||
Input
|
||||
```python
|
||||
agent = FunctionCallingAgent.from_tools(
|
||||
[call_tool, email_tool, order_food_tool],
|
||||
llm=llm,
|
||||
# memory is provided
|
||||
memory=memory_from_client, # or memory_from_config
|
||||
verbose=True,
|
||||
)
|
||||
response = agent.chat("I am feeling hungry, order me something and send me the bill")
|
||||
print(response)
|
||||
```
|
||||
|
||||
Output
|
||||
```text
|
||||
> 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.
|
||||
```
|
||||
<Note> The agent is able to remember the past preferences that user shared and use them to perform actions. </Note>
|
||||
@@ -0,0 +1,360 @@
|
||||
---
|
||||
title: LlamaIndex Multi-Agent Learning System
|
||||
---
|
||||
|
||||
<Snippet file="blank-notif.mdx" />
|
||||
|
||||
Build an intelligent multi-agent learning system that uses Mem0 to maintain persistent memory across multiple specialized agents. This example demonstrates how to create a tutoring system where different agents collaborate while sharing a unified memory layer.
|
||||
|
||||
## Overview
|
||||
|
||||
This example showcases a **Multi-Agent Personal Learning System** that combines:
|
||||
- **LlamaIndex AgentWorkflow** for multi-agent orchestration
|
||||
- **Mem0** for persistent, shared memory across agents
|
||||
- **Multi-agents** that collaborate on teaching tasks
|
||||
|
||||
The system consists of two agents:
|
||||
- **TutorAgent**: Primary instructor for explanations and concept teaching
|
||||
- **PracticeAgent**: Generates exercises and tracks learning progress
|
||||
|
||||
Both agents share the same memory context, enabling seamless collaboration and continuous learning from student interactions.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Persistent Memory**: Agents remember previous interactions across sessions
|
||||
- **Multi-Agent Collaboration**: Agents can hand off tasks to each other
|
||||
- **Personalized Learning**: Adapts to individual student needs and learning styles
|
||||
- **Progress Tracking**: Monitors learning patterns and skill development
|
||||
- **Memory-Driven Teaching**: References past struggles and successes
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Install the required packages:
|
||||
|
||||
```bash
|
||||
pip install llama-index-core llama-index-memory-mem0 openai python-dotenv
|
||||
```
|
||||
|
||||
Set up your environment variables:
|
||||
- `MEM0_API_KEY`: Your Mem0 Platform API key
|
||||
- `OPENAI_API_KEY`: Your OpenAI API key
|
||||
|
||||
You can obtain your Mem0 Platform API key from the [Mem0 Platform](https://app.mem0.ai).
|
||||
|
||||
## Complete Implementation
|
||||
|
||||
```python
|
||||
"""
|
||||
Multi-Agent Personal Learning System: Mem0 + LlamaIndex AgentWorkflow Example
|
||||
|
||||
INSTALLATIONS:
|
||||
!pip install llama-index-core llama-index-memory-mem0 openai
|
||||
|
||||
You need MEM0_API_KEY and OPENAI_API_KEY to run the example.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# LlamaIndex imports
|
||||
from llama_index.core.agent.workflow import AgentWorkflow, FunctionAgent
|
||||
from llama_index.llms.openai import OpenAI
|
||||
from llama_index.core.tools import FunctionTool
|
||||
|
||||
# Memory integration
|
||||
from llama_index.memory.mem0 import Mem0Memory
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
class MultiAgentLearningSystem:
|
||||
"""
|
||||
Multi-Agent Architecture:
|
||||
- TutorAgent: Main teaching and explanations
|
||||
- PracticeAgent: Exercises and skill reinforcement
|
||||
- Shared Memory: Both agents learn from student interactions
|
||||
"""
|
||||
|
||||
def __init__(self, student_id: str):
|
||||
self.student_id = student_id
|
||||
self.llm = OpenAI(model="gpt-4o", temperature=0.2)
|
||||
|
||||
# Memory context for this student
|
||||
self.memory_context = {"user_id": student_id, "app": "learning_assistant"}
|
||||
self.memory = Mem0Memory.from_client(
|
||||
context=self.memory_context
|
||||
)
|
||||
|
||||
self._setup_agents()
|
||||
|
||||
def _setup_agents(self):
|
||||
"""Setup two agents that work together and share memory"""
|
||||
|
||||
# TOOLS
|
||||
async def assess_understanding(topic: str, student_response: str) -> str:
|
||||
"""Assess student's understanding of a topic and save insights"""
|
||||
# Simulate assessment logic
|
||||
if "confused" in student_response.lower() or "don't understand" in student_response.lower():
|
||||
assessment = f"STRUGGLING with {topic}: {student_response}"
|
||||
insight = f"Student needs more help with {topic}. Prefers step-by-step explanations."
|
||||
elif "makes sense" in student_response.lower() or "got it" in student_response.lower():
|
||||
assessment = f"UNDERSTANDS {topic}: {student_response}"
|
||||
insight = f"Student grasped {topic} quickly. Can move to advanced concepts."
|
||||
else:
|
||||
assessment = f"PARTIAL understanding of {topic}: {student_response}"
|
||||
insight = f"Student has basic understanding of {topic}. Needs reinforcement."
|
||||
|
||||
return f"Assessment: {assessment}\nInsight saved: {insight}"
|
||||
|
||||
async def track_progress(topic: str, success_rate: str) -> str:
|
||||
"""Track learning progress and identify patterns"""
|
||||
progress_note = f"Progress on {topic}: {success_rate} - {datetime.now().strftime('%Y-%m-%d')}"
|
||||
return f"Progress tracked: {progress_note}"
|
||||
|
||||
# Convert to FunctionTools
|
||||
tools = [
|
||||
FunctionTool.from_defaults(async_fn=assess_understanding),
|
||||
FunctionTool.from_defaults(async_fn=track_progress)
|
||||
]
|
||||
|
||||
# AGENTS
|
||||
# Tutor Agent - Main teaching and explanation
|
||||
self.tutor_agent = FunctionAgent(
|
||||
name="TutorAgent",
|
||||
description="Primary instructor that explains concepts and adapts to student needs",
|
||||
system_prompt="""
|
||||
You are a patient, adaptive programming tutor. Your key strength is REMEMBERING and BUILDING on previous interactions.
|
||||
|
||||
Key Behaviors:
|
||||
1. Always check what the student has learned before (use memory context)
|
||||
2. Adapt explanations based on their preferred learning style
|
||||
3. Reference previous struggles or successes
|
||||
4. Build progressively on past lessons
|
||||
5. Use assess_understanding to evaluate responses and save insights
|
||||
|
||||
MEMORY-DRIVEN TEACHING:
|
||||
- "Last time you struggled with X, so let's approach Y differently..."
|
||||
- "Since you prefer visual examples, here's a diagram..."
|
||||
- "Building on the functions we covered yesterday..."
|
||||
|
||||
When student shows understanding, hand off to PracticeAgent for exercises.
|
||||
""",
|
||||
tools=tools,
|
||||
llm=self.llm,
|
||||
can_handoff_to=["PracticeAgent"]
|
||||
)
|
||||
|
||||
# Practice Agent - Exercises and reinforcement
|
||||
self.practice_agent = FunctionAgent(
|
||||
name="PracticeAgent",
|
||||
description="Creates practice exercises and tracks progress based on student's learning history",
|
||||
system_prompt="""
|
||||
You create personalized practice exercises based on the student's learning history and current level.
|
||||
|
||||
Key Behaviors:
|
||||
1. Generate problems that match their skill level (from memory)
|
||||
2. Focus on areas they've struggled with previously
|
||||
3. Gradually increase difficulty based on their progress
|
||||
4. Use track_progress to record their performance
|
||||
5. Provide encouraging feedback that references their growth
|
||||
|
||||
MEMORY-DRIVEN PRACTICE:
|
||||
- "Let's practice loops again since you wanted more examples..."
|
||||
- "Here's a harder version of the problem you solved yesterday..."
|
||||
- "You've improved a lot in functions, ready for the next level?"
|
||||
|
||||
After practice, can hand back to TutorAgent for concept review if needed.
|
||||
""",
|
||||
tools=tools,
|
||||
llm=self.llm,
|
||||
can_handoff_to=["TutorAgent"]
|
||||
)
|
||||
|
||||
# Create the multi-agent workflow
|
||||
self.workflow = AgentWorkflow(
|
||||
agents=[self.tutor_agent, self.practice_agent],
|
||||
root_agent=self.tutor_agent.name,
|
||||
initial_state={
|
||||
"current_topic": "",
|
||||
"student_level": "beginner",
|
||||
"learning_style": "unknown",
|
||||
"session_goals": []
|
||||
}
|
||||
)
|
||||
|
||||
async def start_learning_session(self, topic: str, student_message: str = "") -> str:
|
||||
"""
|
||||
Start a learning session with multi-agent memory-aware teaching
|
||||
"""
|
||||
|
||||
if student_message:
|
||||
request = f"I want to learn about {topic}. {student_message}"
|
||||
else:
|
||||
request = f"I want to learn about {topic}."
|
||||
|
||||
# The magic happens here - multi-agent memory is automatically shared!
|
||||
response = await self.workflow.run(
|
||||
user_msg=request,
|
||||
memory=self.memory
|
||||
)
|
||||
|
||||
return str(response)
|
||||
|
||||
async def get_learning_history(self) -> str:
|
||||
"""Show what the system remembers about this student"""
|
||||
try:
|
||||
# Search memory for learning patterns
|
||||
memories = self.memory.search(
|
||||
user_id=self.student_id,
|
||||
query="learning machine learning"
|
||||
)
|
||||
|
||||
if memories and memories.get('results'):
|
||||
history = "\n".join(f"- {m['memory']}" for m in memories['results'])
|
||||
return history
|
||||
else:
|
||||
return "No learning history found yet. Let's start building your profile!"
|
||||
|
||||
except Exception as e:
|
||||
return f"Memory retrieval error: {str(e)}"
|
||||
|
||||
|
||||
async def run_learning_agent():
|
||||
|
||||
learning_system = MultiAgentLearningSystem(student_id="Alexander")
|
||||
|
||||
# First session
|
||||
print("Session 1:")
|
||||
response = await learning_system.start_learning_session(
|
||||
"Vision Language Models",
|
||||
"I'm new to machine learning but I have good hold on Python and have 4 years of work experience.")
|
||||
print(response)
|
||||
|
||||
# Second session - multi-agent memory will remember the first
|
||||
print("\nSession 2:")
|
||||
response2 = await learning_system.start_learning_session(
|
||||
"Machine Learning", "what all did I cover so far?")
|
||||
print(response2)
|
||||
|
||||
# Show what the multi-agent system remembers
|
||||
print("\nLearning History:")
|
||||
history = await learning_system.get_learning_history()
|
||||
print(history)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Run the example"""
|
||||
print("Multi-agent Learning System powered by LlamaIndex and Mem0")
|
||||
|
||||
async def main():
|
||||
await run_learning_agent()
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
### 1. Memory Context Setup
|
||||
|
||||
```python
|
||||
# Memory context for this student
|
||||
self.memory_context = {"user_id": student_id, "app": "learning_assistant"}
|
||||
self.memory = Mem0Memory.from_client(context=self.memory_context)
|
||||
```
|
||||
|
||||
The memory context identifies the specific student and application, ensuring memory isolation and proper retrieval.
|
||||
|
||||
### 2. Agent Collaboration
|
||||
|
||||
```python
|
||||
# Agents can hand off to each other
|
||||
can_handoff_to=["PracticeAgent"] # TutorAgent can hand off to PracticeAgent
|
||||
can_handoff_to=["TutorAgent"] # PracticeAgent can hand off back
|
||||
```
|
||||
|
||||
Agents collaborate seamlessly, with the TutorAgent handling explanations and the PracticeAgent managing exercises.
|
||||
|
||||
### 3. Shared Memory
|
||||
|
||||
```python
|
||||
# Both agents share the same memory instance
|
||||
response = await self.workflow.run(
|
||||
user_msg=request,
|
||||
memory=self.memory # Shared across all agents
|
||||
)
|
||||
```
|
||||
|
||||
All agents in the workflow share the same memory context, enabling true collaborative learning.
|
||||
|
||||
### 4. Memory-Driven Interactions
|
||||
|
||||
The system prompts guide agents to:
|
||||
- Reference previous learning sessions
|
||||
- Adapt to discovered learning styles
|
||||
- Build progressively on past lessons
|
||||
- Track and respond to learning patterns
|
||||
|
||||
## Running the Example
|
||||
|
||||
```python
|
||||
# Initialize the learning system
|
||||
learning_system = MultiAgentLearningSystem(student_id="Alexander")
|
||||
|
||||
# Start a learning session
|
||||
response = await learning_system.start_learning_session(
|
||||
"Vision Language Models",
|
||||
"I'm new to machine learning but I have good hold on Python and have 4 years of work experience."
|
||||
)
|
||||
|
||||
# Continue learning in a new session (memory persists)
|
||||
response2 = await learning_system.start_learning_session(
|
||||
"Machine Learning",
|
||||
"what all did I cover so far?"
|
||||
)
|
||||
|
||||
# Check learning history
|
||||
history = await learning_system.get_learning_history()
|
||||
```
|
||||
|
||||
## Expected Output
|
||||
|
||||
The system will demonstrate memory-aware interactions:
|
||||
|
||||
```
|
||||
Session 1:
|
||||
I understand you want to learn about Vision Language Models and you mentioned you're new to machine learning but have a strong Python background with 4 years of experience. That's a great foundation to build on!
|
||||
|
||||
Let me start with an explanation tailored to your programming background...
|
||||
[Agent provides explanation and may hand off to PracticeAgent for exercises]
|
||||
|
||||
Session 2:
|
||||
Based on our previous session, I remember we covered Vision Language Models and I noted that you have a strong Python background with 4 years of experience. You mentioned being new to machine learning, so we started with foundational concepts...
|
||||
[Agent references previous session and builds upon it]
|
||||
```
|
||||
|
||||
## Key Benefits
|
||||
|
||||
1. **Persistent Learning**: Agents remember across sessions, creating continuity
|
||||
2. **Collaborative Teaching**: Multiple specialized agents work together seamlessly
|
||||
3. **Personalized Adaptation**: System learns and adapts to individual learning styles
|
||||
4. **Scalable Architecture**: Easy to add more specialized agents
|
||||
5. **Memory Efficiency**: Shared memory prevents duplication and ensures consistency
|
||||
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Clear Agent Roles**: Define specific responsibilities for each agent
|
||||
2. **Memory Context**: Use descriptive context for memory isolation
|
||||
3. **Handoff Strategy**: Design clear handoff criteria between agents
|
||||
5. **Memory Hygiene**: Regularly review and clean memory for optimal performance
|
||||
|
||||
## Help & Resources
|
||||
|
||||
- [LlamaIndex Agent Workflows](https://docs.llamaindex.ai/en/stable/use_cases/agents/)
|
||||
- [Mem0 Platform](https://app.mem0.ai/)
|
||||
|
||||
<Snippet file="get-help.mdx" />
|
||||
@@ -0,0 +1,227 @@
|
||||
---
|
||||
title: Mem0 as an Agentic Tool
|
||||
---
|
||||
|
||||
|
||||
Integrate Mem0's memory capabilities with OpenAI's Agents SDK to create AI agents with persistent memory.
|
||||
You can create agents that remember past conversations and use that context to provide better responses.
|
||||
|
||||
## Installation
|
||||
|
||||
First, install the required packages:
|
||||
```bash
|
||||
pip install mem0ai pydantic openai-agents
|
||||
```
|
||||
|
||||
You'll also need a custom agents framework for this implementation.
|
||||
|
||||
## Setting Up Environment Variables
|
||||
|
||||
Store your Mem0 API key as an environment variable:
|
||||
|
||||
```bash
|
||||
export MEM0_API_KEY="your_mem0_api_key"
|
||||
```
|
||||
|
||||
Or in your Python script:
|
||||
|
||||
```python
|
||||
import os
|
||||
os.environ["MEM0_API_KEY"] = "your_mem0_api_key"
|
||||
```
|
||||
|
||||
## Code Structure
|
||||
|
||||
The integration consists of three main components:
|
||||
|
||||
1. **Context Manager**: Defines user context for memory operations
|
||||
2. **Memory Tools**: Functions to add, search, and retrieve memories
|
||||
3. **Memory Agent**: An agent configured to use these memory tools
|
||||
|
||||
## Step-by-Step Implementation
|
||||
|
||||
### 1. Import Dependencies
|
||||
|
||||
```python
|
||||
from __future__ import annotations
|
||||
import os
|
||||
import asyncio
|
||||
from pydantic import BaseModel
|
||||
try:
|
||||
from mem0 import AsyncMemoryClient
|
||||
except ImportError:
|
||||
raise ImportError("mem0 is not installed. Please install it using 'pip install mem0ai'.")
|
||||
from agents import (
|
||||
Agent,
|
||||
ItemHelpers,
|
||||
MessageOutputItem,
|
||||
RunContextWrapper,
|
||||
Runner,
|
||||
ToolCallItem,
|
||||
ToolCallOutputItem,
|
||||
TResponseInputItem,
|
||||
function_tool,
|
||||
)
|
||||
```
|
||||
|
||||
### 2. Define Memory Context
|
||||
|
||||
```python
|
||||
class Mem0Context(BaseModel):
|
||||
user_id: str | None = None
|
||||
```
|
||||
|
||||
### 3. Initialize the Mem0 Client
|
||||
|
||||
```python
|
||||
client = AsyncMemoryClient(api_key=os.getenv("MEM0_API_KEY"))
|
||||
```
|
||||
|
||||
### 4. Create Memory Tools
|
||||
|
||||
#### Add to Memory
|
||||
|
||||
```python
|
||||
@function_tool
|
||||
async def add_to_memory(
|
||||
context: RunContextWrapper[Mem0Context],
|
||||
content: str,
|
||||
) -> str:
|
||||
"""
|
||||
Add a message to Mem0
|
||||
Args:
|
||||
content: The content to store in memory.
|
||||
"""
|
||||
messages = [{"role": "user", "content": content}]
|
||||
user_id = context.context.user_id or "default_user"
|
||||
await client.add(messages, user_id=user_id)
|
||||
return f"Stored message: {content}"
|
||||
```
|
||||
|
||||
#### Search Memory
|
||||
|
||||
```python
|
||||
@function_tool
|
||||
async def search_memory(
|
||||
context: RunContextWrapper[Mem0Context],
|
||||
query: str,
|
||||
) -> str:
|
||||
"""
|
||||
Search for memories in Mem0
|
||||
Args:
|
||||
query: The search query.
|
||||
"""
|
||||
user_id = context.context.user_id or "default_user"
|
||||
memories = await client.search(query, user_id=user_id, output_format="v1.1")
|
||||
results = '\n'.join([result["memory"] for result in memories["results"]])
|
||||
return str(results)
|
||||
```
|
||||
|
||||
#### Get All Memories
|
||||
|
||||
```python
|
||||
@function_tool
|
||||
async def get_all_memory(
|
||||
context: RunContextWrapper[Mem0Context],
|
||||
) -> str:
|
||||
"""Retrieve all memories from Mem0"""
|
||||
user_id = context.context.user_id or "default_user"
|
||||
memories = await client.get_all(user_id=user_id, output_format="v1.1")
|
||||
results = '\n'.join([result["memory"] for result in memories["results"]])
|
||||
return str(results)
|
||||
```
|
||||
|
||||
### 5. Configure the Memory Agent
|
||||
|
||||
```python
|
||||
memory_agent = Agent[Mem0Context](
|
||||
name="Memory Assistant",
|
||||
instructions="""You are a helpful assistant with memory capabilities. You can:
|
||||
1. Store new information using add_to_memory
|
||||
2. Search existing information using search_memory
|
||||
3. Retrieve all stored information using get_all_memory
|
||||
When users ask questions:
|
||||
- If they want to store information, use add_to_memory
|
||||
- If they're searching for specific information, use search_memory
|
||||
- If they want to see everything stored, use get_all_memory""",
|
||||
tools=[add_to_memory, search_memory, get_all_memory],
|
||||
)
|
||||
```
|
||||
|
||||
### 6. Implement the Main Runtime Loop
|
||||
|
||||
```python
|
||||
async def main():
|
||||
current_agent: Agent[Mem0Context] = memory_agent
|
||||
input_items: list[TResponseInputItem] = []
|
||||
context = Mem0Context()
|
||||
while True:
|
||||
user_input = input("Enter your message (or 'quit' to exit): ")
|
||||
if user_input.lower() == 'quit':
|
||||
break
|
||||
input_items.append({"content": user_input, "role": "user"})
|
||||
result = await Runner.run(current_agent, input_items, context=context)
|
||||
for new_item in result.new_items:
|
||||
agent_name = new_item.agent.name
|
||||
if isinstance(new_item, MessageOutputItem):
|
||||
print(f"{agent_name}: {ItemHelpers.text_message_output(new_item)}")
|
||||
elif isinstance(new_item, ToolCallItem):
|
||||
print(f"{agent_name}: Calling a tool")
|
||||
elif isinstance(new_item, ToolCallOutputItem):
|
||||
print(f"{agent_name}: Tool call output: {new_item.output}")
|
||||
else:
|
||||
print(f"{agent_name}: Skipping item: {new_item.__class__.__name__}")
|
||||
input_items = result.to_input_list()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Storing Information
|
||||
|
||||
```
|
||||
User: Remember that my favorite color is blue
|
||||
Agent: Calling a tool
|
||||
Agent: Tool call output: Stored message: my favorite color is blue
|
||||
Agent: I've stored that your favorite color is blue in my memory. I'll remember that for future conversations.
|
||||
```
|
||||
|
||||
### Searching Memory
|
||||
|
||||
```
|
||||
User: What's my favorite color?
|
||||
Agent: Calling a tool
|
||||
Agent: Tool call output: my favorite color is blue
|
||||
Agent: Your favorite color is blue, based on what you've told me earlier.
|
||||
```
|
||||
|
||||
### Retrieving All Memories
|
||||
|
||||
```
|
||||
User: What do you know about me?
|
||||
Agent: Calling a tool
|
||||
Agent: Tool call output: favorite color is blue
|
||||
my birthday is on March 15
|
||||
Agent: Based on our previous conversations, I know that:
|
||||
1. Your favorite color is blue
|
||||
2. Your birthday is on March 15
|
||||
```
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
### Custom User IDs
|
||||
|
||||
You can specify different user IDs to maintain separate memory stores for multiple users:
|
||||
|
||||
```python
|
||||
context = Mem0Context(user_id="user123")
|
||||
```
|
||||
|
||||
|
||||
## Resources
|
||||
|
||||
- [Mem0 Documentation](https://docs.mem0.ai)
|
||||
- [Mem0 Dashboard](https://app.mem0.ai/dashboard)
|
||||
- [API Reference](https://docs.mem0.ai/api-reference)
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: Mem0 Demo
|
||||
---
|
||||
|
||||
You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete setup instructions to get you started.
|
||||
|
||||
<video
|
||||
autoPlay
|
||||
muted
|
||||
loop
|
||||
playsInline
|
||||
className="w-full aspect-video rounded-lg"
|
||||
src="https://github.com/user-attachments/assets/cebc4f8e-bdb9-4837-868d-13c5ab7bb433"
|
||||
></video>
|
||||
|
||||
You can try the [Mem0 Demo](https://mem0-4vmi.vercel.app) live here.
|
||||
|
||||
## Overview
|
||||
|
||||
The Personalized AI Companion leverages Mem0 to retain information across interactions, enabling a tailored learning experience. It creates memories for each user interaction and integrates with OpenAI's GPT models to provide detailed and context-aware responses to user queries.
|
||||
|
||||
## Setup
|
||||
|
||||
Before you begin, follow these steps to set up the demo application:
|
||||
|
||||
1. Clone the Mem0 repository:
|
||||
```bash
|
||||
git clone https://github.com/mem0ai/mem0.git
|
||||
```
|
||||
|
||||
2. Navigate to the demo application folder:
|
||||
```bash
|
||||
cd mem0/examples/mem0-demo
|
||||
```
|
||||
|
||||
3. Install dependencies:
|
||||
```bash
|
||||
pnpm install
|
||||
```
|
||||
|
||||
4. Set up environment variables by creating a `.env` file in the project root with the following content:
|
||||
```bash
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
MEM0_API_KEY=your_mem0_api_key
|
||||
```
|
||||
You can obtain your `MEM0_API_KEY` by signing up at [Mem0 API Dashboard](https://app.mem0.ai/dashboard/api-keys).
|
||||
|
||||
5. Start the development server:
|
||||
```bash
|
||||
pnpm run dev
|
||||
```
|
||||
|
||||
## Enhancing the Next.js Application
|
||||
|
||||
Once the demo is running, you can customize and enhance the Next.js application by modifying the components in the `mem0-demo` folder. Consider:
|
||||
- Adding new memory features to improve contextual retention.
|
||||
- Customizing the UI to better suit your application needs.
|
||||
- Integrating additional APIs or third-party services to extend functionality.
|
||||
|
||||
## Full Code
|
||||
|
||||
You can find the complete source code for this demo on GitHub:
|
||||
[Mem0 Demo GitHub](https://github.com/mem0ai/mem0/tree/main/examples/mem0-demo)
|
||||
|
||||
## Conclusion
|
||||
|
||||
This setup demonstrates how to build an AI Companion that maintains memory across interactions using Mem0. The system continuously adapts to user interactions, making future responses more relevant and personalized. Experiment with the application and enhance it further to suit your use case!
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
---
|
||||
title: 'Healthcare Assistant with Mem0 and Google ADK'
|
||||
description: 'Build a personalized healthcare agent that remembers patient information across conversations using Mem0 and Google ADK'
|
||||
---
|
||||
|
||||
|
||||
# Healthcare Assistant with Memory
|
||||
|
||||
This example demonstrates how to build a healthcare assistant that remembers patient information across conversations using Google ADK and Mem0.
|
||||
|
||||
## Overview
|
||||
|
||||
The Healthcare Assistant helps patients by:
|
||||
- Remembering their medical history and symptoms
|
||||
- Providing general health information
|
||||
- Scheduling appointment reminders
|
||||
- Maintaining a personalized experience across conversations
|
||||
|
||||
By integrating Mem0's memory layer with Google ADK, the assistant maintains context about the patient without requiring them to repeat information.
|
||||
|
||||
## Setup
|
||||
|
||||
Before you begin, make sure you have:
|
||||
|
||||
Installed Google ADK and Mem0 SDK:
|
||||
```bash
|
||||
pip install google-adk mem0ai python-dotenv
|
||||
```
|
||||
|
||||
## Code Breakdown
|
||||
|
||||
Let's get started and understand the different components required in building a healthcare assistant powered by memory
|
||||
|
||||
```python
|
||||
# Import dependencies
|
||||
import os
|
||||
import asyncio
|
||||
from google.adk.agents import Agent
|
||||
from google.adk.runners import Runner
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.genai import types
|
||||
from mem0 import MemoryClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Set up environment variables
|
||||
# os.environ["GOOGLE_API_KEY"] = "your-google-api-key"
|
||||
# os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
|
||||
# Define a global user ID for simplicity
|
||||
USER_ID = "Alex"
|
||||
|
||||
# Initialize Mem0 client
|
||||
mem0 = MemoryClient()
|
||||
```
|
||||
|
||||
## Define Memory Tools
|
||||
|
||||
First, we'll create tools that allow our agent to store and retrieve information using Mem0:
|
||||
|
||||
```python
|
||||
def save_patient_info(information: str) -> dict:
|
||||
"""Saves important patient information to memory."""
|
||||
|
||||
# Store in Mem0
|
||||
response = mem0_client.add(
|
||||
[{"role": "user", "content": information}],
|
||||
user_id=USER_ID,
|
||||
run_id="healthcare_session",
|
||||
metadata={"type": "patient_information"}
|
||||
)
|
||||
|
||||
|
||||
def retrieve_patient_info(query: str) -> dict:
|
||||
"""Retrieves relevant patient information from memory."""
|
||||
|
||||
# Search Mem0
|
||||
results = mem0_client.search(
|
||||
query,
|
||||
user_id=USER_ID,
|
||||
limit=5,
|
||||
threshold=0.7, # Higher threshold for more relevant results
|
||||
output_format="v1.1"
|
||||
)
|
||||
|
||||
# Format and return the results
|
||||
if results and len(results) > 0:
|
||||
memories = [memory["memory"] for memory in results.get('results', [])]
|
||||
return {
|
||||
"status": "success",
|
||||
"memories": memories,
|
||||
"count": len(memories)
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"status": "no_results",
|
||||
"memories": [],
|
||||
"count": 0
|
||||
}
|
||||
```
|
||||
|
||||
## Define Healthcare Tools
|
||||
|
||||
Next, we'll add tools specific to healthcare assistance:
|
||||
|
||||
```python
|
||||
def schedule_appointment(date: str, time: str, reason: str) -> dict:
|
||||
"""Schedules a doctor's appointment."""
|
||||
# In a real app, this would connect to a scheduling system
|
||||
appointment_id = f"APT-{hash(date + time) % 10000}"
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"appointment_id": appointment_id,
|
||||
"confirmation": f"Appointment scheduled for {date} at {time} for {reason}",
|
||||
"message": "Please arrive 15 minutes early to complete paperwork."
|
||||
}
|
||||
```
|
||||
|
||||
## Create the Healthcare Assistant Agent
|
||||
|
||||
Now we'll create our main agent with all the tools:
|
||||
|
||||
```python
|
||||
# Create the agent
|
||||
healthcare_agent = Agent(
|
||||
name="healthcare_assistant",
|
||||
model="gemini-1.5-flash", # Using Gemini for healthcare assistant
|
||||
description="Healthcare assistant that helps patients with health information and appointment scheduling.",
|
||||
instruction="""You are a helpful Healthcare Assistant with memory capabilities.
|
||||
|
||||
Your primary responsibilities are to:
|
||||
1. Remember patient information using the 'save_patient_info' tool when they share symptoms, conditions, or preferences.
|
||||
2. Retrieve past patient information using the 'retrieve_patient_info' tool when relevant to the current conversation.
|
||||
3. Help schedule appointments using the 'schedule_appointment' tool.
|
||||
|
||||
IMPORTANT GUIDELINES:
|
||||
- Always be empathetic, professional, and helpful.
|
||||
- Save important patient information like symptoms, conditions, allergies, and preferences.
|
||||
- Check if you have relevant patient information before asking for details they may have shared previously.
|
||||
- Make it clear you are not a doctor and cannot provide medical diagnosis or treatment.
|
||||
- For serious symptoms, always recommend consulting a healthcare professional.
|
||||
- Keep all patient information confidential.
|
||||
""",
|
||||
tools=[save_patient_info, retrieve_patient_info, schedule_appointment]
|
||||
)
|
||||
```
|
||||
|
||||
## Set Up Session and Runner
|
||||
|
||||
```python
|
||||
# Set up Session Service and Runner
|
||||
session_service = InMemorySessionService()
|
||||
|
||||
# Define constants for the conversation
|
||||
APP_NAME = "healthcare_assistant_app"
|
||||
USER_ID = "Alex"
|
||||
SESSION_ID = "session_001"
|
||||
|
||||
# Create a session
|
||||
session = session_service.create_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
|
||||
# Create the runner
|
||||
runner = Runner(
|
||||
agent=healthcare_agent,
|
||||
app_name=APP_NAME,
|
||||
session_service=session_service
|
||||
)
|
||||
```
|
||||
|
||||
## Interact with the Healthcare Assistant
|
||||
|
||||
```python
|
||||
# Function to interact with the agent
|
||||
async def call_agent_async(query, runner, user_id, session_id):
|
||||
"""Sends a query to the agent and returns the final response."""
|
||||
print(f"\n>>> Patient: {query}")
|
||||
|
||||
# Format the user's message
|
||||
content = types.Content(
|
||||
role='user',
|
||||
parts=[types.Part(text=query)]
|
||||
)
|
||||
|
||||
# Set user_id for tools to access
|
||||
save_patient_info.user_id = user_id
|
||||
retrieve_patient_info.user_id = user_id
|
||||
|
||||
# Run the agent
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id,
|
||||
session_id=session_id,
|
||||
new_message=content
|
||||
):
|
||||
if event.is_final_response():
|
||||
if event.content and event.content.parts:
|
||||
response = event.content.parts[0].text
|
||||
print(f"<<< Assistant: {response}")
|
||||
return response
|
||||
|
||||
return "No response received."
|
||||
|
||||
# Example conversation flow
|
||||
async def run_conversation():
|
||||
# First interaction - patient introduces themselves with key information
|
||||
await call_agent_async(
|
||||
"Hi, I'm Alex. I've been having headaches for the past week, and I have a penicillin allergy.",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
|
||||
# Request for health information
|
||||
await call_agent_async(
|
||||
"Can you tell me more about what might be causing my headaches?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
|
||||
# Schedule an appointment
|
||||
await call_agent_async(
|
||||
"I think I should see a doctor. Can you help me schedule an appointment for next Monday at 2pm?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
|
||||
# Test memory - should remember patient name, symptoms, and allergy
|
||||
await call_agent_async(
|
||||
"What medications should I avoid for my headaches?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
|
||||
# Run the conversation example
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_conversation())
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
This healthcare assistant demonstrates several key capabilities:
|
||||
|
||||
1. **Memory Storage**: When Alex mentions her headaches and penicillin allergy, the agent stores this information in Mem0 using the `save_patient_info` tool.
|
||||
|
||||
2. **Contextual Retrieval**: When Alex asks about headache causes, the agent uses the `retrieve_patient_info` tool to recall her specific situation.
|
||||
|
||||
3. **Memory Application**: When discussing medications, the agent remembers Alex's penicillin allergy without her needing to repeat it, providing safer and more personalized advice.
|
||||
|
||||
4. **Conversation Continuity**: The agent maintains context across the entire conversation session, creating a more natural and efficient interaction.
|
||||
|
||||
## Key Implementation Details
|
||||
|
||||
### User ID Management
|
||||
|
||||
Instead of passing the user ID as a parameter to the memory tools (which would require modifying the ADK's tool calling system), we attach it directly to the function object:
|
||||
|
||||
```python
|
||||
# Set user_id for tools to access
|
||||
save_patient_info.user_id = user_id
|
||||
retrieve_patient_info.user_id = user_id
|
||||
```
|
||||
|
||||
Inside the tool functions, we retrieve this attribute:
|
||||
|
||||
```python
|
||||
# Get user_id from session state or use default
|
||||
user_id = getattr(save_patient_info, 'user_id', 'default_user')
|
||||
```
|
||||
|
||||
This approach allows our tools to maintain user context without complicating their parameter signatures.
|
||||
|
||||
### Mem0 Integration
|
||||
|
||||
The integration with Mem0 happens through two primary functions:
|
||||
|
||||
1. `mem0_client.add()` - Stores new information with appropriate metadata
|
||||
2. `mem0_client.search()` - Retrieves relevant memories using semantic search
|
||||
|
||||
The `threshold` parameter in the search function ensures that only highly relevant memories are returned.
|
||||
|
||||
## Conclusion
|
||||
|
||||
This example demonstrates how to build a healthcare assistant with persistent memory using Google ADK and Mem0. The integration allows for a more personalized patient experience by maintaining context across conversation turns, which is particularly valuable in healthcare scenarios where continuity of information is crucial.
|
||||
|
||||
By storing and retrieving patient information intelligently, the assistant provides more relevant responses without requiring the patient to repeat their medical history, symptoms, or preferences.
|
||||
@@ -0,0 +1,126 @@
|
||||
---
|
||||
title: Mem0 with Mastra
|
||||
---
|
||||
|
||||
In this example you'll learn how to use the Mem0 to add long-term memory capabilities to [Mastra's agent](https://mastra.ai/) via tool-use.
|
||||
This memory integration can work alongside Mastra's [agent memory features](https://mastra.ai/docs/agents/01-agent-memory).
|
||||
|
||||
You can find the complete example code in the [Mastra repository](https://github.com/mastra-ai/mastra/tree/main/examples/memory-with-mem0).
|
||||
|
||||
## Overview
|
||||
|
||||
This guide will show you how to integrate Mem0 with Mastra to add long-term memory capabilities to your agents. We'll create tools that allow agents to save and retrieve memories using Mem0's API.
|
||||
|
||||
### Installation
|
||||
|
||||
1. **Install the Integration Package**
|
||||
|
||||
To install the Mem0 integration, run:
|
||||
|
||||
```bash
|
||||
npm install @mastra/mem0
|
||||
```
|
||||
|
||||
2. **Add the Integration to Your Project**
|
||||
|
||||
Create a new file for your integrations and import the integration:
|
||||
|
||||
```typescript integrations/index.ts
|
||||
import { Mem0Integration } from "@mastra/mem0";
|
||||
|
||||
export const mem0 = new Mem0Integration({
|
||||
config: {
|
||||
apiKey: process.env.MEM0_API_KEY!,
|
||||
userId: "alice",
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
3. **Use the Integration in Tools or Workflows**
|
||||
|
||||
You can now use the integration when defining tools for your agents or in workflows.
|
||||
|
||||
```typescript tools/index.ts
|
||||
import { createTool } from "@mastra/core";
|
||||
import { z } from "zod";
|
||||
import { mem0 } from "../integrations";
|
||||
|
||||
export const mem0RememberTool = createTool({
|
||||
id: "Mem0-remember",
|
||||
description:
|
||||
"Remember your agent memories that you've previously saved using the Mem0-memorize tool.",
|
||||
inputSchema: z.object({
|
||||
question: z
|
||||
.string()
|
||||
.describe("Question used to look up the answer in saved memories."),
|
||||
}),
|
||||
outputSchema: z.object({
|
||||
answer: z.string().describe("Remembered answer"),
|
||||
}),
|
||||
execute: async ({ context }) => {
|
||||
console.log(`Searching memory "${context.question}"`);
|
||||
const memory = await mem0.searchMemory(context.question);
|
||||
console.log(`\nFound memory "${memory}"\n`);
|
||||
|
||||
return {
|
||||
answer: memory,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const mem0MemorizeTool = createTool({
|
||||
id: "Mem0-memorize",
|
||||
description:
|
||||
"Save information to mem0 so you can remember it later using the Mem0-remember tool.",
|
||||
inputSchema: z.object({
|
||||
statement: z.string().describe("A statement to save into memory"),
|
||||
}),
|
||||
execute: async ({ context }) => {
|
||||
console.log(`\nCreating memory "${context.statement}"\n`);
|
||||
// to reduce latency memories can be saved async without blocking tool execution
|
||||
void mem0.createMemory(context.statement).then(() => {
|
||||
console.log(`\nMemory "${context.statement}" saved.\n`);
|
||||
});
|
||||
return { success: true };
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
4. **Create a new agent**
|
||||
|
||||
```typescript agents/index.ts
|
||||
import { openai } from '@ai-sdk/openai';
|
||||
import { Agent } from '@mastra/core/agent';
|
||||
import { mem0MemorizeTool, mem0RememberTool } from '../tools';
|
||||
|
||||
export const mem0Agent = new Agent({
|
||||
name: 'Mem0 Agent',
|
||||
instructions: `
|
||||
You are a helpful assistant that has the ability to memorize and remember facts using Mem0.
|
||||
`,
|
||||
model: openai('gpt-4o'),
|
||||
tools: { mem0RememberTool, mem0MemorizeTool },
|
||||
});
|
||||
```
|
||||
|
||||
5. **Run the agent**
|
||||
|
||||
```typescript index.ts
|
||||
import { Mastra } from '@mastra/core/mastra';
|
||||
import { createLogger } from '@mastra/core/logger';
|
||||
|
||||
import { mem0Agent } from './agents';
|
||||
|
||||
export const mastra = new Mastra({
|
||||
agents: { mem0Agent },
|
||||
logger: createLogger({
|
||||
name: 'Mastra',
|
||||
level: 'error',
|
||||
}),
|
||||
});
|
||||
```
|
||||
|
||||
In the example above:
|
||||
- We import the `@mastra/mem0` integration.
|
||||
- We define two tools that uses the Mem0 API client to create new memories and recall previously saved memories.
|
||||
- The tool accepts `question` as an input and returns the memory as a string.
|
||||
@@ -0,0 +1,538 @@
|
||||
---
|
||||
title: 'Mem0 with OpenAI Agents SDK for Voice'
|
||||
description: 'Integrate memory capabilities into your voice agents using Mem0 and OpenAI Agents SDK'
|
||||
---
|
||||
|
||||
# Building Voice Agents with Memory using Mem0 and OpenAI Agents SDK
|
||||
|
||||
This guide demonstrates how to combine OpenAI's Agents SDK for voice applications with Mem0's memory capabilities to create a voice assistant that remembers user preferences and past interactions.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before you begin, make sure you have:
|
||||
|
||||
1. Installed OpenAI Agents SDK with voice dependencies:
|
||||
```bash
|
||||
pip install 'openai-agents[voice]'
|
||||
```
|
||||
|
||||
2. Installed Mem0 SDK:
|
||||
```bash
|
||||
pip install mem0ai
|
||||
```
|
||||
|
||||
3. Installed other required dependencies:
|
||||
```bash
|
||||
pip install numpy sounddevice pydantic
|
||||
```
|
||||
|
||||
4. Set up your API keys:
|
||||
- OpenAI API key for the Agents SDK
|
||||
- Mem0 API key from the Mem0 Platform
|
||||
|
||||
## Code Breakdown
|
||||
|
||||
Let's break down the key components of this implementation:
|
||||
|
||||
### 1. Setting Up Dependencies and Environment
|
||||
|
||||
```python
|
||||
# OpenAI Agents SDK imports
|
||||
from agents import (
|
||||
Agent,
|
||||
function_tool
|
||||
)
|
||||
from agents.voice import (
|
||||
AudioInput,
|
||||
SingleAgentVoiceWorkflow,
|
||||
VoicePipeline
|
||||
)
|
||||
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
|
||||
|
||||
# Mem0 imports
|
||||
from mem0 import AsyncMemoryClient
|
||||
|
||||
# Set up API keys (replace with your actual keys)
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
|
||||
# Define a global user ID for simplicity
|
||||
USER_ID = "voice_user"
|
||||
|
||||
# Initialize Mem0 client
|
||||
mem0_client = AsyncMemoryClient()
|
||||
```
|
||||
|
||||
This section handles:
|
||||
- Importing required modules from OpenAI Agents SDK and Mem0
|
||||
- Setting up environment variables for API keys
|
||||
- Defining a simple user identification system (using a global variable)
|
||||
- Initializing the Mem0 client that will handle memory operations
|
||||
|
||||
### 2. Memory Tools with Function Decorators
|
||||
|
||||
The `@function_tool` decorator transforms Python functions into callable tools for the OpenAI agent. Here are the key memory tools:
|
||||
|
||||
#### Storing User Memories
|
||||
|
||||
```python
|
||||
import logging
|
||||
|
||||
# Set up logging at the top of your file
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
force=True
|
||||
)
|
||||
logger = logging.getLogger("memory_voice_agent")
|
||||
|
||||
# Then use logger in your function tools
|
||||
@function_tool
|
||||
async def save_memories(
|
||||
memory: str
|
||||
) -> str:
|
||||
"""Store a user memory in memory."""
|
||||
# This will be visible in your console
|
||||
logger.debug(f"Saving memory: {memory} for user {USER_ID}")
|
||||
|
||||
# Store the preference in Mem0
|
||||
memory_content = f"User memory - {memory}"
|
||||
await mem0_client.add(
|
||||
memory_content,
|
||||
user_id=USER_ID,
|
||||
)
|
||||
|
||||
return f"I've saved your memory: {memory}"
|
||||
```
|
||||
|
||||
This function:
|
||||
- Takes a memory string
|
||||
- Creates a formatted memory string
|
||||
- Stores it in Mem0 using the `add()` method
|
||||
- Includes metadata to categorize the memory for easier retrieval
|
||||
- Returns a confirmation message that the agent will speak
|
||||
|
||||
#### Finding Relevant Memories
|
||||
|
||||
```python
|
||||
@function_tool
|
||||
async def search_memories(
|
||||
query: str
|
||||
) -> str:
|
||||
"""
|
||||
Find memories relevant to the current conversation.
|
||||
Args:
|
||||
query: The search query to find relevant memories
|
||||
"""
|
||||
print(f"Finding memories related to: {query}")
|
||||
results = await mem0_client.search(
|
||||
query,
|
||||
user_id=USER_ID,
|
||||
limit=5,
|
||||
threshold=0.7, # Higher threshold for more relevant results
|
||||
output_format="v1.1"
|
||||
)
|
||||
|
||||
# Format and return the results
|
||||
if not results.get('results', []):
|
||||
return "I don't have any relevant memories about this topic."
|
||||
|
||||
memories = [f"• {result['memory']}" for result in results.get('results', [])]
|
||||
return "Here's what I remember that might be relevant:\n" + "\n".join(memories)
|
||||
```
|
||||
|
||||
This tool:
|
||||
- Takes a search query string
|
||||
- Passes it to Mem0's semantic search to find related memories
|
||||
- Sets a threshold for relevance to ensure quality results
|
||||
- Returns a formatted list of relevant memories or a default message
|
||||
|
||||
### 3. Creating the Voice Agent
|
||||
|
||||
```python
|
||||
def create_memory_voice_agent():
|
||||
# Create the agent with memory-enabled tools
|
||||
agent = Agent(
|
||||
name="Memory Assistant",
|
||||
instructions=prompt_with_handoff_instructions(
|
||||
"""You're speaking to a human, so be polite and concise.
|
||||
Always respond in clear, natural English.
|
||||
You have the ability to remember information about the user.
|
||||
Use the save_memories tool when the user shares an important information worth remembering.
|
||||
Use the search_memories tool when you need context from past conversations or user asks you to recall something.
|
||||
""",
|
||||
),
|
||||
model="gpt-4o",
|
||||
tools=[save_memories, search_memories],
|
||||
)
|
||||
|
||||
return agent
|
||||
```
|
||||
|
||||
This function:
|
||||
- Creates an OpenAI Agent with specific instructions
|
||||
- Configures it to use gpt-4o (you can use other models)
|
||||
- Registers the memory-related tools with the agent
|
||||
- Uses `prompt_with_handoff_instructions` to include standard voice agent behaviors
|
||||
|
||||
### 4. Microphone Recording Functionality
|
||||
|
||||
```python
|
||||
async def record_from_microphone(duration=5, samplerate=24000):
|
||||
"""Record audio from the microphone for a specified duration."""
|
||||
print(f"Recording for {duration} seconds...")
|
||||
|
||||
# Create a buffer to store the recorded audio
|
||||
frames = []
|
||||
|
||||
# Callback function to store audio data
|
||||
def callback(indata, frames_count, time_info, status):
|
||||
frames.append(indata.copy())
|
||||
|
||||
# Start recording
|
||||
with sd.InputStream(samplerate=samplerate, channels=1, callback=callback, dtype=np.int16):
|
||||
await asyncio.sleep(duration)
|
||||
|
||||
# Combine all frames into a single numpy array
|
||||
audio_data = np.concatenate(frames)
|
||||
return audio_data
|
||||
```
|
||||
|
||||
This function:
|
||||
- Creates a simple asynchronous microphone recording function
|
||||
- Uses the sounddevice library to capture audio input
|
||||
- Stores frames in a buffer during recording
|
||||
- Combines frames into a single numpy array when complete
|
||||
- Returns the audio data for processing
|
||||
|
||||
### 5. Main Loop and Voice Processing
|
||||
|
||||
```python
|
||||
async def main():
|
||||
# Create the agent
|
||||
agent = create_memory_voice_agent()
|
||||
|
||||
# Set up the voice pipeline
|
||||
pipeline = VoicePipeline(
|
||||
workflow=SingleAgentVoiceWorkflow(agent)
|
||||
)
|
||||
|
||||
# Configure TTS settings
|
||||
pipeline.config.tts_settings.voice = "alloy"
|
||||
pipeline.config.tts_settings.speed = 1.0
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Get user input
|
||||
print("\nPress Enter to start recording (or 'q' to quit)...")
|
||||
user_input = input()
|
||||
if user_input.lower() == 'q':
|
||||
break
|
||||
|
||||
# Record and process audio
|
||||
audio_data = await record_from_microphone(duration=5)
|
||||
audio_input = AudioInput(buffer=audio_data)
|
||||
result = await pipeline.run(audio_input)
|
||||
|
||||
# Play response and handle events
|
||||
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
|
||||
player.start()
|
||||
|
||||
agent_response = ""
|
||||
print("\nAgent response:")
|
||||
|
||||
async for event in result.stream():
|
||||
if event.type == "voice_stream_event_audio":
|
||||
player.write(event.data)
|
||||
elif event.type == "voice_stream_event_content":
|
||||
content = event.data
|
||||
agent_response += content
|
||||
print(content, end="", flush=True)
|
||||
|
||||
# Save the agent's response to memory
|
||||
if agent_response:
|
||||
try:
|
||||
await mem0_client.add(
|
||||
f"Agent response: {agent_response}",
|
||||
user_id=USER_ID,
|
||||
metadata={"type": "agent_response"}
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Failed to store memory: {e}")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
```
|
||||
|
||||
This main function orchestrates the entire process:
|
||||
1. Creates the memory-enabled voice agent
|
||||
2. Sets up the voice pipeline with TTS settings
|
||||
3. Implements an interactive loop for recording and processing voice input
|
||||
4. Handles streaming of response events (both audio and text)
|
||||
5. Automatically saves the agent's responses to memory
|
||||
6. Includes proper error handling and exit mechanisms
|
||||
|
||||
## Create a Memory-Enabled Voice Agent
|
||||
|
||||
Now that we've explained each component, here's the complete implementation that combines OpenAI Agents SDK for voice with Mem0's memory capabilities:
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
import os
|
||||
import logging
|
||||
from typing import Optional, List, Dict, Any
|
||||
import numpy as np
|
||||
import sounddevice as sd
|
||||
from pydantic import BaseModel
|
||||
|
||||
# OpenAI Agents SDK imports
|
||||
from agents import (
|
||||
Agent,
|
||||
function_tool
|
||||
)
|
||||
from agents.voice import (
|
||||
AudioInput,
|
||||
SingleAgentVoiceWorkflow,
|
||||
VoicePipeline
|
||||
)
|
||||
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
|
||||
|
||||
# Mem0 imports
|
||||
from mem0 import AsyncMemoryClient
|
||||
|
||||
# Set up API keys (replace with your actual keys)
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
|
||||
# Define a global user ID for simplicity
|
||||
USER_ID = "voice_user"
|
||||
|
||||
# Initialize Mem0 client
|
||||
mem0_client = AsyncMemoryClient()
|
||||
|
||||
# Create tools that utilize Mem0's memory
|
||||
@function_tool
|
||||
async def save_memories(
|
||||
memory: str
|
||||
) -> str:
|
||||
"""
|
||||
Store a user memory in memory.
|
||||
Args:
|
||||
memory: The memory to save
|
||||
"""
|
||||
print(f"Saving memory: {memory} for user {USER_ID}")
|
||||
|
||||
# Store the preference in Mem0
|
||||
memory_content = f"User memory - {memory}"
|
||||
await mem0_client.add(
|
||||
memory_content,
|
||||
user_id=USER_ID,
|
||||
)
|
||||
|
||||
return f"I've saved your memory: {memory}"
|
||||
|
||||
@function_tool
|
||||
async def search_memories(
|
||||
query: str
|
||||
) -> str:
|
||||
"""
|
||||
Find memories relevant to the current conversation.
|
||||
Args:
|
||||
query: The search query to find relevant memories
|
||||
"""
|
||||
print(f"Finding memories related to: {query}")
|
||||
results = await mem0_client.search(
|
||||
query,
|
||||
user_id=USER_ID,
|
||||
limit=5,
|
||||
threshold=0.7, # Higher threshold for more relevant results
|
||||
output_format="v1.1"
|
||||
)
|
||||
|
||||
# Format and return the results
|
||||
if not results.get('results', []):
|
||||
return "I don't have any relevant memories about this topic."
|
||||
|
||||
memories = [f"• {result['memory']}" for result in results.get('results', [])]
|
||||
return "Here's what I remember that might be relevant:\n" + "\n".join(memories)
|
||||
|
||||
# Create the agent with memory-enabled tools
|
||||
def create_memory_voice_agent():
|
||||
# Create the agent with memory-enabled tools
|
||||
agent = Agent(
|
||||
name="Memory Assistant",
|
||||
instructions=prompt_with_handoff_instructions(
|
||||
"""You're speaking to a human, so be polite and concise.
|
||||
Always respond in clear, natural English.
|
||||
You have the ability to remember information about the user.
|
||||
Use the save_memories tool when the user shares an important information worth remembering.
|
||||
Use the search_memories tool when you need context from past conversations or user asks you to recall something.
|
||||
""",
|
||||
),
|
||||
model="gpt-4o",
|
||||
tools=[save_memories, search_memories],
|
||||
)
|
||||
|
||||
return agent
|
||||
|
||||
async def record_from_microphone(duration=5, samplerate=24000):
|
||||
"""Record audio from the microphone for a specified duration."""
|
||||
print(f"Recording for {duration} seconds...")
|
||||
|
||||
# Create a buffer to store the recorded audio
|
||||
frames = []
|
||||
|
||||
# Callback function to store audio data
|
||||
def callback(indata, frames_count, time_info, status):
|
||||
frames.append(indata.copy())
|
||||
|
||||
# Start recording
|
||||
with sd.InputStream(samplerate=samplerate, channels=1, callback=callback, dtype=np.int16):
|
||||
await asyncio.sleep(duration)
|
||||
|
||||
# Combine all frames into a single numpy array
|
||||
audio_data = np.concatenate(frames)
|
||||
return audio_data
|
||||
|
||||
async def main():
|
||||
print("Starting Memory Voice Agent")
|
||||
|
||||
# Create the agent and context
|
||||
agent = create_memory_voice_agent()
|
||||
|
||||
# Set up the voice pipeline
|
||||
pipeline = VoicePipeline(
|
||||
workflow=SingleAgentVoiceWorkflow(agent)
|
||||
)
|
||||
|
||||
# Configure TTS settings
|
||||
pipeline.config.tts_settings.voice = "alloy"
|
||||
pipeline.config.tts_settings.speed = 1.0
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Get user input
|
||||
print("\nPress Enter to start recording (or 'q' to quit)...")
|
||||
user_input = input()
|
||||
if user_input.lower() == 'q':
|
||||
break
|
||||
|
||||
# Record and process audio
|
||||
audio_data = await record_from_microphone(duration=5)
|
||||
audio_input = AudioInput(buffer=audio_data)
|
||||
|
||||
print("Processing your request...")
|
||||
|
||||
# Process the audio input
|
||||
result = await pipeline.run(audio_input)
|
||||
|
||||
# Create an audio player
|
||||
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
|
||||
player.start()
|
||||
|
||||
# Store the agent's response for adding to memory
|
||||
agent_response = ""
|
||||
|
||||
print("\nAgent response:")
|
||||
# Play the audio stream as it comes in
|
||||
async for event in result.stream():
|
||||
if event.type == "voice_stream_event_audio":
|
||||
player.write(event.data)
|
||||
elif event.type == "voice_stream_event_content":
|
||||
# Accumulate and print the text response
|
||||
content = event.data
|
||||
agent_response += content
|
||||
print(content, end="", flush=True)
|
||||
|
||||
print("\n")
|
||||
|
||||
# Example of saving the conversation to Mem0 after completion
|
||||
if agent_response:
|
||||
try:
|
||||
await mem0_client.add(
|
||||
f"Agent response: {agent_response}",
|
||||
user_id=USER_ID,
|
||||
metadata={"type": "agent_response"}
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Failed to store memory: {e}")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## Key Features of This Implementation
|
||||
|
||||
This implementation offers several key features:
|
||||
|
||||
1. **Simplified User Management**: Uses a global `USER_ID` variable for simplicity, but can be extended to manage multiple users.
|
||||
|
||||
2. **Real Microphone Input**: Includes a `record_from_microphone()` function that captures actual voice input from your microphone.
|
||||
|
||||
3. **Interactive Voice Loop**: Implements a continuous interaction loop, allowing for multiple back-and-forth exchanges.
|
||||
|
||||
4. **Memory Management Tools**:
|
||||
- `save_memories`: Stores user memories in Mem0
|
||||
- `search_memories`: Searches for relevant past information
|
||||
|
||||
5. **Voice Configuration**: Demonstrates how to configure TTS settings for the voice response.
|
||||
|
||||
## Running the Example
|
||||
|
||||
To run this example:
|
||||
|
||||
1. Replace the placeholder API keys with your actual keys
|
||||
2. Make sure your microphone is properly connected
|
||||
3. Run the script with Python 3.8 or newer
|
||||
4. Press Enter to start recording, then speak your request
|
||||
5. Press 'q' to quit the application
|
||||
|
||||
The agent will listen to your request, process it through the OpenAI model, utilize Mem0 for memory operations as needed, and respond both through text output and voice speech.
|
||||
|
||||
## Best Practices for Voice Agents with Memory
|
||||
|
||||
1. **Optimizing Memory for Voice**: Keep memories concise and relevant for voice responses.
|
||||
|
||||
2. **Forgetting Mechanism**: Implement a way to delete or expire memories that are no longer relevant.
|
||||
|
||||
3. **Context Preservation**: Store enough context with each memory to make retrieval effective.
|
||||
|
||||
4. **Error Handling**: Implement robust error handling for memory operations, as voice interactions should continue smoothly even if memory operations fail.
|
||||
|
||||
## Conclusion
|
||||
|
||||
By combining OpenAI's Agents SDK with Mem0's memory capabilities, you can create voice agents that maintain persistent memory of user preferences and past interactions. This significantly enhances the user experience by making conversations more natural and personalized.
|
||||
|
||||
As you build your voice application, experiment with different memory strategies and filtering approaches to find the optimal balance between comprehensive memory and efficient retrieval for your specific use case.
|
||||
|
||||
## Debugging Function Tools
|
||||
|
||||
When working with the OpenAI Agents SDK, you might notice that regular `print()` statements inside `@function_tool` decorated functions don't appear in your console output. This is because the Agents SDK captures and redirects standard output when executing these functions.
|
||||
|
||||
To effectively debug your function tools, use Python's `logging` module instead:
|
||||
|
||||
```python
|
||||
import logging
|
||||
|
||||
# Set up logging at the top of your file
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
force=True
|
||||
)
|
||||
logger = logging.getLogger("memory_voice_agent")
|
||||
|
||||
# Then use logger in your function tools
|
||||
@function_tool
|
||||
async def save_memories(
|
||||
memory: str
|
||||
) -> str:
|
||||
"""Store a user memory in memory."""
|
||||
# This will be visible in your console
|
||||
logger.debug(f"Saving memory: {memory} for user {USER_ID}")
|
||||
|
||||
# Rest of your function...
|
||||
```
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
title: Mem0 with Ollama
|
||||
---
|
||||
|
||||
## Running Mem0 Locally with Ollama
|
||||
|
||||
Mem0 can be utilized entirely locally by leveraging Ollama for both the embedding model and the language model (LLM). This guide will walk you through the necessary steps and provide the complete code to get you started.
|
||||
|
||||
### Overview
|
||||
|
||||
By using Ollama, you can run Mem0 locally, which allows for greater control over your data and models. This setup uses Ollama for both the embedding model and the language model, providing a fully local solution.
|
||||
|
||||
### Setup
|
||||
|
||||
Before you begin, ensure you have Mem0 and Ollama installed and properly configured on your local machine.
|
||||
|
||||
### Full Code Example
|
||||
|
||||
Below is the complete code to set up and use Mem0 locally with Ollama:
|
||||
|
||||
```python
|
||||
from mem0 import Memory
|
||||
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"collection_name": "test",
|
||||
"host": "localhost",
|
||||
"port": 6333,
|
||||
"embedding_model_dims": 768, # Change this according to your local model's dimensions
|
||||
},
|
||||
},
|
||||
"llm": {
|
||||
"provider": "ollama",
|
||||
"config": {
|
||||
"model": "llama3.1:latest",
|
||||
"temperature": 0,
|
||||
"max_tokens": 2000,
|
||||
"ollama_base_url": "http://localhost:11434", # Ensure this URL is correct
|
||||
},
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "ollama",
|
||||
"config": {
|
||||
"model": "nomic-embed-text:latest",
|
||||
# Alternatively, you can use "snowflake-arctic-embed:latest"
|
||||
"ollama_base_url": "http://localhost:11434",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Initialize Memory with the configuration
|
||||
m = Memory.from_config(config)
|
||||
|
||||
# Add a memory
|
||||
m.add("I'm visiting Paris", user_id="john")
|
||||
|
||||
# Retrieve memories
|
||||
memories = m.get_all(user_id="john")
|
||||
```
|
||||
|
||||
### Key Points
|
||||
|
||||
- **Configuration**: The setup involves configuring the vector store, language model, and embedding model to use local resources.
|
||||
- **Vector Store**: Qdrant is used as the vector store, running on localhost.
|
||||
- **Language Model**: Ollama is used as the LLM provider, with the "llama3.1:latest" model.
|
||||
- **Embedding Model**: Ollama is also used for embeddings, with the "nomic-embed-text:latest" model.
|
||||
|
||||
### Conclusion
|
||||
|
||||
This local setup of Mem0 using Ollama provides a fully self-contained solution for memory management and AI interactions. It allows for greater control over your data and models while still leveraging the powerful capabilities of Mem0.
|
||||
@@ -0,0 +1,218 @@
|
||||
---
|
||||
title: Memory-Guided Content Writing
|
||||
---
|
||||
|
||||
This guide demonstrates how to leverage **Mem0** to streamline content writing by applying your unique writing style and preferences using persistent memory.
|
||||
|
||||
## Why Use Mem0?
|
||||
|
||||
Integrating Mem0 into your writing workflow helps you:
|
||||
|
||||
1. **Store persistent writing preferences** ensuring consistent tone, formatting, and structure.
|
||||
2. **Automate content refinement** by retrieving preferences when rewriting or reviewing content.
|
||||
3. **Scale your writing style** so it applies consistently across multiple documents or sessions.
|
||||
|
||||
## Setup
|
||||
|
||||
```python
|
||||
import os
|
||||
from openai import OpenAI
|
||||
from mem0 import MemoryClient
|
||||
|
||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
|
||||
|
||||
# Set up Mem0 and OpenAI client
|
||||
client = MemoryClient()
|
||||
openai = OpenAI()
|
||||
|
||||
USER_ID = "content_writer"
|
||||
RUN_ID = "smart_editing_session"
|
||||
```
|
||||
|
||||
## **Storing Your Writing Preferences in Mem0**
|
||||
|
||||
```python
|
||||
def store_writing_preferences():
|
||||
"""Store your writing preferences in Mem0."""
|
||||
|
||||
preferences = """My writing preferences:
|
||||
1. Use headings and sub-headings for structure.
|
||||
2. Keep paragraphs concise (8–10 sentences max).
|
||||
3. Incorporate specific numbers and statistics.
|
||||
4. Provide concrete examples.
|
||||
5. Use bullet points for clarity.
|
||||
6. Avoid jargon and buzzwords."""
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Here are my writing style preferences."},
|
||||
{"role": "assistant", "content": preferences}
|
||||
]
|
||||
|
||||
response = client.add(
|
||||
messages,
|
||||
user_id=USER_ID,
|
||||
run_id=RUN_ID,
|
||||
metadata={"type": "preferences", "category": "writing_style"}
|
||||
)
|
||||
|
||||
return response
|
||||
```
|
||||
|
||||
## **Editing Content Using Stored Preferences**
|
||||
|
||||
```python
|
||||
def apply_writing_style(original_content):
|
||||
"""Use preferences stored in Mem0 to guide content rewriting."""
|
||||
|
||||
results = client.search(
|
||||
query="What are my writing style preferences?",
|
||||
version="v2",
|
||||
filters={
|
||||
"AND": [
|
||||
{
|
||||
"user_id": USER_ID
|
||||
},
|
||||
{
|
||||
"run_id": RUN_ID
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
if not results:
|
||||
print("No preferences found.")
|
||||
return None
|
||||
|
||||
preferences = "\n".join(r["memory"] for r in results.get('results', []))
|
||||
|
||||
system_prompt = f"""
|
||||
You are a writing assistant.
|
||||
|
||||
Apply the following writing style preferences to improve the user's content:
|
||||
|
||||
Preferences:
|
||||
{preferences}
|
||||
"""
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"""Original Content:
|
||||
{original_content}"""}
|
||||
]
|
||||
|
||||
response = openai.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=messages
|
||||
)
|
||||
clean_response = response.choices[0].message.content.strip()
|
||||
|
||||
return clean_response
|
||||
```
|
||||
|
||||
## **Complete Workflow: Content Editing**
|
||||
|
||||
```python
|
||||
def content_writing_workflow(content):
|
||||
"""Automated workflow for editing a document based on writing preferences."""
|
||||
|
||||
# Store writing preferences (if not already stored)
|
||||
store_writing_preferences() # Ideally done once, or with a conditional check
|
||||
|
||||
# Edit the document with Mem0 preferences
|
||||
edited_content = apply_writing_style(content)
|
||||
|
||||
if not edited_content:
|
||||
return "Failed to edit document."
|
||||
|
||||
# Display results
|
||||
print("\n=== ORIGINAL DOCUMENT ===\n")
|
||||
print(content)
|
||||
|
||||
print("\n=== EDITED DOCUMENT ===\n")
|
||||
print(edited_content)
|
||||
|
||||
return edited_content
|
||||
```
|
||||
|
||||
## **Example Usage**
|
||||
|
||||
```python
|
||||
# Define your document
|
||||
original_content = """Project Proposal
|
||||
|
||||
The following proposal outlines our strategy for the Q3 marketing campaign.
|
||||
We believe this approach will significantly increase our market share.
|
||||
|
||||
Increase brand awareness
|
||||
Boost sales by 15%
|
||||
Expand our social media following
|
||||
|
||||
We plan to launch the campaign in July and continue through September.
|
||||
"""
|
||||
|
||||
# Run the workflow
|
||||
result = content_writing_workflow(original_content)
|
||||
```
|
||||
|
||||
## **Expected Output**
|
||||
|
||||
Your document will be transformed into a structured, well-formatted version based on your preferences.
|
||||
|
||||
### **Original Document**
|
||||
```
|
||||
Project Proposal
|
||||
|
||||
The following proposal outlines our strategy for the Q3 marketing campaign.
|
||||
We believe this approach will significantly increase our market share.
|
||||
|
||||
Increase brand awareness
|
||||
Boost sales by 15%
|
||||
Expand our social media following
|
||||
|
||||
We plan to launch the campaign in July and continue through September.
|
||||
```
|
||||
|
||||
### **Edited Document**
|
||||
```
|
||||
# **Project Proposal**
|
||||
|
||||
## **Q3 Marketing Campaign Strategy**
|
||||
|
||||
This proposal outlines our strategy for the Q3 marketing campaign. We aim to significantly increase our market share with this approach.
|
||||
|
||||
### **Objectives**
|
||||
|
||||
- **Increase Brand Awareness**: Implement targeted advertising and community engagement to enhance visibility.
|
||||
- **Boost Sales by 15%**: Increase sales by 15% compared to Q2 figures.
|
||||
- **Expand Social Media Following**: Grow our social media audience by 20%.
|
||||
|
||||
### **Timeline**
|
||||
|
||||
- **Launch Date**: July
|
||||
- **Duration**: July – September
|
||||
|
||||
### **Key Actions**
|
||||
|
||||
- **Targeted Advertising**: Utilize platforms like Google Ads and Facebook to reach specific demographics.
|
||||
- **Community Engagement**: Host webinars and live Q&A sessions.
|
||||
- **Content Creation**: Produce engaging videos and infographics.
|
||||
|
||||
### **Supporting Data**
|
||||
|
||||
- **Previous Campaign Success**: Our Q2 campaign increased sales by 12%. We will refine similar strategies for Q3.
|
||||
- **Social Media Growth**: Last year, our Instagram followers grew by 25% during a similar campaign.
|
||||
|
||||
### **Conclusion**
|
||||
|
||||
We believe this strategy will effectively increase our market share. To achieve these goals, we need your support and collaboration. Let’s work together to make this campaign a success. Please review the proposal and provide your feedback by the end of the week.
|
||||
```
|
||||
|
||||
Mem0 enables a seamless, intelligent content-writing workflow, perfect for content creators, marketers, and technical writers looking to scale their personal tone and structure across work.
|
||||
|
||||
## Help & Resources
|
||||
|
||||
- [Mem0 Platform](https://app.mem0.ai/)
|
||||
|
||||
<Snippet file="get-help.mdx" />
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user