-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
340 lines (266 loc) · 9.8 KB
/
Copy pathvector_store.py
File metadata and controls
340 lines (266 loc) · 9.8 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
"""
Vector store interface and implementations for RAG.
This module provides pluggable vector store backends for storing and retrieving
chunk embeddings. All implementations are local-first (no external APIs).
"""
import json
import pickle
from abc import ABC, abstractmethod
from pathlib import Path
from typing import List, Dict, Any, Tuple, Optional
import numpy as np
from tqdm import tqdm
from logger import get_logger
logger = get_logger()
class VectorStore(ABC):
"""
Abstract base class for vector stores.
All implementations must be local-first (no external APIs required).
"""
@abstractmethod
def add(self, embeddings: np.ndarray, chunks: List[Dict[str, Any]], ids: List[str]):
"""
Add embeddings and associated chunks to the store.
Args:
embeddings: (N, D) array of embedding vectors
chunks: List of chunk dictionaries with metadata
ids: List of unique chunk IDs
"""
pass
@abstractmethod
def search(
self,
query_embedding: np.ndarray,
top_k: int = 5,
filters: Optional[Dict[str, Any]] = None
) -> List[Tuple[Dict[str, Any], float]]:
"""
Search for similar chunks.
Args:
query_embedding: Query vector
top_k: Number of results to return
filters: Optional metadata filters (e.g., {"chapter": "Chapter 1"})
Returns:
List of (chunk, similarity_score) tuples, sorted by similarity
"""
pass
@abstractmethod
def save(self, path: Path):
"""Save the vector store to disk."""
pass
@abstractmethod
def load(self, path: Path):
"""Load the vector store from disk."""
pass
@abstractmethod
def get_stats(self) -> Dict[str, Any]:
"""Get statistics about the vector store."""
pass
class InMemoryVectorStore(VectorStore):
"""
Simple in-memory vector store using numpy for similarity search.
Features:
- Fast cosine similarity search
- Metadata-based filtering
- Persistent storage via pickle
- No external dependencies beyond numpy
Best for:
- Small to medium datasets (< 100k chunks)
- Development and testing
- Fully local deployment
"""
def __init__(self):
"""Initialize empty vector store."""
self.embeddings = None # (N, D) array
self.chunks = [] # List of chunk dicts
self.ids = [] # List of chunk IDs
self.embedding_dim = None
def add(self, embeddings: np.ndarray, chunks: List[Dict[str, Any]], ids: List[str]):
"""
Add embeddings and chunks to the store.
Args:
embeddings: (N, D) array of embedding vectors
chunks: List of chunk dictionaries
ids: List of unique chunk IDs
"""
if len(embeddings) != len(chunks) or len(embeddings) != len(ids):
raise ValueError("Embeddings, chunks, and ids must have same length")
# Initialize or validate embedding dimension
if self.embedding_dim is None:
self.embedding_dim = embeddings.shape[1]
elif embeddings.shape[1] != self.embedding_dim:
raise ValueError(f"Embedding dimension mismatch: {embeddings.shape[1]} != {self.embedding_dim}")
# Append to existing data
if self.embeddings is None:
self.embeddings = embeddings
else:
self.embeddings = np.vstack([self.embeddings, embeddings])
self.chunks.extend(chunks)
self.ids.extend(ids)
def search(
self,
query_embedding: np.ndarray,
top_k: int = 5,
filters: Optional[Dict[str, Any]] = None
) -> List[Tuple[Dict[str, Any], float]]:
"""
Search for similar chunks using cosine similarity.
Args:
query_embedding: Query vector (D,)
top_k: Number of results to return
filters: Optional metadata filters
Returns:
List of (chunk, similarity_score) tuples
"""
if self.embeddings is None or len(self.chunks) == 0:
return []
# Validate query embedding dimension
if len(query_embedding) != self.embedding_dim:
raise ValueError(f"Query embedding dimension mismatch: {len(query_embedding)} != {self.embedding_dim}")
# Compute cosine similarities
similarities = self._cosine_similarity_batch(query_embedding, self.embeddings)
# Apply metadata filters
if filters:
valid_indices = self._apply_filters(filters)
# Set invalid similarities to -inf
mask = np.ones(len(similarities), dtype=bool)
mask[valid_indices] = False
similarities[mask] = -np.inf
# Get top-k indices
top_indices = np.argsort(similarities)[::-1][:top_k]
# Filter out -inf scores (filtered out chunks)
results = []
for idx in top_indices:
score = similarities[idx]
if score == -np.inf:
continue
results.append((self.chunks[idx], float(score)))
return results
def save(self, path: Path):
"""
Save vector store to disk using pickle.
Args:
path: Output file path (will create .pkl file)
"""
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
data = {
"embeddings": self.embeddings,
"chunks": self.chunks,
"ids": self.ids,
"embedding_dim": self.embedding_dim,
}
with open(path, "wb") as f:
pickle.dump(data, f)
def load(self, path: Path):
"""
Load vector store from disk.
Args:
path: Input file path (.pkl file)
"""
path = Path(path)
if not path.exists():
raise FileNotFoundError(f"Vector store file not found: {path}")
with open(path, "rb") as f:
data = pickle.load(f)
self.embeddings = data["embeddings"]
self.chunks = data["chunks"]
self.ids = data["ids"]
self.embedding_dim = data["embedding_dim"]
def get_stats(self) -> Dict[str, Any]:
"""Get statistics about the vector store."""
stats = {
"num_chunks": len(self.chunks),
"embedding_dim": self.embedding_dim,
}
# Count chunks by role
if self.chunks:
role_counts = {}
for chunk in self.chunks:
role = chunk.get("metadata", {}).get("pedagogical_role", "unknown")
role_counts[role] = role_counts.get(role, 0) + 1
stats["chunks_by_role"] = role_counts
# Count chunks by section
section_counts = {}
for chunk in self.chunks:
section = chunk.get("metadata", {}).get("section", "unknown")
section_counts[section] = section_counts.get(section, 0) + 1
stats["chunks_by_section"] = section_counts
return stats
def _cosine_similarity_batch(self, query: np.ndarray, embeddings: np.ndarray) -> np.ndarray:
"""
Compute cosine similarity between query and batch of embeddings.
Args:
query: Query vector (D,)
embeddings: Batch of vectors (N, D)
Returns:
Similarity scores (N,) in range [0, 1]
"""
# Normalize query
query_norm = query / (np.linalg.norm(query) + 1e-8)
# Normalize embeddings
embeddings_norm = embeddings / (np.linalg.norm(embeddings, axis=1, keepdims=True) + 1e-8)
# Compute dot products
similarities = np.dot(embeddings_norm, query_norm)
# Normalize from [-1, 1] to [0, 1]
similarities = (similarities + 1) / 2
return similarities
def _apply_filters(self, filters: Dict[str, Any]) -> List[int]:
"""
Find indices of chunks matching metadata filters.
Args:
filters: Dictionary of metadata filters
Returns:
List of valid chunk indices
"""
valid_indices = []
for idx, chunk in enumerate(self.chunks):
metadata = chunk.get("metadata", {})
# Check if all filter conditions are met
match = True
for key, value in filters.items():
if metadata.get(key) != value:
match = False
break
if match:
valid_indices.append(idx)
return valid_indices
def build_vector_store_from_chunks(
chunks_path: Path,
embedding_engine,
output_path: Path,
show_progress: bool = True
) -> InMemoryVectorStore:
"""
Build a vector store from a chunks JSON file.
Args:
chunks_path: Path to chunks.json or chunks_semantic.json
embedding_engine: EmbeddingEngine instance for generating embeddings
output_path: Path to save the vector store
show_progress: Whether to show progress bar
Returns:
Populated InMemoryVectorStore
"""
# Load chunks
with open(chunks_path, "r", encoding="utf-8") as f:
chunks = json.load(f)
if not chunks:
raise ValueError("No chunks found in file")
logger.info(f"Building vector store from {len(chunks)} chunks...")
# Extract chunk IDs and texts
ids = [chunk["id"] for chunk in chunks]
texts = [chunk["text"] for chunk in chunks]
# Generate embeddings (uses caching automatically)
embeddings = embedding_engine.embed_batch(
texts,
batch_size=32,
show_progress=show_progress
)
# Create and populate vector store
store = InMemoryVectorStore()
store.add(embeddings, chunks, ids)
# Save to disk
store.save(output_path)
logger.info(f"Vector store saved to {output_path}")
logger.info(f"Total chunks indexed: {len(chunks)}")
return store