在 Colab 試用 Imagen Vertex AI 的 Imagen 提供以 LLM 為基礎的提示重寫工具,也稱為提示重寫器。提示重寫工具會在提示中加入更多細節,協助您取得更高品質的輸出圖片。
如果停用提示重寫工具,可能會影響圖片品質,以及輸出內容與所提供提示的相似程度。下列模型版本預設會啟用這項功能:
-
imagen-3.0-generate-002 -
imagen-4.0-generate-001 -
imagen-4.0-fast-generate-001 -
imagen-4.0-ultra-generate-001
只有在原始提示詞少於 30 個字時,系統才會透過 API 回應提供重寫的提示詞。
使用提示詞重寫工具
如要使用提示重寫工具,請按照下列步驟操作:
控制台
在 Google Cloud 控制台中,依序前往「Vertex AI」>「Media Studio」頁面。
按一下「Imagen」。系統會顯示 Imagen Media Studio 圖像生成頁面。
在「設定」面板中調整下列選項:
模型:從可用選項中選擇模型。
如要進一步瞭解可用模型,請參閱「Imagen 模型」。
結果數量:調整滑桿或輸入介於 1 至 4 之間的值。
在「Negative prompt」(反向提示) 方塊中,輸入提示詞,說明您不希望在圖片中生成的內容。
在「輸入提示」方塊中,按一下「幫我寫」。
「增強提示」視窗隨即顯示。
在「Current prompt」(目前提示) 方塊中輸入提示,然後按一下「Enhance」(強化)。
重寫的提示詞會顯示在「強化提示詞」方塊中。您可以編輯強化提示,或直接使用。
按一下「插入」,即可使用顯示的提示。
提示詞會插入「輸入提示詞」方塊。
按一下「 生成」。
REST
如要進一步瞭解 Imagen API,請參閱下列資源:
- 方法:
endpoints.predict VisionGenerativeModelInstanceVisionGenerativeModelParamsVisionGenerativeModelResult
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- MODEL_VERSION:要使用的圖像生成模型版本。
如要進一步瞭解模型版本和功能,請參閱「模型版本」。
- LOCATION:專案的區域。例如:
us-central1、europe-west2或asia-northeast3。如需可用區域清單,請參閱「Vertex AI 的生成式 AI 位置」。使用區域 API 端點時,端點網址中的區域會決定處理要求的位置,如果資源路徑中的LOCATION與其衝突,則會遭到忽略。 - TEXT_PROMPT:文字提示詞,引導模型生成圖片。生成圖片前,系統會使用以 LLM 為基礎的提示重寫工具,為這項基本提示加入更多細節和描述性語言。
-
IMAGE_COUNT:整數,說明要生成的圖片數量。可接受的值為
1-4,預設值為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"
}
]
}