-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
368 lines (318 loc) · 13.7 KB
/
Copy pathvector_store.py
File metadata and controls
368 lines (318 loc) · 13.7 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
import json
from pathlib import Path
from typing import Dict, List, Optional, Union
import chromadb
from sentence_transformers import SentenceTransformer
from models import BoundingBox, Chunk, FigureRef, QueryResult
_TAG_PREFIX = "tag_"
class VectorStore:
"""ChromaDB-backed vector store for :class:`~models.Chunk` objects.
Chunks are embedded with a sentence-transformers model and stored in a
persistent local ChromaDB collection. The ``file_hash`` field on every
chunk is stored as metadata so that results can later be filtered to a
single document, and the corresponding DoclingDocument can be retrieved
from a :class:`~document_store.DocumentStore` for visual grounding.
Tags are stored as ChromaDB metadata with a ``tag_`` prefix so they can be
used as filter predicates in :meth:`query`.
"""
def __init__(
self,
collection_name: str = "vector",
persist_directory: str = "./vector_data/vector/embeddings",
embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2",
):
persist_path = Path(persist_directory)
persist_path.mkdir(parents=True, exist_ok=True)
self.client = chromadb.PersistentClient(path=str(persist_path))
self.embeddings = SentenceTransformer(embedding_model)
self.collection = self.client.get_or_create_collection(
name=collection_name,
metadata={"hnsw:space": "cosine"},
)
self.collection_name = collection_name
# ------------------------------------------------------------------
# Serialization helpers
# ------------------------------------------------------------------
def _to_meta(self, chunk: Chunk, tags: Optional[Dict[str, str]] = None) -> dict:
"""Flatten a Chunk to a ChromaDB-compatible metadata dict (str/int/float values).
Tags are stored with a ``tag_`` prefix so they can be filtered via
ChromaDB ``where`` clauses without colliding with built-in fields.
"""
meta = {
"index": chunk.index,
# Use -1 as a sentinel for None; ChromaDB does not support null values.
"page_number": chunk.page_number if chunk.page_number is not None else -1,
"page_range": json.dumps(chunk.page_range),
"headings": json.dumps(chunk.headings),
"doc_items": json.dumps(chunk.doc_items),
"bboxes": json.dumps([b.model_dump() for b in chunk.bboxes]),
"figures": json.dumps([f.model_dump() for f in chunk.figures]),
"document_name": chunk.document_name,
"file_hash": chunk.file_hash,
"file_extension": chunk.file_extension,
}
if tags:
for k, v in tags.items():
meta[f"{_TAG_PREFIX}{k}"] = v
return meta
def _from_meta(self, id: str, text: str, meta: dict) -> Chunk:
"""Reconstruct a Chunk from a ChromaDB result row."""
page_no = meta["page_number"]
return Chunk(
id=id,
index=meta["index"],
text=text,
page_number=page_no if page_no != -1 else None,
page_range=json.loads(meta.get("page_range", "[]")),
headings=json.loads(meta["headings"]),
doc_items=json.loads(meta["doc_items"]),
bboxes=[BoundingBox(**b) for b in json.loads(meta["bboxes"])],
figures=[FigureRef(**f) for f in json.loads(meta.get("figures", "[]"))],
document_name=meta["document_name"],
file_hash=meta["file_hash"],
file_extension=meta["file_extension"],
)
# ------------------------------------------------------------------
# CRUD
# ------------------------------------------------------------------
def create(self, chunks: List[Chunk], tags: Optional[Dict[str, str]] = None) -> None:
"""Embed and store a list of chunks in the collection.
Args:
chunks: Chunks to add. Their ``id`` values must be unique within
the collection (the Chunker generates ``"{file_hash}_{index}"``
ids which satisfy this across documents).
tags: Optional dict of ``key -> value`` tags to attach to every
chunk's metadata (stored with a ``tag_`` prefix).
"""
if not chunks:
return
texts = [c.text for c in chunks]
embeddings = self.embeddings.encode(texts).tolist()
self.collection.upsert(
ids=[c.id for c in chunks],
embeddings=embeddings,
documents=texts,
metadatas=[self._to_meta(c, tags=tags) for c in chunks],
)
def _fetch_window(self, hit: Chunk, window: int) -> List[Chunk]:
"""Fetch up to *window* chunks before and after *hit* from the same document."""
if window == 0:
return [hit]
start = max(0, hit.index - window)
end = hit.index + window
raw = self.collection.get(
where={
"$and": [
{"file_hash": {"$eq": hit.file_hash}},
{"index": {"$gte": start}},
{"index": {"$lte": end}},
]
},
include=["documents", "metadatas"],
)
chunks = [
self._from_meta(
id=raw["ids"][i],
text=raw["documents"][i],
meta=raw["metadatas"][i],
)
for i in range(len(raw["ids"]))
]
chunks.sort(key=lambda c: c.index)
return chunks
def query(
self,
query_text: str,
top_k: int = 5,
file_hash: Optional[Union[str, List[str]]] = None,
window: int = 0,
tags: Optional[Dict[str, Union[str, List[str]]]] = None,
) -> List[QueryResult]:
"""Semantic search over stored chunks.
Args:
query_text: Natural-language query.
top_k: Maximum number of results to return.
file_hash: Restrict results to one or more documents.
Pass a single hash string, a list of hash strings,
or ``None`` to search across all documents.
window: Number of adjacent chunks to include before and after each
hit. ``0`` returns only the matching chunk itself.
tags: Optional dict of tag filters. Values may be a single string
(exact match, ``$eq``) or a list of strings (OR match, ``$in``).
All supplied tag keys must match (AND semantics across keys).
Tags are stored with a ``tag_`` prefix in ChromaDB metadata.
Returns:
List of :class:`~models.QueryResult` objects ranked by cosine
similarity. Each result exposes the matching ``chunk`` and a
``context`` list of surrounding chunks sorted by index.
"""
total = self.collection.count()
if total == 0:
return []
query_embedding = self.embeddings.encode(query_text).tolist()
# Build where clause from file_hash + tags filters
conditions = []
if file_hash is not None:
if isinstance(file_hash, list):
conditions.append({"file_hash": {"$in": file_hash}})
else:
conditions.append({"file_hash": {"$eq": file_hash}})
if tags:
for k, v in tags.items():
field = f"{_TAG_PREFIX}{k}"
if isinstance(v, list):
# OR logic: any of the listed values matches
conditions.append({field: {"$in": v}})
else:
conditions.append({field: {"$eq": v}})
if len(conditions) == 0:
where = None
elif len(conditions) == 1:
where = conditions[0]
else:
where = {"$and": conditions}
# When filtering, the filtered subset may be smaller than top_k.
# ChromaDB raises if n_results exceeds the number of matching chunks.
# Fetch at most top_k IDs to clamp n_results without scanning the full collection.
if where is not None:
sample = self.collection.get(where=where, limit=top_k)
filtered_count = len(sample["ids"])
if filtered_count == 0:
return []
n_results = filtered_count
else:
n_results = min(top_k, total)
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=n_results,
where=where,
)
query_results = []
seen_chunk_ids: set[str] = set()
for i in range(len(results["documents"][0])):
hit = self._from_meta(
id=results["ids"][0][i],
text=results["documents"][0][i],
meta=results["metadatas"][0][i],
)
# Skip hits whose chunk was already covered by a prior result's
# context window — avoids returning the same text multiple times.
if hit.id in seen_chunk_ids:
continue
context = self._fetch_window(hit, window)
for c in context:
seen_chunk_ids.add(c.id)
query_results.append(QueryResult(chunk=hit, context=context))
return query_results
def list_documents(self) -> List[dict]:
"""Return one summary entry per unique document stored in the collection.
Returns:
List of dicts with ``file_hash``, ``document_name``, ``file_extension``,
and ``chunk_count`` keys, sorted by document_name.
"""
raw = self.collection.get(include=["metadatas"])
seen: dict[str, dict] = {}
for meta in raw["metadatas"]:
fh = meta["file_hash"]
if fh not in seen:
seen[fh] = {
"file_hash": fh,
"document_name": meta["document_name"],
"file_extension": meta["file_extension"],
"chunk_count": 0,
}
seen[fh]["chunk_count"] += 1
return sorted(seen.values(), key=lambda d: d["document_name"])
def delete(self, file_hash: str) -> None:
"""Remove all chunks belonging to a document.
Args:
file_hash: The ``binary_hash`` of the document whose chunks should
be removed.
"""
results = self.collection.get(where={"file_hash": file_hash})
if results["ids"]:
self.collection.delete(ids=results["ids"])
def update(self, chunks: List[Chunk], tags: Optional[Dict[str, str]] = None) -> None:
"""Replace all chunks for a document (delete existing, then re-add).
Args:
chunks: New chunks for the document. All chunks must share the
same ``file_hash``.
tags: Optional tags to store on the replacement chunks.
"""
if not chunks:
return
self.delete(chunks[0].file_hash)
self.create(chunks, tags=tags)
def remove_tag_metadata(self, file_hash: str, tag_keys: List[str]) -> None:
"""Remove ``tag_<key>`` fields from ChromaDB metadata for all chunks of a document.
Args:
file_hash: The document whose chunks should be updated.
tag_keys: Tag keys to remove (without the ``tag_`` prefix).
"""
results = self.collection.get(
where={"file_hash": {"$eq": file_hash}},
include=["metadatas"],
)
if not results["ids"]:
return
updated_metas = []
for m in results["metadatas"]:
updated = dict(m)
for k in tag_keys:
updated.pop(f"{_TAG_PREFIX}{k}", None)
updated_metas.append(updated)
self.collection.update(
ids=results["ids"],
metadatas=updated_metas,
)
def set_tag_metadata(self, file_hash: str, tags: Dict[str, str]) -> None:
"""Merge tag fields into ChromaDB metadata for all chunks of a document.
Args:
file_hash: The document whose chunks should be updated.
tags: Dict of tag key -> value to merge. Keys are stored with the ``tag_`` prefix.
"""
results = self.collection.get(
where={"file_hash": {"$eq": file_hash}},
include=["metadatas"],
)
if not results["ids"]:
return
updated_metas = []
for m in results["metadatas"]:
updated = dict(m)
for k, v in tags.items():
updated[f"{_TAG_PREFIX}{k}"] = v
updated_metas.append(updated)
self.collection.update(
ids=results["ids"],
metadatas=updated_metas,
)
def has_chunks(self, file_hash: str) -> bool:
"""Return True if any chunks exist for a document in the collection.
Uses a limit=1 fetch to avoid scanning the entire collection.
"""
result = self.collection.get(
where={"file_hash": {"$eq": file_hash}},
limit=1,
)
return bool(result["ids"])
def rename_document(self, file_hash: str, new_name: str) -> None:
"""Update the document_name metadata on all chunks for a document.
No re-embedding required — only the stored metadata field changes.
Args:
file_hash: The document to rename.
new_name: The new display name.
"""
results = self.collection.get(
where={"file_hash": {"$eq": file_hash}},
include=["metadatas"],
)
if not results["ids"]:
return
updated_metas = [
{**m, "document_name": new_name} for m in results["metadatas"]
]
self.collection.update(
ids=results["ids"],
metadatas=updated_metas,
)