-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
382 lines (306 loc) · 12.4 KB
/
Copy pathvector_store.py
File metadata and controls
382 lines (306 loc) · 12.4 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
"""
Vector store operations using ChromaDB.
"""
import chromadb
from chromadb.config import Settings
from typing import List, Dict, Optional, Any
import numpy as np
import logging
import threading
import concurrent.futures
import config
logger = logging.getLogger(__name__)
# A timed-out ChromaDB call keeps running (a running future can't be cancelled),
# so a small pool would be permanently saturated after a few hangs — every later
# call would then time out waiting for a free worker. A large pool means a hung
# call leaks at most one thread instead of poisoning the shared timeout facility.
# ponytail: 32 workers; if hangs ever pile up, fix the hang, don't grow the pool.
_chroma_executor = concurrent.futures.ThreadPoolExecutor(max_workers=32, thread_name_prefix="chroma-timeout")
def _chroma_call(fn, *args, timeout: float = 5.0, **kwargs) -> Any:
"""Run a ChromaDB call with a timeout. Raises TimeoutError if it hangs.
Note: cancel() cannot stop an already-running call; on timeout the worker
thread keeps running until the underlying call returns. The large pool above
bounds the damage of that leak.
"""
fut = _chroma_executor.submit(fn, *args, **kwargs)
try:
return fut.result(timeout=timeout)
except concurrent.futures.TimeoutError as err:
fut.cancel()
raise TimeoutError(f"ChromaDB operation timed out after {timeout}s") from err
# Global client cache
_chroma_client = None
_lock = threading.Lock()
def get_chroma_client() -> chromadb.PersistentClient:
"""
Get or create ChromaDB client with persistence (thread-safe).
"""
global _chroma_client
if _chroma_client is not None:
return _chroma_client
with _lock:
if _chroma_client is not None:
return _chroma_client
logger.info(f"Initializing ChromaDB at: {config.CHROMA_DB_DIR}")
_chroma_client = chromadb.PersistentClient(
path=str(config.CHROMA_DB_DIR),
settings=Settings(
anonymized_telemetry=False,
allow_reset=True
)
)
return _chroma_client
def get_or_create_collection(
collection_name: str = None,
reset: bool = False
) -> chromadb.Collection:
"""
Get or create a ChromaDB collection.
Args:
collection_name: Name of the collection (default from config)
reset: If True, delete existing collection and create new one
Returns:
ChromaDB collection instance
"""
if collection_name is None:
collection_name = config.COLLECTION_NAME
client = get_chroma_client()
# Reset if requested
if reset:
try:
client.delete_collection(name=collection_name)
logger.info(f"Deleted existing collection: {collection_name}")
except Exception:
pass # Collection doesn't exist
# Get or create collection
# ef_construction/max_neighbors only take effect when the collection is first
# created; get_or_create ignores them for an existing collection. ef_search is
# applied per query and can be retuned by re-creating with a new value.
collection = client.get_or_create_collection(
name=collection_name,
metadata={"description": "Multilingual scientific papers"},
configuration={"hnsw": {
"space": config.DISTANCE_METRIC,
"ef_construction": config.HNSW_EF_CONSTRUCTION,
"ef_search": config.HNSW_EF_SEARCH,
"max_neighbors": config.HNSW_M,
}}
)
logger.info(f"Collection '{collection_name}' ready. Current size: {collection.count()}")
return collection
def add_documents(
texts: List[str],
embeddings: np.ndarray,
metadatas: List[Dict[str, Any]],
ids: List[str],
collection: chromadb.Collection = None
) -> None:
"""
Add documents to the vector store.
Args:
texts: List of text chunks
embeddings: Numpy array of embeddings, shape (n_docs, embedding_dim)
metadatas: List of metadata dictionaries for each document
ids: List of unique IDs for each document
collection: ChromaDB collection (uses default if None)
"""
if collection is None:
collection = get_or_create_collection()
# Convert embeddings to list of lists
embeddings_list = embeddings.tolist()
logger.info(f"Adding {len(ids)} chunks. Unique IDs: {len(set(ids))}")
if len(ids) != len(set(ids)):
from collections import Counter
duplicates = {k: v for k, v in Counter(ids).items() if v > 1}
logger.error(f"Duplicate IDs detected before upsert: {duplicates}")
# Add to collection (upsert replaces existing, inserts new)
_chroma_call(collection.upsert,
documents=texts,
embeddings=embeddings_list,
metadatas=metadatas,
ids=ids
)
logger.info(f"Added {len(texts)} documents. Total in collection: {collection.count()}")
def search(
query_embedding: np.ndarray,
top_k: int = None,
filter_dict: Optional[Dict[str, Any]] = None,
collection: chromadb.Collection = None
) -> Dict[str, List]:
"""
Search for similar documents using vector similarity.
Args:
query_embedding: Query embedding, shape (embedding_dim,)
top_k: Number of results to return (default from config)
filter_dict: Optional metadata filter (e.g., {"year": 2023})
collection: ChromaDB collection (uses default if None)
Returns:
Dictionary with keys:
- 'ids': List of document IDs
- 'documents': List of document texts
- 'metadatas': List of metadata dicts
- 'distances': List of distances (lower is more similar)
"""
if collection is None:
collection = get_or_create_collection()
if top_k is None:
top_k = config.DEFAULT_TOP_K
# Convert embedding to list
query_embedding_list = query_embedding.tolist()
# Search
results = _chroma_call(collection.query,
query_embeddings=[query_embedding_list],
n_results=top_k,
where=filter_dict,
include=["documents", "metadatas", "distances"]
)
# Flatten results (query returns list of lists)
return {
'ids': results['ids'][0],
'documents': results['documents'][0],
'metadatas': results['metadatas'][0],
'distances': results['distances'][0]
}
def delete_collection(collection_name: str = None) -> None:
"""
Delete a collection from ChromaDB.
Args:
collection_name: Name of collection to delete (default from config)
"""
if collection_name is None:
collection_name = config.COLLECTION_NAME
client = get_chroma_client()
try:
client.delete_collection(name=collection_name)
logger.info(f"Deleted collection: {collection_name}")
except Exception as e:
logger.error(f"Error deleting collection: {e}")
def get_collection_stats(collection: chromadb.Collection = None) -> Dict[str, Any]:
"""
Get statistics about a collection.
Args:
collection: ChromaDB collection (uses default if None)
Returns:
Dictionary with collection statistics
"""
if collection is None:
collection = get_or_create_collection()
count = _chroma_call(collection.count)
# Get a sample to inspect metadata
sample = _chroma_call(collection.peek, limit=1)
stats = {
'name': collection.name,
'count': count,
'metadata': collection.metadata,
}
if sample['metadatas']:
stats['sample_metadata'] = sample['metadatas'][0]
return stats
def get_paper_chunk_counts(collection: chromadb.Collection = None) -> Dict[str, int]:
"""Chunk count per paper_id, for ingestion health / re-ingest tooling."""
if collection is None:
collection = get_or_create_collection()
got = _chroma_call(collection.get, include=['metadatas'])
counts: Dict[str, int] = {}
for meta in got.get('metadatas', []):
pid = (meta or {}).get('paper_id', '')
if pid:
counts[pid] = counts.get(pid, 0) + 1
return counts
def delete_by_paper_id(paper_id: str, collection: chromadb.Collection = None) -> int:
"""
Delete all chunks for a specific paper.
"""
if collection is None:
collection = get_or_create_collection()
ids = _chroma_call(collection.get, where={'paper_id': paper_id}, include=[])['ids']
_chroma_call(collection.delete, where={'paper_id': paper_id})
return len(ids)
def update_paper_metadata(paper_id: str, updates: dict, collection: chromadb.Collection = None) -> int:
"""Update metadata fields on all chunks for a paper. Returns chunk count updated."""
if collection is None:
collection = get_or_create_collection()
result = _chroma_call(collection.get, where={'paper_id': paper_id}, include=['metadatas'])
ids = result.get('ids', [])
if not ids:
return 0
new_metadatas = [{**m, **updates} for m in result['metadatas']]
_chroma_call(collection.update, ids=ids, metadatas=new_metadatas)
return len(ids)
def find_similar_paper(
title: str,
year: str = None,
threshold: float = 0.9,
collection: chromadb.Collection = None,
) -> Optional[str]:
"""Return an existing paper_id whose title is a near-duplicate of `title`, or None.
Cross-ingestion dedup for re-uploads under a different filename. Uses
difflib.SequenceMatcher (stdlib) rather than a fuzzy-matching dependency —
good enough for near-identical title comparison at this corpus scale.
"""
if collection is None:
collection = get_or_create_collection()
result = _chroma_call(collection.get, include=['metadatas'])
seen: Dict[str, dict] = {}
for meta in result.get('metadatas', []):
pid = meta.get('paper_id')
if pid and pid not in seen:
seen[pid] = meta
from difflib import SequenceMatcher
norm_title = title.strip().lower()
best_pid, best_ratio = None, 0.0
for pid, meta in seen.items():
if year and meta.get('year') and str(meta['year']) != str(year):
continue
ratio = SequenceMatcher(None, norm_title, str(meta.get('title', '')).strip().lower()).ratio()
if ratio > best_ratio:
best_pid, best_ratio = pid, ratio
return best_pid if best_ratio >= threshold else None
if __name__ == "__main__":
# Test vector store functionality
print("Testing ChromaDB Vector Store")
print("=" * 60)
# Create test collection
print("\n1. Creating test collection...")
collection = get_or_create_collection("test_collection", reset=True)
# Add test documents
print("\n2. Adding test documents...")
test_docs = [
"Diabetes is a metabolic disease.",
"Treatment includes insulin therapy.",
"Machine learning can predict disease outcomes.",
]
# Create dummy embeddings (in real use, these come from embedding model)
test_embeddings = np.random.randn(len(test_docs), config.EMBEDDING_DIMENSION)
test_embeddings = test_embeddings / np.linalg.norm(test_embeddings, axis=1, keepdims=True)
test_metadata = [
{"paper_id": "paper1", "title": "Diabetes Research", "section": "introduction"},
{"paper_id": "paper1", "title": "Diabetes Research", "section": "methods"},
{"paper_id": "paper2", "title": "ML in Medicine", "section": "results"},
]
test_ids = ["doc1", "doc2", "doc3"]
add_documents(test_docs, test_embeddings, test_metadata, test_ids, collection)
# Test search
print("\n3. Testing search...")
query_emb = np.random.randn(config.EMBEDDING_DIMENSION)
query_emb = query_emb / np.linalg.norm(query_emb)
results = search(query_emb, top_k=2, collection=collection)
print(f"\nTop {len(results['documents'])} results:")
for i, (doc, metadata, dist) in enumerate(zip(
results['documents'],
results['metadatas'],
results['distances']
)):
print(f"\n{i+1}. Distance: {dist:.4f}")
print(f" Text: {doc}")
print(f" Metadata: {metadata}")
# Get stats
print("\n4. Collection statistics...")
stats = get_collection_stats(collection)
print(f"Name: {stats['name']}")
print(f"Count: {stats['count']}")
print(f"Sample metadata: {stats.get('sample_metadata', {})}")
# Cleanup
print("\n5. Cleaning up test collection...")
delete_collection("test_collection")
print("Done!")