ai.analyze_sentiment היא כלי AI מובנה של AlloyDB AI שמסווג את הסנטימנט של הטקסט כחיובי, שלילי או ניטרלי. היכולת הזו מוטמעת ישירות במסד הנתונים, וכך AlloyDB Omni מאפשר לעבד נתונים לא מובְנים בלי ליצור צינורות עיבוד נתונים מורכבים של חילוץ, טרנספורמציה וטעינה (ETL) או שילובים של שירותים חיצוניים.
היתרונות של פונקציית ניתוח הסנטימנטים:
- ידע על העולם: אוסף עצום של עובדות, מושגים, קשרים והבנה של ההקשר של העולם, שמודלי שפה גדולים (LLM) רוכשים במהלך שלב האימון מראש.
- ניתוח בזמן אמת: שימוש ב-SQL כדי להביא את הידע של Gemini על העולם לנתונים של הארגון.
- יכולת התאמה לעומס: תמיכה בעיבוד מבוסס-מערך ומבוסס-סמן כדי לטפל באלפי שורות ביעילות.
- פשטות: מספקת הפשטה ברמה גבוהה שמנהלת באופן אוטומטי את הפעלת המודל ואת הכנת הנתונים.
הפונקציה של AlloyDB AI לניתוח סנטימנטים תומכת בכמה תרחישים לדוגמה, כולל:
- משוב מלקוחות: סיווג של אלפי ביקורות גולמיות ולא מובנות על מוצרים כדי לזהות את רמות שביעות רצון הלקוחות.
- מעקב אחרי רשתות חברתיות: ניתוח הסנטימנט של תגובות או אזכורים ברשתות החברתיות כדי להעריך את התפיסה הציבורית של מותג.
לפני שמתחילים
לפני שמשתמשים בפונקציה ai.analyze_sentiment, צריך לוודא שעומדים בדרישות הבאות:
הפעלת התוסף
מוודאים שהתוסף google_ml_integration (גרסה 1.6.3 ואילך) מותקן, ושמנוע השאילתות של AI מופעל.
כדי להפעיל את הדגל google_ml_integration.enable_ai_query_engine ב-AlloyDB Omni, משתמשים בפקודה SET. הדגל הזה קובע את הגישה לפונקציות AI כמו ai.analyze_sentiment.
מוודאים שגרסת התוסף
google_ml_integrationהיא 1.6.3 ואילך. כדי לבדוק את הגרסה, מריצים את הפקודה הבאה:SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';אם צריך לעדכן את התוסף, מריצים את הפקודה הבאה:
ALTER EXTENSION google_ml_integration UPDATE;מפעילים את הדגל לסשן הנוכחי או למסד הנתונים כולו. כדי להפעיל את הדגל בסשן הנוכחי, מריצים את הפקודה הבאה:
SET google_ml_integration.enable_ai_query_engine = '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 id, 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.
);
WITH sentiment_results AS (
SELECT
ARRAY_AGG(id ORDER BY id) as ids,
ai.analyze_sentiment(
inputs => array_agg(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;
הפלט לדוגמה:
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(
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(
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;
הפלט שיתקבל:
input | output
----------------------------------+----------
This movie is very good | positive
The actors play the parts well | positive
I like the music in this film | positive
The story is easy to follow | positive
Many people will enjoy this show | positive
The film is too long | negative
I do not like the ending | negative
This movie is very boring | negative
The story is okay | neutral
Some parts are fine | neutral