Update Mori_Technical_RAGwithFAISS.py
Browse files- Mori_Technical_RAGwithFAISS.py +38 -17
Mori_Technical_RAGwithFAISS.py
CHANGED
|
@@ -17,18 +17,19 @@ import faiss
|
|
| 17 |
import numpy as np
|
| 18 |
import torch
|
| 19 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
model_name = "intfloat/multilingual-e5-base"
|
| 26 |
-
index_basename = "mori"
|
| 27 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Lazy loading (solo en la primera llamada a retrieve_docs)
|
| 34 |
_rag_model = None
|
|
@@ -43,22 +44,42 @@ def _ensure_rag_loaded(verbose: bool = False):
|
|
| 43 |
"""
|
| 44 |
global _rag_model, _rag_index, _rag_metas
|
| 45 |
|
|
|
|
| 46 |
if _rag_model is None:
|
| 47 |
if verbose:
|
| 48 |
print(f"[*] Cargando modelo RAG ({model_name}) en {DEVICE}…")
|
| 49 |
_rag_model = SentenceTransformer(model_name, device=DEVICE)
|
| 50 |
|
|
|
|
| 51 |
if _rag_index is None:
|
| 52 |
if verbose:
|
| 53 |
-
print(f"[*]
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
if _rag_metas is None:
|
| 58 |
if verbose:
|
| 59 |
-
print(f"[*]
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
_rag_metas = json.load(f)
|
| 63 |
|
| 64 |
|
|
@@ -106,4 +127,4 @@ if __name__ == "__main__":
|
|
| 106 |
qs = "¿Para qué sirve un isolation forest?"
|
| 107 |
docs = retrieve_docs(qs, k=3, verbose=True)
|
| 108 |
for d in docs:
|
| 109 |
-
print(f"[score={d['score']:.3f}] {d['input']} -> {d['output']}")
|
|
|
|
| 17 |
import numpy as np
|
| 18 |
import torch
|
| 19 |
from sentence_transformers import SentenceTransformer
|
| 20 |
+
from huggingface_hub import hf_hub_download
|
| 21 |
|
| 22 |
+
#***************************************************************************
|
| 23 |
+
#Loading FAISS Vec DB
|
| 24 |
+
#***************************************************************************
|
| 25 |
+
DATASET_REPO_ID = "tecuhtli/Mori_FAISS_Full"
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# 🔹 Nombres de archivo dentro del dataset
|
| 28 |
+
FAISS_FILENAME = "mori.faiss" # <-- AJUSTA SI TU ARCHIVO SE LLAMA DIFERENTE
|
| 29 |
+
METAS_FILENAME = "mori_metas.json" # idem
|
| 30 |
+
|
| 31 |
+
model_name = "intfloat/multilingual-e5-base"
|
| 32 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
|
| 34 |
# Lazy loading (solo en la primera llamada a retrieve_docs)
|
| 35 |
_rag_model = None
|
|
|
|
| 44 |
"""
|
| 45 |
global _rag_model, _rag_index, _rag_metas
|
| 46 |
|
| 47 |
+
# Modelo de embeddings
|
| 48 |
if _rag_model is None:
|
| 49 |
if verbose:
|
| 50 |
print(f"[*] Cargando modelo RAG ({model_name}) en {DEVICE}…")
|
| 51 |
_rag_model = SentenceTransformer(model_name, device=DEVICE)
|
| 52 |
|
| 53 |
+
# Índice FAISS
|
| 54 |
if _rag_index is None:
|
| 55 |
if verbose:
|
| 56 |
+
print(f"[*] Descargando índice FAISS desde dataset: {DATASET_REPO_ID}/{FAISS_FILENAME}…")
|
| 57 |
+
|
| 58 |
+
# Descarga el archivo al caché local de HF y devuelve la ruta
|
| 59 |
+
faiss_local_path = hf_hub_download(
|
| 60 |
+
repo_id=DATASET_REPO_ID,
|
| 61 |
+
repo_type="dataset",
|
| 62 |
+
filename=FAISS_FILENAME,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if verbose:
|
| 66 |
+
print(f"[*] Leyendo índice FAISS desde {faiss_local_path}…")
|
| 67 |
+
_rag_index = faiss.read_index(str(faiss_local_path))
|
| 68 |
|
| 69 |
+
# Metadatos
|
| 70 |
if _rag_metas is None:
|
| 71 |
if verbose:
|
| 72 |
+
print(f"[*] Descargando metadatos desde dataset: {DATASET_REPO_ID}/{METAS_FILENAME}…")
|
| 73 |
+
|
| 74 |
+
metas_local_path = hf_hub_download(
|
| 75 |
+
repo_id=DATASET_REPO_ID,
|
| 76 |
+
repo_type="dataset",
|
| 77 |
+
filename=METAS_FILENAME,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if verbose:
|
| 81 |
+
print(f"[*] Leyendo metadatos desde {metas_local_path}…")
|
| 82 |
+
with open(metas_local_path, "r", encoding="utf-8") as f:
|
| 83 |
_rag_metas = json.load(f)
|
| 84 |
|
| 85 |
|
|
|
|
| 127 |
qs = "¿Para qué sirve un isolation forest?"
|
| 128 |
docs = retrieve_docs(qs, k=3, verbose=True)
|
| 129 |
for d in docs:
|
| 130 |
+
print(f"[score={d['score']:.3f}] {d['input']} -> {d['output']}")
|