How to use RetrieverQueryEngine in LlamaIndex
Quick answer
Use
RetrieverQueryEngine in llamaindex to perform efficient retrieval-augmented querying by combining a retriever with a query engine. Initialize a retriever from your index, then create a RetrieverQueryEngine with it to run queries that fetch relevant documents before answering.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install llamaindex openai
Setup
Install llamaindex and set your OpenAI API key as an environment variable.
pip install llamaindex openai Step by step
This example shows how to create a RetrieverQueryEngine from a GPTVectorStoreIndex and query it.
import os
from llamaindex import GPTVectorStoreIndex, SimpleDirectoryReader, RetrieverQueryEngine
# Load documents from a local directory
documents = SimpleDirectoryReader('data').load_data()
# Create an index from documents
index = GPTVectorStoreIndex.from_documents(documents)
# Create a retriever from the index
retriever = index.as_retriever()
# Initialize RetrieverQueryEngine with the retriever
query_engine = RetrieverQueryEngine(retriever=retriever)
# Query the engine
response = query_engine.query('What is the main topic of the documents?')
print(response.response) output
The main topic of the documents is ...
Common variations
- Use different index types like
GPTListIndexorGPTTreeIndexwith retrievers. - Customize retriever parameters such as
search_kwargsfor fine-tuning retrieval. - Use async querying by calling
await query_engine.aquery(...)in async contexts. - Switch to other LLMs by passing a custom
llmparameter to the index or query engine.
Troubleshooting
- If queries return irrelevant results, increase the retriever's
kparameter to fetch more documents. - Ensure your OpenAI API key is set correctly in
os.environ['OPENAI_API_KEY']. - Check that your documents are loaded properly and the index is built without errors.
- For slow queries, consider caching the index or using a smaller model.
Key Takeaways
- Use RetrieverQueryEngine to combine retrieval with LLM querying for efficient document QA.
- Create a retriever from your index and pass it to RetrieverQueryEngine for best results.
- Adjust retriever parameters like number of documents retrieved to improve answer relevance.