Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index LLM Selectors

From Leeroopedia
Revision as of 11:48, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Run_llama_Llama_index_LLM_Selectors.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 llm to Settings.llm
  • Defaults prompt_template_str to DEFAULT_SINGLE_SELECT_PROMPT_TMPL
  • Defaults output_parser to SelectionOutputParser()
  • Constructs a SingleSelectPrompt with PromptType.SINGLE_SELECT

_select / _aselect

Both methods follow the same pattern:

  1. Build choices text using _build_choices_text()
  2. Call self._llm.predict() (or apredict()) with the prompt, passing num_choices, context_list, and query_str
  3. Parse the prediction through the prompt's output parser
  4. Convert to SelectorResult via _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 SelectionOutputParser parses the LLM's free-text response into structured Answer objects containing choice indices and reasons.
  • Prompts are exposed via the PromptMixin interface, enabling runtime prompt customization.

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment