File size: 920 Bytes
de850e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
075c515
de850e8
075c515
de850e8
 
 
 
 
 
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
# 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",
    )