AI Aggregate Function
The AI_AGG function performs AI evaluation at the
aggregation level rather than per row. This function concatenates input text from multiple
rows into a single aggregated string and sends that text to the model once per
group.
Note:
For information about limitations with using AI Functions, see Known Issues with AI Functions.
If no GROUP BY clause is present, all rows are treated as a single group.
If grouping is present, one AI result is produced per group.
Use this function for:
- Group-level summaries
- Trend or theme extraction
- Aggregated sentiment analysis
- Rollup insights
Syntax
AI_AGG(<input>, <prompt>, <model_id>)
Parameters
| Parameter | Description |
|---|---|
input |
String
|
prompt |
String
|
model_id |
String literal
|
Examples
This example summarizes all reviews.
SELECT
AI_AGG(
'Reviews: ' || REVIEW_TEXT,
'Summarize all the reviews in one paragraph. No more than 4 sentences. Return only the summary.',
'model'.'id'
)
FROM REVIEWS;
This example summarizing all reviews per product.
SELECT
PRODUCT_ID, AI_AGG( 'Reviews: ' ||
REVIEW_TEXT, 'Summarize all the reviews per product in one
paragraph. No more than 4 sentences. Return only the summary.', 'model'.'id' ) FROM REVIEWS GROUP BY PRODUCT_ID;
Best Practices
Keep these best practices in mind when you use the
AI_AGG function:
- Avoid vague prompts. Be sure to clearly describe what the model should generate for the group.
- Specify the desired output format explicitly to prevent extra or unexpected text.
- Provide descriptive labels for each input value to help the model understand what each piece of data represents.
- Ensure the prompt references the same labels used in the input.
- Use punctuation and separators to clearly distinguish between multiple input values.
- Cast non-string expressions and columns to string.
- Use consistent and predictable labels across queries to improve reliability.
- Keep the number of rows per group reasonable.
- Keep the number of groups reasonable.