bano1 commited on
Commit
cdb5c98
·
verified ·
1 Parent(s): 741b8ab

Create vector_store.py

Browse files
Files changed (1) hide show
  1. src/vector_store.py +64 -0
src/vector_store.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import faiss
2
+ import numpy as np
3
+
4
+
5
+ class VectorStore:
6
+
7
+
8
+ def __init__(self):
9
+
10
+ self.index = None
11
+ self.documents = []
12
+
13
+
14
+
15
+ def build(self, embeddings, documents):
16
+
17
+ embeddings = np.array(
18
+ embeddings
19
+ ).astype("float32")
20
+
21
+
22
+ dimension = embeddings.shape[1]
23
+
24
+
25
+ self.index = faiss.IndexFlatL2(
26
+ dimension
27
+ )
28
+
29
+
30
+ self.index.add(
31
+ embeddings
32
+ )
33
+
34
+
35
+ self.documents = documents
36
+
37
+
38
+
39
+ def search(self, query_embedding, k=5):
40
+
41
+ query_embedding = np.array(
42
+ [query_embedding]
43
+ ).astype("float32")
44
+
45
+
46
+ distances, indices = self.index.search(
47
+ query_embedding,
48
+ k
49
+ )
50
+
51
+
52
+ results=[]
53
+
54
+
55
+ for idx in indices[0]:
56
+
57
+ if idx < len(self.documents):
58
+
59
+ results.append(
60
+ self.documents[idx]
61
+ )
62
+
63
+
64
+ return results