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,
)