プロンプト書き換えツールを使用する

画像生成を試す(Vertex AI Studio)

Colab で Imagen を試す

Vertex AI の Imagen には、プロンプト書き換えツールとも呼ばれる LLM ベースのプロンプト書き換えツールが用意されています。プロンプト リライターは、プロンプトに詳細を追加して、より高品質な出力画像を取得するのに役立ちます。

プロンプト リライターを無効にすると、画像の品質と、出力が指定したプロンプトにどれだけ似ているかに影響する可能性があります。この機能は、次のモデル バージョンでデフォルトで有効になっています。

  • imagen-4.0-generate-001
  • imagen-4.0-fast-generate-001
  • imagen-4.0-ultra-generate-001
  • imagen-3.0-generate-002

書き換えられたプロンプトは、元のプロンプトの長さが 30 語未満の場合にのみ、API レスポンスによって返されます。

プロンプトの書き換えツールを使用する

プロンプト リライターを使用する手順は次のとおりです。

コンソール

  1. Google Cloud コンソールで、[Vertex AI] > [Media Studio] ページに移動します。

    Media Studio に移動

  2. [Imagen] をクリックします。Imagen Media Studio の画像生成ページが表示されます。

  3. [設定] パネルで、次のオプションを調整します。

    • モデル: 使用可能なオプションからモデルを選択します。

      使用可能なモデルの詳細については、Imagen モデルをご覧ください。

    • 検索結果の表示件数: スライダーを調整するか、14 の値を入力します。

    • [ネガティブ プロンプト] ボックスに、画像に生成したくない内容を説明するプロンプトを入力します。

  4. [プロンプトを入力] ボックスで、[文書作成サポート] をクリックします。

    [プロンプトの改善] ウィンドウが表示されます。

  5. [現在のプロンプト] ボックスにプロンプトを入力し、[強化] をクリックします。

    書き換えられたプロンプトが [強化されたプロンプト] ボックスに表示されます。拡張プロンプトを編集することも、表示されたまま使用することもできます。

  6. [挿入] をクリックして、表示されたプロンプトを使用します。

    プロンプトが [プロンプトを入力] ボックスに挿入されます。

  7. [生成] をクリックします。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際の Google Cloud プロジェクト ID
  • MODEL_VERSION: 使用する画像生成モデルのバージョン。

    モデルのバージョンと機能の詳細については、モデルのバージョンをご覧ください。

  • LOCATION: プロジェクトのリージョン。たとえば、us-central1europe-west2asia-northeast3 です。使用可能なリージョンの一覧については、Vertex AI の生成 AI のロケーションをご覧ください。
  • TEXT_PROMPT: 生成する画像をモデルに指示するテキスト プロンプト。画像が生成される前に、このベース プロンプトは、LLM ベースのプロンプト書き換えツールを使用して、より詳細で記述的な言語で拡張されます。
  • IMAGE_COUNT: 生成する画像の数を表す整数。指定できる値は 14 です。デフォルト値は 4 です。
  • PROMPT_SETTING: ブール値。true は拡張プロンプトを有効にし、false は拡張プロンプトを無効にします。デフォルト値は true です。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict

リクエストの本文(JSON):

{
  "instances": [
    {
      "prompt": "TEXT_PROMPT"
    }
  ],
  "parameters": {
    "sampleCount": IMAGE_COUNT,
    "enhancePrompt": PROMPT_SETTING
  }
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict"

PowerShell

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict" | Select-Object -Expand Content
プロンプトの機能強化が有効になっている場合、レスポンスには、補正されたプロンプトとそれに関連付けられた生成画像を示す prompt フィールドが追加されます。
  {
    "predictions": [
      {
        "mimeType": "MIME_TYPE",
        "prompt": "ENHANCED_PROMPT_1",
        "bytesBase64Encoded": "BASE64_IMG_BYTES_1"
      },
      {
        "mimeType": "MIME_TYPE",
        "prompt": "ENHANCED_PROMPT_2",
        "bytesBase64Encoded": "BASE64_IMG_BYTES_2"
      }
    ]
  }

たとえば、次のサンプル レスポンスは、"sampleCount": 2"prompt": "A raccoon wearing formal clothes, wearing a top hat. Oil painting in the style of Vincent Van Gogh." を含むリクエストに対するものです。レスポンスは、それぞれ機能強化済みプロンプトと生成された画像バイトを base64 でエンコードした 2 つの予測オブジェクトを返します。

{
  "predictions": [
    {
      "mimeType": "image/png",
      "prompt": "An oil painting in the style of Vincent van Gogh,
        depicting a raccoon adorned in a finely tailored tuxedo, complete with a
        crisp white shirt and a bow tie. The raccoon also sports a classic top
        hat, perched jauntily on its head. The painting uses thick, swirling
        brushstrokes characteristic of van Gogh, with vibrant hues of blue,
        yellow, and green in the background, contrasting with the dark tones of
        the raccoon's attire. The light source is subtly placed, casting a
        dramatic shadow of the raccoon's attire onto the surface it sits upon,
        further enhancing the depth and dimensionality of the composition. The
        overall impression is one of a whimsical and sophisticated character, a
        raccoon elevated to a higher class through its formal attire, rendered
        in van Gogh's iconic style.",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "mimeType": "image/png",
      "prompt": "An oil painting in the style of Vincent van Gogh featuring
        a raccoon in a dapper suit, complete with a black jacket, crisp white
        shirt, and a black bow tie. The raccoon is wearing a black top hat,
        adding a touch of elegance to its ensemble. The painting is rendered
        with characteristic van Gogh brushwork, utilizing thick, impasto strokes
        of color. The background is a swirl of blues, greens, and yellows,
        creating a vibrant yet slightly chaotic atmosphere that contrasts with
        the raccoon's formal attire. The lighting is dramatic, casting sharp
        shadows and highlighting the textures of the fabric and the raccoon's
        fur, enhancing the sense of realism within the fantastical scene. The
        composition focuses on the raccoon's proud posture, highlighting the
        whimsical contrast of a wild animal dressed in formal attire, captured
        in the unique artistic language of van Gogh. ",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    }
  ]
}

次のステップ