semantic-ranker-default-003 などの Vertex AI ランキング モデルを使用して、アプリケーションの検索結果をランク付けして再ランク付けする方法について説明します。ai.rank() 関数を使用すると、クエリとの関連性に基づいてドキュメントをスコアリングし、クエリの順序を改善するためにドキュメントを再ランク付けして、ベクトル検索の結果を改善できます。
Vertex AI Ranking API は、ドキュメントのリストを取得し、ドキュメントが特定のクエリ(検索文字列)にどの程度関連しているかに基づいてドキュメントをランク付けします。ai.rank() 関数を使用すると、ドキュメントが特定のクエリにどの程度適切に回答しているかのスコアが返されます。
このページの手順は、AlloyDB Omni と生成 AI のコンセプトに詳しい方向けのものです。
AlloyDB Omni は ai スキーマを予約して作成します。
始める前に
検索結果をランク付けする前に、次の操作を行います。
- Vertex AI モデルへのユーザー アクセスを構成します。
google_ml_integrationの最新バージョンがインストールされていることを確認します。インストールされているバージョンを確認するには、次のコマンドを実行します。
SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration'; extversion ------------ 1.5.2 (1 row)
拡張機能がインストールされていない場合、またはインストールされているバージョンが 1.5.2 より前の場合は、拡張機能を更新します。
CREATE EXTENSION IF NOT EXISTS google_ml_integration; ALTER EXTENSION google_ml_integration UPDATE;
上記のコマンドを実行したときに問題が発生した場合、または上記のコマンドを実行しても拡張機能がバージョン 1.5.2 にアップデートされない場合は、Google Cloud サポートにお問い合わせください。
AlloyDB AI クエリエンジンの機能を使用するには、
google_ml_integration.enable_ai_query_engineフラグをonに設定します。- テキスト エディタを使用して、AlloyDB Omni のインストールの
postgresql.conf構成ファイルで次のフラグを設定します。google_ml_integration.enable_ai_query_engine = on
postgresql.confファイルを保存したら、AlloyDB Omni サービスを再起動して変更を有効にします。sudo systemctl restart alloydbomni18
- テキスト エディタを使用して、AlloyDB Omni のインストールの
Discovery Engine API を有効にする
<p>To use ranking models, you must enable the Discovery Engine API.<br>
Replace <code><var>PROJECT_ID</var></code> with your
Google Cloud project ID and <code><var>PROJECT_NUMBER</var></code>
with your corresponding project number.</p>
<pre class="prettyprint lang-terminal">
# Enable Discovery Engine API
gcloud services enable discoveryengine.googleapis.com --project=<var>PROJECT_ID</var>
gcloud projects add-iam-policy-binding <var>PROJECT_ID</var> \
--member="serviceAccount:service-<var>PROJECT_NUMBER</var>@iam.gserviceaccount.com" \
--role="roles/discoveryengine.viewer"
</pre>
<p>Model registration for ranking isn't required for Vertex AI models.
You can use the Vertex AI model name as the
<code>model_id</code>, which is shown in the following example.
</p>
<pre class="prettyprint lang-sh">
SELECT index, score
FROM
ai.rank(
model_id => 'semantic-ranker-default-003',
search_string => 'Affordable family-friendly vacation spots in Southeast Asia?',
documents =>
ARRAY[
'Luxury resorts in South Korea',
'Family vacation packages for Vietnam: Ha Long Bay and Hoi An',
'Budget-friendly beaches in Thailand perfect for families',
'A backpacker guide to solo travel in India'])
</pre>
<p>A common use case for the semantic ranker is to rerank the results returned
by vector search for better query ordering. The following example shows how to
use the semantic ranking model for this use case. The example retrieves an
initial result set for the query <code>personal fitness
equipment</code> using vector search. These results are then re-ranked to
return the top five results.
</p>
<pre class="prettyprint lang-sh">
WITH initial_ranking AS (
SELECT id, description, ROW_NUMBER() OVER () AS ref_number
FROM product
ORDER BY
embedding <=> google_ml.embedding(
'gemini-embedding-001', 'personal fitness equipment')::vector
LIMIT 10
), reranked_results AS (
SELECT index, score
FROM ai.rank(
model_id => 'semantic-ranker-default-003',
search_string => 'personal fitness equipment',
documents => (SELECT ARRAY_AGG(description ORDER BY ref_number) FROM initial_ranking),
top_n => 5)
)
SELECT id, description
FROM initial_ranking, reranked_results
WHERE initial_ranking.ref_number = reranked_results.index
ORDER BY reranked_results.score DESC;
</pre>
<p>For a list of available models and use cases, see <a href="/generative-ai-app-builder/docs/ranking#models">Supported models</a>.</p>
Vertex AI と統合して拡張機能をインストールする
詳細については、クラウドベースのモデルをクエリするように AlloyDB Omni インストールを構成するをご覧ください。
必要なロール
ディスカバリー エンジンのランキング モデルを使用するために必要な権限を取得するには、your project に対するディスカバリー エンジン閲覧者(roles/discoveryengine.viewer)の Identity and Access Management(IAM)ロールを AlloyDB Omni サービス アカウントに付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織に対するアクセス権の管理をご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
検索結果をランク付けする
次の SQL クエリは、検索結果をランク付けする方法を示しています。
SELECT
ai.rank(
model_id => 'MODEL_ID',
search_string => 'SEARCH_STRING',
documents => ARRAY['DOCUMENT_1', 'DOCUMENT_2', 'DOCUMENT_3']);
次のように置き換えます。
| パラメータ | 説明 |
|---|---|
MODEL_ID |
定義するモデル エンドポイントの一意の ID。 |
SEARCH_STRING |
レコードのランク付けに使用する検索文字列。 |
DOCUMENTS |
ランク付けする一意のテキスト文字列の配列。 |
サポートされている Vertex AI ランキング モデルの一覧については、サポートされているモデルをご覧ください。
例
Vertex AI ランキング モデルを使用して検索結果をランク付けするには、次のクエリを実行します。
SELECT index, score
FROM
ai.rank(
model_id => 'semantic-ranker-default-003',
search_string => 'AlloyDB is a PostgreSQL compatible AI database that is ready for production.',
documents =>
ARRAY[
'Alloys are made from combination of metals',
'The best enterprise-ready PostgreSQL database.',
'You can feel the heat in Alloy apartments.']);
レスポンスは、各ドキュメントと、検索クエリとの関連性に基づくスコアで構成された表になります。レスポンスの例を次に示します。
index | score
-------+------------
2 | 0.33
1 | 0.28
3 | 0.16
(3 rows)
エンベディングに変換されたレビューの説明のリストを含む AlloyDB Omni データベースの例について考えてみましょう。次のサンプルコード スニペットは、ランキング モデルを使用し、クエリに対するレビューの説明の意味的類似度に基づいて、上位にランク付けされた商品の名前を取得する方法を示しています。
WITH initial_ranking AS (
SELECT product_id, name, review, review_id, ROW_NUMBER() OVER () AS ref_number
FROM user_reviews
ORDER BY
review_desc_embedding <=> google_ml.embedding(
'gemini-embedding-001', 'good desserts')::vector
LIMIT 10
), reranked_results AS (
SELECT index, score
FROM
ai.rank(
model_id => 'semantic-ranker-512',
search_string => 'good desserts',
documents => (SELECT ARRAY_AGG(review ORDER BY ref_number) FROM initial_ranking),
top_n => 5)
)
SELECT product_id, name
FROM initial_ranking, reranked_results
WHERE initial_ranking.ref_number = reranked_results.index
ORDER BY reranked_results.score DESC;