LlamaIndex Querying and Retrieval
Control what is retrieved, how much is retrieved, whether results are reranked and how evidence reaches the answer.
#Query engine
pythonquery_engine = index.as_query_engine() response = query_engine.query("Summarize this document.") print(response.response) print(response.source_nodes)
The response text and its source nodes should be inspected together.
#Retrieval without answer generation
pythonretriever = index.as_retriever(similarity_top_k=3) nodes = retriever.retrieve("What is a vector database?")
#Additional patterns
A chat engine can retain conversational context:
pythonchat_engine = index.as_chat_engine() chat_engine.chat("My name is Alex.") response = chat_engine.chat("What name did I give you?")
A router can select among indexes. A sub-question workflow can divide a comparison into smaller retrieval tasks and combine the results. Neither is necessary for every query.
#Retrieval improvements
Metadata filters restrict the relevant dates/categories. Reranking can narrow a larger initial set—for example twenty candidates to three reviewed passages. Hybrid search combines semantic and keyword signals. Measure basic recall before adding each stage.
#Structured responses
pythonfrom pydantic import BaseModel class Info(BaseModel): name: str amount: float query_engine = index.as_query_engine(output_cls=Info)
This illustrates a typed output target; validate the selected integration's support and the actual returned values.
If answers are only partly reliable, inspect top_k, metadata scope and candidate ordering before changing the generation prompt or model.