Implementation:Run llama Llama index LLM Selectors
Overview
This module provides two LLM-based selector classes -- LLMSingleSelector and LLMMultiSelector -- that use a language model to choose one or more options from a set of candidates. The selectors format candidate descriptions into a numbered list, prompt the LLM to make a selection, and parse the structured output back into selector results. Both classes support synchronous and asynchronous execution.
Source File: llama-index-core/llama_index/core/selectors/llm_selectors.py (234 lines)
Module: llama_index.core.selectors.llm_selectors
Dependencies
| Module | Import |
|---|---|
llama_index.core.base.base_selector |
BaseSelector, SelectorResult, SingleSelection
|
llama_index.core.llms |
LLM
|
llama_index.core.output_parsers.base |
StructuredOutput
|
llama_index.core.output_parsers.selection |
Answer, SelectionOutputParser
|
llama_index.core.prompts.base |
BasePromptTemplate
|
llama_index.core.prompts.mixin |
PromptDictType
|
llama_index.core.prompts.prompt_type |
PromptType
|
llama_index.core.selectors.prompts |
DEFAULT_SINGLE_SELECT_PROMPT_TMPL, DEFAULT_MULTI_SELECT_PROMPT_TMPL, SingleSelectPrompt, MultiSelectPrompt
|
llama_index.core.settings |
Settings
|
llama_index.core.tools.types |
ToolMetadata
|
llama_index.core.types |
BaseOutputParser
|
Helper Functions
_build_choices_text
def _build_choices_text(choices: Sequence[ToolMetadata]) -> str
Converts a sequence of ToolMetadata objects into a numbered text list. Each choice description has its newlines collapsed into spaces and is prefixed with a 1-based index:
(1) Description of first choice (2) Description of second choice
This function is also imported and reused by the pydantic_selectors module.
_structured_output_to_selector_result
def _structured_output_to_selector_result(output: Any) -> SelectorResult
Converts a StructuredOutput containing a list of Answer objects into a SelectorResult. Each answer's choice field is adjusted from 1-based to 0-based indexing by subtracting 1.
LLMSingleSelector
Class Definition
class LLMSingleSelector(BaseSelector):
"""
LLM single selector.
LLM-based selector that chooses one out of many options.
"""
Constructor
def __init__(
self,
llm: LLM,
prompt: SingleSelectPrompt,
) -> None
| Parameter | Type | Description |
|---|---|---|
llm |
LLM |
The language model instance |
prompt |
SingleSelectPrompt |
The prompt template for single selection (must have an output parser) |
Raises ValueError if the prompt does not have an output parser attached.
from_defaults
@classmethod
def from_defaults(
cls,
llm: Optional[LLM] = None,
prompt_template_str: Optional[str] = None,
output_parser: Optional[BaseOutputParser] = None,
) -> "LLMSingleSelector"
Factory method that:
- Defaults
llmtoSettings.llm - Defaults
prompt_template_strtoDEFAULT_SINGLE_SELECT_PROMPT_TMPL - Defaults
output_parsertoSelectionOutputParser() - Constructs a
SingleSelectPromptwithPromptType.SINGLE_SELECT
_select / _aselect
Both methods follow the same pattern:
- Build choices text using
_build_choices_text() - Call
self._llm.predict()(orapredict()) with the prompt, passingnum_choices,context_list, andquery_str - Parse the prediction through the prompt's output parser
- Convert to
SelectorResultvia_structured_output_to_selector_result()
Prompt Management
_get_prompts()returns{"prompt": self._prompt}_update_prompts()allows replacing the prompt via the"prompt"key
LLMMultiSelector
Class Definition
class LLMMultiSelector(BaseSelector):
"""
LLM multi selector.
LLM-based selector that chooses multiple out of many options.
"""
Constructor
def __init__(
self,
llm: LLM,
prompt: MultiSelectPrompt,
max_outputs: Optional[int] = None,
) -> None
| Parameter | Type | Default | Description |
|---|---|---|---|
llm |
LLM |
required | The language model instance |
prompt |
MultiSelectPrompt |
required | The prompt template for multi selection (must have an output parser) |
max_outputs |
Optional[int] |
None |
Maximum number of selections; defaults to the total number of choices at runtime |
from_defaults
@classmethod
def from_defaults(
cls,
llm: Optional[LLM] = None,
prompt_template_str: Optional[str] = None,
output_parser: Optional[BaseOutputParser] = None,
max_outputs: Optional[int] = None,
) -> "LLMMultiSelector"
Key difference from LLMSingleSelector: calls output_parser.format(prompt_template_str) to inject output formatting instructions into the prompt template before constructing the MultiSelectPrompt.
_select / _aselect
Same pattern as LLMSingleSelector, with the addition of max_outputs passed to the LLM predict call. If max_outputs is None, it defaults to len(choices).
Index Conversion
Both selectors use 1-based indexing in the LLM prompt (choices are numbered starting from 1) and convert back to 0-based indexing in _structured_output_to_selector_result() by subtracting 1 from each answer's choice field.
Design Notes
- The output parser is mandatory -- both constructors validate that
self._prompt.output_parser is not None. - The
_build_choices_text()helper is deliberately extracted as a module-level function so it can be reused by the pydantic selector module. - The
SelectionOutputParserparses the LLM's free-text response into structuredAnswerobjects containing choice indices and reasons. - Prompts are exposed via the
PromptMixininterface, enabling runtime prompt customization.
See Also
- Pydantic Selectors -- Structured output selectors using function calling
- EmbeddingSingleSelector -- Embedding-based selector alternative
- RouterRetriever -- Primary consumer of selectors
- Core Types --
BaseOutputParserused by the selectors