The llm_first function is used to extract the first matching result that satisfies a condition defined by a model’s prompt and column data. It operates across rows, typically combined with a GROUP BY clause, to return the first relevant row for each group.

Modalities

Text

Select the most relevant row using tabular text columns.

Image

Select by visual criteria with type: 'image'.

Voice

Select using spoken content via type: 'audio' and transcription_model (OpenAI / Azure).

1. Usage Examples

1.1. Example without GROUP BY

Retrieve the first relevant product feature across all rows:
Description: This query returns the first relevant feature from all product descriptions and product names, based on the provided prompt.

1.2. Example with GROUP BY

Retrieve the first relevant product feature for each product category:
Description: The query groups the products by category and returns the first relevant feature for each group.

1.3. Using a Named Prompt with GROUP BY

Use a reusable prompt, such as “first-relevant-detail”, to extract the first relevant feature for each product category:
Description: This example leverages a named prompt (first-relevant-detail) to extract the first relevant feature for each product category. The query groups the results by category.

1.4. Advanced Example with Multiple Columns and GROUP BY

Retrieve the first relevant feature for products grouped by category, using both the product name and description:
Description: This query extracts the first relevant feature from both the product_name and product_description columns, grouped by product category (in this case, electronics).

2. Input Parameters

2.1 Model Configuration

  • Parameter: model_name and secret_name

2.1.1 Model Selection

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

2.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:

2.2. Prompt Configuration

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

2.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:

3. Output

  • Type: JSON object.
  • Behavior: Returns the selected row’s input columns as JSON (the most relevant row for the prompt). Optionally pass a JSON schema in model_parameters to constrain the ranking model (OpenAI response_format, Ollama format, Anthropic output_format / tool-use); the SQL return value is still the chosen row.
Example return value for the selected row: