How to use query engine in LlamaIndex
Quick answer
Use the
llama_index library to build an index from your documents and then create a QueryEngine to run natural language queries against it. The query engine processes your input and returns relevant document snippets or answers.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install llama-index openai
Setup
Install the llama-index package and set your OpenAI API key as an environment variable.
- Install with
pip install llama-index openai - Set environment variable in your shell:
export OPENAI_API_KEY='your_api_key'
pip install llama-index openai Step by step
This example shows how to create a simple document index and query it using the QueryEngine in LlamaIndex.
import os
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, QueryEngine
# Load documents from a directory
documents = SimpleDirectoryReader('data').load_data()
# Build an index from documents
index = GPTVectorStoreIndex.from_documents(documents)
# Create a query engine from the index
query_engine = QueryEngine(index=index)
# Run a query
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
You can customize the query engine by using different index types like GPTListIndex or GPTTreeIndex, or by enabling streaming responses. Async usage is also supported.
from llama_index import GPTListIndex
# Build a list index instead
list_index = GPTListIndex.from_documents(documents)
query_engine = QueryEngine(index=list_index)
response = query_engine.query('Summarize the documents.')
print(response.response) output
Summary of the documents: ...
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If queries return empty or irrelevant results, ensure your documents are loaded properly and the index is built without errors.
- Check your network connection if API calls fail.
Key Takeaways
- Use
GPTVectorStoreIndexor other index types to build your document index. - Create a
QueryEnginefrom the index to run natural language queries. - Set your OpenAI API key in
os.environbefore running queries. - Customize the query engine with different index types or async calls for flexibility.
- Troubleshoot by verifying API keys, document loading, and network connectivity.