マルチモーダル データセット

Vertex AI のマルチモーダル データセットを使用すると、生成 AI 用のマルチモーダル データセットを作成、管理、共有、使用できます。マルチモーダル データセットには、次の主な機能があります。

  • BigQuery、DataFrame、Cloud Storage の JSONL ファイルからデータセットを読み込むことができます。

  • データセットを一度作成して、教師ありファインチューニングバッチ予測など、さまざまなジョブタイプで使用します。これにより、データの重複や形式の問題を防ぐことができます。

  • すべての生成 AI データセットを 1 つのマネージド ロケーションに保存します。

  • スキーマと構造を検証し、ダウンストリーム タスクに必要なリソースを定量化することで、タスクを開始する前にエラーを検出し、費用を見積もることができます。

マルチモーダル データセットは、Vertex AI SDK for Python または REST API を使用して使用できます。

マルチモーダル データセットは、Vertex AI のマネージド データセットの一種です。他のタイプのマネージド データセットとは次の点で異なります。

  • マルチモーダル データセットには、任意のモダリティ(テキスト、画像、音声、動画)のデータを含めることができます。他のタイプのマネージド データセットは、単一のモダリティ専用です。
  • マルチモーダル データセットは、Vertex AI の生成 AI サービス(生成モデルを使用したチューニングやバッチ予測など)でのみ使用できます。他のマネージド データセット タイプは、Vertex AI 予測モデルでのみ使用できます。
  • マルチモーダル データセットは、データのプレビュー、リクエストの検証、費用の見積もりに使用される assembleassess などの追加メソッドをサポートしています。
  • マルチモーダル データセットは、大規模なデータセット用に最適化された BigQuery に保存されます。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI, BigQuery, and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI, BigQuery, and Cloud Storage APIs.

    Enable the APIs

  8. Vertex AI SDK for Python をインストールして初期化する
  9. 次のライブラリをインポートします。
    from google.cloud.aiplatform.preview import datasets
    
    # To use related features, you may also need to import some of the following features:
    from vertexai.preview.tuning import sft
    from vertexai.batch_prediction import BatchPredictionJob
    
    from vertexai.generative_models import Content, Part, Tool, ToolConfig, SafetySetting, GenerationConfig, FunctionDeclaration
    
  10. データセットを作成する

    さまざまなソースからマルチモーダル dataset を作成できます。

    • Pandas DataFrame から

      my_dataset = datasets.MultimodalDataset.from_pandas(
          dataframe=my_dataframe,
          target_table_id=table_id    # optional
      )
      
    • BigQuery DataFrame から:

      my_dataset = datasets.MultimodalDataset.from_bigframes(
          dataframe=my_dataframe,
          target_table_id=table_id    # optional
      )
      
    • BigQuery テーブルから

      my_dataset_from_bigquery = datasets.MultimodalDataset.from_bigquery(
          bigquery_uri=f"bq://projectId.datasetId.tableId"
      )
      
    • BigQuery テーブルから REST API を使用して

      curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT/locations/LOCATION/datasets" \
      -d '{
        "display_name": "TestDataset",
        "metadataSchemaUri": "gs://google-cloud-aiplatform/schema/dataset/metadata/multimodal_1.0.0.yaml",
        "metadata": {
          "inputConfig": {
            "bigquery_source": {
              "uri": "bq://projectId.datasetId.tableId"
            }
          }
        }
      }'
      
    • Cloud Storage の JSONL ファイルから読み込みます。次の例では、JSONL ファイルに Gemini 用にすでにフォーマットされたリクエストが含まれているため、アセンブリは必要ありません。

      my_dataset = datasets.MultimodalDataset.from_gemini_request_jsonl(
        gcs_uri = gcs_uri_of_jsonl_file,
      )
      
    • 既存のマルチモーダル データセットから

      # Get the most recently created dataset
      first_dataset = datasets.MultimodalDataset.list()[0]
      
      # Load dataset based on its name
      same_dataset = datasets.MultimodalDataset(first_dataset.name)
      

    テンプレートを作成して適用する

    テンプレートは、マルチモーダル データセットをモデルに渡すことができる形式に変換する方法を定義します。これは、チューニング ジョブまたはバッチ予測ジョブの実行に必要です。

    Vertex AI SDK for Python

    1. テンプレートを作成します。テンプレートを作成する方法は 2 つあります。

      • construct_single_turn_template ヘルパー メソッドを使用します。
      template_config = datasets.construct_single_turn_template(
              prompt="This is the image: {image_uris}",
              response="{labels}",
              system_instruction='You are a botanical image classifier. Analyze the provided image '
                      'and determine the most accurate classification of the flower.'
                      'These are the only flower categories: [\'daisy\', \'dandelion\', \'roses\', \'sunflowers\', \'tulips\'].'
                      'Return only one category per image.'
      )
      
      • GeminiExample からテンプレートを手動で作成します。これにより、マルチターンの会話など、より細かい粒度が可能になります。次のコードサンプルには、field_mapping を指定するためのコメントアウトされたコードも含まれています。これにより、データセットの列名とは異なるプレースホルダ名を使用できます。次に例を示します。
      # Define a GeminiExample
      gemini_example = datasets.GeminiExample(
          contents=[
              Content(role="user", parts=[Part.from_text("This is the image: {image_uris}")]),
              Content(role="model", parts=[Part.from_text("This is the flower class: {label}.")]),
            Content(role="user", parts=[Part.from_text("Your response should only contain the class label.")]),
            Content(role="model", parts=[Part.from_text("{label}")]),
      
            # Optional: If you specify a field_mapping, you can use different placeholder values. For example:
            # Content(role="user", parts=[Part.from_text("This is the image: {uri_placeholder}")]),
            # Content(role="model", parts=[Part.from_text("This is the flower class: {flower_placeholder}.")]),
            # Content(role="user", parts=[Part.from_text("Your response should only contain the class label.")]),
            # Content(role="model", parts=[Part.from_text("{flower_placeholder}")]),
          ],
          system_instruction=Content(
              parts=[
                  Part.from_text(
                      'You are a botanical image classifier. Analyze the provided image '
                      'and determine the most accurate classification of the flower.'
                      'These are the only flower categories: [\'daisy\', \'dandelion\', \'roses\', \'sunflowers\', \'tulips\'].'
                      'Return only one category per image.'
                  )
              ]
          ),
      )
      
      # construct the template, specifying a map for the placeholder
      template_config = datasets.GeminiTemplateConfig(
          gemini_example=gemini_example,
      
          # Optional: Map the template placeholders to the column names of your dataset.
          # Not required if the template placesholders are column names of the dataset.
          # field_mapping={"uri_placeholder": "image_uris", "flower_placeholder": "labels"},
      )
      
    2. データセットに接続します。

      my_dataset.attach_template_config(template_config=template_config)
      

    REST

    patch メソッドを呼び出し、metadata フィールドを次のように更新します。

    • BigQuery テーブルの URI。BigQuery テーブルから作成されたデータセットの場合、これはソース bigquery_uri です。JSONL や DataFrame などの他のソースから作成されたデータセットの場合、これはデータがコピーされた BigQuery テーブルです。
    • gemini_template_config
    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d $'{
      "metadata": {
        "input_config": {
          "bigquery_source": {
            "uri": "bq://projectId.datasetId.tableId"
          }
        },
        "gemini_template_config_source": {
          "gemini_template_config": {
            "gemini_example": {
              "contents": [
                {
                  "role": "user",
                  "parts": [
                    {
                      "text": "This is the image: {image_uris}"
    
                    }
                  ]
                },
                {
                  "role": "model",
                  "parts": [
                    {
                      "text": "response"
                    }
                  ]
                }
              ]
            "systemInstruction": {
                "parts": [
                    {
                        "text": "You are a botanical image classifier."
                    }
                ]
              }
            }
          }
        }
      }
    }' \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID?updateMask=metadata"
    

    (省略可)データセットを組み立てる

    assemble メソッドは、テンプレートを適用してデータセットを変換し、出力を新しい BigQuery テーブルに保存します。これにより、モデルに渡される前にデータをプレビューできます。

    デフォルトでは、データセットに添付された template_config が使用されますが、テンプレートを指定してデフォルトの動作をオーバーライドできます。

    Vertex AI SDK for Python

    table_id, assembly = my_dataset.assemble(template_config=template_config)
    
    # Inspect the results
    assembly.head()
    

    REST

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID:assemble" \
    -d '{}'
    

    たとえば、マルチモーダル データセットに次のデータが含まれているとします。

    image_uris ラベル
    1 gs://cloud-samples-data/ai-platform/flowers/daisy/1396526833_fb867165be_n.jpg デイジー

    次に、assemble メソッドは、各行にリクエスト本文を含む table_id という名前の新しい BigQuery テーブルを作成します。次に例を示します。

    {
      "contents": [
        {
          "parts": [
            {
              "text": "This is the image: "
            },
            {
              "fileData": {
                "fileUri": "gs://cloud-samples-data/ai-platform/flowers/daisy/1396526833_fb867165be_n.jpg",
                "mimeType": "image/jpeg"
              }
            }
          ],
          "role": "user"
        },
        {
          "parts": [
            {
              "text": "daisy"
            }
          ],
          "role": "model"
        }
      ],
      "systemInstruction": {
        "parts": [
          {
            "text": "You are a botanical image classifier. Analyze the provided image and determine the most accurate classification of the flower.These are the only flower categories: ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'].Return only one category per image."
          }
        ]
      }
    }
    

    モデルをチューニングする

    マルチモーダル データセットを使用して Gemini モデルをチューニングできます。

    (省略可)データセットを検証する

    データセットを評価して、データセットの形式エラーやモデルエラーなどのエラーが含まれているかどうかを確認します。

    Vertex AI SDK for Python

    assess_tuning_validity() を呼び出します。デフォルトでは、データセットに添付された template_config が使用されますが、テンプレートを指定してデフォルトの動作をオーバーライドできます。

    # Attach template
    my_dataset.attach_template_config(template_config=template_config)
    
    # Validation for tuning
    validation = my_dataset.assess_tuning_validity(
        model_name="gemini-2.0-flash-001",
        dataset_usage="SFT_TRAINING"
    )
    
    # Inspect validation result
    validation.errors
    

    REST

    assess メソッドを呼び出し、TuningValidationAssessmentConfig を指定します。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID:assess" \
    -d '{
      "tuningValidationAssessmentConfig": {
        "modelName": "projects/PROJECT_ID/locations/LOCATION/models/gemini-2.0-flash-001",
        "datasetUsage": "SFT_TRAINING"
      }
    }'
    

    (省略可)リソース使用量を推定する

    データセットを評価して、チューニング ジョブのトークン数と課金対象文字数を取得します。

    Vertex AI SDK for Python

    assess_tuning_resources() を呼び出します。

    # Resource estimation for tuning.
    tuning_resources = my_dataset.assess_tuning_resources(
        model_name="gemini-2.0-flash-001"
    )
    
    print(tuning_resources)
    # For example, TuningResourceUsageAssessmentResult(token_count=362688, billable_character_count=122000)
    

    REST

    assess メソッドを呼び出し、TuningResourceUsageAssessmentConfig を指定します。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID:assess" \
    -d '{
      "tuningResourceUsageAssessmentConfig": {
        "modelName": "projects/PROJECT_ID/locations/LOCATION/models/gemini-2.0-flash-001"
      }
    }'
    

    チューニング ジョブを実行する

    Vertex AI SDK for Python

    from vertexai.tuning import sft
    
    sft_tuning_job = sft.train(
      source_model="gemini-2.0-flash-001",
      # Pass the Vertex Multimodal Datasets directly
      train_dataset=my_multimodal_dataset,
      validation_dataset=my_multimodal_validation_dataset,
    )
    

    Google Gen AI SDK

    from google import genai
    from google.genai.types import HttpOptions, CreateTuningJobConfig
    
    client = genai.Client(http_options=HttpOptions(api_version="v1"))
    
    tuning_job = client.tunings.tune(
      base_model="gemini-2.0-flash-001",
      # Pass the resource name of the Vertex Multimodal Dataset, not the dataset object
      training_dataset={
          "vertex_dataset_resource": my_multimodal_dataset.resource_name
      },
      # Optional
      config=CreateTuningJobConfig(
          tuned_model_display_name="Example tuning job"),
    )
    

    詳細については、チューニング ジョブを作成するをご覧ください。

    バッチ予測

    マルチモーダル データセットを使用してバッチ予測を取得できます。

    (省略可)データセットを検証する

    データセットを評価して、データセットの形式エラーやモデルエラーなどのエラーが含まれているかどうかを確認します。

    Vertex AI SDK for Python

    assess_batch_prediction_validity() を呼び出します。デフォルトでは、データセットに添付された template_config が使用されますが、テンプレートを指定してデフォルトの動作をオーバーライドできます。

    # Attach template
    my_dataset.attach_template_config(template_config=template_config)
    
    # Validation for batch prediction
    validation = my_dataset.assess_batch_prediction_validity(
        model_name="gemini-2.0-flash-001",
        dataset_usage="SFT_TRAINING"
    )
    
    # Inspect validation result
    validation.errors
    

    REST

    assess メソッドを呼び出し、batchPredictionValidationAssessmentConfig を指定します。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID:assess" \
    -d '{
      "batchPredictionValidationAssessmentConfig": {
        "modelName": "projects/PROJECT_ID/locations/LOCATION/models/gemini-2.0-flash-001",
      }
    }'
    

    (省略可)リソース使用量を推定する

    データセットを評価して、ジョブのトークン数を取得します。

    Vertex AI SDK for Python

    assess_batch_prediction_resources() を呼び出します。

    batch_prediction_resources = my_dataset.assess_batch_prediction_resources(
        model_name="gemini-2.0-flash"
    )
    
    print(batch_prediction_resources)
    # For example, BatchPredictionResourceUsageAssessmentResult(token_count=362688, audio_token_count=122000)
    

    REST

    assess メソッドを呼び出し、batchPredictionResourceUsageAssessmentConfig を指定します。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID:assess" \
    -d '{
      "batchPredictionResourceUsageAssessmentConfig": {
        "modelName": "projects/PROJECT_ID/locations/LOCATION/models/gemini-2.0-flash-001"
      }
    }'
    

    バッチ予測ジョブを実行する

    マルチモーダル データセットを使用してバッチ予測を行うには、組み立てられた出力の BigQuerytable_id を渡します。

    Vertex AI SDK for Python

    from vertexai.batch_prediction import BatchPredictionJob
    
    # Dataset needs to have an attached template_config to batch prediction
    my_dataset.attach_template_config(template_config=template_config)
    
    # assemble dataset to get assembly table id
    assembly_table_id, _ = my_dataset.assemble()
    
    batch_prediction_job = BatchPredictionJob.submit(
        source_model="gemini-2.0-flash-001",
        input_dataset=assembly_table_id,
    )
    

    Google Gen AI SDK

    from google import genai
    
    client = genai.Client(http_options=HttpOptions(api_version="v1"))
    
    # Attach template_config and assemble dataset
    my_dataset.attach_template_config(template_config=template_config)
    assembly_table_id, _ = my_dataset.assemble()
    
    job = client.batches.create(
        model="gemini-2.0-flash-001",
        src=assembly_table_id,
    )
    

    詳細については、バッチ予測ジョブをリクエストするをご覧ください。

    制限事項

    • マルチモーダル データセットは、生成 AI 機能でのみ使用できます。AutoML トレーニングカスタム トレーニングなどの非生成 AI 機能では使用できません。

    • マルチモーダル データセットは、Gemini などの Google モデルでのみ使用できます。サードパーティのモデルでは使用できません。

    料金

    モデルをチューニングしたり、バッチ予測ジョブを実行したりすると、生成 AI の使用量BigQuery でのデータセットのクエリに対して課金されます。

    マルチモーダル データセットの作成、組み立て、評価を行うと、BigQuery でのマルチモーダル データセットの保存とクエリに対して課金されます。具体的には、次のオペレーションはこれらの基盤となるサービスを使用します。

    • Create 個のデータセット

      • 既存の BigQuery テーブルまたは DataFrame から作成されたデータセットには、追加のストレージ費用は発生しません。これは、データの別のコピーを保存するのではなく、論理ビューを使用するためです。
      • 他のソースから作成されたデータセットは、データを新しい BigQuery テーブルにコピーするため、BigQuery でストレージ費用が発生します。たとえば、アクティブな論理ストレージの場合、1 GiB あたり月額 $0.02 です。
    • Assemble 個のデータセット

      • このメソッドは、モデル リクエスト形式の完全なデータセットを含む新しい BigQuery テーブルを作成します。これにより、BigQuery でストレージ費用が発生します。たとえば、アクティブな論理ストレージの場合、1 GiB あたり月額 $0.02 です。

      • このメソッドはデータセットを 1 回読み取るため、BigQuery でクエリ費用が発生します。たとえば、オンデマンド コンピューティングの料金は TiB あたり $6.25 です。データセットの検証とリソースの見積もり

    • Assess はデータセットを 1 回読み取るため、BigQuery でクエリ費用が発生します。たとえば、オンデマンド コンピューティングの料金は TiB あたり $6.25 です。

    料金計算ツールを使用すると、予想使用量に基づいて費用の見積もりを出すことができます。