הפונקציה ai.summarize משתמשת במודלים גדולים של שפה (LLM) כמו Gemini כדי לנסח מחדש ולארגן מחדש טקסט קלט לצורה המהותית ביותר שלו. הפונקציה הזו לוכדת את הטענה המרכזית של המסמך תוך שמירה על הטון והניואנסים המקוריים של הכותב. באמצעות הפונקציה הזו, אפשר לחלץ ממערכי נתונים גדולים ממצאים ומסקנות חשובים בלי לבדוק אותם באופן ידני, ולשלוט באורך הפלט על ידי הגדרת אילוצים כמו מספר מילים או מגבלות על מספר המשפטים בהנחיה. אפשר גם להשתמש בעיבוד ברצף (batch processing) או בצילומי מצב מבוססי-סמן כדי לעבד מיליוני שורות באופן אסינכרוני.
פונקציית סיכום התוכן של AlloyDB AI תומכת במגוון תרחישי שימוש, כולל, בין היתר:
- תמלילי פגישות: סיכום של טקסט שיחה כדי להתמקד בהחלטות ובפעולות לביצוע.
- תיעוד טכני: תמצות של מתודולוגיה מורכבת, תוצאות והשלכות לסקירות כלליות.
- יומני תמיכת לקוחות: סיכום של כמה ביקורות משתמשים או כרטיסי תמיכה לסיכום מאוחד של בעיות נפוצות.
לפני שמתחילים
לפני שמשתמשים בפונקציה ai.summarize, צריך לוודא שעומדים בדרישות הבאות.
הפעלת התוסף
מוודאים שמותקנת הגרסה העדכנית של התוסף google_ml_integration.enable_preview_ai_functions (גרסה 1.5.7 ומעלה) ושהפונקציונליות של התצוגה המקדימה מופעלת.
כדי להפעיל את הדגל google_ml_integration.enable_preview_ai_functions ב-AlloyDB Omni, משתמשים בפקודה SET. הדגל הזה קובע את הגישה לפונקציות AI בגרסת טרום-השקה כמו
ai.summarize.
מוודאים שגרסת
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 כדי לעבד ביעילות כמה רשומות טקסט בקריאה אחת.
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 ואז לעבד את התוצאות אחת אחת כדי לאחסן אותן בחזרה במסד הנתונים.
בדוגמה הבאה מוצג אופן השימוש בסמנים עם הפונקציה ai.summarize כדי לעבד ביעילות שורות מהטבלה movie_reviews, ליצור סיכום לכל ביקורת ולאחסן את הסיכומים האלה בטבלה חדשה בשם review_summaries.
הגישה הזו מבוססת על סמן, והיא שימושית לטיפול במערכי נתונים גדולים מדי לעיבוד באצווה אחת או להכללה בזיכרון בבת אחת.
הפרמטר 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, והסיכומים מאוחסנים בטבלה ומקושרים למזהי הביקורות המקוריים.
-- 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_id על סמך כל הביקורות בטבלה movie_reviews.
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."