ai.summarize 関数は、Gemini などの大規模言語モデル(LLM)を使用して、入力テキストを言い換えて再構成し、最も重要な形式にします。この関数は、元の著者のトーンとニュアンスを維持しながら、ドキュメントのコアとなる主張を捉えます。この関数を使用すると、手動で確認することなく、膨大なデータセットから重要な調査結果と結論を抽出できます。また、プロンプトで単語数や文数の上限などの制約を指定して、出力の長さを制御できます。バッチ処理またはカーソルベースのスナップショットを使用して、数百万行を非同期で処理することもできます。
AlloyDB AI コンテンツ要約関数は、次のようなさまざまなユースケースをサポートしています。
- 会議の議事録: 会話テキストを要約して、決定事項とアクション アイテムに焦点を当てます。
- 技術ドキュメント: 複雑な方法論、結果、影響を概要にまとめます。
- カスタマー サポート ログ: 複数のユーザー レビューまたはサポート チケットを、共通の問題に関する 1 つの統合された要約に集約します。
始める前に
ai.summarize 関数を使用する前に、次の要件を満たしていることを確認してください。
拡張機能を有効にする
google_ml_integration.enable_preview_ai_functions 拡張機能(バージョン 1.5.7 以降)の最新バージョンがインストールされ、プレビュー機能が有効になっていることを確認します。
AlloyDB で google_ml_integration.enable_preview_ai_functions フラグを有効にするには、SET コマンドを使用します。このフラグは、ai.summarize などのプレビュー AI 関数へのアクセスを制御します。
google_ml_integration extensionがバージョン 1.5.7 以降であることを確認します。バージョンを確認するには、次のコマンドを実行します。SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';これらのプレビュー関数を含むバージョンにアップグレードする必要がある場合は、次のコマンドを呼び出します。
CALL google_ml.upgrade_to_preview_version();現在のセッションまたはデータベース全体でフラグを有効にします。現在のセッションでフラグを有効にするには、次のコマンドを実行します。
SET google_ml_integration.enable_preview_ai_functions = 'on';この変更では、データベースの再起動は必要ありません。このフラグのデフォルト値は
offです。
サンプルテーブルを作成する
このドキュメントの要約関数の例に沿って操作するには、テーブルを作成し、次の映画レビューを入力します。
CREATE TABLE movie_reviews (
id INT PRIMARY KEY,
movie_id INT,
review TEXT
);
INSERT INTO movie_reviews (id, movie_id, review) VALUES
(1, 1, 'The Midnight Echo represents a masterclass in atmospheric tension. The cinematography captures the isolation of the frozen tundra perfectly while the lead actor delivers a superb performance. Although the pacing slows down significantly in the second act, the final twist remains unexpected. This is a truly remarkable experience.'),
(2, 1, 'Neon Velocity delivers high octane action but lacks the narrative depth required to make the stakes feel real. The visual effects are undeniably stunning, creating a vibrant cyberpunk world that pops off the screen. However, the dialogue feels cliched and the secondary characters are thin. This film is a shallow blockbuster.'),
-- more rows are present in this table
単一の文字列を要約する
単一のテキスト ブロックを処理するには、ai.summarize のスカラー バージョンを使用します。
SELECT ai.summarize(
prompt => 'TEXT_CONTENT',
model_id => 'MODEL_ID' -- Optional
);
次のサンプルクエリを実行して、映画レビューの簡潔な要約を提供します。
SELECT ai.summarize(review) FROM movie_reviews;
出力例を次に示します。
id | summary
----+-----------------------------------------------------------
1 | "Atmospheric thriller with a haunting performance and shocking twist."
2 | "Visually stunning cyberpunk action film lacks narrative depth."
...
バッチで要約する
複数のレコードを効率的に処理するには、配列ベースのバージョンを使用します。
SELECT ai.summarize(
prompts => TEXT[],
batch_size => INT, -- Optional. The default is 10.
model_id => VARCHAR(100) -- Optional. The default value is gemini-2.5-flash-lite.
);
次の例は、ai.summarize 関数の配列ベースのバージョンを使用して、1 回の呼び出しで複数のテキスト レコードを効率的に処理する方法を示しています。
WITH summarized_results AS (
SELECT
ARRAY_AGG(id ORDER BY id) as ids,
ai.summarize(
prompts => array_agg( 'Please summarize this in max 10 words, review : ' || review
ORDER BY id),
batch_size => 15) as summaries
FROM movie_reviews
),
correlated_results AS (
SELECT ids[i] as ID, summaries[i] as summary
FROM summarized_results,
generate_series(1, array_length(ids, 1)) AS i
)
SELECT movie_reviews.id, correlated_results.summary as summary
FROM movie_reviews
JOIN correlated_results ON movie_reviews.id = correlated_results.id
ORDER BY movie_reviews.id DESC ;
出力例を次に示します。
id | summary
---+----------------------------------------------------------------------------
1 | "Masterclass in tension, haunting performance with a twist."
2 | "High octane action, stunning visuals, shallow blockbuster."
3 | "Gentle, moving exploration of emotional honesty."
カーソルを使用して要約する
カーソルを使用すると、大量のレビューを効率的に処理できます。レビューを AI 関数にフィードし、結果を 1 つずつ処理してデータベースに保存します。
次の例は、ai.summarize 関数でカーソルを使用して、movie_reviews テーブルの行を効率的に処理し、各レビューの要約を生成して、review_summaries という新しいテーブルに保存する方法を示しています。このカーソルベースのアプローチは、1 つのバッチで処理するには大きすぎるか、メモリに一度に収まらない可能性のある大規模なデータセットを処理する場合に便利です。
input_cursor パラメータは REFCURSOR を取ります。つまり、カーソル名を指定する必要があります。これは、SQL クエリの結果へのポインタとして機能します。ai.summarize 関数は、このカーソルからデータを取得して、要約の入力として使用します。
CREATE OR REPLACE FUNCTION ai.summarize(
prompt TEXT,
input_cursor REFCURSOR,
batch_size INT DEFAULT NULL,
model_id VARCHAR(100) DEFAULT NULL)
RETURNS REFCURSOR
次の例では、テーブル review_summaries を作成し(必要な場合)、映画レビューを反復処理し、AI 関数を使用して各レビューの簡潔な要約を生成し、元のレビュー ID にリンクされた要約をテーブルに保存します。
-- Create a table to store the results
CREATE TABLE IF NOT EXISTS review_summaries (
review_id INT,
summary_text TEXT
);
DO $$
DECLARE
-- Use descriptive cursor and variable names
review_cursor REFCURSOR;
result_record RECORD;
cursor_response REFCURSOR;
id_array INT[];
idx INT := 1;
BEGIN
-- 1. Open cursor for the input text
OPEN review_cursor FOR
SELECT review AS prompt FROM movie_reviews ORDER BY id;
-- 2. Call the AI summarize function
cursor_response := ai.summarize(
prompt => 'Please summarize the following review in max 10 words: ',
input_cursor => review_cursor
);
-- 3. Map IDs into an array to maintain relational integrity during the loop
SELECT ARRAY_AGG(id ORDER BY id) INTO id_array FROM movie_reviews;
-- 4. Iterate through AI results and insert into the results table
LOOP
FETCH cursor_response INTO result_record;
EXIT WHEN NOT FOUND;
INSERT INTO review_summaries (review_id, summary_text)
VALUES (id_array[idx], result_record.output);
idx := idx + 1;
END LOOP;
-- 5. Clean up cursors
CLOSE review_cursor;
CLOSE cursor_response;
END;
$$;
-- Verify results
SELECT * FROM review_summaries;
出力例を次に示します。
review_id | summary_text
-----------+---------------------------------------------------------------------------
1 | "Masterclass in atmospheric tension with haunting performance, devastating twist."
2 | "High octane action, stunning visuals, but shallow blockbuster."
3 | "Gentle, moving exploration of grief with poetic screenplay."
行全体の要約を集計する
ai.agg_summarize 関数は、列の複数の行にわたって動作します。値を統合されたプロンプトに集約して、グループ全体の単一の要約を生成します。
次の SQL クエリでは、ai.agg_summarize 集計関数を使用して、movie_reviews テーブルのすべてのレビューに基づいて、movie_id ごとに 1 つの統合された要約を生成します。
select ai.agg_summarize(review) from movie_reviews group by movie_id;
ai.summarize は各行を個別に要約しますが、ai.agg_summarize は複数の行のテキストを 1 つの入力に結合して、行のグループ全体の統合された要約を生成します。
出力例を次に示します。
agg_summarize
"The Midnight Echo is a masterclass in atmospheric tension, featuring haunting performances and stunning cinematography, though its pacing falters in the second act before a final twist. Neon Velocity offers stunning visual effects and high-octane action within a vibrant cyberpunk world, but suffers from shallow narrative depth, cliched dialogue, and underdeveloped characters. Garden of Whispers is a gentle, moving, and emotionally honest exploration of and life, characterized by poetic screenwriting and natural performances. Shadow Protocol ambitiously attempts to reinvent the spy thriller with a non-linear narrative and precise action, but ultimately confuses the audience and presents a derivative central mystery. The Last Alchemist uniquely blends historical drama with subtle fantasy, boasting meticulous production design and electric chemistry between its protagonists."