File size: 1,652 Bytes
cbc5a3a
 
 
a3b2ab5
cbc5a3a
 
a3b2ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc5a3a
 
 
 
 
a3b2ab5
cbc5a3a
 
 
a3b2ab5
cbc5a3a
a3b2ab5
cbc5a3a
 
a3b2ab5
cbc5a3a
a3b2ab5
 
cbc5a3a
 
a3b2ab5
cbc5a3a
a3b2ab5
 
cbc5a3a
 
 
 
a3b2ab5
cbc5a3a
 
 
 
 
 
 
 
a3b2ab5
 
cbc5a3a
 
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
55
56
57
58
59
60
61
62
import json
import faiss
import numpy as np
import os


# ---------- LOAD SETTINGS ----------
def load_settings():
    with open("config/settings.json", "r", encoding="utf-8") as f:
        return json.load(f)

SETTINGS = load_settings()

EMBEDDING_MODEL = SETTINGS["embedding_model"]
FAISS_INDEX_PATH = SETTINGS["faiss_index_path"]
METADATA_PATH = SETTINGS["metadata_path"]
TOP_K = SETTINGS["top_k"]


# ---------- GLOBAL CACHES ----------
_model = None
_index = None
_metadata = None


# ---------- LOAD RESOURCES (LAZY) ----------
def load_resources():
    global _model, _index, _metadata

    # Load embedding model lazily
    if _model is None:
        from sentence_transformers import SentenceTransformer
        _model = SentenceTransformer(EMBEDDING_MODEL)

    # Load FAISS index if present
    if _index is None:
        if not os.path.exists(FAISS_INDEX_PATH):
            raise FileNotFoundError("FAISS index not found. Build the index first.")
        _index = faiss.read_index(FAISS_INDEX_PATH)

    # Load metadata if present
    if _metadata is None:
        if not os.path.exists(METADATA_PATH):
            raise FileNotFoundError("Metadata file not found. Build the index first.")
        with open(METADATA_PATH, "r", encoding="utf-8") as f:
            _metadata = json.load(f)


# ---------- RETRIEVAL ----------
def retrieve(query: str):
    load_resources()

    query_embedding = _model.encode([query]).astype("float32")
    distances, indices = _index.search(query_embedding, TOP_K)

    results = []
    for idx in indices[0]:
        if idx < len(_metadata):
            results.append(_metadata[idx])

    return results