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 memoriessearch(query: str, conversation_id: int, limit: int)- Search memoriesupdate(memory_id: int, text: str)- Update a memorydelete(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d92e1fc7600f4d7122891acd988e2b03126fb4ce8d02750777b6ff3f2052f57f
|
|
| MD5 |
48769640c8c665634c371644f9d0edc7
|
|
| BLAKE2b-256 |
f10fd399bb42a767800c125ceddcc6f0c554f5acebe790ba0bdf3e90c7ab439c
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15cdf620fc797c3d42cd5b0b9780f252d0a0c555ac7cefef96015e1b3079c5b6
|
|
| MD5 |
70d056c879f83a26d798f4e6c1b64753
|
|
| BLAKE2b-256 |
8b01b7714423c860158796c1f41ea922968a980d8760e65cf3436aea6f05cf0f
|