-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
341 lines (289 loc) · 11.7 KB
/
vector_store.py
File metadata and controls
341 lines (289 loc) · 11.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
"""Vector database with hybrid search support."""
import json
from pathlib import Path
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
import numpy as np
@dataclass
class SearchResult:
"""A search result with score and metadata."""
chunk_id: str
content: str
score: float
metadata: Dict[str, Any]
parent_content: Optional[str] = None # For parent-document retrieval
class VectorStore:
"""
ChromaDB-based vector store with hybrid search capabilities.
Features:
- Dense vector similarity search
- Metadata filtering (by chapter, severity, CVE, etc.)
- Parent-document retrieval
- Reciprocal Rank Fusion for hybrid results
"""
def __init__(
self,
collection_name: str = "cybersecurity_book",
persist_directory: str = "./vector_db",
distance_metric: str = "cosine"
):
self.collection_name = collection_name
self.persist_directory = persist_directory
self.distance_metric = distance_metric
self._client = None
self._collection = None
self._parent_store: Dict[str, str] = {} # parent_id -> content
@property
def client(self):
"""Lazy load ChromaDB client."""
if self._client is None:
import chromadb
from chromadb.config import Settings
self._client = chromadb.PersistentClient(
path=self.persist_directory,
settings=Settings(anonymized_telemetry=False)
)
return self._client
@property
def collection(self):
"""Get or create the collection."""
if self._collection is None:
self._collection = self.client.get_or_create_collection(
name=self.collection_name,
metadata={"hnsw:space": self.distance_metric}
)
return self._collection
def add_chunks(
self,
chunk_ids: List[str],
embeddings: np.ndarray,
contents: List[str],
metadatas: List[Dict[str, Any]],
batch_size: int = 100
):
"""
Add chunks to the vector store.
Args:
chunk_ids: Unique IDs for each chunk
embeddings: numpy array of embeddings
contents: Text content of each chunk
metadatas: Metadata for each chunk
batch_size: Batch size for insertion
"""
# Store parent content separately
for i, meta in enumerate(metadatas):
if meta.get('is_parent', False):
self._parent_store[chunk_ids[i]] = contents[i]
# Filter out parent chunks from vector store (we only search children)
child_indices = [i for i, m in enumerate(metadatas) if not m.get('is_parent', False)]
if not child_indices:
return
child_ids = [chunk_ids[i] for i in child_indices]
child_embeddings = embeddings[child_indices]
child_contents = [contents[i] for i in child_indices]
child_metadatas = [self._clean_metadata(metadatas[i]) for i in child_indices]
# Add in batches
for i in range(0, len(child_ids), batch_size):
end_idx = min(i + batch_size, len(child_ids))
self.collection.add(
ids=child_ids[i:end_idx],
embeddings=child_embeddings[i:end_idx].tolist(),
documents=child_contents[i:end_idx],
metadatas=child_metadatas[i:end_idx]
)
# Save parent store
self._save_parent_store()
print(f"Added {len(child_ids)} child chunks to vector store")
print(f"Stored {len(self._parent_store)} parent chunks for retrieval")
def _clean_metadata(self, metadata: Dict[str, Any]) -> Dict[str, Any]:
"""Clean metadata for ChromaDB (only supports str, int, float, bool)."""
cleaned = {}
for key, value in metadata.items():
if isinstance(value, (str, int, float, bool)):
cleaned[key] = value
elif isinstance(value, list):
# Convert lists to comma-separated strings
cleaned[key] = ",".join(str(v) for v in value)
elif value is None:
cleaned[key] = ""
else:
cleaned[key] = str(value)
return cleaned
def _save_parent_store(self):
"""Save parent store to disk."""
parent_file = Path(self.persist_directory) / "parent_store.json"
parent_file.parent.mkdir(parents=True, exist_ok=True)
with open(parent_file, 'w') as f:
json.dump(self._parent_store, f)
def _load_parent_store(self):
"""Load parent store from disk."""
parent_file = Path(self.persist_directory) / "parent_store.json"
if parent_file.exists():
with open(parent_file, 'r') as f:
self._parent_store = json.load(f)
def search(
self,
query_embedding: np.ndarray,
n_results: int = 10,
where: Optional[Dict[str, Any]] = None,
include_parent: bool = True
) -> List[SearchResult]:
"""
Search for similar chunks.
Args:
query_embedding: Query vector
n_results: Number of results to return
where: Metadata filter (e.g., {"chapter": "Network Security"})
include_parent: Whether to include parent content in results
Returns:
List of SearchResult objects
"""
# Load parent store if needed
if include_parent and not self._parent_store:
self._load_parent_store()
results = self.collection.query(
query_embeddings=[query_embedding.tolist()],
n_results=n_results,
where=where,
include=["documents", "metadatas", "distances"]
)
search_results = []
for i in range(len(results['ids'][0])):
chunk_id = results['ids'][0][i]
content = results['documents'][0][i]
metadata = results['metadatas'][0][i]
distance = results['distances'][0][i]
# Convert distance to similarity score
score = 1 - distance if self.distance_metric == "cosine" else 1 / (1 + distance)
# Get parent content if available
parent_content = None
if include_parent:
parent_id = metadata.get('parent_id', '')
if parent_id and parent_id in self._parent_store:
parent_content = self._parent_store[parent_id]
search_results.append(SearchResult(
chunk_id=chunk_id,
content=content,
score=score,
metadata=metadata,
parent_content=parent_content
))
return search_results
def hybrid_search(
self,
query_embedding: np.ndarray,
query_text: str,
n_results: int = 10,
dense_weight: float = 0.7,
where: Optional[Dict[str, Any]] = None
) -> List[SearchResult]:
"""
Hybrid search combining dense and sparse (keyword) retrieval.
Uses Reciprocal Rank Fusion (RRF) to merge results.
"""
# Dense search
dense_results = self.search(
query_embedding=query_embedding,
n_results=n_results * 2, # Get more for fusion
where=where,
include_parent=False
)
# Sparse search (keyword matching via ChromaDB's where_document)
try:
sparse_results = self.collection.query(
query_texts=[query_text],
n_results=n_results * 2,
where=where,
include=["documents", "metadatas", "distances"]
)
sparse_search_results = []
for i in range(len(sparse_results['ids'][0])):
sparse_search_results.append(SearchResult(
chunk_id=sparse_results['ids'][0][i],
content=sparse_results['documents'][0][i],
score=1.0, # Keyword matches are binary
metadata=sparse_results['metadatas'][0][i]
))
except:
sparse_search_results = []
# Reciprocal Rank Fusion
fused = self._reciprocal_rank_fusion(
dense_results=dense_results,
sparse_results=sparse_search_results,
dense_weight=dense_weight,
k=60 # RRF constant
)
# Load parent content for final results
self._load_parent_store()
for result in fused[:n_results]:
parent_id = result.metadata.get('parent_id', '')
if parent_id in self._parent_store:
result.parent_content = self._parent_store[parent_id]
return fused[:n_results]
def _reciprocal_rank_fusion(
self,
dense_results: List[SearchResult],
sparse_results: List[SearchResult],
dense_weight: float,
k: int = 60
) -> List[SearchResult]:
"""
Merge results using Reciprocal Rank Fusion.
RRF score = sum(1 / (k + rank))
"""
scores: Dict[str, float] = {}
result_map: Dict[str, SearchResult] = {}
# Score dense results
for rank, result in enumerate(dense_results, start=1):
rrf_score = dense_weight * (1 / (k + rank))
scores[result.chunk_id] = scores.get(result.chunk_id, 0) + rrf_score
result_map[result.chunk_id] = result
# Score sparse results
for rank, result in enumerate(sparse_results, start=1):
rrf_score = (1 - dense_weight) * (1 / (k + rank))
scores[result.chunk_id] = scores.get(result.chunk_id, 0) + rrf_score
if result.chunk_id not in result_map:
result_map[result.chunk_id] = result
# Sort by fused score
sorted_ids = sorted(scores.keys(), key=lambda x: scores[x], reverse=True)
# Update scores in results
fused = []
for chunk_id in sorted_ids:
result = result_map[chunk_id]
result.score = scores[chunk_id]
fused.append(result)
return fused
def filter_by_category(
self,
query_embedding: np.ndarray,
category: str,
n_results: int = 10
) -> List[SearchResult]:
"""
Search within a specific cybersecurity category.
Categories: network_security, web_security, cryptography, malware,
authentication, incident_response, compliance, cloud_security
"""
where = {"categories": {"$contains": category}}
return self.search(query_embedding, n_results, where)
def filter_by_severity(
self,
query_embedding: np.ndarray,
severity: str,
n_results: int = 10
) -> List[SearchResult]:
"""
Search for chunks with specific severity level.
Severities: critical, high, medium, low
"""
where = {"severity": severity}
return self.search(query_embedding, n_results, where)
def get_chunk_count(self) -> int:
"""Get the number of chunks in the store."""
return self.collection.count()
def clear(self):
"""Clear all data from the store."""
self.client.delete_collection(self.collection_name)
self._collection = None
self._parent_store = {}
print(f"Cleared collection: {self.collection_name}")