1. はじめに
この Codelab では、ベクトル検索と Vertex AI エンベディングを組み合わせて、Cloud SQL for PostgreSQL AI 統合を使用する方法について学習します。
前提条件
- Google Cloud コンソールの基本的な知識
- コマンドライン インターフェースと Cloud Shell の基本的なスキル
学習内容
- Cloud SQL for PostgreSQL インスタンスをデプロイする方法
- データベースを作成し、Cloud SQL AI インテグレーションを有効にする方法
- データベースにデータを読み込む方法
- Cloud SQL で Vertex AI エンベディング モデルを使用する方法
- Vertex AI 生成モデルを使用して結果を拡充する方法
- ベクトル インデックスを使用してパフォーマンスを改善する方法
必要なもの
- Google Cloud アカウントと Google Cloud プロジェクト
- Google Cloud コンソールと Cloud Shell をサポートするウェブブラウザ(Chrome など)
2. 設定と要件
セルフペース型の環境設定
- Google Cloud Console にログインして、プロジェクトを新規作成するか、既存のプロジェクトを再利用します。Gmail アカウントも Google Workspace アカウントもまだお持ちでない場合は、アカウントを作成してください。
- プロジェクト名は、このプロジェクトの参加者に表示される名称です。Google API では使用されない文字列です。いつでも更新できます。
- プロジェクト ID は、すべての Google Cloud プロジェクトにおいて一意でなければならず、不変です(設定後は変更できません)。Cloud コンソールでは一意の文字列が自動生成されます。通常は、この内容を意識する必要はありません。ほとんどの Codelab では、プロジェクト ID(通常は
PROJECT_ID
と識別されます)を参照する必要があります。生成された ID が好みではない場合は、ランダムに別の ID を生成できます。または、ご自身で試して、利用可能かどうかを確認することもできます。このステップ以降は変更できず、プロジェクトを通して同じ ID になります。 - なお、3 つ目の値として、一部の API が使用するプロジェクト番号があります。これら 3 つの値について詳しくは、こちらのドキュメントをご覧ください。
- 次に、Cloud のリソースや API を使用するために、Cloud コンソールで課金を有効にする必要があります。この Codelab の操作をすべて行って、費用が生じたとしても、少額です。このチュートリアルの終了後に請求が発生しないようにリソースをシャットダウンするには、作成したリソースを削除するか、プロジェクトを削除します。Google Cloud の新規ユーザーは、300 米ドル分の無料トライアル プログラムをご利用いただけます。
Cloud Shell を起動する
Google Cloud はノートパソコンからリモートで操作できますが、この Codelab では、Google Cloud Shell(Cloud 上で動作するコマンドライン環境)を使用します。
Google Cloud Console で、右上のツールバーにある Cloud Shell アイコンをクリックします。
プロビジョニングと環境への接続にはそれほど時間はかかりません。完了すると、次のように表示されます。
この仮想マシンには、必要な開発ツールがすべて用意されています。永続的なホーム ディレクトリが 5 GB 用意されており、Google Cloud で稼働します。そのため、ネットワークのパフォーマンスと認証機能が大幅に向上しています。この Codelab での作業はすべて、ブラウザ内から実行できます。インストールは不要です。
3. はじめに
API を有効にする
出力:
Cloud Shell で、プロジェクト ID が設定されていることを確認します。
gcloud config set project [YOUR-PROJECT-ID]
環境変数 PROJECT_ID を設定します。
PROJECT_ID=$(gcloud config get-value project)
必要なサービスをすべて有効にします。
gcloud services enable sqladmin.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
aiplatform.googleapis.com
想定される出力
student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417 Updated property [core/project]. student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-14650] student@cloudshell:~ (test-project-001-402417)$ student@cloudshell:~ (test-project-001-402417)$ gcloud services enable sqladmin.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ aiplatform.googleapis.com Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.
4. Cloud SQL インスタンスを作成する
Vertex AI とデータベースを統合した Cloud SQL インスタンスを作成します。
データベース パスワードを作成する
デフォルトのデータベース ユーザーのパスワードを定義します。独自のパスワードを定義することも、乱数関数を使用してパスワードを生成することもできます。
export CLOUDSQL_PASSWORD=`openssl rand -hex 12`
生成されたパスワードの値をメモします。
echo $CLOUDSQL_PASSWORD
Cloud SQL for PostgreSQL インスタンスを作成する
Cloud Shell セッションで、次のコマンドを実行します。
gcloud sql instances create my-cloudsql-instance \
--database-version=POSTGRES_16 \
--tier=db-custom-1-3840 \
--region=us-central1 \
--edition=ENTERPRISE \
--enable-google-ml-integration \
--database-flags cloudsql.enable_google_ml_integration=on
インスタンスを作成したら、インスタンスのデフォルト ユーザーのパスワードを設定し、そのパスワードで接続できるかどうかを確認する必要があります。
gcloud sql users set-password postgres \
--instance=my-cloudsql-instance \
--password=$CLOUDSQL_PASSWORD
コマンドを実行し、接続の準備ができたらプロンプトにパスワードを入力します。
gcloud sql connect my-cloudsql-instance --user=postgres
Vertex AI のインテグレーションを有効にする
Vertex AI 統合を使用できるように、内部 Cloud SQL サービス アカウントに必要な権限を付与します。
Cloud SQL 内部サービス アカウントのメールアドレスを取得して、変数としてエクスポートします。
SERVICE_ACCOUNT_EMAIL=$(gcloud sql instances describe my-cloudsql-instance --format="value(serviceAccountEmailAddress)")
echo $SERVICE_ACCOUNT_EMAIL
Cloud SQL サービス アカウントに Vertex AI へのアクセス権を付与します。
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \
--role="roles/aiplatform.user"
インスタンスの作成と構成の詳細については、こちらの Cloud SQL ドキュメントをご覧ください。
5. データベースを準備する
次に、データベースを作成してベクトルのサポートを有効にする必要があります。
データベースを作成
quickstart_db という名前のデータベースを作成します。これを行うには、PostgreSQL の psql などのコマンドライン データベース クライアント、SDK、Cloud SQL Studio などのさまざまなオプションがあります。データベースの作成とインスタンスへの接続には SDK(gcloud)を使用します。
Cloud Shell でコマンドを実行してデータベースを作成します。
gcloud sql databases create quickstart_db --instance=my-cloudsql-instance
拡張機能を有効にする
Vertex AI とベクトルを使用できるようにするには、作成したデータベースで 2 つの拡張機能を有効にする必要があります。
Cloud Shell でコマンドを実行して、作成したデータベースに接続します(パスワードを指定する必要があります)。
gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres
接続が正常に完了したら、sql セッションで次の 2 つのコマンドを実行する必要があります。
CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector CASCADE;
SQL セッションを終了します。
exit;
6. データの読み込み
次に、データベースにオブジェクトを作成し、データを読み込む必要があります。ここでは、架空の Cymbal ストアのデータを使用します。データは、一般公開の Google Storage バケットで CSV 形式で入手できます。
まず、データベースに必要なオブジェクトをすべて作成する必要があります。そのためには、すでにおなじみの gcloud sql connect コマンドと gcloud storage コマンドを使用して、スキーマ オブジェクトをダウンロードしてデータベースにインポートします。
Cloud Shell で、インスタンスの作成時にメモしたパスワードを指定します。
gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres
前のコマンドで具体的に何を行ったのでしょうか。データベースに接続し、ダウンロードした SQL コードを実行して、テーブル、インデックス、シーケンスを作成しました。
次のステップはデータを読み込むことです。そのためには、Google Cloud Storage から CSV ファイルをダウンロードする必要があります。
gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv .
gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv .
gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv .
次に、データベースに接続する必要があります。
gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres
CSV ファイルからデータをインポートします。
\copy cymbal_products from 'cymbal_products.csv' csv header
\copy cymbal_inventory from 'cymbal_inventory.csv' csv header
\copy cymbal_stores from 'cymbal_stores.csv' csv header
SQL セッションを終了します。
exit;
独自のデータがあり、CSV ファイルが Cloud コンソールから利用可能な Cloud SQL インポート ツールと互換性がある場合は、コマンドライン アプローチの代わりにこのツールを使用できます。
7. エンベディングを作成する
次のステップでは、Google Vertex AI の textembedding-004 モデルを使用して商品の説明のエンベディングを作成し、ベクトルデータとして保存します。
データベースに接続します。
gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres
エンベディング関数を使用して、cymbal_products テーブルに仮想列のエンベディングを作成します。
ALTER TABLE cymbal_products ADD COLUMN embedding vector(768) GENERATED ALWAYS AS (embedding('text-embedding-004',product_description)) STORED;
時間はかかる場合がありますが、900 ~ 1,000 行の場合は 5 分以内で完了します。通常はもっと早く完了します。
8. 類似検索を実行する
これで、説明に対して計算されたベクトル値と、リクエストで取得したベクトル値に基づいて、類似性検索を使用して検索を実行できるようになりました。
SQL クエリは、gcloud sql connect を使用して同じコマンドライン インターフェースから実行できます。または、Cloud SQL Studio から実行することもできます。マルチ行クエリや複雑なクエリは、Cloud SQL Studio で管理することをおすすめします。
Cloud SQL Studio を起動する
コンソールで、先ほど作成した Cloud SQL インスタンスをクリックします。
開くと、右側のパネルに Cloud SQL Studio が表示されます。それをクリックします。
データベース名と認証情報を指定するダイアログが開きます。
- データベース: quickstart_db
- ユーザー: postgres
- Password: メイン データベース ユーザーのメモしたパスワード
[AUTHENTICATE] ボタンをクリックします。
次のウィンドウが開きます。右側の [Editor] タブをクリックして SQL エディタを開きます。
これで、クエリを実行する準備が整いました。
クエリを実行
クエリを実行して、クライアントのリクエストに最も関連性の高い利用可能なプロダクトのリストを取得します。ベクトル値を取得するために Vertex AI に渡すリクエストは、「この地域でよく育つ果樹は何ですか?」というようなものです。
リクエストに最も適した最初の 10 個のアイテムを選択するために実行できるクエリは次のとおりです。
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 10;
クエリをコピーして Cloud SQL Studio エディタに貼り付け、[実行] ボタンをクリックするか、quickstart_db データベースに接続しているコマンドライン セッションに貼り付けます。
クエリに一致する商品のリストです。
product_name | description | sale_price | zip_code | distance -------------------------+----------------------------------------------------------------------------------+------------+----------+--------------------- Cherry Tree | This is a beautiful cherry tree that will produce delicious cherries. It is an d | 75.00 | 93230 | 0.43922018972266397 Meyer Lemon Tree | Meyer Lemon trees are California's favorite lemon tree! Grow your own lemons by | 34 | 93230 | 0.4685112926118228 Toyon | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e | 10.00 | 93230 | 0.4835677149651668 California Lilac | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d | 5.00 | 93230 | 0.4947204525907498 California Peppertree | This is a beautiful peppertree that can grow to be over 30 feet tall. It is an e | 25.00 | 93230 | 0.5054166905547247 California Black Walnut | This is a beautiful walnut tree that can grow to be over 80 feet tall. It is a d | 100.00 | 93230 | 0.5084219510932597 California Sycamore | This is a beautiful sycamore tree that can grow to be over 100 feet tall. It is | 300.00 | 93230 | 0.5140519790508755 Coast Live Oak | This is a beautiful oak tree that can grow to be over 100 feet tall. It is an ev | 500.00 | 93230 | 0.5143126438081371 Fremont Cottonwood | This is a beautiful cottonwood tree that can grow to be over 100 feet tall. It i | 200.00 | 93230 | 0.5174774727252058 Madrone | This is a beautiful madrona tree that can grow to be over 80 feet tall. It is an | 50.00 | 93230 | 0.5227400803389093 (10 rows)
9. 取得したデータを使用して LLM のレスポンスを改善する
実行されたクエリの結果を使用して、クライアント アプリケーションに対する生成 AI LLM のレスポンスを改善し、Vertex AI 生成基盤言語モデルへのプロンプトの一部として提供されたクエリ結果を使用して有意な出力を準備できます。
これを実現するには、ベクトル検索の結果を含む JSON を生成してから、生成された JSON を Vertex AI の LLM モデルのプロンプトに追加して、有意な出力を作成する必要があります。最初のステップでは JSON を生成してから Vertex AI Studio でテストし、最後のステップではアプリケーションで使用できる SQL ステートメントに組み込みます。
JSON 形式で出力を生成する
クエリを変更して、出力を JSON 形式で生成し、Vertex AI に渡す行を 1 つだけ返します。
Cloud SQL for PostgreSQL
クエリの例を次に示します。
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
出力に期待される JSON は次のとおりです。
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
Vertex AI Studio でプロンプトを実行する
生成された JSON を使用して、Vertex AI Studio の生成 AI テキストモデルにプロンプトの一部として提供できます。
Cloud コンソールで Vertex AI Studio Chat を開きます。
追加の API を有効にするよう求められる場合があります。このリクエストは無視できます。ラボを完了するために追加の API は必要ありません。
使用するプロンプトは次のとおりです。
You are a friendly advisor helping to find a product based on the customer's needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","description":"some description","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
[place your JSON here]
The customer asked "What tree is growing the best here?"
You should give information about the product, price and some supplemental information.
Do not ask any additional questions and assume location based on the zip code provided in the list of products.
JSON プレースホルダをクエリからのレスポンスに置き換えると、次のように表示されます。
You are a friendly advisor helping to find a product based on the customer's needs. Based on the client request we have loaded a list of products closely related to search. The list in JSON format with list of values like {"product_name":"name","description":"some description","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"} Here is the list of products: [{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}] The customer asked "What tree is growing the best here?" You should give information about the product, price and some supplemental information. Do not ask any additional questions and assume location based on the zip code provided in the list of products.
JSON 値を使用して gemini-2.0-flash モデルでプロンプトを実行した場合の結果は次のとおりです。
この例でモデルから得られた回答は次のとおりです。モデルとパラメータは時間の経過とともに変化するため、結果が異なる場合があります。
「承知いたしました。ご利用いただける商品リストに基づいて、お客様の地域で育ちやすい樹木としては、桜の木が最適です。
価格は $75.00 です。
お客様の郵便番号(93230)の栽培条件について具体的な詳細は把握していませんが、一般的にサクラは、温暖な気候で水はけの良い土壌の地域でよく育つとされています。通常、果実を結実させるには冬の寒さが一定期間必要となるため、この点に注意が必要です。ただし、条件が整えば、美しさだけでなく美味しいチェリーも収穫できるため、庭に植えておくとよいでしょう。」
PSQL でプロンプトを実行する
また、Cloud SQL AI と Vertex AI のインテグレーションを使用して、データベース内で SQL を直接使用して生成モデルから類似のレスポンスを取得することもできます。ただし、gemini-2.0-flash-exp モデルを使用するには、まずモデルを登録する必要があります。
Cloud SQL for PostgreSQL で実行する
拡張機能をバージョン 1.4.2 以降にアップグレードします(現在のバージョンがそれより前の場合)。前述のように gcloud sql connect から quickstart_db データベースに接続するか(または Cloud SQL Studio を使用)、次のコマンドを実行します。
SELECT extversion from pg_extension where extname='google_ml_integration';
返された値が 1.4.2 未満の場合は、次のコマンドを実行します。
ALTER EXTENSION google_ml_integration UPDATE TO '1.4.2';
次に、google_ml_integration.enable_model_support データベース フラグを「on」に設定する必要があります。これを行うには、ウェブ コンソール インターフェースを使用するか、次の gcloud コマンドを実行します。
gcloud sql instances patch my-cloudsql-instance \
--database-flags google_ml_integration.enable_model_support=on,cloudsql.enable_google_ml_integration=on
このコマンドがバックグラウンドで実行されるまで 1 ~ 3 分ほどかかります。その後、psql セッションで新しいフラグを検証するか、Cloud SQL Studio を使用して quickstart_db データベースに接続します。
show google_ml_integration.enable_model_support;
psql セッションからの想定される出力は「on」です。
quickstart_db => show google_ml_integration.enable_model_support; google_ml_integration.enable_model_support -------------------------------------------- on (1 row)
次に、2 つのモデルを登録する必要があります。1 つ目は、すでに使用されている text-embedding-004 モデルです。モデル登録機能を有効にしたため、登録する必要があります。
psql または Cloud SQL Studio でモデル実行を登録するには、次のコードを使用します。
CALL
google_ml.create_model(
model_id => 'text-embedding-004',
model_provider => 'google',
model_qualified_name => 'text-embedding-004',
model_type => 'text_embedding',
model_auth_type => 'cloudsql_service_agent_iam',
model_in_transform_fn => 'google_ml.vertexai_text_embedding_input_transform',
model_out_transform_fn => 'google_ml.vertexai_text_embedding_output_transform');
次に登録するモデルは gemini-2.0-flash-001 で、ユーザー フレンドリーな出力の生成に使用されます。
CALL
google_ml.create_model(
model_id => 'gemini-2.0-flash-001',
model_request_url => 'https://us-central1-aiplatform.googleapis.com/v1/projects/$PROJECT_ID/locations/us-central1/publishers/google/models/gemini-2.0-flash-001:streamGenerateContent',
model_provider => 'google',
model_auth_type => 'cloudsql_service_agent_iam');
登録済みのモデルのリストは、google_ml.model_info_view から情報を選択することでいつでも確認できます。
select model_id,model_type from google_ml.model_info_view;
出力例を次に示します。
quickstart_db=> select model_id,model_type from google_ml.model_info_view; model_id | model_type --------------------------------------+---------------- textembedding-gecko | text_embedding textembedding-gecko@001 | text_embedding gemini-1.5-pro:streamGenerateContent | generic gemini-1.5-pro:generateContent | generic gemini-1.0-pro:generateContent | generic text-embedding-004 | text_embedding gemini-2.0-flash-001 | generic
これで、サブクエリで生成された JSON を使用して、SQL を使用して生成 AI テキストモデルにプロンプトの一部として提供できます。
データベースへの psql または Cloud SQL Studio セッションでクエリを実行します。
WITH trees AS (
SELECT
cp.product_name,
cp.product_description AS description,
cp.sale_price,
cs.zip_code,
cp.uniq_id AS product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci ON
ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON
cs.store_id = ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> google_ml.embedding('text-embedding-004',
'What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1),
prompt AS (
SELECT
'You are a friendly advisor helping to find a product based on the customer''s needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","product_description":"some description","sale_price":10}
Here is the list of products:' || json_agg(trees) || 'The customer asked "What kind of fruit trees grow well here?"
You should give information about the product, price and some supplemental information' AS prompt_text
FROM
trees),
response AS (
SELECT
json_array_elements(google_ml.predict_row( model_id =>'gemini-2.0-flash-001',
request_body => json_build_object('contents',
json_build_object('role',
'user',
'parts',
json_build_object('text',
prompt_text)))))->'candidates'->0->'content'->'parts'->0->'text' AS resp
FROM
prompt)
SELECT
string_agg(resp::text,
' ')
FROM
response;
想定される出力は次のとおりです。出力は、モデルのバージョンとパラメータによって異なる場合があります。
"That" "'s a great question! Based on your location (assuming you're" " in zip code 93230), I have a suggestion for a" " fruit tree that should thrive.\n\nWe have the **Cherry Tree** available.\n\n**Product Name:** Cherry Tree\n\n**Description:** This is a beautiful cherry" " tree that will produce delicious cherries. It's a deciduous tree (meaning it loses its leaves in the fall) growing to about 15 feet tall." " The leaves are dark green in summer, turning a beautiful red in the fall. Cherry trees are known for their beauty, shade, and privacy.\n\n**Sale Price:** $75.00\n\n**Important Considerations for Growing" " Cherry Trees:**\n\n* **Climate:** Cherry trees prefer a cool, moist climate, and 93230 falls within a suitable range (USDA zones 4-9). However, it's always a good idea to" " check the specific microclimate of your property (sun exposure, drainage etc.).\n* **Soil:** They do best in sandy soil. If your soil is different, you may need to amend it to improve drainage.\n* **Pollination:** Many cherry varieties require a second, compatible cherry tree for proper pollination" ". Check the specific pollination needs of this variety before purchase if you want a significant cherry yield.\n\nThis cherry tree is a beautiful addition to any yard and will provide you with delicious cherries if you can meet its needs. Would you like to know more about its pollination requirements, or perhaps see if we have any other" " fruit trees suitable for your area?\n" ""
10. 最近傍インデックスを作成する
データセットは非常に小さく、レスポンス時間は主に AI モデルとのやり取りに依存します。ただし、数百万ものベクトルがある場合、ベクトル検索にレスポンス時間の大部分が費やされ、システムに大きな負荷がかかります。これを改善するには、ベクトルの上にインデックスを構築します。
HNSW インデックスを作成する
テストでは HNSW インデックス タイプを試します。HNSW は Hierarchical Navigable Small World の略で、マルチレイヤ グラフ インデックスを表します。
エンベディング列のインデックスを作成するには、エンベディング列、距離関数、必要に応じて m や ef_constructions などのパラメータを定義する必要があります。パラメータの詳細については、ドキュメントをご覧ください。
CREATE INDEX cymbal_products_embeddings_hnsw ON cymbal_products
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
予想される出力:
quickstart_db=> CREATE INDEX cymbal_products_embeddings_hnsw ON cymbal_products USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64); CREATE INDEX quickstart_db=>
レスポンスの比較
これで、EXPLAIN モードでベクトル検索クエリを実行し、インデックスが使用されたかどうかを確認できます。
EXPLAIN (analyze)
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
予想される出力:
Aggregate (cost=779.12..779.13 rows=1 width=32) (actual time=1.066..1.069 rows=1 loops=1) -> Subquery Scan on trees (cost=769.05..779.12 rows=1 width=142) (actual time=1.038..1.041 rows=1 loops=1) -> Limit (cost=769.05..779.11 rows=1 width=158) (actual time=1.022..1.024 rows=1 loops=1) -> Nested Loop (cost=769.05..9339.69 rows=852 width=158) (actual time=1.020..1.021 rows=1 loops=1) -> Nested Loop (cost=768.77..9316.48 rows=852 width=945) (actual time=0.858..0.859 rows=1 loops=1) -> Index Scan using cymbal_products_embeddings_hnsw on cymbal_products cp (cost=768.34..2572.47 rows=941 width=941) (actual time=0.532..0.539 rows=3 loops=1) Order By: (embedding <=> '[0.008864171,0.03693164,-0.024245683,... <redacted> ...,0.017593635,-0.040275685,-0.03914233,-0.018452475,0.00826032,-0.07372604 ]'::vector) -> Index Scan using product_inventory_pkey on cymbal_inventory ci (cost=0.42..7.17 rows=1 width=37) (actual time=0.104..0.104 rows=0 loops=3) Index Cond: ((store_id = 1583) AND (uniq_id = (cp.uniq_id)::text)) Filter: (inventory > 0) Rows Removed by Filter: 1 -> Materialize (cost=0.28..8.31 rows=1 width=8) (actual time=0.133..0.134 rows=1 loops=1) -> Index Scan using product_stores_pkey on cymbal_stores cs (cost=0.28..8.30 rows=1 width=8) (actual time=0.129..0.129 rows=1 loops=1) Index Cond: (store_id = 1583) Planning Time: 112.398 ms Execution Time: 1.221 ms
出力から、クエリで「cymbal_products_embeddings_hnsw を使用したインデックス スキャン」が使用されていることが明確にわかります。
explain なしでクエリを実行すると、次のように表示されます。
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
予想される出力:
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
結果は同じで、インデックスなしの検索で上位に表示された同じ桜の木が返されます。パラメータとインデックスの種類によっては、結果が若干異なる場合があります。テストでは、インデックス登録されたクエリの結果が 131.301 ms で、インデックスなしの場合の 167.631 ms と比較して短縮されましたが、非常に小さなデータセットを扱っていたため、より大きなデータでは差がさらに大きくなります。
ベクトル用に利用可能なさまざまなインデックスを試すことができます。また、ドキュメントで、Langchain 統合を使用したラボや例をさらに確認できます。
11. 環境をクリーンアップする
Cloud SQL インスタンスを削除します。
ラボの終了時に Cloud SQL インスタンスを破棄する
接続が切断され、以前の設定がすべて失われた場合は、Cloud Shell でプロジェクトと環境変数を定義します。
export INSTANCE_NAME=my-cloudsql-instance
export PROJECT_ID=$(gcloud config get-value project)
インスタンスを削除します。
gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID
想定されるコンソール出力:
student@cloudshell:~$ gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID All of the instance data will be lost when the instance is deleted. Do you want to continue (Y/n)? y Deleting Cloud SQL instance...done. Deleted [https://sandbox.googleapis.com/v1beta4/projects/test-project-001-402417/instances/my-cloudsql-instance].
12. 完了
以上で、この Codelab は完了です。
学習した内容
- Cloud SQL for PostgreSQL インスタンスをデプロイする方法
- データベースを作成し、Cloud SQL AI インテグレーションを有効にする方法
- データベースにデータを読み込む方法
- Cloud SQL で Vertex AI エンベディング モデルを使用する方法
- Vertex AI 生成モデルを使用して結果を拡充する方法
- ベクトル インデックスを使用してパフォーマンスを改善する方法
HNSW ではなく ScaNN インデックスを使用する AlloyDB の Codelab を試す
13. アンケート
出力: