Skip to main content

Long-term memory system for AI conversations - similar to Mem0/Supermemory

Project description

ContextMemory

Long-term memory system for AI conversations - similar to Mem0/Supermemory.

ContextMemory extracts, stores, and retrieves important facts from conversations, enabling AI assistants to remember user preferences, context, and history across sessions.

Installation

pip install contextmemory

Quick Start

1. Configure

Option A: Programmatic (Recommended)

from contextmemory import configure

configure(
    openai_api_key="sk-...",  # Required
    database_url="postgresql://...",  # Optional - defaults to SQLite
)

Option B: Environment Variables

export OPENAI_API_KEY="sk-..."
export DATABASE_URL="postgresql://..."  # Optional

2. Initialize Database

from contextmemory import create_table

create_table()  # Creates all required tables

3. Use Memory System

from contextmemory import Memory, SessionLocal

# Create session and memory instance
db = SessionLocal()
memory = Memory(db)

# Add memories from a conversation
messages = [
    {"role": "user", "content": "Hi, I'm John and I love Python programming"},
    {"role": "assistant", "content": "Nice to meet you John! Python is great."},
]
memory.add(messages=messages, conversation_id=1)

# Search memories
results = memory.search(
    query="What programming language does the user like?",
    conversation_id=1,
    limit=5
)
print(results)
# {'query': '...', 'results': [{'memory_id': 1, 'memory': 'User loves Python programming', 'score': 0.89}]}

# Update a memory
memory.update(memory_id=1, text="User is an expert Python developer")

# Delete a memory
memory.delete(memory_id=1)

Features

  • Automatic Memory Extraction: Uses LLM to extract important facts from conversations
  • Semantic Search: Find relevant memories using embedding-based similarity
  • Memory Deduplication: Automatically updates or removes duplicate memories
  • Flexible Storage: SQLite (default) or PostgreSQL
  • Easy Configuration: Programmatic or environment variable setup

Configuration Options

Parameter Required Default Description
openai_api_key Yes - Your OpenAI API key
database_url No ~/.contextmemory/memory.db Database connection URL
debug No False Enable debug logging

Database Support

  • SQLite (default): No additional setup required
  • PostgreSQL: Install with pip install contextmemory[postgres]

API Reference

configure(openai_api_key, database_url=None, debug=False)

Initialize the library configuration.

Memory(db: Session)

Main memory interface class.

Methods:

  • add(messages: List[dict], conversation_id: int) - Extract and store memories
  • search(query: str, conversation_id: int, limit: int) - Search memories
  • update(memory_id: int, text: str) - Update a memory
  • delete(memory_id: int) - Delete a memory

create_table()

Create all required database tables.

SessionLocal()

Get a new database session.

License

MIT License - see LICENSE file.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Links

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

contextmemory-0.1.2.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

contextmemory-0.1.2-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file contextmemory-0.1.2.tar.gz.

File metadata

  • Download URL: contextmemory-0.1.2.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for contextmemory-0.1.2.tar.gz
Algorithm Hash digest
SHA256 86361042894d4d9ac7531a6d31ca097a0affbef47fb9869a39341c78e47ace68
MD5 cbb1bed43f46b68a8e93aeb3d35a31f3
BLAKE2b-256 75bfe9b9a2cdf0112841e4837dc106476f9e54f9a6e44b15b647019f485b17e4

See more details on using hashes here.

File details

Details for the file contextmemory-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: contextmemory-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for contextmemory-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c02654e283a4f5ca10b790b8ce457115c4e8007e65465dc6d5729ce8935bd790
MD5 18271ed286f03100d6857bfc6acd664a
BLAKE2b-256 dc539ecff9bb4e151bf531aca65f168d61176ff3ea1350dd7ddb2af8eea83d8d

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