|
from sentence_transformers import SentenceTransformer |
|
import faiss |
|
import numpy as np |
|
|
|
|
|
index = faiss.read_index("database/pdf_sections_index.faiss") |
|
|
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
def search_faiss(query, k=3): |
|
query_vector = model.encode([query])[0].astype('float32') |
|
query_vector = np.expand_dims(query_vector, axis=0) |
|
distances, indices = index.search(query_vector, k) |
|
return distances, indices |
|
|
|
|
|
query = "What is mental Health?" |
|
distances, indices = search_faiss(query) |
|
|
|
print(f"Query: {query}") |
|
print(f"Distances: {distances}") |
|
print(f"Indices: {indices}") |