Repository navigation
Expand file tree
/
Copy pathembeddings.py
More file actions
459 lines (379 loc) · 14.6 KB
/
Copy pathembeddings.py
File metadata and controls
459 lines (379 loc) · 14.6 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""
Embedding service for semantic search using ChromaDB and sentence-transformers.
Manages persistent vector store and provides search operations.
CSV remains source of truth; ChromaDB is a derived index.
"""
from pathlib import Path
from typing import Optional, List, Dict, Any, Callable
import logging
# Data paths (same pattern as operations.py)
DATA_DIR = Path(__file__).parent / "data"
CHROMA_DIR = DATA_DIR / "chroma_db"
MEMORY_MATTER_CSV = DATA_DIR / "memory_matter.csv"
class EmbeddingService:
"""
Singleton embedding service for semantic memory search.
Features:
- Persistent ChromaDB storage in ./data/chroma_db/
- Lazy model loading (first use)
- Metadata filtering on search
- Sync status tracking
"""
def __init__(
self,
model_name: str = "all-MiniLM-L6-v2",
collection_name: str = "memory_matter"
):
self.model_name = model_name
self.collection_name = collection_name
self._model = None
self._client = None
self._collection = None
self._initialized = False
self._init_error = None
def _lazy_init(self) -> bool:
"""Initialize ChromaDB and model on first use. Returns success status."""
if self._initialized:
return True
if self._init_error:
return False
try:
import chromadb
from sentence_transformers import SentenceTransformer
# Initialize persistent client
CHROMA_DIR.mkdir(parents=True, exist_ok=True)
self._client = chromadb.PersistentClient(path=str(CHROMA_DIR))
# Get or create collection with cosine similarity
self._collection = self._client.get_or_create_collection(
name=self.collection_name,
metadata={"hnsw:space": "cosine"}
)
# Load embedding model
self._model = SentenceTransformer(self.model_name)
self._initialized = True
return True
except ImportError as e:
self._init_error = f"Missing dependencies: {e}"
logging.warning(f"[embeddings] {self._init_error}")
return False
except Exception as e:
self._init_error = str(e)
logging.warning(f"[embeddings] Init failed: {self._init_error}")
return False
def is_available(self) -> bool:
"""Check if embeddings service is available and initialized."""
if self._initialized:
return True
return self._lazy_init()
def embed_text(self, text: str) -> List[float]:
"""Generate embedding vector for text."""
if not self._lazy_init():
raise RuntimeError(f"Embedding service unavailable: {self._init_error}")
return self._model.encode(text).tolist()
def add_memory(
self,
memory_id: str,
raw: str,
metadata: Dict[str, Any]
) -> bool:
"""
Add or update a memory embedding.
Args:
memory_id: Memory ID (e.g., mm_001)
raw: The raw memory text to embed
metadata: Additional metadata (timestamp, layers, tags, etc.)
"""
if not self._lazy_init():
return False
try:
# Generate embedding
embedding = self.embed_text(raw)
# ChromaDB metadata must be string/int/float/bool
clean_metadata = {}
for k, v in metadata.items():
if v is None:
continue
if isinstance(v, (str, int, float, bool)):
clean_metadata[k] = v
else:
clean_metadata[k] = str(v)
# Upsert (add or update)
self._collection.upsert(
ids=[memory_id],
embeddings=[embedding],
documents=[raw],
metadatas=[clean_metadata]
)
return True
except Exception as e:
logging.warning(f"[embeddings] Failed to add {memory_id}: {e}")
return False
def remove_memory(self, memory_id: str) -> bool:
"""Remove a memory from the index."""
if not self._lazy_init():
return False
try:
self._collection.delete(ids=[memory_id])
return True
except Exception as e:
logging.warning(f"[embeddings] Failed to remove {memory_id}: {e}")
return False
def semantic_search(
self,
query: str,
limit: int = 10,
layer: Optional[str] = None,
min_toxicity: Optional[int] = None,
max_toxicity: Optional[int] = None,
min_nutrient: Optional[int] = None,
max_nutrient: Optional[int] = None,
date_start: Optional[str] = None,
date_end: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
Semantic search with optional metadata filters.
Args:
query: Natural language search query
limit: Max results to return
layer: Filter by layer (substring match in layers field)
min_toxicity, max_toxicity: Toxicity range filter
min_nutrient, max_nutrient: Nutrient density range filter
date_start, date_end: Date range filter (YYYY-MM-DD strings)
Returns:
List of {id, score, raw, metadata} dicts sorted by relevance
"""
if not self._lazy_init():
return []
try:
# Build where clause for filters
where_conditions = []
# Note: ChromaDB uses exact match for strings, not substring
# Layer filtering is limited - will do post-filter if needed
if layer:
where_conditions.append({"layers": {"$contains": layer}})
if min_toxicity is not None:
where_conditions.append({"toxicity": {"$gte": min_toxicity}})
if max_toxicity is not None:
where_conditions.append({"toxicity": {"$lte": max_toxicity}})
if min_nutrient is not None:
where_conditions.append({"nutrient_density": {"$gte": min_nutrient}})
if max_nutrient is not None:
where_conditions.append({"nutrient_density": {"$lte": max_nutrient}})
if date_start:
where_conditions.append({"timestamp_actual": {"$gte": date_start}})
if date_end:
where_conditions.append({"timestamp_actual": {"$lte": date_end}})
# Combine conditions
where = None
if len(where_conditions) == 1:
where = where_conditions[0]
elif len(where_conditions) > 1:
where = {"$and": where_conditions}
# Generate query embedding
query_embedding = self.embed_text(query)
# Search with more results to account for filtering
search_limit = limit * 2 if where else limit
results = self._collection.query(
query_embeddings=[query_embedding],
n_results=search_limit,
where=where,
include=["documents", "metadatas", "distances"]
)
# Format results
formatted = []
if results and results['ids'] and results['ids'][0]:
for i, memory_id in enumerate(results['ids'][0]):
# Convert distance to similarity score (cosine distance to similarity)
distance = results['distances'][0][i]
score = 1 - distance # Cosine similarity
metadata = results['metadatas'][0][i] if results['metadatas'] else {}
# Post-filter for layer substring match if needed
if layer and 'layers' in metadata:
if layer.lower() not in metadata['layers'].lower():
continue
formatted.append({
'id': memory_id,
'score': round(score, 4),
'raw': results['documents'][0][i],
'metadata': metadata
})
if len(formatted) >= limit:
break
return formatted
except Exception as e:
logging.warning(f"[embeddings] Search failed: {e}")
return []
def find_similar(
self,
memory_id: str,
limit: int = 5
) -> List[Dict[str, Any]]:
"""
Find memories similar to a given memory.
Args:
memory_id: Source memory ID
limit: Max similar memories to return
Returns:
List of similar memories (excluding the source)
"""
if not self._lazy_init():
return []
try:
# Get the memory's embedding
result = self._collection.get(
ids=[memory_id],
include=["embeddings"]
)
if not result or result.get('embeddings') is None or len(result['embeddings']) == 0:
return []
embedding = result['embeddings'][0]
if embedding is None:
return []
# Search, getting extra to exclude self
results = self._collection.query(
query_embeddings=[embedding],
n_results=limit + 1,
include=["documents", "metadatas", "distances"]
)
# Format and exclude self
formatted = []
if results and results['ids'] and results['ids'][0]:
for i, mid in enumerate(results['ids'][0]):
if mid == memory_id:
continue
distance = results['distances'][0][i]
score = 1 - distance
formatted.append({
'id': mid,
'score': round(score, 4),
'raw': results['documents'][0][i],
'metadata': results['metadatas'][0][i] if results['metadatas'] else {}
})
if len(formatted) >= limit:
break
return formatted
except Exception as e:
logging.warning(f"[embeddings] Find similar failed: {e}")
return []
def get_sync_status(self) -> Dict[str, Any]:
"""
Get sync status between CSV and ChromaDB.
Returns dict with: initialized, indexed_count, csv_count, in_sync
"""
import pandas as pd
# Count CSV entries
csv_count = 0
if MEMORY_MATTER_CSV.exists():
try:
df = pd.read_csv(MEMORY_MATTER_CSV)
csv_count = len(df)
except Exception:
pass
if not self._initialized and not self._lazy_init():
return {
'initialized': False,
'indexed_count': 0,
'csv_count': csv_count,
'in_sync': False,
'error': self._init_error
}
try:
indexed_count = self._collection.count()
return {
'initialized': True,
'indexed_count': indexed_count,
'csv_count': csv_count,
'in_sync': indexed_count == csv_count
}
except Exception as e:
return {
'initialized': False,
'indexed_count': 0,
'csv_count': csv_count,
'in_sync': False,
'error': str(e)
}
def rebuild_index(
self,
progress_callback: Optional[Callable[[int, int], None]] = None
) -> Dict[str, Any]:
"""
Rebuild entire index from CSV.
Args:
progress_callback: Optional callback(current, total) for progress updates
Returns:
Dict with success, total, indexed, errors
"""
import pandas as pd
if not self._lazy_init():
return {
'success': False,
'total': 0,
'indexed': 0,
'errors': [{'error': self._init_error}]
}
try:
# Clear existing collection
try:
self._client.delete_collection(self.collection_name)
except Exception:
pass
# Recreate collection
self._collection = self._client.create_collection(
name=self.collection_name,
metadata={"hnsw:space": "cosine"}
)
# Read all memories from CSV
if not MEMORY_MATTER_CSV.exists():
return {
'success': True,
'total': 0,
'indexed': 0,
'errors': []
}
df = pd.read_csv(MEMORY_MATTER_CSV)
total = len(df)
indexed = 0
errors = []
for i, row in df.iterrows():
try:
memory_id = row['id']
raw = str(row.get('raw', ''))
if not raw or raw == 'nan':
continue
metadata = {
'timestamp_actual': str(row.get('timestamp_actual', '')),
'layers': str(row.get('layers', '')),
'tags': str(row.get('tags', '')),
'source_type': str(row.get('source_type', ''))
}
# Handle numeric fields
try:
metadata['toxicity'] = int(row.get('toxicity', 0))
except (ValueError, TypeError):
metadata['toxicity'] = 0
try:
metadata['nutrient_density'] = int(row.get('nutrient_density', 0))
except (ValueError, TypeError):
metadata['nutrient_density'] = 0
self.add_memory(memory_id, raw, metadata)
indexed += 1
if progress_callback:
progress_callback(i + 1, total)
except Exception as e:
errors.append({'id': str(row.get('id', f'row_{i}')), 'error': str(e)})
return {
'success': True,
'total': total,
'indexed': indexed,
'errors': errors
}
except Exception as e:
return {
'success': False,
'total': 0,
'indexed': 0,
'errors': [{'error': str(e)}]
}
# Singleton instance
embeddings = EmbeddingService()