teff.memory.base¶
teff.memory.base
¶
Long-term memory: a namespace store over a vector store.
Memory is knowledge that outlives a single run — facts, preferences, and history kept per user/agent and looked up semantically. Checkpoints snapshot one run; this module stores small cross-session facts.
The :class:MemoryStore is a thin namespace layer over a
:class:~teff.rag.base.VectorStore plus an
:class:~teff.rag.embedder.Embedder:
- A memory item is a
{text, ...}dict;textis what gets embedded. - Items live under hierarchical namespaces (tuples of strings, rooted on the user/session id — never the checkpoint id). A search under a namespace also matches items stored in deeper sub-namespaces.
putis an upsert keyed bynamespace::key; expired items (TTL) are skipped on read.
The same store/embedder pair powers :class:MemoryTool for agents.
Classes:
| Name | Description |
|---|---|
MemoryItem |
A single stored memory. |
MemoryStore |
Namespace-scoped semantic memory over a :class: |
MemoryItem
dataclass
¶
A single stored memory.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
Item key within its namespace. |
value |
dict
|
The stored |
namespace |
tuple[str, ...]
|
The namespace the item lives under. |
updated_at |
float
|
Unix timestamp of the last write. |
score |
float | None
|
Similarity score from a semantic search, or |
Source code in teff/memory/base.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
MemoryStore
¶
Namespace-scoped semantic memory over a :class:VectorStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
VectorStore
|
Backing vector store (in-memory, sqlite, qdrant, ...). |
required |
embedder
|
Embedder
|
Embedding service used for |
required |
ttl
|
float | None
|
Default seconds an item lives unless overridden at |
None
|
Methods:
| Name | Description |
|---|---|
cleanup |
Delete expired items; return how many were removed. |
delete |
Remove the item under |
get |
Return the item under |
list |
Return the keys stored under namespace (recency order). |
put |
Upsert value under |
search |
Return the k most relevant items under namespace. |
Attributes:
| Name | Type | Description |
|---|---|---|
store |
VectorStore
|
The backing vector store (exposed for lifecycle tools). |
Source code in teff/memory/base.py
67 68 69 70 71 72 73 74 75 76 77 78 79 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
cleanup
async
¶
cleanup(*, max_age=None)
Delete expired items; return how many were removed.
max_age additionally removes items whose updated_at is
older than that many seconds. Expired items are removed from the
backing store (not merely hidden).
Source code in teff/memory/base.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
delete
async
¶
delete(namespace, key)
Remove the item under namespace::key (no-op if absent).
Source code in teff/memory/base.py
146 147 148 | |
get
async
¶
get(namespace, key)
Return the item under namespace::key, or None.
Expired items are reported as missing.
Source code in teff/memory/base.py
133 134 135 136 137 138 139 140 141 142 143 144 | |
list
async
¶
list(namespace, limit=100, offset=0)
Return the keys stored under namespace (recency order).
Source code in teff/memory/base.py
201 202 203 204 205 206 207 | |
put
async
¶
put(namespace, key, value, *, ttl=_UNSET)
Upsert value under namespace::key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
tuple[str, ...]
|
Hierarchical path ( |
required |
key
|
str
|
Unique key within the namespace; writing to an existing key overwrites it. |
required |
value
|
dict
|
The memory dict; must contain a non-empty |
required |
ttl
|
float | None | object
|
|
_UNSET
|
Source code in teff/memory/base.py
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 | |
search
async
¶
search(namespace, *, query=None, k=10, filter=None)
Return the k most relevant items under namespace.
With query, items are ranked by semantic similarity to it; a
namespace match also covers deeper sub-namespaces (prefix
semantics). Without query, the most recently written items are
returned instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
tuple[str, ...]
|
Namespace subtree to search. |
required |
query
|
str | None
|
Natural-language query; when |
None
|
k
|
int
|
Maximum number of results. |
10
|
filter
|
dict | None
|
Extra metadata filter DSL (see
:func: |
None
|
Source code in teff/memory/base.py
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |