ai.analyze_sentiment 関数は、テキストの感情をポジティブ、ネガティブ、ニュートラルに分類する AlloyDB AI の組み込みツールです。この機能をデータベースに直接埋め込むことで、AlloyDB for PostgreSQL では、複雑な抽出、変換、読み込み(ETL)パイプラインや外部サービス統合を構築することなく、非構造化データを処理できます。
感情分析機能には次の利点があります。
- 世界知識: 大規模言語モデル(LLM)が事前トレーニング フェーズで取得する、事実、コンセプト、関係、コンテキストの理解に関する膨大なコレクション。
- リアルタイム分析: SQL を使用して、Gemini の世界知識をエンタープライズ データに適用します。
- スケーラビリティ: 配列ベースとカーソルベースの処理をサポートし、数千行を効率的に処理します。
- シンプルさ: モデルの呼び出しとデータ準備を自動的に管理する高レベルの抽象化を提供します。
AlloyDB AI の感情分析関数は、次のような複数のユースケースをサポートしています。
- 顧客からのフィードバック: 数千件の未加工の非構造化商品レビューを分類して、顧客満足度を特定します。
- ソーシャル メディア モニタリング: ソーシャル メディアでの言及やコメントの感情を分析して、ブランドに対する一般の認識を把握します。
始める前に
ai.analyze_sentiment 関数を使用する前に、次の要件を満たしていることを確認してください。
拡張機能を有効にする
google_ml_integration.enable_preview_ai_functions 拡張機能の最新バージョン(バージョン 1.5.7 以降)がインストールされ、プレビュー機能が有効になっていることを確認します。
AlloyDB で google_ml_integration.enable_preview_ai_functions フラグを有効にするには、SET コマンドを使用します。このフラグは、ai.analyze_sentiment などのプレビュー版の AI 機能へのアクセスを制御します。
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');
1 つの文字列の感情分析
単一のテキスト入力の感情を評価するには、ai.analyze_sentiment のスカラー バージョンを使用します。
SELECT ai.analyze_sentiment(
prompt => 'TEXT_CONTENT',
model_id => 'MODEL_ID' -- Optional. The default value is gemini-2.5-flash-lite.
);
次の例は、id 列と review_content 列にレビュー データを保存する reviews というテーブルに保存されているテキスト データに対して行レベルの感情分析を実行する方法を示しています。この例では、テーブルの各行の review_content 列に ai.analyze_sentiment() 関数を適用する SELECT クエリを実行します。この関数は、各レビューを個別に処理し、計算された感情(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
感情をバッチで分析する
大規模なデータセットでパフォーマンスを向上させるには、関数の配列ベースのバージョンを使用して、1 回の呼び出しで複数の文字列を処理します。
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.
);
次の例では、reviews という名前のテーブルから購入者レビューの感情分析を行います。
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 モデルを介してデータをストリーミングできます。
カーソルベースの ai.analyze_sentiment の関数シグネチャは次のとおりです。
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 関数にデータを 1 行ずつフィードする方法を示しています。
-- 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
次のステップ
- コンテンツを要約する。
- AI 関数の概要を読む。