ניתוח הסנטימנט בטקסט

בחירת גרסת תיעוד:

הפונקציה ai.analyze_sentiment היא כלי מובנה ב-AlloyDB AI שמסווג את הסנטימנט של טקסט כחיובי, שלילי או ניטרלי. הטמעת היכולת הזו ישירות במסד הנתונים מאפשרת לכם לעבד נתונים לא מובנים בלי ליצור צינורות מורכבים של חילוץ, טרנספורמציה וטעינה (ETL) או שילובים של שירותים חיצוניים.

היתרונות של פונקציית ניתוח הסנטימנט:

  • ידע על העולם: אוסף עצום של עובדות, מושגים, קשרים והבנה הקשרית של העולם שמודלי שפה גדולים (LLM) רוכשים במהלך שלב האימון המוקדם שלהם.
  • ניתוח בזמן אמת: משתמשים בידע של Gemini על העולם כדי לנתח נתונים ארגוניים באמצעות SQL.
  • יכולת הרחבה: תמיכה בעיבוד מבוסס מערכים ומבוסס סמן כדי לטפל ביעילות באלפי שורות.
  • פשטות: מספק הפשטה ברמה גבוהה שמנהלת את הפעלת המודל ואת הכנת הנתונים באופן אוטומטי.

פונקציית ניתוח הסנטימנטים של AlloyDB AI תומכת בכמה תרחישי שימוש, כולל:

  • משוב מלקוחות: סיווג של אלפי ביקורות גולמיות ולא מובנות על מוצרים כדי לזהות את רמות שביעות רצון הלקוחות.
  • מעקב ברשתות החברתיות: ניתוח הסנטימנט של אזכורים או תגובות ברשתות החברתיות כדי לאמוד את תפיסת הציבור לגבי מותג.

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

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

הפעלת התוסף

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

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

  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 IF NOT EXISTS reviews (
    id INT PRIMARY KEY,
    review_content TEXT
);

INSERT INTO reviews (id, review_content) VALUES 
(1, 'This movie is very good'),
(2, 'The actors play the parts well'),
(3, 'I like the music in this film'),
(4, 'The story is easy to follow'),
(5, 'Many people will enjoy this show'),
(6, 'The film is too long'),
(7, 'I do not like the ending'),
(8, 'This movie is very boring'),
(9, 'The story is okay'),
(10, 'Some parts are fine');

ניתוח הסנטימנט של מחרוזת אחת

כדי להעריך את הסנטימנט של קלט טקסט יחיד, משתמשים בגרסה הסקלרית של ai.analyze_sentiment.

SELECT ai.analyze_sentiment(
  prompt => 'TEXT_CONTENT',
  model_id => 'MODEL_ID' -- Optional. The default value is gemini-2.5-flash-lite.
);

בדוגמה הבאה מוצג איך לבצע ניתוח סנטימנט ברמת השורה בנתוני טקסט שמאוחסנים בטבלה בשם reviews עם העמודות id ו-review_content לאחסון נתוני ביקורות. בדוגמה מבוצעת שאילתת SELECT שמחילה את הפונקציה ai.analyze_sentiment() על העמודה review_content בכל שורה בטבלה. הפונקציה הזו מעבדת כל ביקורת בנפרד ומחזירה את הסנטימנט המחושב שלה (positive,‏ negative או neutral).

--- Row Level sentiment analysis
SELECT ai.analyze_sentiment(review_content) FROM reviews;

הפלט לדוגמה:

 id | analyze_sentiment
----+-------------------
  1 | positive
  2 | positive
  3 | positive
  4 | positive
  5 | positive
  6 | negative
  7 | negative
  8 | negative
  9 | neutral
 10 | neutral

ניתוח סנטימנטים באצווה

כדי לשפר את הביצועים במערכי נתונים גדולים, כדאי להשתמש בגרסה מבוססת המערך של הפונקציה כדי לעבד כמה מחרוזות בקריאה אחת.

SELECT ai.analyze_sentiment(
  prompts => ARRAY['TEXT_1', 'TEXT_2'],
  batch_size => BATCH_SIZE, -- Optional. The default value is 10.
  model_id => 'MODEL_ID' -- Optional. The default value is gemini-2.5-flash-lite.
);
WITH sentiment_results AS (
SELECT
  ARRAY_AGG(id ORDER BY id) as ids,
  ai.analyze_sentiment( 
    prompts => array_agg( 'Please analyze the sentiment of this review  : ' || review_content
      ORDER BY id),
    batch_size => 15) as sentiments 
FROM reviews
),
correlated_results AS (
    SELECT ids[i] as id, sentiments[i] as sentiment
    FROM sentiment_results,
    generate_series(1, array_length(ids, 1)) AS i
)
SELECT reviews.id, correlated_results.sentiment as sentiment 
FROM reviews
JOIN correlated_results ON reviews.id = correlated_results.id
ORDER BY reviews.id DESC;

הפלט לדוגמה:

 id | sentiment
----+-----------
  1 | positive
  2 | positive
  3 | positive
  4 | positive
  5 | positive
  6 | negative
  7 | negative
  8 | negative
  9 | neutral
 10 | neutral

ניתוח סנטימנטים באמצעות סמנים

הפונקציה שמבוססת על סמן נועדה לעיבוד יעיל של מערכי נתונים גדולים, כי היא מאפשרת למערכת להזרים נתונים דרך מודל ה-AI באצוות ניהוליות בלי לדרוש טעינה של כל הנתונים לזיכרון בבת אחת.

CREATE OR REPLACE FUNCTION ai.analyze_sentiment(
    prompt TEXT,
    input_cursor REFCURSOR,
    batch_size INT DEFAULT NULL,
    model_id VARCHAR(100) DEFAULT NULL)
  RETURNS REFCURSOR

עכשיו אפשר להשתמש בפונקציה ai.analyze_sentiment. מכיוון שהיא מצפה ל-REFCURSOR, צריך לפתוח סמן לנתוני הקלט שרוצים לנתח. בדוגמה הזו, מנתחים את review_content מהטבלה reviews.

בדוגמה הבאה מוצג אופן הזנת הנתונים לפונקציה ai.analyze_sentiment שורה אחר שורה באמצעות סמן:

-- Start a transaction
BEGIN;

-- Declare a cursor for the review content
DECLARE review_cursor REFCURSOR;

-- Open the cursor with the query to fetch the review content
OPEN review_cursor FOR SELECT review_content FROM reviews;

-- Call the AI function, passing the cursor
-- This function will return another cursor containing the results
DECLARE result_cursor REFCURSOR;
SELECT ai.analyze_sentiment(
    prompt => 'Analyze the sentiment of the following movie review:',
    input_cursor => review_cursor,
    batch_size => 5  -- Optional: Process in batches of 5
) INTO result_cursor;

-- Fetch and display results from the result_cursor
-- The exact way to fetch from a REFCURSOR depends on the SQL environment.
-- This is a conceptual example.
FETCH ALL FROM result_cursor;

-- Close the cursors
CLOSE review_cursor;
CLOSE result_cursor;

-- End the transaction
COMMIT;

הפלט שיתקבל:

review_content                 | sentiment | score
-------------------------------+-----------+-------
This movie is very good        | Positive  |   0.9
The actors play the parts well | Positive  |   0.8
I like the music in this film  | Positive  |   0.8
The story is easy to follow    | Positive  |   0.7
Many people will enjoy this show | Positive  |   0.8
The film is too long           | Negative  |  -0.6
I do not like the ending       | Negative  |  -0.8
This movie is very boring      | Negative  |  -0.9
The story is okay              | Neutral   |   0.1
Some parts are fine            | Neutral   |   0.2

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