Implementation:Run llama Llama index BaseSelector
| Knowledge Sources | |
|---|---|
| Domains | LLM Framework, Selection, Routing |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
BaseSelector is the abstract base class for LLM-powered selector components that choose one or more options from a list of choices based on a query, used primarily in router query engines and retriever routing.
Description
This module defines the selection framework used for routing queries to appropriate sub-components. It contains several key classes and utilities:
Data models:
- SingleSelection -- A Pydantic model representing a single choice, containing an index (int) and a reason (str) explaining why the choice was selected.
- MultiSelection -- A Pydantic model containing a list of SingleSelection objects. Provides convenience properties:
- ind -- Returns the single index (raises ValueError if more than one selection exists).
- reason -- Returns the single reason (raises ValueError if more than one).
- inds -- Returns a list of all selected indices.
- reasons -- Returns a list of all selection reasons.
- SelectorResult -- An alias for MultiSelection, used for clarity in function signatures.
Helper functions:
- _wrap_choice(choice) -- Converts a MetadataType (either a string or ToolMetadata) into a ToolMetadata object.
- _wrap_query(query) -- Converts a QueryType (either a string or QueryBundle) into a QueryBundle object.
BaseSelector class:
- Inherits from PromptMixin and DispatcherSpanMixin.
- select(choices, query) -- Synchronous public method. Wraps choices and query, then delegates to abstract _select.
- aselect(choices, query) -- Async public method. Wraps choices and query, then delegates to abstract _aselect.
- _select and _aselect -- Abstract methods that subclasses must implement.
Usage
Subclass BaseSelector to create LLM-based selectors that route queries. This is used in RouterQueryEngine and RouterRetriever where the selector determines which sub-engine or sub-retriever should handle a given query based on descriptions of available choices.
Code Reference
Source Location
- Repository: Run_llama_Llama_index
- File: llama-index-core/llama_index/core/base/base_selector.py
- Lines: 1-103
Signature
class SingleSelection(BaseModel):
index: int
reason: str
class MultiSelection(BaseModel):
selections: List[SingleSelection]
@property
def ind(self) -> int: ...
@property
def reason(self) -> str: ...
@property
def inds(self) -> List[int]: ...
@property
def reasons(self) -> List[str]: ...
SelectorResult = MultiSelection
class BaseSelector(PromptMixin, DispatcherSpanMixin):
def select(
self, choices: Sequence[MetadataType], query: QueryType
) -> SelectorResult: ...
async def aselect(
self, choices: Sequence[MetadataType], query: QueryType
) -> SelectorResult: ...
@abstractmethod
def _select(
self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult: ...
@abstractmethod
async def _aselect(
self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult: ...
Import
from llama_index.core.base.base_selector import (
BaseSelector,
SingleSelection,
MultiSelection,
SelectorResult,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| choices | Sequence[MetadataType] | Yes | A sequence of choices, each being either a string description or a ToolMetadata object. |
| query | QueryType (str or QueryBundle) | Yes | The query used to determine which choice(s) to select. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | SelectorResult (MultiSelection) | Contains a list of SingleSelection objects, each with an index and a reason string. |
Usage Examples
Basic Usage
from llama_index.core.base.base_selector import BaseSelector, SelectorResult, SingleSelection
from llama_index.core.tools.types import ToolMetadata
from llama_index.core.schema import QueryBundle
from typing import Sequence
class MySelector(BaseSelector):
def _get_prompts(self):
return {}
def _update_prompts(self, prompts):
pass
def _select(
self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult:
# Simple keyword-based selection
for i, choice in enumerate(choices):
if query.query_str.lower() in choice.description.lower():
return SelectorResult(
selections=[SingleSelection(index=i, reason="Keyword match")]
)
return SelectorResult(
selections=[SingleSelection(index=0, reason="Default selection")]
)
async def _aselect(
self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult:
return self._select(choices, query)
# Usage
selector = MySelector()
result = selector.select(
choices=["Financial data analysis", "Scientific paper search"],
query="What were Q3 earnings?"
)
print(result.ind, result.reason)