SeeDream Images Generation API 串接說明

本文將介紹一種 SeeDream Images Generation API 串接說明,它可以透過輸入自訂參數來生成 SeeDream 官方的圖片。

申請流程

要使用 SeeDream Images Generation API,首先到 Ace Data Cloud 控制台 取得您的 API Token,留作備用。

如果你尚未登入或註冊,會自動跳轉到登入頁面邀請你註冊和登入,完成後會自動返回目前頁面。

通用應用程式的 API Key 可用於帳號已開放的服務;專用應用程式的 Key 按該應用程式的服務範圍使用。請檢查 Key 的權限、有效期和限額,並確認應用程式餘額足以支付本次請求。可用試用額度以帳號顯示為準,餘額不足時點擊「儲值」按鈕即可。

📘 完整文件:SeeDream Images Generation API →

模型選擇

模型 適用情境
doubao-seedream-5-5-261028 多輪編輯、文字生成透明圖、多參考圖透明編輯、圖層拆分與文字圖層辨識
doubao-seedream-5-0-flash-260915 低延遲單圖生成、多參考圖編輯、透明圖編輯與圖層拆分
doubao-seedream-5-0-pro-260628 單圖生成、精細編輯、透明圖編輯與圖層拆分
doubao-seedream-5-0-lite-260128 單圖、組圖、串流輸出與網路搜尋
doubao-seedream-4-5-251128 / doubao-seedream-4-0-250828 舊版影像生成和編輯

5.5 和 5.0 Flash 使用相同的 POST /seedream/images,都支援 1K、1.5K、2K 和最多 10 張參考圖。兩款模型的圖層拆分任務都只接收一張圖片,返回 URL。5.5 可設定 layer_decomposition_options: {"max_images": 12, "text_recognition": true}。最多輸出 100 張圖片(包含底圖),可辨識的文字圖層會在 data[].text_recognition 返回逐行文字和樣式。5.0 Flash 最多返回 1 張底圖和 16 個圖層。

5.5 支援使用 background: "transparent" 直接生成透明 PNG。5.0 Flash 的透明圖編輯需要一張有透明通道的參考圖。5.5 按成功輸出圖片的像素級別收費,並按每張輸入參考圖加收 0.13 Credits。5.0 Flash 按每張成功輸出圖片收取 0.31 Credits,參考圖免費。圖層拆分按實際成功輸出張數計費,底圖也計入。參見 即時定價。

基本使用

首先先了解一下基本的使用方式,就是輸入提示詞 prompt、生成行為 action、圖片尺寸 size,便可獲得處理後的結果,首先需要簡單地傳遞一個 action 欄位,它的值為 generate,然後我們還需要輸入提示詞,具體內容如下:

可以看到這裡我們設定了 Request Headers,包括:

  • accept:想要接收何種格式的回應結果,這裡填寫為 application/json,即 JSON 格式。
  • authorization:呼叫 API 的金鑰,申請之後可以直接下拉選擇。

另外設定了 Request Body,包括:

  • prompt:提示詞。
  • model:請傳入上表中的完整模型 ID。
  • image:輸入圖片 URL、Base64 字串或陣列。5.5、5.0 Pro 和 5.0 Flash 最多 10 張參考圖。
  • size:解析度級別或 宽x高。5.5、5.0 Pro 和 5.0 Flash 支援 1K、1.5K、2K。5.0 Lite 支援 2K、3K、4K。
  • sequential_image_generation: 組圖:基於您輸入的內容,生成的一組內容相關的圖片。doubao-seedream-5-0-lite-260128、doubao-seedream-4-5-251128、doubao-seedream-4-0-250828 支援該參數,預設為 disabled。
  • stream: 控制是否開啟串流輸出模式。doubao-seedream-5-0-lite-260128、doubao-seedream-4-5-251128、doubao-seedream-4-0-250828 支援該參數,預設是 false。
  • response_format: 指定生成影像的返回格式。預設是 url,也支援 b64_json。
  • watermark: 是否在生成的圖片中新增浮水印。預設是 true。
  • output_format:輸出格式為 jpeg 或 png。5.5 預設 PNG,5.0 Pro/Flash/Lite 預設 JPEG。
  • tools: 設定模型要呼叫的工具,目前支援 web_search(網路搜尋)。僅 Seedream 5.0 Lite 支援。
  • optimize_prompt_options: 提示詞最佳化設定。5.0 Pro 支援 standard/fast;5.5、5.0 Flash、5.0 Lite 與 4.5 僅支援 standard;4.0 支援 standard/fast。
  • background:支援 transparent 和 opaque。5.5 另支援 auto。透明輸出使用 PNG。
  • layer_decomposition:5.5、5.0 Pro 和 5.0 Flash 支援。設為 true 時必須輸入一張 PNG/JPEG,可不傳 prompt 自動拆分,或用自然語言/<bbox> 指定元素;size 支援 auto/1K/1.5K/2K。該模式不能與組圖、串流、網路搜尋或 background 同時使用。
  • callback_url:需要回呼結果的 URL。
  • async:是否以非同步模式處理。設為 true 時介面立即返回 task_id,無須提供 callback_url,隨後透過 /seedream/tasks 輪詢取得結果。

選擇之後,可以發現右側也生成了對應程式碼,如圖所示:

點擊「Try」按鈕即可進行測試,如上圖所示,這裡我們就得到了如下結果:

{
  "success": true,
  "task_id": "80ceeed1-17d4-4eb7-82e0-18b34290f36e",
  "trace_id": "96b7fdc8-0fc8-4e2e-82a9-83c0a82f0a08",
  "data": [
    {
      "prompt": "A single matte blue cube centered on a clean white studio background, neutral lighting",
      "size": "2048x2048",
      "image_url": "https://cdn.acedata.cloud/assets/examples/seedream/db93b46e-c302-4676-8a11-63f0ba638a27-1c6f66f6b7e8.jpg"
    }
  ]
}

回傳結果一共有多個欄位,介紹如下:

  • success,此時影片生成任務的狀態情況。
  • task_id,此時影片生成任務 ID。
  • trace_id,此時影片生成追蹤 ID。
  • data,此時影像生成任務的結果列表。
    • image_url,此時圖片生成任務的連結。
    • prompt,提示詞。
    • size: 生成圖片的像素

可以看到我們得到了滿意的圖片資訊,我們只需要根據結果中 data 的圖片連結位址取得生成的 SeeDream 圖片即可。

另外如果想生成對應的串接程式碼,可以直接複製生成,例如 CURL 的程式碼如下:

curl -X POST 'https://api.acedata.cloud/seedream/images' \
-H 'accept: application/json' \
-H 'authorization: Bearer ${token}' \
-H 'content-type: application/json' \
-d '{
  "action": "generate",
  "model": "doubao-seedream-5-0-lite-260128",
  "prompt": "A single matte blue cube centered on a clean white studio background, neutral lighting"
}'

編輯圖片任務

如果想對某張圖片進行編輯的話,首先參數 image 必須傳入需要編輯的圖片連結

  • model:傳入上表中支援影像編輯的完整模型 ID。
  • image:上傳需要編輯的圖片,一張或者多張

填寫範例如下:

對應的程式碼:

import requests

url = "https://api.acedata.cloud/seedream/images"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "doubao-seedream-4-0-250828",
  "prompt": "Keep the model pose and the liquid garment flowing shape unchanged. Change the clothing material from silver metal to completely transparent water (or glass). Through the liquid flow, the details of the model skin are visible. The light and shadow effect shifts from reflection to refraction.",
  "image": ["https://ark-project.tos-cn-beijing.volces.com/doc_image/seedream4_5_imageToimage.png"],
  "size": "2K",
  "watermark": False
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

點擊執行,可以發現會立即得到一個結果,如下:

{
    "success": true,
    "task_id": "c9aaffa2-b8ac-40ff-8468-43e77cb9ddde",
    "trace_id": "131a40c3-2eaf-44c9-af28-c9b408577286",
    "data": [
        {
            "prompt": "Keep the model pose and the liquid garment flowing shape unchanged. Change the clothing material from silver metal to completely transparent water (or glass). Through the liquid flow, the details of the model skin are visible. The light and shadow effect shifts from reflection to refraction.",
            "size": "2048x2048",
            "image_url": "https://platform.cdn.acedata.cloud/seedream/3e88db7e-4771-4f6a-adbd-5ae4590c5d59.jpg"
        }
    ]
}

可以看到,生成的效果是對原圖片進行編輯的效果,結果與上文類似。

圖層拆分(Seedream 5.0 Pro)

圖層拆分會把一張輸入圖拆為 1 張底圖和最多 16 個可獨立編輯的透明 PNG 圖層。以下請求讓模型自動識別主要元素;如需指定元素,可增加 prompt,也可以在提示詞中使用正規化 <bbox> 座標。

curl -X POST 'https://api.acedata.cloud/seedream/images' \
-H 'accept: application/json' \
-H 'authorization: Bearer ${token}' \
-H 'content-type: application/json' \
-d '{
  "model": "doubao-seedream-5-0-pro-260628",
  "image": "https://example.com/poster.png",
  "layer_decomposition": true,
  "size": "2K",
  "watermark": false
}'

回傳的 data 按 z_index 從底到頂排列。底圖的 z_index 為 0;圖層還包含 name、description 和 bounding_box.absolute/normalized。使用絕對座標重組時,將圖層縮放到 [right-left, bottom-top],放到 [left, top],再按 z_index 升冪堆疊。任一圖層生成失敗時整次拆分失敗。

串流輸出

Lite/4.x 設定 stream: true 時,請求標頭使用 accept: application/x-ndjson。介面逐行回傳 image_generation.partial_succeeded 或 image_generation.partial_failed,最後回傳唯一的 image_generation.completed 事件及最終 usage;只有完成事件觸發一次計費。串流模式不能與 async 或 callback_url 同時使用。

非同步回呼

由於 SeeDream Images Generation API 生成的時間相對較長,大約需要 1-2 分鐘,如果 API 長時間無回應,HTTP 請求會一直保持連線,導致額外的系統資源消耗,所以本 API 也提供了非同步回呼的支援。

整體流程是:用戶端發起請求的時候,額外指定一個 callback_url 欄位,用戶端發起 API 請求之後,API 會立刻回傳一個結果,包含一個 task_id 的欄位資訊,代表目前的任務 ID。當任務完成之後,生成圖片的結果會透過 POST JSON 的形式傳送到用戶端指定的 callback_url,其中也包括了 task_id 欄位,這樣任務結果就可以透過 ID 關聯起來了。

如果你沒有可供回呼的公網位址,也可以不指定 callback_url,而是在請求中設定 async 欄位為 true。此時介面同樣會立即回傳 task_id,但不會推送結果,你需要攜帶該 task_id 呼叫 /seedream/tasks 介面輪詢任務狀態來取得最終結果。

下面我們透過範例來了解具體怎樣操作。

點擊執行,可以發現會立即得到一個結果,如下:

{
  "task_id": "c9aaffa2-b8ac-40ff-8468-43e77cb9ddde"
}

內容如下:

{
    "success": true,
    "task_id": "c9aaffa2-b8ac-40ff-8468-43e77cb9ddde",
    "trace_id": "131a40c3-2eaf-44c9-af28-c9b408577286",
    "data": [
        {
            "prompt": "Keep the model pose and the liquid garment flowing shape unchanged. Change the clothing material from silver metal to completely transparent water (or glass). Through the liquid flow, the details of the model skin are visible. The light and shadow effect shifts from reflection to refraction.",
            "size": "2048x2048",
            "image_url": "https://platform.cdn.acedata.cloud/seedream/3e88db7e-4771-4f6a-adbd-5ae4590c5d59.jpg"
        }
    ]
}

可以看到結果中有一個 task_id 欄位,其他欄位都和上文類似,透過該欄位即可實現任務的關聯。

錯誤處理

在呼叫 API 時,如果遇到錯誤,API 會回傳相應的錯誤代碼和資訊。例如:

  • 400 token_mismatched:請求錯誤,可能是因為參數缺失或無效。
  • 400 api_not_implemented:請求錯誤,可能是因為參數缺失或無效。
  • 401 invalid_token:未經授權,授權權杖無效或缺失。
  • 429 too_many_requests:請求過多,您已超過速率限制。
  • 500 api_error:伺服器內部錯誤,伺服器發生問題。

錯誤回應範例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

結論

透過本文件,您已經了解如何使用 SeeDream Images Generation API,透過輸入提示詞來產生圖片。希望本文件能幫助您更好地串接和使用該 API。如有任何問題,請隨時聯絡我們的技術支援團隊。