Tạo một hình ảnh
Sử dụng CometAPI POST /v1/images/generations để tạo hình ảnh với các model hình ảnh tương thích OpenAI và các tùy chọn điều khiển theo từng model.
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 sử dụng các tùy chọn điều khiển theo từng model 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 OpenAI Create image để xem danh sách tham số hiện tại.
Chọn model trước
- Sử dụng model hình ảnh GPT như
gpt-image-2cho các request text-to-image với các tùy chọn điều khiển nhưoutput_format,quality, hoặcbackground - Sử dụng
qwen-imagekhi bạn cần đúng nhà cung cấp đó, nhưng giữnở mức 1 - Chọn một model ID hình ảnh hiện có từ trang Models
Request đầu tiên an toàn
- Bắt đầu với
gpt-image-2 - Giữ
sizeở1024x1024 - Model hình ảnh GPT trả về dữ liệu hình ảnh 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 kiểu hình ảnh được mã hóa cụ thể nhưjpeg - Kiểm tra hướng dẫn tạo hình ảnh của OpenAI trước khi thêm
background,output_compression, hoặc streaming - Sử dụng một prompt và một hình ảnh đầu ra trước khi thêm batch generation hoặc tinh chỉnh phong cách
Chạy một tác vụ hình ảnh async
Sử dụngasync: true cho các tác vụ hình ảnh chạy lâu khi client của bạn ưu tiên luồng gửi rồi polling thay vì giữ một kết nối HTTP mở.
Request tạo sẽ trả về data.task_id.
Polling 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 là tham số OpenAI. OpenAI ghi nhận stream và partial_images cho các model hình ảnh GPT. Chế độ tác vụ bất đồng bộ của CometAPI trả về metadata tác vụ ở dạng JSON và sử dụng polling.
Sử dụng chế độ tác vụ async với các model ID đã được ghi nhận sau: gpt-image-2 và doubao-seedream-4-0-250828. Với các model hình ảnh khác, hãy dùng tạo đồng bộ hoặc streaming trừ khi hỗ trợ tác vụ async được ghi nhận cho model đó.
Khi một request bao gồm cả async: true và stream: true, chế độ tác vụ async sẽ được ưu tiên. Request tạo sẽ trả về metadata tác vụ JSON thay vì một luồng SSE.
Hành vi request theo từng model
response_formatchỉ áp dụng cho các model DALL·E; các model hình ảnh GPT trả về dữ liệu base64 và bỏ qua tham số này- Các model hình ảnh GPT sử dụng các tùy chọn điều khiển chỉ dành cho GPT như
output_format,quality,background, vàoutput_compression - Làm theo hướng dẫn tạo hình ảnh của OpenAI để biết các tùy chọn theo từng model mới nhất
qwen-imagekhông hỗ trợn > 1
Ủ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. See the OpenAI image generation guide for the latest model-specific values.
Requested 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.