Spaces:
Sleeping
Sleeping
Update rag_query.py
Browse files- rag_query.py +38 -0
rag_query.py
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
|
| 6 |
+
from config.settings import *
|
| 7 |
+
|
| 8 |
+
_model = None
|
| 9 |
+
_index = None
|
| 10 |
+
_metadata = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def load_resources():
|
| 14 |
+
global _model, _index, _metadata
|
| 15 |
+
|
| 16 |
+
if _model is None:
|
| 17 |
+
_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 18 |
+
|
| 19 |
+
if _index is None:
|
| 20 |
+
_index = faiss.read_index(FAISS_INDEX_PATH)
|
| 21 |
+
|
| 22 |
+
if _metadata is None:
|
| 23 |
+
with open(METADATA_PATH, "r", encoding="utf-8") as f:
|
| 24 |
+
_metadata = json.load(f)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def retrieve(query: str):
|
| 28 |
+
load_resources()
|
| 29 |
+
|
| 30 |
+
query_embedding = _model.encode([query]).astype("float32")
|
| 31 |
+
distances, indices = _index.search(query_embedding, TOP_K)
|
| 32 |
+
|
| 33 |
+
results = []
|
| 34 |
+
for idx in indices[0]:
|
| 35 |
+
meta = _metadata[idx]
|
| 36 |
+
results.append(meta)
|
| 37 |
+
|
| 38 |
+
return results
|