Tạo hình ảnh
Sử dụng CometAPI POST /v1/images/generations để tạo hình ảnh bằng các mô hình hình ảnh tương thích OpenAI và các tùy chọn điều khiển dành riêng cho mô hình.
Tài liệu tham khảo chính thức
- Đọc hướng dẫn tạo hình ảnh của OpenAI trước khi bạn dựa vào các tùy chọn điều khiển dành riêng cho mô hình như
background,output_compression, streaming hoặc các tùy chọn hình ảnh GPT trong tương lai. - Sử dụng tài liệu tham khảo Create image của OpenAI để biết danh sách tham số hiện tại.
Chọn mô hình trước
- Sử dụng một mô hình hình ảnh GPT như
gpt-image-2cho các yêu cầu chuyển văn bản thành hình ảnh với các tùy chọn điều khiển nhưoutput_format,qualityhoặcbackground - Sử dụng
gpt-image-2.5-sunbursthoặcgpt-image-2.5-flarekhi bạn cần nền trong suốt hoặc các mức chất lượngxhighvàmax - Sử dụng
qwen-imagekhi bạn cần chính nhà cung cấp đó, nhưng giữnở mức 1 - Chọn một ID mô hình hình ảnh hiện tại từ trang Models
Yêu cầu đầu tiên an toàn
- Bắt đầu với
gpt-image-2 - Giữ
sizeở1024x1024 - Các mô hình hình ảnh GPT trả về dữ liệu hình ảnh được mã hóa base64 trong
b64_json; hãy giải mã dữ liệu đó để lưu tệp hình ảnh - Chỉ thêm
output_formatkhi bạn cần một loại hình ảnh được mã hóa cụ thể nhưjpeg - Hãy dùng một prompt và một hình ảnh đầu ra trước khi thêm tính năng tạo hàng loạt hoặc tinh chỉnh phong cách
Chạy tác vụ hình ảnh bất đồng bộ
Sử dụngasync: true cho các tác vụ hình ảnh chạy lâu khi ứng dụng khách của bạn ưu tiên luồng gửi rồi thăm dò thay vì giữ một kết nối HTTP mở. Yêu cầu tạo trả về data.task_id.
Thăm dò Truy xuất một tác vụ tạo hình ảnh cho đến khi data.status là success hoặc failure.
Trường async là một phần mở rộng của CometAPI cho route này, không phải tham số OpenAI. OpenAI ghi nhận stream và partial_images cho các mô hình hình ảnh GPT. Chế độ tác vụ bất đồng bộ của CometAPI trả về siêu dữ liệu tác vụ JSON và sử dụng cơ chế thăm dò.
Sử dụng chế độ tác vụ bất đồng bộ với các ID mô hình đã được ghi nhận sau: gpt-image-2 và doubao-seedream-4-0-250828. Với các mô hình hình ảnh khác, hãy sử dụng tạo đồng bộ hoặc streaming, trừ khi mô hình đó có tài liệu về hỗ trợ tác vụ bất đồng bộ.
Khi một yêu cầu bao gồm cả async: true và stream: true, chế độ tác vụ bất đồng bộ được ưu tiên. Yêu cầu tạo trả về siêu dữ liệu tác vụ JSON thay vì luồng SSE.
Hành vi yêu cầu dành riêng cho mô hình
response_formatchỉ áp dụng cho các mô hình DALL·E; các mô hình hình ảnh GPT trả về dữ liệu base64 và bỏ qua tham số này- Các mô hình hình ảnh GPT sử dụng các tùy chọn điều khiển GPT-only như
output_format,quality,backgroundvàoutput_compression qualitychấp nhậnlow,medium,highvàautotrên các mô hình hình ảnh GPT;gpt-image-2.5-sunburstvàgpt-image-2.5-flarecũng chấp nhậnxhighvàmaxoutput_compressionáp dụng khioutput_formatlàwebphoặcjpeg; không có tác dụng vớipngpartial_imageschỉ áp dụng khistreamlàtrue- Hãy xem hướng dẫn tạo hình ảnh của OpenAI để biết các tùy chọn dành riêng cho mô hình mới nhất
qwen-imagekhông hỗ trợn > 1
Tạo nền trong suốt
Đặtbackground thành transparent để tạo một chủ thể tách biệt không có nền tô. Tính năng này được hỗ trợ trên gpt-image-2.5-sunburst và gpt-image-2.5-flare.
Tính trong suốt cần định dạng đầu ra có kênh alpha. Đặt output_format thành png hoặc webp. JPEG không có kênh alpha, vì vậy yêu cầu trong suốt với output_format: "jpeg" sẽ bị từ chối.
Phản hồi trả về dữ liệu hình ảnh base64 trong b64_json kèm kênh alpha. Giải mã dữ liệu đó để lưu tệp:
background thành opaque để buộc dùng nền đặc, hoặc auto để mô hình tự quyết định. Phản hồi lặp lại giá trị đã áp dụng trong trường background ở cấp cao nhất.
Ủy quyền
Bearer token authentication. Use your CometAPI key.
Nội dung
The image generation model to use. Choose a current model from the Models page.
Text description of the image you want to generate.
"A paper boat floating on calm water at sunrise."
Number of images to generate. Keep this at 1 for the broadest compatibility.
Quality setting for models that support it. GPT image models accept low, medium, high, and auto. gpt-image-2.5-sunburst and gpt-image-2.5-flare also accept xhigh and max. dall-e-3 accepts standard and hd. See the OpenAI image generation guide for the latest model-specific values.
"low"
Background mode for the generated image. Set transparent to generate an isolated subject with no background fill; this requires output_format set to png or webp, and returns an error with jpeg. Set opaque for a solid background, or auto to let the model decide. Supported on gpt-image-2.5-sunburst and gpt-image-2.5-flare.
transparent, opaque, auto Compression level for the output image, from 0 to 100. Applies when output_format is webp or jpeg. Lower values produce smaller files with more compression artifacts.
0 <= x <= 100Content moderation level for GPT image models. low is less restrictive; auto is the default.
low, auto Number of partial images to emit while a streaming response is in progress, from 0 to 3. Each partial image arrives as an image_generation.partial_image event before the final image_generation.completed event. Applies when stream is true.
0 <= x <= 3Requested output size. Supported values depend on the selected model. See the OpenAI image generation guide for the latest model-specific ranges.
"1024x1024"
The response container for dall-e-2 and dall-e-3. This parameter is not supported for GPT image models, which return base64-encoded image data.
url, b64_json The encoded image type for GPT image model results, such as png, jpeg, or webp. See the OpenAI image generation guide for current GPT image output controls.
"jpeg"
Set this to true to receive server-sent image generation events instead of waiting for the completed JSON response. Streaming responses use text/event-stream and can include final events such as image_generation.completed. When stream and async are both true, async task mode takes precedence and the create request returns JSON instead of a streaming image response.
CometAPI asynchronous task mode. Set this to true to return immediately with data.task_id, then poll GET /v1/images/generations/{task_id} for the final image data. Documented model IDs for this mode: gpt-image-2 and doubao-seedream-4-0-250828. This is a CometAPI extension, not an OpenAI parameter. When async and stream are both true, async takes precedence and returns JSON task metadata instead of an SSE stream.
Phản hồi
Image generation result. Synchronous requests return completed image data. Async requests return a task response with data.task_id.
- Completed image response
- Async task response
Unix timestamp for the completed generation.
Background mode returned by models that expose it.
Encoded image type returned by GPT image models.
Quality level returned by models that expose it.
Output size returned by models that expose it.
Token usage details when returned by the selected model.