The llm_rerank aggregate function implements progressive reranking using a sliding window strategy, as introduced by Xueguang Ma et al. (2023) in their paper Zero-Shot Listwise Document Reranking with a Large Language Model. This approach addresses the input length limitations of transformer-based models, which can only process a fixed number of tokens at a time (e.g., 16,384 tokens for gpt-4o). When tasked with reranking a list of documents that exceeds the model’s token limit, the function uses a sliding window mechanism to progressively rank subsets of documents.

Modalities

Text

Rerank rows using tabular text columns.

Image

Rerank by visual relevance with type: 'image'.

Voice

Rerank using spoken content via type: 'audio' and transcription_model (OpenAI / Azure). Ranking uses the transcribed text under the hood.

1. Function Overview

llm_rerank is designed to rank a list of documents or rows based on the relevance to a given prompt. When the list length exceeds the model’s input limit, the function uses a sliding window strategy to progressively rerank the documents:

1.1. Window Size

The model ranks a fixed number of documents (e.g., m documents) at a time.

1.2. Sliding Window

After ranking the last m documents, the window shifts towards the list’s beginning by half its size (m/2) and repeats.

1.3. Top-Ranking

This ensures the most relevant documents reach the top quickly, enhancing the relevance of top results. While this approach does not fully reorder the entire list, it is effective in improving the top-ranked results by iteratively ranking smaller subsets of documents.

2. Usage Examples

2.1. Example without GROUP BY

Rerank documents based on their relevance to a given query:
Description: This query will return the documents ordered by relevance based on the provided prompt.

2.2. Example with GROUP BY

Rerank documents for each category based on their relevance:
Description: In this case, the query groups documents by category and reranks them within each category based on relevance.

2.3. Using a Named Prompt with GROUP BY

Use a reusable prompt, such as “document-ranking”, to rank documents based on relevance to a specific query:
Description: This example leverages a named prompt (document-ranking) to rerank documents within each category.

2.4. Advanced Example

Use the llm_rerank function to rerank documents based on their content:
Description: This example will rerank the documents within the defined subset based on their relevance to cutting-edge AI research.

3. Input Parameters

3.1 Model Configuration

  • Parameter: model_name and secret_name

3.1.1 Model Selection

  • Description: Specifies the model used for text generation.
  • Example:

3.1.2 Model Selection with Secret

  • Description: Specifies the model along with the secret name to be used for authentication when accessing the model.
  • Example:

3.2. Prompt Configuration

Two types of prompts can be used:
  1. Inline Prompt
    • Directly provides the prompt in the query.
    • Example:
  2. Named Prompt
    • Refers to a pre-configured prompt by name.
    • Example:
  3. Named Prompt with Version
    • Refers to a specific version of a pre-configured prompt.
    • Example:

3.3. Context Columns Configuration

  • Key: context_columns array.
  • Purpose: Maps table columns to provide input data for the model. Each column can have three properties:
    • data: The SQL column data (required)
    • name: Custom name for the column to be referenced in the prompt (optional)
    • type: Data type — "tabular" (default), "image", or "audio" (audio requires transcription_model)
  • Example:

4. Output

  • Type: JSON object.
  • Behavior: Returns the input rows reordered by relevance to the prompt. The JSON mirrors the input column values. You can still pass provider model_parameters (including JSON schemas) to constrain how the ranking model responds — see Model parameters.
Example (with schema-constrained ranking model):
Typical output is an ordered JSON array of the input documents, for example: