----------------------
To include a reranker in your retriever, you need to set up a query engine with a reranker. Here are some examples of how to do this using different rerankers:
- LLMRerank: This reranker uses a Language Model (LLM) to rerank the retrieved nodes. Here's an example of how to set it up:
from llama_index import ServiceContext, VectorStoreIndex
from llama_index.postprocessor import LLMRerank
from llama_index.query_engine import RetrieverQueryEngine
from llama_index.retrievers import VectorIndexRetriever
from llama_index.vector_stores.google.generativeai import GoogleVectorStore, google_service_context
from llama_index.response_synthesizers.google.generativeai import GoogleTextSynthesizer
store = GoogleVectorStore.from_corpus(corpus_id=SESSION_CORPUS_ID)
index = VectorStoreIndex.from_vector_store(vector_store=store, service_context=google_service_context)
response_synthesizer = GoogleTextSynthesizer.from_defaults(temperature=0.2, answer_style=GenerateAnswerRequest.AnswerStyle.ABSTRACTIVE)
reranker = LLMRerank(
top_n=10,
service_context=ServiceContext.from_defaults(llm=gemini, embed_model=None),
)
query_engine = RetrieverQueryEngine.from_args(
retriever=VectorIndexRetriever(index=index, similarity_top_k=20),
node_postprocessors=[reranker],
response_synthesizer=response_synthesizer,
)
(Source:
GitHub ())
- SentenceTransformerRerank: This reranker uses a Sentence Transformer model for reranking. Here's an example of how to set it up:
from llama_index.postprocessor import SentenceTransformerRerank
reranker = SentenceTransformerRerank(top_n=4, model="BAAI/bge-reranker-base")
(Source:
GitHub ())