Package com.tnsai.memory.advanced
Class VectorMemoryStore
java.lang.Object
com.tnsai.memory.advanced.VectorMemoryStore
In-memory vector store for semantic search over memory entries.
Stores entries alongside their embedding vectors and supports cosine similarity search. Uses brute-force comparison, which is sufficient for typical agent memory sizes (<10k entries).
The first admitted vector pins the store's embedding dimension for its lifetime. Later additions and non-empty searches must use that dimension. Thread-safe via ConcurrentHashMap for the entry and vector maps.
-
Constructor Summary
ConstructorsConstructorDescriptionVectorMemoryStore(EmbeddingFunction embeddingFunction) Creates a new in-memory vector memory store.VectorMemoryStore(EmbeddingFunction embeddingFunction, VectorIndex index) Creates a store over an explicit backend. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(MemoryEntry entry) Adds a memory entry and computes its embedding.voidadd(MemoryEntry entry, float[] vector) Adds a memory entry with a pre-computed embedding.voidclear()Clears all entries and vectors without resetting the pinned dimension.booleanRemoves an entry by ID.Searches for the top-K most similar entries to the query.searchByVector(float[] queryVector, int topK) Searches for the top-K most similar entries to a pre-computed query vector.intsize()Returns the number of stored entries.
-
Constructor Details
-
VectorMemoryStore
Creates a new in-memory vector memory store.- Parameters:
embeddingFunction- the function to compute embeddings
-
VectorMemoryStore
Creates a store over an explicit backend.- Parameters:
embeddingFunction- the function to compute embeddingsindex- storage backend- Since:
- 0.14.0
-
-
Method Details
-
add
Adds a memory entry and computes its embedding.- Parameters:
entry- the memory entry to add
-
add
Adds a memory entry with a pre-computed embedding.- Parameters:
entry- the memory entryvector- the pre-computed embedding vector
-
search
Searches for the top-K most similar entries to the query.- Parameters:
query- the search query texttopK- maximum number of results- Returns:
- scored entries sorted by descending cosine similarity
-
searchByVector
Searches for the top-K most similar entries to a pre-computed query vector.- Parameters:
queryVector- the query embedding vectortopK- maximum number of results- Returns:
- scored entries sorted by descending cosine similarity
-
remove
Removes an entry by ID.- Parameters:
id- the entry ID- Returns:
- true if the entry was present and removed
-
size
public int size()Returns the number of stored entries. -
clear
public void clear()Clears all entries and vectors without resetting the pinned dimension.
-