- カテゴリ:
/sql-reference/functions-aggregation`(一般):doc:/sql-reference/functions-string`(AI 関数)
AI_AGG¶
自然言語の指示を使用して、テキストデータの列を削除します。
たとえば、AI_AGG(reviews, 'Describe the most common complaints mentioned in the book reviews')
はユーザーフィードバックの概要を返します。
COMPLETE (SNOWFLAKE.CORTEX) や SUMMARIZE (SNOWFLAKE.CORTEX) とは異なり、この関数は言語モデルのコンテキストウィンドウの最大値よりも大きなデータセットをサポートします。
- こちらもご覧ください:
構文¶
AI_AGG( <expr>, <instruction> )
引数¶
必須:
expr
これは、レストランのレビューや電話の記録など、集計処理が実行されるテキストを含んでいる式です。
instruction
実行する集約の自然言語仕様を含む文字列。たとえば、「レビューを要約する」または「言及されているすべての人物を識別し、それぞれの簡単な履歴を書く」が該当します。
戻り値¶
集計の結果を含んでいる文字列を返します。
次の場合、関数は、提供されたデータに回答が含まれていないことを示す可能性があります。
データの集計方法を指定する明確な指示を提示していない
データに指示を完了するのに必要な情報が存在しない
使用上の注意¶
最適なパフォーマンスを得るためには、以下のガイドラインに従ってください。
指示には平易な英語のテキストを使用します。
質問するのではなく、宣言的な指示を提示します。例えば、「これを要約できますか?」のような質問の代わりに、「通話記録を要約してください」を使用します。
指示にあるテキストについて説明します。例えば、「要約」のような指示の代わりに、「通話記録を要約してください」を使用します。
意図されたユースケースについて説明してください。例えば、「最高のレビューを見つける」ではなく、「レストランのウェブサイトでハイライトするために、最も肯定的でよく書かれたレストランのレビューを見つける」を使用します。
指示を複数のステップに分割することを検討してください。たとえば、「新しい記事を要約してください」の代わりに、「視点が異なるイベントを提示するさまざまなパブリッシャーの新しい記事が提示されます。重要な情報が失われないようにしながら、ソーステキストの簡潔かつ精密な要約を作成してください」を使用します。
例¶
AI_AGG は、文字列定数の単純なスカラー関数として使用できます。次の例で AI_AGG は、単一の文字列として提示されている製品評価を要約するために使用されます。
SELECT AI_AGG('[Excellent, Excellent, Great, Mediocre]',
'Summarize the product ratings for a blog post targeting consumers');
Overall, the product has received overwhelmingly positive reviews, with the majority of users rating it as 'Excellent' or 'Great'. Only a small percentage of users had a mediocre experience with the product. This suggests that the product is well-liked by most consumers and is a great option for those looking for a reliable choice.
AI_AGG はデータ列でも使用できます。次の例では、上記の例の製品評価は、共通テーブル式 を使用してテーブルの列として提示されています。
WITH reviews AS (
SELECT 'The restaurant was excellent.' AS review
UNION ALL SELECT 'Excellent! I loved the pizza!'
UNION ALL SELECT 'It was great, but the service was meh.'
UNION ALL SELECT 'Mediocre food and mediocre service'
)
SELECT AI_AGG(review, 'Summarize the restaurant reviews for potential consumers')
FROM reviews;
Reviews for this restaurant are mixed. Some customers had a very positive experience, describing the restaurant as "excellent" and loving the pizza. However, others had a more neutral or negative experience, citing mediocre food and service.
AI_AGG は GROUPBY と組み合わせて使用することもできます。次の例では、レビューに関するテーブルに記載されている2つの製品(product_id
列で識別)の製品評価を要約しています。
WITH reviews AS (
SELECT 1 AS restaurant_id, 'The restaurant was excellent.' AS review
UNION ALL SELECT 1, 'Excellent! I loved the pizza!'
UNION ALL SELECT 1, 'It was great, but the service was meh.'
UNION ALL SELECT 1, 'Mediocre food and mediocre service'
UNION ALL SELECT 2, 'Terrible quality ingredients, I should have eaten at home.'
UNION ALL SELECT 2, 'Bad restaurant, I would avoid this place.'
)
SELECT restaurant_id,
AI_AGG(review, 'Summarize the restaurant reviews for potential consumers')
FROM reviews
GROUP BY 1;
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| RESTAURANT_ID | SUMMARIZED_REVIEW |
|---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | Reviews for this restaurant are mixed. Some customers had a very positive experience, describing the restaurant as "excellent" and loving the pizza. However, others had a more neutral or negative experience, citing mediocre food and service. |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2 | Two reviewers had extremely negative experiences at this restaurant, citing poor quality ingredients and advising others to avoid it. |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
指示は、さまざまな集約タスクに使用し、応答のスタイルとトーンを構成することができます。次の例では、指示を使用して各製品で最もポジティブな評価を見つけ、評価のフランス語とポーランド語の翻訳を提示しています。
WITH reviews AS (
SELECT 1 AS product_id, 'Excellent' AS review
UNION ALL SELECT 1, 'Excellent'
UNION ALL SELECT 1, 'Great'
UNION ALL SELECT 1, 'Mediocre'
UNION ALL SELECT 2, 'Terrible'
UNION ALL SELECT 2, 'Bad'
UNION ALL SELECT 2, 'Average'
)
SELECT product_id,
AI_AGG(review, 'Identify the most positive rating and translate it into French and Polish, one word only') AS summarized_review
FROM reviews
GROUP BY 1;
+------------+--------------------+
| PRODUCT_ID | SUMMARIZED_REVIEW |
|------------+--------------------+
| 1 | French: Excellent |
| | Polish: Doskonały |
+------------+--------------------+
| 2 | French: Moyen |
| | Polish: Przeciętny |
+------------+--------------------+
AI_SUMMARIZE_AGG もご参照ください。