-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
312 lines (254 loc) · 9.34 KB
/
memory.py
File metadata and controls
312 lines (254 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""vibeMemory — shared core: Qdrant, FastEmbed, and memory operations."""
from __future__ import annotations
import os
import time
import uuid
from datetime import UTC, datetime
from typing import Any
from langchain_core.runnables import RunnableLambda
from langgraph.graph import END, StateGraph
from typing_extensions import TypedDict
from qdrant_client import QdrantClient
from qdrant_client.models import (
Distance,
FieldCondition,
Filter,
MatchValue,
PointIdsList,
PointStruct,
VectorParams,
)
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
COLLECTION = os.getenv("QDRANT_COLLECTION", "memories")
EMBED_MODEL = os.getenv("EMBED_MODEL", "BAAI/bge-small-en-v1.5")
VECTOR_SIZE = 384 # bge-small-en-v1.5 output dim
SIMILARITY_THRESHOLD = float(os.getenv("SIMILARITY_THRESHOLD", "0.92"))
MAX_TEXT_LEN = int(os.getenv("MAX_TEXT_LEN", "2000"))
QDRANT_RETRY_ATTEMPTS = 5
QDRANT_RETRY_DELAY = 2.0
# ---------------------------------------------------------------------------
# Qdrant client
# ---------------------------------------------------------------------------
_qdrant: QdrantClient | None = None
def get_qdrant() -> QdrantClient:
if _qdrant is None:
raise RuntimeError("Qdrant not initialized — call init() first")
return _qdrant
def _init_qdrant() -> None:
global _qdrant
for attempt in range(1, QDRANT_RETRY_ATTEMPTS + 1):
try:
client = QdrantClient(url=QDRANT_URL)
client.get_collections()
_qdrant = client
break
except Exception as exc:
if attempt == QDRANT_RETRY_ATTEMPTS:
raise RuntimeError(
f"Cannot connect to Qdrant at {QDRANT_URL} after "
f"{QDRANT_RETRY_ATTEMPTS} attempts: {exc}"
) from exc
print(
f"[vibeMemory] Qdrant not ready (attempt {attempt}), "
f"retrying in {QDRANT_RETRY_DELAY}s…"
)
time.sleep(QDRANT_RETRY_DELAY)
existing = [c.name for c in _qdrant.get_collections().collections]
if COLLECTION not in existing:
_qdrant.create_collection(
collection_name=COLLECTION,
vectors_config=VectorParams(size=VECTOR_SIZE, distance=Distance.COSINE),
)
print(f"[vibeMemory] Created Qdrant collection '{COLLECTION}'")
else:
print(f"[vibeMemory] Using existing Qdrant collection '{COLLECTION}'")
# ---------------------------------------------------------------------------
# FastEmbed
# ---------------------------------------------------------------------------
_embedder: Any = None
def get_embedder() -> Any:
if _embedder is None:
raise RuntimeError("Embedder not initialized — call init() first")
return _embedder
def _init_embedder() -> None:
global _embedder
from fastembed import TextEmbedding
print(f"[vibeMemory] Loading embedding model '{EMBED_MODEL}' (may take a moment on first run)…")
_embedder = TextEmbedding(model_name=EMBED_MODEL)
print("[vibeMemory] Embedding model ready.")
def embed_text(text: str) -> list[float]:
vectors = list(get_embedder().embed([text]))
return vectors[0].tolist()
# ---------------------------------------------------------------------------
# Normalization — LangGraph pipeline
# ---------------------------------------------------------------------------
class _NormState(TypedDict):
text: str
def _trim(state: _NormState) -> _NormState:
return {"text": " ".join(state["text"].split())}
def _truncate(state: _NormState) -> _NormState:
return {"text": state["text"][:MAX_TEXT_LEN]}
def _lowercase(state: _NormState) -> _NormState:
return {"text": state["text"].lower()}
_norm_graph = StateGraph(_NormState)
_norm_graph.add_node("trim", RunnableLambda(_trim))
_norm_graph.add_node("truncate", RunnableLambda(_truncate))
_norm_graph.add_node("lowercase", RunnableLambda(_lowercase))
_norm_graph.set_entry_point("trim")
_norm_graph.add_edge("trim", "truncate")
_norm_graph.add_edge("truncate", "lowercase")
_norm_graph.add_edge("lowercase", END)
_normalizer = _norm_graph.compile()
def normalize_text(text: str) -> str:
"""Run text through the LangGraph trim → truncate → lowercase pipeline."""
return _normalizer.invoke({"text": text})["text"]
# ---------------------------------------------------------------------------
# Memory operations
# ---------------------------------------------------------------------------
def remember(
text: str,
scope: str = "default",
tags: list[str] | None = None,
source: str | None = None,
) -> dict[str, Any]:
"""Store a memory. Merges with an existing one if cosine similarity >= threshold."""
normalized = normalize_text(text)
vector = embed_text(normalized)
qdrant = get_qdrant()
results = qdrant.search(
collection_name=COLLECTION,
query_vector=vector,
query_filter=Filter(
must=[FieldCondition(key="scope", match=MatchValue(value=scope))]
),
limit=1,
with_payload=True,
)
if results and results[0].score >= SIMILARITY_THRESHOLD:
existing = results[0]
point_id = existing.id
payload = existing.payload or {}
payload["text"] = normalized
payload["tags"] = tags or payload.get("tags", [])
payload["source"] = source or payload.get("source")
qdrant.set_payload(
collection_name=COLLECTION,
payload=payload,
points=[point_id],
)
return {
"id": str(point_id),
"text": normalized,
"scope": scope,
"tags": payload["tags"],
"source": payload["source"],
"created_at": payload.get("created_at"),
"merged": True,
"similarity": round(results[0].score, 4),
}
point_id = str(uuid.uuid4())
created_at = datetime.now(UTC).isoformat()
payload = {
"text": normalized,
"scope": scope,
"tags": tags or [],
"source": source,
"created_at": created_at,
}
qdrant.upsert(
collection_name=COLLECTION,
points=[PointStruct(id=point_id, vector=vector, payload=payload)],
)
return {
"id": point_id,
"text": normalized,
"scope": scope,
"tags": tags or [],
"source": source,
"created_at": created_at,
"merged": False,
}
def recall(
query: str,
scope: str = "default",
limit: int = 5,
) -> list[dict[str, Any]]:
"""Retrieve memories semantically ranked by similarity to the query."""
vector = embed_text(normalize_text(query))
qdrant = get_qdrant()
results = qdrant.search(
collection_name=COLLECTION,
query_vector=vector,
query_filter=Filter(
must=[FieldCondition(key="scope", match=MatchValue(value=scope))]
),
limit=limit,
with_payload=True,
)
return [
{
"id": str(r.id),
"score": round(r.score, 4),
"text": r.payload.get("text", "") if r.payload else "",
"scope": r.payload.get("scope", scope) if r.payload else scope,
"tags": r.payload.get("tags", []) if r.payload else [],
"source": r.payload.get("source") if r.payload else None,
"created_at": r.payload.get("created_at") if r.payload else None,
}
for r in results
]
def forget(memory_id: str) -> dict[str, Any]:
"""Delete a memory by UUID."""
get_qdrant().delete(
collection_name=COLLECTION,
points_selector=PointIdsList(points=[memory_id]),
)
return {"id": memory_id, "deleted": True}
def list_memories(
scope: str = "default",
limit: int = 50,
) -> list[dict[str, Any]]:
"""Browse all memories in a scope without semantic ranking."""
qdrant = get_qdrant()
results, _ = qdrant.scroll(
collection_name=COLLECTION,
scroll_filter=Filter(
must=[FieldCondition(key="scope", match=MatchValue(value=scope))]
),
limit=limit,
with_payload=True,
)
return [
{
"id": str(r.id),
"text": r.payload.get("text", "") if r.payload else "",
"scope": r.payload.get("scope", scope) if r.payload else scope,
"tags": r.payload.get("tags", []) if r.payload else [],
"source": r.payload.get("source") if r.payload else None,
"created_at": r.payload.get("created_at") if r.payload else None,
}
for r in results
]
def list_scopes() -> list[str]:
"""Return all distinct scope values present in the collection."""
qdrant = get_qdrant()
all_records, _ = qdrant.scroll(
collection_name=COLLECTION,
limit=1000,
with_payload=["scope"],
)
seen: set[str] = set()
for r in all_records:
if r.payload and (s := r.payload.get("scope")):
seen.add(s)
return sorted(seen)
# ---------------------------------------------------------------------------
# Startup
# ---------------------------------------------------------------------------
def init() -> None:
"""Initialize embedder and Qdrant. Call once at process startup."""
_init_embedder()
_init_qdrant()