使用提示重寫工具

試用圖片生成功能 (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」頁面。

    前往媒體工作室

  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 方法和網址:

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 編碼產生的圖片位元組。

{
  "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"
    }
  ]
}

後續步驟