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

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmemory-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 1bf092834877288e948efae9943e9dff9b49394661bc180a165beab17e09ec6c
MD5 882d21cadd0fa8009509785fddc8c054
BLAKE2b-256 79c3c68cdfaeef1be9421b6071c318f646c9cc728d57a4a69bf88d708117f3f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contextmemory-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 91e1b1de5d20f96a1505b0033ec958007d1f5f2fa51a5f3a9314e11cd6cba9ad
MD5 3712ca91f56cfbd638195bd4a2e3697f
BLAKE2b-256 4a60c18974848bf061fee50edcbd2755e2fd786dc4d32dbf1d123d2c9e2d1b73

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