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 TransformRetriever

From Leeroopedia

Overview

The TransformRetriever is a lightweight retriever decorator that applies a query transformation before delegating to an underlying retriever. This enables preprocessing of queries -- such as rephrasing, decomposition, or augmentation -- as a composable retrieval pipeline step.

Source File: llama-index-core/llama_index/core/retrievers/transform_retriever.py (44 lines)

Module: llama_index.core.retrievers.transform_retriever

Class Definition

class TransformRetriever(BaseRetriever):
    """
    Transform Retriever.

    Takes in an existing retriever and a query transform and runs the query transform
    before running the retriever.
    """

Dependencies

Module Import
llama_index.core.base.base_retriever BaseRetriever
llama_index.core.callbacks.base CallbackManager
llama_index.core.indices.query.query_transform.base BaseQueryTransform
llama_index.core.prompts.mixin PromptMixinType
llama_index.core.schema NodeWithScore, QueryBundle

Constructor

def __init__(
    self,
    retriever: BaseRetriever,
    query_transform: BaseQueryTransform,
    transform_metadata: Optional[dict] = None,
    callback_manager: Optional[CallbackManager] = None,
    object_map: Optional[dict] = None,
    verbose: bool = False,
) -> None
Parameter Type Default Description
retriever BaseRetriever required The underlying retriever to delegate to after transformation
query_transform BaseQueryTransform required The query transformation to apply before retrieval
transform_metadata Optional[dict] None Optional metadata dictionary passed to the query transform
callback_manager Optional[CallbackManager] None Optional callback manager
object_map Optional[dict] None Optional object map
verbose bool False Whether to enable verbose output

Core Methods

_retrieve

def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]

The retrieval method performs two steps:

  1. Transform: Calls self._query_transform.run(query_bundle, metadata=self._transform_metadata) to produce a transformed QueryBundle.
  2. Retrieve: Passes the transformed query to self._retriever.retrieve(query_bundle) and returns the results directly.

_get_prompt_modules

def _get_prompt_modules(self) -> PromptMixinType

Returns the query transform as a prompt sub-module under the key "query_transform", enabling prompt inspection and modification through the PromptMixin interface.

Pipeline Pattern

Original QueryBundle
  -> query_transform.run(query_bundle, metadata)
     -> Transformed QueryBundle
        -> retriever.retrieve(transformed_query)
           -> List[NodeWithScore]

Design Notes

  • This class follows the decorator pattern -- it wraps an existing retriever without modifying it, adding transformation behavior transparently.
  • The transform_metadata parameter allows passing context-specific information to the transform (e.g., conversation history, user preferences).
  • At only 44 lines, this is the most minimal retriever implementation, demonstrating the composability of the retriever abstraction.
  • The BaseQueryTransform can implement any transformation strategy: query rephrasing, HyDE (Hypothetical Document Embeddings), step-back prompting, etc.

See Also

Page Connections

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