File size: 1,368 Bytes
fb61bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
import faiss
import numpy as np

def build_faiss_index(embeddings):
    """
    Build a FAISS index from embeddings.

    Args:
        embeddings (np.ndarray): Array of embeddings.

    Returns:
        faiss.Index: FAISS index.
    """
    dimension = embeddings.shape[1]
    index = faiss.IndexFlatIP(dimension)  # Inner product for cosine similarity
    # Normalize embeddings for cosine similarity
    embeddings = embeddings.astype('float32')
    embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
    index.add(embeddings)
    return index

def save_faiss_index(index, path):
    """
    Save FAISS index to file.

    Args:
        index (faiss.Index): FAISS index.
        path (str): Path to save the index.
    """
    faiss.write_index(index, path)

def load_faiss_index(path):
    """
    Load FAISS index from file.

    Args:
        path (str): Path to the index file.

    Returns:
        faiss.Index: Loaded FAISS index.
    """
    return faiss.read_index(path)

def create_and_save_index(embeddings_path, index_path):
    """
    Load embeddings, build index, and save it.

    Args:
        embeddings_path (str): Path to embeddings.npy.
        index_path (str): Path to save index.faiss.
    """
    embeddings = np.load(embeddings_path)
    index = build_faiss_index(embeddings)
    save_faiss_index(index, index_path)