forked from anarchy-ai/LLM-VM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrate_FAISS.py
42 lines (35 loc) · 1.45 KB
/
Integrate_FAISS.py
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
# We are import the require library
import faiss
import numpy as np
# We are define the class VectorDB
class VectorDB:
def __init__(self,dimension):
self.dimension = dimension
self.vectors = []
# We are create faiss index
self.index = faiss.IndexFlatL2(dimension)
def add_vector(self, vector):
if len(vector) != self.dimension:
raise ValueError("Vector dimension mismatch")
self.vectors.append(vector)
# We are add vector to the FAISS index
self.index.add(np.array[vector], dtype= np.float32)
def search_vector(self, query_vector, k=5):
if len(query_vector) != self.dimension:
raise ValueError("Query vector dimension mismatch")
# We are search for the nearest neighbors with FAISS
distances, indices = self.index.search(np.array([query_vector], dtype=np.float32), k)
return distances [0], indices[0]
# We are define example usage
if __name__ == "__main__":
# We are create a vectorDB instance
vector_db = VectorDB(dimension=100)
# We are add vectors to the database
vectors_to_add = [[0.1]* 100, [0.2]*100, [0.3]*100]
for vector in vectors_to_add:
vector_db.add_vector(vector)
# We are search for nearest neighbors
query_vector = [0.15]* 100
distances, indices = vector_db.search_vector(query_vector)
print("Distances:", distances)
print("Indices:", indices)