|
import os |
|
import faiss |
|
import numpy as np |
|
from pathlib import Path |
|
import sys |
|
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
|
from import_Path import BASE_DIR |
|
|
|
|
|
def save_faiss_embeddings_index(embeddings, file_name): |
|
|
|
if not isinstance(embeddings, np.ndarray): |
|
embeddings = embeddings.numpy() |
|
embeddings = embeddings.astype('float32') |
|
|
|
|
|
|
|
|
|
|
|
index = faiss.IndexFlatL2(embeddings.shape[1]) |
|
index.add(embeddings) |
|
|
|
|
|
|
|
index_path = BASE_DIR / "embeddings" / file_name |
|
faiss.write_index(index, str(index_path)) |
|
|
|
def load_faiss_index(index_path): |
|
index = faiss.read_index(index_path) |
|
return index |
|
|
|
def normalize_embeddings(embeddings): |
|
|
|
embeddings = embeddings / np.linalg.norm(embeddings, axis=1)[:, None] |
|
return embeddings |