סיכום התוכן

הפונקציה ai.summarize ב-AlloyDB ל-PostgreSQL משתמשת במודלים גדולים של שפה (LLM) כמו Gemini כדי לצמצם ולארגן מחדש טקסט קלט לנקודות החשובות ביותר שלו. כשמריצים סיכום ישירות ב-SQL, אפשר לחלץ ממערכי נתונים גדולים תובנות חשובות בלי לבדוק אותם באופן ידני, לשלוט באורך הפלט על ידי ציון מגבלות של מילים או משפטים בהנחיה ולעבד שורות באופן אסינכרוני בקבוצות.

אתם יכולים להשתמש ב-ai.summarize כדי לתמצת תמלילי פגישות להחלטות ולפעולות לביצוע, לסכם מסמכים טכניים לסקירות כלליות ברמה גבוהה או לאסוף יומני תמיכת לקוחות וביקורות משתמשים לסיכומי בעיות מאוחדים.

לפני שמתחילים

לפני שמשתמשים בפונקציה ai.summarize, צריך לוודא שעומדים בדרישות הבאות:

הפעלת התוסף

מוודאים שמותקנת הגרסה העדכנית של התוסף google_ml_integration.enable_preview_ai_functions (גרסה 1.5.7 ואילך) ושהפונקציונליות של Preview מופעלת.

כדי להפעיל את הדגל google_ml_integration.enable_preview_ai_functions ב-AlloyDB, משתמשים בפקודה SET. הדגל הזה קובע את הגישה לפונקציות AI בגרסת טרום-השקה, כמו ai.summarize.

  1. מוודאים שגרסת google_ml_integration extension היא 1.5.7 ואילך. כדי לבדוק את הגרסה, מריצים את הפקודה הבאה:

    SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
    

    אם אתם צריכים לשדרג לגרסה שכוללת את פונקציות התצוגה המקדימה האלה, התקשרו למספר הבא:

    CALL google_ml.upgrade_to_preview_version();
    
  2. מפעילים את הדגל לסשן הנוכחי או למסד הנתונים כולו. כדי להפעיל את הדגל בסשן הנוכחי, מריצים את הפקודה הבאה:

    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-3.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."

המאמרים הבאים