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.0.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.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmemory-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 53c872aada730391725b5fc20997ee214315e831bf41a5e9ffc91730af6060ab
MD5 eb3a042980eeb5145f86497d781391da
BLAKE2b-256 fc9bf6212dcae440dd79b4334c29bab9f9b56c181fb679acd8af4fd8d0aa8684

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contextmemory-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e97a57e5902fc057eb99bf94e1680d10026fee93158c064194f18de7d20ae2d
MD5 f27e700838490c8d59c3ca5444637b0a
BLAKE2b-256 254fdd8a1adf5e7b49aeb60d42da883661a7dfe5dedda9abd46bc330c5d62333

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