-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
26 lines (23 loc) · 883 Bytes
/
vector_store.py
File metadata and controls
26 lines (23 loc) · 883 Bytes
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
import faiss
import numpy as np
class VectorStore:
def __init__(self, embedding_dim):
"""
Initializes a FAISS index for storing embeddings.
"""
self.index = faiss.IndexFlatL2(embedding_dim)
self.text_chunks = []
def add_embeddings(self, embeddings, chunks):
"""
Adds embeddings and corresponding text chunks to the index.
"""
self.index.add(np.array(embeddings).astype("float32"))
self.text_chunks.extend(chunks)
def search(self, query_embedding, top_k=5):
"""
Searches the index for top_k similar embeddings.
"""
query_embedding = np.array([query_embedding]).astype("float32")
distances, indices = self.index.search(query_embedding, top_k)
results = [self.text_chunks[i] for i in indices[0]]
return results