Skip to main content

A Django implementation of the mem0 memory system

Project description

Django Mem0 Client

PyPI version License: MIT

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

  1. Add 'mem0client' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # ...
    'mem0client',  # Not 'django-mem0-client'
    # ...
]
  1. Apply migrations to set up the database:
python manage.py migrate
  1. 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

  1. Start the Django development server:
python manage.py runserver
  1. Open your browser and go to http://127.0.0.1:8000/admin/

  2. Log in with your superuser credentials

  3. 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 memory
  • get(memory_id): Retrieve a memory by ID
  • get_all(user_id=None, agent_id=None, run_id=None, limit=100): List all memories
  • search(query, user_id=None, agent_id=None, run_id=None, limit=100, filters=None): Search for memories
  • update(memory_id, data): Update a memory by ID
  • delete(memory_id): Delete a memory by ID
  • delete_all(user_id=None, agent_id=None, run_id=None): Delete all memories matching filters
  • history(memory_id): Get the history of changes for a memory
  • reset(): 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:

  1. Add 'mem0client' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # ...
    'mem0client',
    # ...
]
  1. Run migrations:
python manage.py migrate
  1. Import and use the DjangoMemory class 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:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request

Development

For local development:

  1. Clone the repository
  2. Create a virtual environment: python -m venv venv
  3. Activate the virtual environment:
    • Windows: venv\Scripts\activate
    • Unix/MacOS: source venv/bin/activate
  4. Install in development mode: pip install -e .
  5. Run tests: python manage.py test mem0client

Publishing to PyPI

For maintainers who want to publish new versions to PyPI:

  1. Update the version number in setup.py and __init__.py
  2. Create a new distribution:
    python setup.py sdist bdist_wheel
    
  3. 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

django_mem0_client-0.2.2.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_mem0_client-0.2.2-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file django_mem0_client-0.2.2.tar.gz.

File metadata

  • Download URL: django_mem0_client-0.2.2.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_mem0_client-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7caca0bddf51a0344400c0fbe51d0273a17849f7e36105baebc1029b041580ae
MD5 328fec9df9d70d7c7167a6be3ff00350
BLAKE2b-256 0cf3b890026ffdf25bd6a38ab81f597c05c24040e3a74ce1142aa1efcde1836a

See more details on using hashes here.

File details

Details for the file django_mem0_client-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mem0_client-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8214e125fd73475084969427e949877289245814ef42066ed37e984be12682d
MD5 19446209cbe6e1bd42356e4de2ab49ef
BLAKE2b-256 cd2a1318178ed968c192b714ef86117f315b12ddcdf043fc6a1637fc4b65f823

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page