ai.analyze_sentiment 函式是 AlloyDB AI 內建工具,可將文字情緒分類為正面、負面或中立。AlloyDB Omni 直接在資料庫中嵌入這項功能,讓您處理非結構化資料,不必建構複雜的擷取、轉換和載入 (ETL) 管道,也不必整合外部服務。
情緒分析功能可帶來下列好處:
- 世界知識:大型語言模型 (LLM) 在預先訓練階段獲得的龐大事實、概念、關係和世界脈絡理解。
- 即時分析:使用 SQL 將 Gemini 的世界知識帶入企業資料。
- 擴充性:支援以陣列和游標為基礎的處理作業,可有效處理數千列資料。
- 簡單易用:提供高階抽象化功能,可自動管理模型叫用和資料準備作業。
AlloyDB AI 的情緒分析功能支援多種用途,包括:
- 顧客意見回饋:將數千則未經處理的非結構化產品評論分類,找出顧客滿意度。
- 社群媒體監控:分析社群媒體提及或留言的情緒,評估大眾對品牌的觀感。
事前準備
使用 ai.analyze_sentiment 函式前,請確認您符合下列條件。
啟用擴充功能
確認您已安裝最新版擴充功能 (1.5.7 以上版本),並啟用「預覽」功能。google_ml_integration.enable_preview_ai_functions
如要在 AlloyDB Omni 中啟用 google_ml_integration.enable_preview_ai_functions 旗標,請使用 SET 指令。這個標記可控管 AI 預覽功能 (例如 ai.analyze_sentiment) 的存取權。
確認
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 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,因此您需要開啟要分析的輸入資料的游標。在本範例中,您會分析 reviews 資料表中的 review_content。
以下範例說明如何使用游標,逐列將資料提供給 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