How samiksha0shukla/context-memory works
Purpose and Public Entry Points
ContextMemory is a Python library that gives AI conversation systems durable, per-conversation memory. It extracts semantic facts and time-bound episodic “bubbles,” persists them, and retrieves relevant context for a later prompt. The package-facing API is exposed from src/contextmemory/__init__.py; the operational facade is src/contextmemory/memory/memory.py. A standalone repository entry script also exists at main.py.
Configuration and Provider Boundary
Global configuration selects LLM/embedding settings, credentials, debug behavior, and SQLite versus PostgreSQL persistence through src/contextmemory/core/settings.py. src/contextmemory/core/openai_client.py isolates model calls and supports direct OpenAI or OpenRouter-compatible models. Extraction, update classification, and embeddings therefore cross a remote-provider boundary rather than being implemented locally.
Relational Persistence Model
SQLAlchemy-style database setup and session creation are centralized in src/contextmemory/db/database.py. Persistent domain records are split across:
• src/contextmemory/db/models/conversation.py
• src/contextmemory/db/models/message.py
• src/contextmemory/db/models/memory.py
• src/contextmemory/db/models/conversation_summary.py
This is the durable source of truth, backed by default SQLite or configurable PostgreSQL. Conversation IDs scope writes and searches.
Add Pipeline: Extraction to Mutation
Memory.add() in src/contextmemory/memory/memory.py orchestrates ingestion. The extraction stage in src/contextmemory/memory/extractor.py sends the latest interaction to the configured LLM using src/contextmemory/utils/extraction_system_prompt.py, producing semantic facts and episodic candidates. src/contextmemory/memory/add/add_extraction_phase.py structures this extraction flow.
The update stage—src/contextmemory/memory/add/add_updation_phase.py—uses src/contextmemory/memory/tool_classifier.py and src/contextmemory/utils/tool_call_system_prompt.py to decide whether a candidate should be added, updated, replaced due to contradiction, or ignored. This separates probabilistic LLM decisions from database mutation.
Embeddings and Vector Retrieval
src/contextmemory/memory/embeddings.py generates vector representations through the configured embedding provider. src/contextmemory/memory/vector_store.py maintains the FAISS-backed vector index, while src/contextmemory/memory/similarity.py and src/contextmemory/memory/similar_memory_search.py perform ranked similarity lookup. Memory.search() combines a text query, conversation scope, and result limit to return matching stored memories; vector indexing is an acceleration layer over persisted memory records.
Episodic Connection Layer
Episodic memories are created by src/contextmemory/memory/bubble_creator.py. src/contextmemory/memory/connection_finder.py identifies related semantic memories and records/returns those associations. This makes current tasks or events navigable through durable user facts while retaining their distinct time-bound type.
Primary Control Flow and Ownership
A host application owns chat generation and calls the library around it: search before prompting, then add the new user/assistant exchange afterward. ContextMemory owns extraction, classification, embedding, vector search, persistence, and CRUD (add, search, update, delete) behind src/contextmemory/memory/memory.py. The host owns conversation lifecycle, prompt composition, database-session lifetime, and use of retrieved memories in its agent response.
Main entry points: memory.py Memory facade · openai_client.py Provider client · extractor.py Semantic extractor · add_updation_phase.py Update phase
Components 19
Host application
Owns chat lifecycle and prompt composition.
- Chat host · Searches before generation and adds exchanges afterward.
Library facade
- Package API
src/contextmemory/__init__.py - Repository script
main.py - Memory facade · Provides add, search, update, and delete.
src/contextmemory/memory/memory.py
Memory pipeline
- Extraction phase · Structures fact and episodic candidate extraction.
src/contextmemory/memory/add/add_extraction_phase.py - Semantic extractor
src/contextmemory/memory/extractor.py - Extraction prompt
src/contextmemory/utils/extraction_system_prompt.py - Update phase · Decides add, update, replace, or ignore before persistence.
src/contextmemory/memory/add/add_updation_phase.py - Tool classifier
src/contextmemory/memory/tool_classifier.py - Tool-call prompt
src/contextmemory/utils/tool_call_system_prompt.py - Bubble creator · Creates time-bound episodic memories.
src/contextmemory/memory/bubble_creator.py - Connection finder · Links episodic events to related semantic memories.
src/contextmemory/memory/connection_finder.py
Persistence and retrieval
- Embedding generator
src/contextmemory/memory/embeddings.py - FAISS vector index · Accelerates retrieval over persisted memories.
src/contextmemory/memory/vector_store.py - Similarity search
src/contextmemory/memory/similar_memory_search.py - Database sessions · Creates sessions for SQLite or PostgreSQL persistence.
src/contextmemory/db/database.py - Domain records · Conversation, message, memory, and summary records scoped by conversation ID.
src/contextmemory/db/models/memory.py
Model provider
- Global settings · Selects providers, credentials, debug mode, and database backend.
src/contextmemory/core/settings.py - Provider client · Supports OpenAI and OpenRouter-compatible calls.
src/contextmemory/core/openai_client.py
Connections 24
- Chat host → Package API: uses
- Package API → Memory facade: exports
- Repository script → Memory facade: invokes
- Chat host → Memory facade: search then add
- Memory facade → Extraction phase: add
- Extraction phase → Semantic extractor: extracts candidates
- Semantic extractor → Extraction prompt: uses
- Semantic extractor → Provider client: LLM request
- Memory facade → Update phase: classifies mutation
- Update phase → Tool classifier: uses
- Tool classifier → Tool-call prompt: uses
- Tool classifier → Provider client: LLM decision
- Update phase → Bubble creator: creates episodic memory
- Bubble creator → Connection finder: finds links
- Memory facade → Embedding generator: indexes memories
- Embedding generator → Provider client: embedding request
- Embedding generator → FAISS vector index: vectors
- Memory facade → Similarity search: search
- Similarity search → FAISS vector index: nearest neighbors
- Memory facade → Database sessions: CRUD
- Database sessions → Domain records: persists
- Connection finder → Domain records: queries associations
- Global settings → Provider client: configures
- Global settings → Database sessions: selects backend