Vector Memory
DeskBot's vector memory extends the base Memory system with
embedding-based semantic search. When an embedding function is available
(e.g. sentence-transformers), entries are embedded at add-time and can be
retrieved by cosine similarity - so the LLM can recall semantically
relevant past interactions even when exact keywords don't match.
Semantic vs. keyword search
| Mode | How it works | When available |
|---|---|---|
| Keyword search | Substring match on entry content | Always |
| Semantic search | Cosine similarity between query and entry embeddings | When an EmbeddingFn is configured |
Keyword search (search()) works independently of embeddings and is
always available. Semantic search (search_similar()) requires an
embedding function and falls back to reverse-chronological order when
using NoOpEmbedding.
Configuration
Vector memory is opt-in and disabled by default:
When disabled, the base Memory keyword search is used instead.
To use sentence-transformers embeddings:
Core classes
VectorMemory
VectorMemory stores entries with
their pre-computed embedding vectors. It maintains a bounded deque of
entries (default capacity 1024) and supports both keyword and semantic
search:
from robot.ai.vector_memory import VectorMemory, SentenceTransformerEmbedding
# With sentence-transformers
embedding_fn = SentenceTransformerEmbedding(model_name="all-MiniLM-L6-v2")
memory = VectorMemory(embedding_fn=embedding_fn)
# Add entries
memory.add("User asked about the weather", importance=0.3, tags=["weather"])
memory.add("Robot explained the forecast", importance=0.5, tags=["weather", "explanation"])
# Keyword search
results = memory.search("weather")
# Semantic search - returns (entry, similarity) tuples sorted by relevance
similar = memory.search_similar("What's the temperature outside?", limit=5)
for entry, score in similar:
print(f"[{score:.2f}] {entry.content}")
VectorMemoryEntry
Each entry is a VectorMemoryEntry
with:
| Field | Type | Description |
|---|---|---|
timestamp |
datetime |
When the entry was created |
content |
str |
The memory text |
importance |
float |
Importance score (0.0–1.0) |
tags |
tuple[str, ...] |
Optional tags for categorisation |
embedding |
tuple[float, ...] |
Pre-computed embedding vector |
Methods
| Method | Description |
|---|---|
add(content, importance, tags) |
Add an entry, computing its embedding |
recall(limit) |
Return the N most recent entries |
search(query) |
Keyword substring search |
search_similar(query, limit, min_similarity) |
Semantic cosine-similarity search |
clear() |
Remove all entries |
Embedding functions
NoOpEmbedding
NoOpEmbedding is the default
fallback. It returns a zero vector of configurable dimension (default
128), meaning all entries have identical embeddings. search_similar()
returns entries in reverse-chronological order.
SentenceTransformerEmbedding
SentenceTransformerEmbedding
wraps the sentence-transformers library and produces normalised
embeddings suitable for cosine similarity:
from robot.ai.vector_memory import VectorMemory, SentenceTransformerEmbedding
fn = SentenceTransformerEmbedding(model_name="all-MiniLM-L6-v2")
memory = VectorMemory(embedding_fn=fn)
memory.add("The robot likes discussing philosophy")
memory.add("User prefers casual conversation style")
# Semantic search - finds entries by meaning, not just keywords
results = memory.search_similar("intellectual topics", limit=5)
Requires: pip install sentence-transformers
Custom EmbeddingFn
You can implement the EmbeddingFn
protocol to use any embedding backend (OpenAI embeddings, local models,
etc.):
from robot.ai.vector_memory import EmbeddingFn
class MyEmbedding(EmbeddingFn):
def embed(self, text: str) -> list[float]:
# Call your embedding API or model here
return my_model.encode(text)
Cosine similarity
search_similar()
computes cosine similarity between the query embedding and each stored
entry's embedding:
Entries are returned sorted by descending similarity. The min_similarity
parameter (default 0.0) filters out entries below a threshold, allowing
you to exclude weakly related results.
API reference
robot.ai.vector_memory.VectorMemory
dataclass
Memory store with vector-based semantic search.
Entries are embedded at add-time using the configured
:class:EmbeddingFn. If no embedding function is provided,
a :class:NoOpEmbedding is used and :meth:search_similar
returns entries in reverse-chronological order (same as
:class:Memory).
Keyword search (:meth:search) works independently of
embeddings and is always available.
Source code in src/robot/ai/vector_memory.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
add(content, importance=0.5, tags=())
Add an entry, computing and storing its embedding.
Source code in src/robot/ai/vector_memory.py
recall(limit=10)
search(query)
search_similar(query, limit=10, min_similarity=0.0)
Return entries ranked by cosine similarity to query.
Returns a list of (entry, similarity) tuples sorted by
descending similarity. Entries below min_similarity are
excluded.
When using :class:NoOpEmbedding (no real embedding backend),
cosine similarity is meaningless. In that case, the method
returns recent entries in reverse chronological order with a
placeholder similarity of 0.0, making it explicit that no
semantic search was performed.
Source code in src/robot/ai/vector_memory.py
robot.ai.vector_memory.VectorMemoryEntry
dataclass
A memory entry with its pre-computed embedding vector.
Source code in src/robot/ai/vector_memory.py
robot.ai.vector_memory.EmbeddingFn
Bases: Protocol
A function that computes an embedding vector from text.
Implementations may call a local model (sentence-transformers), an API (OpenAI embeddings), or any other backend. The vector dimension must be consistent across calls.
Source code in src/robot/ai/vector_memory.py
robot.ai.vector_memory.NoOpEmbedding
Fallback embedding that returns a zero vector.
Used when no embedding backend is configured - all entries get
the same (zero) embedding and :meth:search_similar falls back
to chronological order.
Source code in src/robot/ai/vector_memory.py
robot.ai.vector_memory.SentenceTransformerEmbedding
Embedding function backed by sentence-transformers.
Requires the sentence-transformers package. Install with::
pip install sentence-transformers
Usage::
from robot.ai.vector_memory import SentenceTransformerEmbedding
fn = SentenceTransformerEmbedding(model_name="all-MiniLM-L6-v2")
memory = VectorMemory(embedding_fn=fn)