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 EmbeddingSingleSelector: Difference between revisions

From Leeroopedia
Auto-imported from implementations/Run_llama_Llama_index_EmbeddingSingleSelector.md
 
Sync from local file
 
Line 117: Line 117:
== See Also ==
== See Also ==


* [[Run_llama_Llama_index_LLM_Selectors|LLM Selectors]] -- LLM-based single and multi selectors
* [[Implementation:Run_llama_Llama_index_LLM_Selectors|LLM Selectors]] -- LLM-based single and multi selectors
* [[Run_llama_Llama_index_Pydantic_Selectors|Pydantic Selectors]] -- Structured output selectors using pydantic programs
* [[Implementation:Run_llama_Llama_index_Pydantic_Selectors|Pydantic Selectors]] -- Structured output selectors using pydantic programs
* [[Run_llama_Llama_index_RouterRetriever|RouterRetriever]] -- The primary consumer of selectors for retriever routing
* [[Implementation:Run_llama_Llama_index_RouterRetriever|RouterRetriever]] -- The primary consumer of selectors for retriever routing


[[Category:Implementations]]
[[Category:Implementations]]

Latest revision as of 10:51, 27 September 2026

Overview

The EmbeddingSingleSelector is a selector that chooses the best single option from a set of candidates by computing embedding similarity between the query and each candidate's description. Unlike LLM-based selectors that require a full language model call, this selector uses vector embeddings for fast, deterministic selection.

Source File: llama-index-core/llama_index/core/selectors/embedding_selectors.py (93 lines)

Module: llama_index.core.selectors.embedding_selectors

Class Definition

class EmbeddingSingleSelector(BaseSelector):
    """
    Embedding selector.

    Embedding selector that chooses one out of many options.
    """

Dependencies

Module Import
llama_index.core.base.base_selector BaseSelector, SelectorResult, SingleSelection
llama_index.core.base.embeddings.base BaseEmbedding
llama_index.core.indices.query.embedding_utils get_top_k_embeddings
llama_index.core.prompts.mixin PromptDictType
llama_index.core.schema QueryBundle
llama_index.core.settings Settings
llama_index.core.tools.types ToolMetadata

Constructor

def __init__(
    self,
    embed_model: BaseEmbedding,
) -> None
Parameter Type Default Description
embed_model BaseEmbedding required The embedding model used to embed both queries and choice descriptions

Factory Method

from_defaults

@classmethod
def from_defaults(
    cls,
    embed_model: Optional[BaseEmbedding] = None,
) -> "EmbeddingSingleSelector"

Creates an instance with an optional embedding model. If not provided, falls back to Settings.embed_model.

Core Methods

_select (synchronous)

def _select(
    self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult

Selection logic:

  1. Embed query: Calls self._embed_model.get_query_embedding(query.query_str) to produce the query embedding vector.
  2. Embed choices: Calls self._embed_model.get_text_embedding(choice.description) for each choice to produce text embedding vectors.
  3. Compute similarity: Uses get_top_k_embeddings() with similarity_top_k=1 to find the most similar choice.
  4. Build result: Returns a SelectorResult containing a single SingleSelection with the index of the best match and a reason string showing the similarity score and choice name.

_aselect (asynchronous)

async def _aselect(
    self, choices: Sequence[ToolMetadata], query: QueryBundle
) -> SelectorResult

Identical logic to _select but uses the async embedding methods aget_query_embedding() and aget_text_embedding().

_get_prompts / _update_prompts

These methods return empty dictionaries and perform no operations, since this selector does not use any prompts.

Selection Result Format

The reason string follows this format:

Top similarity match: {score:.2f}, {choice_name}

For example: "Top similarity match: 0.87, financial_retriever"

Design Notes

  • This selector always returns exactly one selection (it is a single-selector, not a multi-selector). The similarity_top_k=1 parameter is hardcoded.
  • The embedding IDs passed to get_top_k_embeddings are simply list(range(len(choices))), mapping directly to choice indices.
  • This selector is useful when LLM calls are too expensive or slow for routing decisions, or when the choice descriptions are sufficiently distinct in embedding space.
  • Unlike LLM selectors, this approach does not require prompt engineering and is deterministic for a given embedding model.

See Also