Usage Patternο
Get Startedο
Get a retriever from index:
retriever = index.as_retriever()
Retrieve relevant context for a question:
nodes = retriever.retrieve('Who is Paul Graham?')
Note: To learn how to build an index, see Index
High-Level APIο
Selecting a Retrieverο
You can select the index-specific retriever class via retriever_mode
.
For example, with a SummaryIndex
:
retriever = summary_index.as_retriever(
retriever_mode='llm',
)
This creates a SummaryIndexLLMRetriever on top of the summary index.
See Retriever Modes for a full list of (index-specific) retriever modes and the retriever classes they map to.
Configuring a Retrieverο
In the same way, you can pass kwargs to configure the selected retriever.
Note: take a look at the API reference for the selected retriever classβ constructor parameters for a list of valid kwargs.
For example, if we selected the βllmβ retriever mode, we might do the following:
retriever = summary_index.as_retriever(
retriever_mode='llm',
choice_batch_size=5,
)
Low-Level Composition APIο
You can use the low-level composition API if you need more granular control.
To achieve the same outcome as above, you can directly import and construct the desired retriever class:
from llama_index.indices.list import SummaryIndexLLMRetriever
retriever = SummaryIndexLLMRetriever(
index=summary_index,
choice_batch_size=5,
)