A Django implementation of the mem0 memory system
Project description
Django Mem0 Client
A Django implementation of the mem0 memory system, using Django models for storing memory history instead of SQLite directly.
Overview
This project provides a Django-based client for the mem0 memory system. It maintains full compatibility with the original mem0 implementation while leveraging Django's ORM capabilities for memory history storage.
Key benefits:
- Integration with Django's powerful ORM
- Admin interface for viewing and managing memory history
- Seamless integration with existing Django applications
- Maintains all vector store and embedding functionality from mem0
Installation
Installation from PyPI
The easiest way to install is from PyPI:
pip install django-mem0-client
Prerequisites
- Python 3.8+
- Django 4.0+
- mem0 library
Setting Up in Your Django Project
- Add 'mem0client' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
# ...
'mem0client', # Not 'django-mem0-client'
# ...
]
- Apply migrations to set up the database:
python manage.py migrate
- Create a superuser for the admin interface (optional):
python manage.py createsuperuser
Usage
Basic Usage
import os
import django
from mem0.configs.base import MemoryConfig
# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
django.setup()
# Import after Django setup
from mem0client.memory_client import DjangoMemory
# Create a memory client with default configuration
memory = DjangoMemory()
# Add a memory
result = memory.add(
messages="John likes to play tennis on Tuesdays with his friend Mike.",
user_id="user123",
agent_id="agent456"
)
# Search for memories
search_results = memory.search(query="tennis", user_id="user123")
# Get memory history
memory_id = search_results["results"][0]["id"]
history = memory.history(memory_id)
Using Configuration Options
You can customize the memory client using the same configuration options as the original mem0 library:
from mem0.configs.base import MemoryConfig
from mem0client.memory_client import DjangoMemory
# Create a configuration with custom settings
config = MemoryConfig(
embedding_model="text-embedding-3-small", # OpenAI embedding model to use
vector_store_path="./vector_store", # Path to store vectors
distance_metric="cosine", # Distance metric for vector search
add_timestamps=True, # Add timestamps to memories
chunk_size=1000, # Size of text chunks
chunk_overlap=200, # Overlap between chunks
)
# Create a memory client with custom configuration
memory = DjangoMemory(config)
Using from_config Method
You can also initialize the client using a dictionary of configuration options:
from mem0client.memory_client import DjangoMemory
# Configuration as a dictionary
config_dict = {
"embedding_model": "text-embedding-3-small",
"vector_store_path": "./vector_store",
"distance_metric": "cosine",
"add_timestamps": True,
"chunk_size": 1000,
"chunk_overlap": 200,
}
# Create a memory client from config dictionary
memory = DjangoMemory.from_config(config_dict)
Refer to the official mem0 documentation for a complete list of configuration options and their meanings.
Running the Example Script
python example_usage.py
Using the Admin Interface
- Start the Django development server:
python manage.py runserver
-
Open your browser and go to
http://127.0.0.1:8000/admin/ -
Log in with your superuser credentials
-
Navigate to the "Memory histories" section to view and manage memory history entries
API Reference
The DjangoMemory class implements the MemoryBase interface from mem0 and provides the following methods:
add(messages, user_id=None, agent_id=None, run_id=None, metadata=None, filters=None, prompt=None): Create a new memoryget(memory_id): Retrieve a memory by IDget_all(user_id=None, agent_id=None, run_id=None, limit=100): List all memoriessearch(query, user_id=None, agent_id=None, run_id=None, limit=100, filters=None): Search for memoriesupdate(memory_id, data): Update a memory by IDdelete(memory_id): Delete a memory by IDdelete_all(user_id=None, agent_id=None, run_id=None): Delete all memories matching filtershistory(memory_id): Get the history of changes for a memoryreset(): Reset the memory store
Detailed API documentation can be found in the mem0 official documentation.
Configuration
The client accepts the same configuration parameters as the original mem0 Memory class through the MemoryConfig object. See the mem0 configuration documentation for details.
Integration with Existing Django Projects
To integrate with an existing Django project:
- Add 'mem0client' to your
INSTALLED_APPSin settings.py:
INSTALLED_APPS = [
# ...
'mem0client',
# ...
]
- Run migrations:
python manage.py migrate
- Import and use the
DjangoMemoryclass in your views or services:
from mem0client.memory_client import DjangoMemory
# Create a memory client
memory = DjangoMemory()
# Use memory client methods...
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
Development
For local development:
- Clone the repository
- Create a virtual environment:
python -m venv venv - Activate the virtual environment:
- Windows:
venv\Scripts\activate - Unix/MacOS:
source venv/bin/activate
- Windows:
- Install in development mode:
pip install -e . - Run tests:
python manage.py test mem0client
Publishing to PyPI
For maintainers who want to publish new versions to PyPI:
- Update the version number in
setup.pyand__init__.py - Create a new distribution:
python setup.py sdist bdist_wheel
- Upload to PyPI:
python -m twine upload dist/*
Note: You'll need twine installed (pip install twine) and PyPI credentials configured.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_mem0_client-0.1.2.tar.gz.
File metadata
- Download URL: django_mem0_client-0.1.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
430b24c284a887f7819de9c2ff0546b4ec038f5d341442cf277572fe923fd1f0
|
|
| MD5 |
8241ae256d8fc0bee5cb7b95106d83f9
|
|
| BLAKE2b-256 |
f28e06fd47cafee00207d654aae197143af11f73cdeb16451946242631ea6903
|
File details
Details for the file django_mem0_client-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_mem0_client-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30ec14f2e62441c5a4739a6c34a584b66c0538b99e611b9e4f52c2f3e85d92ae
|
|
| MD5 |
b6fdcddfd842b21a6c97f89686f77312
|
|
| BLAKE2b-256 |
b65e204a7587f5c591c3aedb43103d45ca35217f170145b1dc92f530e884ce0b
|