knowledge-base/docs/examples-mem0-with-ollama.md
Pratik Narola 7689409950 Initial commit: Production-ready Mem0 interface with monitoring
- Complete Mem0 OSS integration with hybrid datastore
- PostgreSQL + pgvector for vector storage
- Neo4j 5.18 for graph relationships
- Google Gemini embeddings integration
- Comprehensive monitoring with correlation IDs
- Real-time statistics and performance tracking
- Production-grade observability features
- Clean repository with no exposed secrets
2025-08-10 17:34:41 +05:30

123 lines
No EOL
4.2 KiB
Markdown

# Running Mem0 Locally with Ollama
## Overview
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.
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.
### Prerequisites
1. **Install Mem0**: `pip install mem0ai`
2. **Install and Configure Ollama**: Ensure Ollama is running on your local machine
3. **Install Qdrant**: Set up Qdrant as your vector store
4. **Pull Required Models**: Download the necessary Ollama models
## Full Code Example
Below is the complete code to set up and use Mem0 locally with Ollama:
```python
import os
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")
```
## Configuration Details
### Vector Store Configuration
- **Provider**: Qdrant (local instance)
- **Host**: localhost
- **Port**: 6333
- **Collection**: test
- **Embedding Dimensions**: 768 (adjust based on your chosen embedding model)
### Language Model Configuration
- **Provider**: Ollama
- **Model**: llama3.1:latest
- **Temperature**: 0 (deterministic responses)
- **Max Tokens**: 2000
- **Base URL**: http://localhost:11434
### Embedding Model Configuration
- **Provider**: Ollama
- **Model**: nomic-embed-text:latest
- **Alternative**: snowflake-arctic-embed:latest
- **Base URL**: http://localhost:11434
## Key Points
1. **Configuration**: The setup involves configuring the vector store, language model, and embedding model to use local resources.
2. **Vector Store**: Qdrant is used as the vector store, running on localhost.
3. **Language Model**: Ollama is used as the LLM provider, with the "llama3.1:latest" model.
4. **Embedding Model**: Ollama is also used for embeddings, with the "nomic-embed-text:latest" model.
5. **Local Control**: This setup provides complete control over your data and models without external dependencies.
## Model Options
### Embedding Models
- **nomic-embed-text:latest**: Recommended for general text embedding
- **snowflake-arctic-embed:latest**: Alternative embedding model
### Language Models
- **llama3.1:latest**: Current recommended model
- Other Ollama-supported models can be used based on your requirements
## Benefits of Local Setup
1. **Data Privacy**: All data processing happens locally
2. **No API Costs**: No external API usage fees
3. **Offline Operation**: Works without internet connectivity
4. **Custom Models**: Use any Ollama-supported models
5. **Full Control**: Complete control over model parameters and data flow
## 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.
The combination of Mem0's memory management capabilities with Ollama's local AI models creates a powerful, privacy-focused solution for building AI applications with persistent memory.