How to use reranker in LlamaIndex
Quick answer
Use the
Reranker class in LlamaIndex to improve search result relevance by reordering retrieved documents based on a reranking model. Initialize a reranker with a supported model, then pass it to your Retriever or QueryEngine to apply reranking on search results.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install llama-index openai>=1.0
Setup
Install llama-index and openai packages, and set your OpenAI API key as an environment variable.
pip install llama-index openai>=1.0 Step by step
This example shows how to create a Reranker using an OpenAI model and integrate it with a Retriever to rerank search results in LlamaIndex.
import os
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, ServiceContext, Reranker
from llama_index.retrievers import VectorIndexRetriever
from openai import OpenAI
# Set up OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Load documents from a directory
documents = SimpleDirectoryReader("./data").load_data()
# Create an index
index = GPTVectorStoreIndex.from_documents(documents)
# Initialize a reranker with an OpenAI model
reranker = Reranker.from_openai(
client=client,
model="gpt-4o",
max_rerank=5 # number of top docs to rerank
)
# Create a retriever with reranker
retriever = VectorIndexRetriever(
index=index,
reranker=reranker
)
# Query with reranking
query = "Explain the benefits of renewable energy."
results = retriever.retrieve(query)
# Print reranked results
for i, doc in enumerate(results):
print(f"Result {i+1}: {doc.get_text()[:200]}...\n") output
Result 1: Renewable energy offers sustainable power sources that reduce greenhouse gas emissions and dependence on fossil fuels... Result 2: Using renewable energy can lower energy costs over time and create jobs in new industries... Result 3: It improves air quality and public health by reducing pollution from traditional energy sources... Result 4: Renewable energy sources are abundant and can help stabilize energy prices... Result 5: Adoption of renewable energy supports energy independence and national security...
Common variations
- Use different models for reranking, such as
gpt-4o-minior other supported OpenAI models. - Apply reranking in an async context by adapting the retriever calls accordingly.
- Combine reranking with other retriever types like
BM25Retrieveror custom retrievers.
Troubleshooting
- If reranking results do not improve relevance, verify your reranker model and
max_rerankparameter. - Ensure your OpenAI API key is correctly set in
os.environ["OPENAI_API_KEY"]. - Check for network issues or API rate limits if requests fail.
Key Takeaways
- Use
Rerankerwith a supported model to reorder search results for better relevance. - Integrate the reranker into your
Retrieverto apply reranking seamlessly. - Adjust
max_rerankto control how many top documents get reranked for efficiency. - Always set your API key securely via environment variables to avoid credential leaks.
- Test different models and retriever combinations to optimize search quality.