Implementation:Run llama Llama index RankGPTRerank
| 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:
- A system message establishes the LLM as "RankGPT", an assistant that ranks passages by relevance.
- A user message describes the task with the query and number of passages.
- Each passage is then presented as a separate user message with a numeric identifier (e.g.,
[1],[2]), each acknowledged by an assistant message. - 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
- Environment:Run_llama_Llama_index_Python_LlamaIndex_Core
- Run_llama_Llama_index_BaseNodePostprocessor - Parent abstract base class
- Run_llama_Llama_index_PromptMixin - Prompt management mixin used for prompt get/update