File size: 1,213 Bytes
9a000fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# core/vector_store.py

import faiss
import io
import logging
from sentence_transformers import SentenceTransformer

logger = logging.getLogger(__name__)

def create_faiss_index(embeddings):
    """Creates a FAISS index from a list of embeddings."""
    if embeddings is None or len(embeddings) == 0:
        logger.warning("No embeddings provided to create FAISS index.")
        return None
    
    dimension = embeddings.shape[1]
    index = faiss.IndexFlatL2(dimension)
    index.add(embeddings.astype('float32'))
    
    # Serialize the index to bytes for in-memory storage
    try:
        bio = io.BytesIO()
        writer = faiss.PyCallbackIOWriter(bio.write)
        faiss.write_index(index, writer)
        return bio.getvalue()
    except Exception as e:
        logger.error(f"Failed to serialize FAISS index: {e}")
        return None

def deserialize_faiss_index(index_bytes: bytes) -> faiss.Index:
    """Deserializes a FAISS index from bytes."""
    try:
        bio = io.BytesIO(index_bytes)
        reader = faiss.PyCallbackIOReader(bio.read)
        return faiss.read_index(reader)
    except Exception as e:
        logger.error(f"Failed to deserialize FAISS index: {e}")
        return None