Spaces:
Runtime error
Runtime error
File size: 1,266 Bytes
06696b5 |
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 |
import faiss
import numpy as np
import os
from sentence_transformers import SentenceTransformer
from retriever.reranker import rerank_documents
# 1. μλ² λ© λͺ¨λΈ λ‘λ
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
# 2. 벑ν°DB (FAISS Index) μ΄κΈ°ν
INDEX_PATH = "data/index/index.faiss"
DOCS_PATH = "data/index/docs.npy"
if os.path.exists(INDEX_PATH) and os.path.exists(DOCS_PATH):
index = faiss.read_index(INDEX_PATH)
documents = np.load(DOCS_PATH, allow_pickle=True)
else:
index = None
documents = None
print("No FAISS index or docs found. Please build the index first.")
# 3. κ²μ ν¨μ
def search_documents(query: str, top_k: int = 5):
if index is None or documents is None:
raise ValueError("Index or documents not loaded. Build the FAISS index first.")
# 1. FAISS rough κ²μ
query_embedding = embedding_model.encode([query], convert_to_tensor=True).cpu().detach().numpy()
distances, indices = index.search(query_embedding, top_k)
results = [documents[idx] for idx in indices[0] if idx != -1]
# 2. Reranking μ μ©
reranked_results = rerank_documents(query, results, top_k=top_k)
return reranked_results
|