calute.memory.storage#
Storage backends for Calute memory system.
- class calute.memory.storage.FileStorage(storage_dir: str = '.calute_memory')[source]#
Bases:
MemoryStorageFile-based persistent storage using pickle serialisation.
Each key-value pair is stored as a separate
.pklfile named by the MD5 hash of its key. An_index.jsonfile tracks the key-to-filename mapping. Suitable for moderate-sized datasets that need persistence across process restarts.- storage_dir#
Pathto the directory holding pickle files and the index.
- _index#
Dictionary mapping string keys to their pickle filenames.
Example
>>> from calute.memory.storage import FileStorage >>> fs = FileStorage("/tmp/my_memory") >>> fs.save("key1", {"data": 42}) True >>> fs.load("key1") {'data': 42}
- delete(key: str) bool[source]#
Delete a pickle file and its index entry.
- Parameters
key – Unique identifier to delete
- Returns
True if key was found and deleted, False otherwise
- exists(key: str) bool[source]#
Check if key exists in the index.
- Parameters
key – Unique identifier to check
- Returns
True if key exists, False otherwise
- list_keys(pattern: str | None = None) list[str][source]#
List all stored keys, optionally filtered by pattern.
- Parameters
pattern – Optional substring pattern to filter keys
- Returns
List of matching key strings
- class calute.memory.storage.MemoryStorage[source]#
Bases:
ABCAbstract base class for memory storage backends.
Defines the common key-value interface that all Calute storage implementations must provide. Concrete subclasses include
SimpleStorage(in-memory),FileStorage(pickle files),SQLiteStorage(database), andRAGStorage(vector-enhanced wrapper).Subclasses must implement
save(),load(),delete(),exists(),list_keys(), andclear().- abstract delete(key: str) bool[source]#
Delete data by key.
- Parameters
key – Unique identifier to delete
- Returns
True if deletion was successful, False if key not found
- abstract exists(key: str) bool[source]#
Check if key exists in storage.
- Parameters
key – Unique identifier to check
- Returns
True if key exists, False otherwise
- abstract list_keys(pattern: str | None = None) list[str][source]#
List all stored keys, optionally filtered by pattern.
- Parameters
pattern – Optional substring pattern to filter keys
- Returns
List of matching key strings
- class calute.memory.storage.RAGStorage(backend: calute.memory.storage.MemoryStorage | None = None, embedding_model: str | None = None, embedding_api_key: str | None = None)[source]#
Bases:
MemoryStorageRAG-enhanced storage with vector similarity search capabilities.
Wraps another
MemoryStoragebackend and augments it with dense vector embeddings for semantic similarity search viasearch_similar(). Supports multiple embedding backends:TF-IDF (default) – no external dependencies, hash-based 256-dimensional vectors.
sentence-transformers – dense embeddings from a local model (requires the
sentence-transformerspackage).OpenAI – remote embeddings via the OpenAI API (requires the
openaipackage and a valid API key).
- backend#
The underlying
MemoryStoragethat handles actual data persistence.
- embeddings#
Dictionary mapping storage keys to their computed embedding vectors.
Example
>>> from calute.memory.storage import RAGStorage, SimpleStorage >>> rag = RAGStorage(SimpleStorage(), embedding_model="tfidf") >>> rag.save("doc1", "The cat sat on the mat") True >>> results = rag.search_similar("feline sitting", limit=5)
- delete(key: str) bool[source]#
Delete data and its embedding.
- Parameters
key – Unique identifier to delete
- Returns
True if key was found and deleted, False otherwise
- exists(key: str) bool[source]#
Check if key exists in backend storage.
- Parameters
key – Unique identifier to check
- Returns
True if key exists, False otherwise
- list_keys(pattern: str | None = None) list[str][source]#
List keys from backend storage.
- Parameters
pattern – Optional substring pattern to filter keys
- Returns
List of matching key strings
- load(key: str) Any | None[source]#
Load data from backend storage.
- Parameters
key – Unique identifier to retrieve
- Returns
Stored data if found, None otherwise
- save(key: str, data: Any) bool[source]#
Save data with computed embedding.
- Parameters
key – Unique identifier for the data
data – Data to store (embeddings computed for str/dict types)
- Returns
True if save was successful, False otherwise
- search_similar(query: str, limit: int = 10, threshold: float = 0.0) list[tuple[str, float, Any]][source]#
Search for items similar to the query.
- Parameters
query – Search query text
limit – Maximum number of results to return
threshold – Minimum similarity score (0.0 to 1.0)
- Returns
List of tuples (key, similarity_score, data) sorted by similarity
- class calute.memory.storage.SQLiteStorage(db_path: str = '.calute_memory/memory.db')[source]#
Bases:
MemoryStorageSQLite-based persistent storage with ACID guarantees.
Uses a local SQLite database for reliable persistent key-value storage. Data is serialised with
pickleand stored as BLOBs. When theWRITE_MEMORYenvironment variable is not set to"1", the backend transparently falls back to a plain in-memory dictionary so that read-only or ephemeral sessions incur no disk I/O.- write_enabled#
Truewhen theWRITE_MEMORYenvironment variable is"1", enabling actual SQLite persistence.
- db_path#
Pathto the SQLite database file.
- _memory_storage#
In-memory fallback dictionary used when
write_enabledisFalse.
Example
>>> import os >>> os.environ["WRITE_MEMORY"] = "1" >>> from calute.memory.storage import SQLiteStorage >>> store = SQLiteStorage("/tmp/mem.db") >>> store.save("k", {"v": 1}) True
- delete(key: str) bool[source]#
Delete from database or in-memory storage.
- Parameters
key – Unique identifier to delete
- Returns
True if key was found and deleted, False otherwise
- exists(key: str) bool[source]#
Check if key exists in storage.
- Parameters
key – Unique identifier to check
- Returns
True if key exists, False otherwise
- list_keys(pattern: str | None = None) list[str][source]#
List all stored keys, optionally filtered by pattern.
- Parameters
pattern – Optional substring pattern to filter keys
- Returns
List of matching key strings, ordered by creation date (newest first)
- class calute.memory.storage.SimpleStorage[source]#
Bases:
MemoryStorageSimple in-memory storage (non-persistent).
Provides fast dictionary-backed key-value storage that exists only in memory. All data is lost when the process terminates. Suitable for testing, development, and short-lived applications.
- _data#
Internal dictionary holding all stored key-value pairs.
- delete(key: str) bool[source]#
Delete data from memory.
- Parameters
key – Unique identifier to delete
- Returns
True if key was found and deleted, False otherwise
- exists(key: str) bool[source]#
Check if key exists in memory.
- Parameters
key – Unique identifier to check
- Returns
True if key exists, False otherwise
- list_keys(pattern: str | None = None) list[str][source]#
List all keys, optionally filtered by pattern.
- Parameters
pattern – Optional substring pattern to filter keys
- Returns
List of matching key strings