law-bot / libs /retrievers.py
anpigon's picture
refactor: Update retrievers.py to include run_name configuration for faiss and bm25 retrievers
075c515
raw
history blame
920 Bytes
# retrievers.py
import pickle
from langchain_community.vectorstores import FAISS
from langchain.retrievers import EnsembleRetriever
from .config import FAISS_DB_INDEX, BM25_INDEX
def load_bm25_retriever():
with open(BM25_INDEX, "rb") as f:
bm25_retriever = pickle.load(f)
return bm25_retriever
def load_faiss_retriever(embeddings):
faiss_db = FAISS.load_local(
FAISS_DB_INDEX, embeddings, allow_dangerous_deserialization=True
)
faiss_retriever = faiss_db.as_retriever(search_type="mmr", search_kwargs={"k": 10})
return faiss_retriever
def load_retrievers(embeddings):
faiss_retriever = load_faiss_retriever(embeddings).with_config(run_name="faiss")
bm25_retriever = load_bm25_retriever().with_config(run_name="bm25")
return EnsembleRetriever(
retrievers=[bm25_retriever, faiss_retriever],
weights=[0.7, 0.3],
search_type="mmr",
)