ai.analyze_sentiment 関数は、テキストの感情をポジティブ、ネガティブ、ニュートラルに分類する組み込みの AlloyDB AI ツールです。AlloyDB Omni では、この機能をデータベースに直接埋め込むことで、複雑な抽出、変換、ロード(ETL)パイプラインや外部サービス統合を構築することなく、非構造化データを処理できます。
感情分析機能には次の利点があります。
- 世界に関する知識: 大規模言語モデル(LLM)が事前トレーニング フェーズで取得する、事実、概念、関係、世界に関するコンテキストの理解の膨大なコレクション。
- リアルタイム分析: SQL を使用して、Gemini の世界に関する知識をエンタープライズ データに適用します。
- スケーラビリティ: 配列ベースとカーソルベースの処理をサポートし、数千行を効率的に処理できます。
- シンプルさ: モデルの呼び出しとデータ準備を自動的に管理する高レベルの抽象化を提供します。
AlloyDB AI の感情分析関数は、次のようなさまざまなユースケースをサポートしています。
- 顧客フィードバック: 数千件の未加工の非構造化製品レビューを分類して、顧客満足度を特定します。
- ソーシャル メディアのモニタリング: ソーシャル メディアでの言及やコメントの感情を分析して、ブランドに対する世間の認識を把握します。
始める前に
ai.analyze_sentiment 関数を使用する前に、次の要件を満たしていることを確認してください。
拡張機能を有効にする
プレビュー機能が有効になっていて、google_ml_integration.enable_preview_ai_functions 拡張機能の最新バージョン(バージョン 1.5.7 以降)がインストールされていることを確認します。
AlloyDB Omni で 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');
単一の文字列の感情を分析する
単一のテキスト入力の感情を評価するには、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.
);
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
次のステップ
- コンテンツを要約する。
- AI 関数の概要を読む。