How to use retriever in LlamaIndex
Quick answer
Use the
Retriever interface in LlamaIndex to query your indexed documents efficiently. Initialize a retriever from your index (e.g., VectorStoreIndex) and call retriever.retrieve(query) to get relevant documents for your query.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install llama-index openai
Setup
Install llama-index and set your OpenAI API key as an environment variable.
- Run
pip install llama-index openai - Set environment variable
OPENAI_API_KEYwith your OpenAI API key
pip install llama-index openai Step by step
This example shows how to create a VectorStoreIndex from documents, get its retriever, and query it for relevant documents.
import os
from llama_index import VectorStoreIndex, SimpleDirectoryReader
# Load documents from a directory
documents = SimpleDirectoryReader('data').load_data()
# Create a vector index from documents
index = VectorStoreIndex.from_documents(documents)
# Get the retriever from the index
retriever = index.as_retriever()
# Query string
query = "What is the main topic of the documents?"
# Retrieve relevant documents
results = retriever.retrieve(query)
# Print retrieved documents content
for i, doc in enumerate(results):
print(f"Document {i+1}:\n{doc.get_text()}\n") output
Document 1: The main topic of the documents is about AI and machine learning. Document 2: These documents discuss the applications of LlamaIndex in data retrieval.
Common variations
You can customize the retriever behavior by passing parameters like search_kwargs to control the number of results or similarity thresholds. Also, you can use other index types like GPTVectorStoreIndex or async retrieval if supported.
retriever = index.as_retriever(search_kwargs={'k': 3}) # Retrieve top 3 documents
results = retriever.retrieve(query)
# Async example (if supported by your environment)
# import asyncio
# async def async_retrieve():
# results = await retriever.aretrieve(query)
# for doc in results:
# print(doc.get_text())
# asyncio.run(async_retrieve()) Troubleshooting
- If retrieval returns empty results, ensure your documents are properly loaded and indexed.
- Check your OpenAI API key environment variable is set correctly.
- For slow retrieval, consider reducing the number of documents or tuning
search_kwargs.
Key Takeaways
- Use
index.as_retriever()to get a retriever for querying documents efficiently. - Customize retrieval with
search_kwargsto control result count and similarity. - Ensure documents are loaded and indexed before retrieval to avoid empty results.