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 RankGPTRerank

From Leeroopedia
Revision as of 11:48, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Run_llama_Llama_index_RankGPTRerank.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Postprocessing, Reranking, LLM
Last Updated 2026-02-11 19:00 GMT

Overview

RankGPTRerank is a node postprocessor that uses an LLM in the RankGPT paradigm to rerank retrieved nodes by constructing a multi-turn chat conversation and asking the model to produce a permutation ranking of passages by relevance.

Description

RankGPTRerank extends BaseNodePostprocessor and implements the RankGPT reranking approach. It works by constructing a structured multi-turn chat conversation:

  1. A system message establishes the LLM as "RankGPT", an assistant that ranks passages by relevance.
  2. A user message describes the task with the query and number of passages.
  3. Each passage is then presented as a separate user message with a numeric identifier (e.g., [1], [2]), each acknowledged by an assistant message.
  4. A final post prompt (configurable via rankgpt_rerank_prompt) asks the model to output the ranked permutation.

The model's response is parsed to extract a permutation of passage indices. The implementation handles duplicate removal and ensures all passages are accounted for (unranked passages are appended at the end). The top top_n nodes are returned based on the reranked order. Both synchronous (_postprocess_nodes) and asynchronous (_apostprocess_nodes) execution paths are supported.

Passage content is truncated to 300 whitespace-delimited tokens before being sent to the LLM to manage context window constraints.

Usage

Use RankGPTRerank when you want to leverage an LLM's language understanding to rerank retrieved passages beyond what embedding similarity alone can achieve. This is effective for complex queries where semantic nuance matters. It defaults to using gpt-3.5-turbo-16k but any LLM can be substituted.

Code Reference

Source Location

  • Repository: Run_llama_Llama_index
  • File: llama-index-core/llama_index/core/postprocessor/rankGPT_rerank.py

Signature

class RankGPTRerank(BaseNodePostprocessor):
    def __init__(
        self,
        top_n: int = 5,
        llm: Optional[LLM] = None,
        verbose: bool = False,
        rankgpt_rerank_prompt: Optional[BasePromptTemplate] = None,
    ):

Import

from llama_index.core.postprocessor.rankGPT_rerank import RankGPTRerank

I/O Contract

Inputs

Name Type Required Description
top_n int No Number of top-ranked nodes to return. Defaults to 5.
llm LLM No LLM to use for reranking. Defaults to OpenAI gpt-3.5-turbo-16k.
verbose bool No Whether to print intermediate reranking steps. Defaults to False.
rankgpt_rerank_prompt BasePromptTemplate No Custom prompt template for the final reranking instruction. Defaults to RANKGPT_RERANK_PROMPT.

Outputs

Name Type Description
nodes List[NodeWithScore] The top_n nodes reordered by the LLM's relevance ranking, retaining original scores.

Usage Examples

from llama_index.core.postprocessor.rankGPT_rerank import RankGPTRerank

# Basic usage with defaults
reranker = RankGPTRerank(top_n=3)

query_engine = index.as_query_engine(
    node_postprocessors=[reranker]
)
response = query_engine.query("What are the benefits of exercise?")

# Custom LLM and verbose output
from llama_index.llms.openai import OpenAI

reranker = RankGPTRerank(
    top_n=5,
    llm=OpenAI(model="gpt-4"),
    verbose=True,
)

Related Pages

Page Connections

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