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.0.tar.gz (7.9 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.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_mem0_client-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 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.0.tar.gz
Algorithm Hash digest
SHA256 8cc5af5d3b9c008df78c1ff6e851d320cbb2e54c3c22f993ee0acdcd1839fa08
MD5 99a99f40d77662a3077bc1ece564a015
BLAKE2b-256 abd6b557087a7a5c2ce45958cabc45d2cce41c07f825e4eeb627f2088427fe5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_mem0_client-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1055b44664cf3571e4baa0a869d0a1e5214f63f89848680219c9ccfdf8602ba
MD5 f1740d52e310ec70fa26f791f3b1a1eb
BLAKE2b-256 1563a8d357a6ad86cc2f4178f89536fc22bb8ca38ed61dabb5ad085bf75d5c51

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