AlloyDB for PostgreSQL 中的 ai.summarize 函式會使用 Gemini 等大型語言模型 (LLM),將輸入文字濃縮並重組為最重要的重點。直接在 SQL 中執行摘要作業,即可從大型資料集擷取重要發現,不必手動審查。您也可以在提示中指定字詞或句子限制,控制輸出長度,並以非同步方式批次處理資料列。
您可以使用 ai.summarize 將會議轉錄稿濃縮成決策和待辦事項、將技術文件總結成高階總覽,或將客戶服務記錄和使用者評論彙整成統一的問題摘要。
事前準備
使用 ai.summarize 函式前,請確認您符合下列需求。
啟用擴充功能
請確認您已安裝最新版本的 google_ml_integration.enable_preview_ai_functions 擴充功能 (1.6.1 以上版本),並啟用預覽功能。
如要在 AlloyDB 中啟用 google_ml_integration.enable_preview_ai_functions 旗標,請使用 SET 指令。這個旗標可控管 ai.summarize 等預先發布版 AI 函式的存取權。
確認
google_ml_integration extension為 1.6.1 以上版本。您可以執行下列指令來查看版本: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(
input => 'TEXT_CONTENT',
additional_instructions => 'INSTRUCTIONS', -- Optional
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(
inputs => TEXT[],
additional_instructions => 'INSTRUCTIONS', -- Optional
batch_size => INT, -- Optional. The default value is 10.
model_id => VARCHAR(100) -- Optional. The default value is gemini-3.5-flash-lite.
);
以下範例說明如何使用陣列版 ai.summarize 函式,在單一呼叫中有效率地處理多筆文字記錄。
WITH summarized_results AS (
SELECT
ARRAY_AGG(id ORDER BY id) as ids,
ai.summarize(
inputs => array_agg(review ORDER BY id),
additional_instructions =>
'Please summarize this in max 10 words, review : ',
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 函式,然後逐一處理結果,再將結果儲存回資料庫。
以下範例說明如何搭配使用游標和 ai.summarize 函式,有效率地處理 movie_reviews 資料表中的資料列、為每則評論生成摘要,並將這些摘要儲存在名為 review_summaries 的新資料表中。這種以游標為基礎的方法,適合用於處理大型資料集,因為這類資料集可能太大,無法在單一批次中處理,或一次載入記憶體。
input_cursor 參數會採用 REFCURSOR。也就是說,您必須提供游標名稱,做為 SQL 查詢結果的指標。接著,ai.summarize 函式會從這個游標擷取資料,做為摘要的輸入內容。
CREATE OR REPLACE FUNCTION ai.summarize(
input_cursor REFCURSOR,
additional_instructions TEXT DEFAULT NULL,
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 input FROM movie_reviews ORDER BY id;
-- 2. Call the AI summarize function
cursor_response := ai.summarize(
input_cursor => review_cursor,
additional_instructions => 'Please summarize the following review in max 10 words: '
);
-- 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."
使用額外指令生成摘要
下列 SQL 查詢會傳遞額外指令,引導摘要生成:
SELECT ai.agg_summarize(review, 'Please summarize this within 10 words') FROM movie_reviews GROUP BY movie_id;
使用特定模型生成摘要
如要指定模型,但不提供額外指示,請傳遞模型 ID,並明確轉換為 VARCHAR,以免系統誤認為指示:
SELECT ai.agg_summarize(review, 'gemini-3.5-flash-lite'::VARCHAR) FROM movie_reviews GROUP BY movie_id;
使用額外指令和特定模型生成摘要
下列 SQL 查詢會傳遞額外指令和特定模型 ID:
SELECT ai.agg_summarize(review, 'Please summarize this within 10 words', 'gemini-3.5-flash-lite') FROM movie_reviews GROUP BY movie_id;
匯總各列摘要
ai.agg_summarize 函式會對資料欄中的多個資料列執行運算。系統會將值匯總成統一的提示,為整個群組生成單一摘要。
下列 SQL 查詢會使用 ai.agg_summarize 匯總函式,根據 movie_reviews 資料表中的所有評論,為每個 movie_id 產生單一、統一的摘要。
SELECT ai.agg_summarize(review) FROM movie_reviews GROUP BY movie_id;
ai.summarize 會個別摘要每一列,ai.agg_summarize 則會將多列的文字合併為單一輸入內容,為整組資料列產生一份整合摘要。
輸出範例如下:
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."