Skip to main content

Long-term memory system for AI conversations

Project description

ContextMemory

Long-term memory system for AI conversations.

ContextMemory extracts, stores, and retrieves important facts from conversations, enabling AI Agents 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.3.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.3-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmemory-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 d92e1fc7600f4d7122891acd988e2b03126fb4ce8d02750777b6ff3f2052f57f
MD5 48769640c8c665634c371644f9d0edc7
BLAKE2b-256 f10fd399bb42a767800c125ceddcc6f0c554f5acebe790ba0bdf3e90c7ab439c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contextmemory-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 15cdf620fc797c3d42cd5b0b9780f252d0a0c555ac7cefef96015e1b3079c5b6
MD5 70d056c879f83a26d798f4e6c1b64753
BLAKE2b-256 8b01b7714423c860158796c1f41ea922968a980d8760e65cf3436aea6f05cf0f

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