הפונקציה ai.analyze_sentiment היא כלי AI מובנה של AlloyDB AI שמסווג את הסנטימנט של הטקסט כחיובי, שלילי או ניטרלי. הטמעת היכולת הזו ישירות במסד הנתונים ב-AlloyDB ל-PostgreSQL מאפשרת להחיל את הבנת השפה של Gemini על נתונים לא מובְנים של הארגון בזמן אמת באמצעות SQL, ולתמוך בעיבוד באצ'ים מבוסס-מערכים ומבוסס-סמן באלפי שורות בלי צינורות עיבוד נתונים מורכבים של חילוץ, טרנספורמציה וטעינה (ETL).
אתם יכולים להשתמש ב-ai.analyze_sentiment כדי לסווג ביקורות על מוצרים ומשוב מלקוחות, או לנתח אזכורים ברשתות החברתיות כדי להעריך את תפיסת המותג.
לפני שמתחילים
לפני שמשתמשים בפונקציה ai.analyze_sentiment, צריך לוודא שעומדים בדרישות הבאות:
הפעלת התוסף
מוודאים שמותקנת הגרסה העדכנית של התוסף google_ml_integration.enable_preview_ai_functions (גרסה 1.5.7 ואילך) ושהפונקציונליות של גרסת טרום-ההשקה מופעלת.
כדי להפעיל את הדגל google_ml_integration.enable_preview_ai_functions ב-AlloyDB, משתמשים בפקודה SET. הדגל הזה קובע את הגישה לפונקציות AI בגרסת טרום-השקה (Preview) כמו ai.analyze_sentiment.
מוודאים שגרסת התוסף
google_ml_integrationהיא 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 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(
input => 'TEXT_CONTENT',
model_id => 'MODEL_ID' -- Optional. The default value is gemini-3.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(
inputs => 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-3.5-flash-lite.
);
בדוגמה הבאה מתבצע ניתוח של הסנטימנט של ביקורות לקוחות מטבלה בשם reviews.
WITH sentiment_results AS (
SELECT
ARRAY_AGG(id ORDER BY id) as ids,
ai.analyze_sentiment(
inputs => 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 בקבוצות שאפשר לנהל, בלי לטעון את כל הנתונים לזיכרון בבת אחת.
חתימת הפונקציה של הגרסה מבוססת-הסמן של ai.analyze_sentiment היא:
CREATE OR REPLACE FUNCTION ai.analyze_sentiment(
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 שורה אחר שורה באמצעות סמן:
-- Run the cursor logic in an anonymous PL/pgSQL block
DO $$
DECLARE
-- Declare a cursor for the review content
review_cursor REFCURSOR;
result_cursor REFCURSOR;
result_record RECORD;
BEGIN
-- 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
result_cursor := ai.analyze_sentiment(
input_cursor => review_cursor,
batch_size => 5 -- Optional: Process in batches of 5. The default value is 10.
);
-- Fetch and display results from the result_cursor
LOOP
FETCH result_cursor INTO result_record;
EXIT WHEN NOT FOUND;
RAISE NOTICE '%', result_record;
END LOOP;
-- Close the cursors
CLOSE review_cursor;
CLOSE result_cursor;
-- End the anonymous block
END $$;
הפלט שיתקבל:
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