Chuyển đến nội dung chính

Nội dung yêu cầu

model
string
mặc định:"dall-e-3"
Model sẽ sử dụng (ví dụ: dall-e-3, flux-pro, midjourney).
prompt
string
bắt buộc
Mô tả văn bản của ảnh mong muốn.
n
integer
mặc định:"1"
Số lượng ảnh cần tạo (1-4, phụ thuộc vào model).
size
string
mặc định:"1024x1024"
Kích thước ảnh. Các tùy chọn thay đổi theo model:
  • DALL-E 3: 1024x1024, 1792x1024, 1024x1792
  • Các model khác: 512x512, 1024x1024, v.v.
quality
string
mặc định:"standard"
Chất lượng ảnh (standard hoặc hd). Chỉ dành cho DALL-E 3.
response_format
string
mặc định:"url"
Định dạng phản hồi: url hoặc b64_json.
style
string
mặc định:"vivid"
Phong cách cho DALL-E 3: vivid hoặc natural.
user
string
Một mã định danh duy nhất cho end-user.

Phản hồi

Phản hồi đồng bộ (DALL-E, Flux Schnell, v.v.)

created
integer
Unix timestamp của thời điểm tạo.
data
array
Mảng các ảnh đã được tạo.Mỗi đối tượng bao gồm:
  • url (string): URL của ảnh đã tạo
  • b64_json (string): Ảnh được mã hóa Base64 (nếu được yêu cầu)
  • revised_prompt (string): Prompt được sử dụng (DALL-E 3)

Phản hồi bất đồng bộ (Midjourney, Flux Pro, Ideogram, v.v.)

Một số model cần thời gian xử lý lâu hơn và trả về phản hồi async:
created
integer
Unix timestamp của thời điểm tạo.
task_id
string
Mã định danh tác vụ duy nhất để polling.
status
string
Trạng thái ban đầu: pending.
poll_url
string
URL tương đối để polling kết quả (ví dụ: /v1/tasks/{id}).
data
array
Mảng chứa dữ liệu giữ chỗ. url sẽ trống cho đến khi hoàn tất.
Khi bạn nhận được status: "pending", hãy sử dụng endpoint Get Image Status để polling kết quả.
curl -X POST "https://api.lemondata.cc/v1/images/generations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A white cat sitting on a windowsill watching rain",
    "size": "1024x1024",
    "quality": "standard",
    "n": 1
  }'
{
  "created": 1706000000,
  "data": [
    {
      "url": "https://...",
      "revised_prompt": "A fluffy white cat with bright eyes sitting peacefully on a wooden windowsill, watching raindrops stream down the glass window..."
    }
  ]
}

Các model khả dụng

ModelLoạiTính năng
dall-e-3Đồng bộChất lượng tốt nhất, cải thiện prompt
dall-e-2Đồng bộNhanh hơn, tiết kiệm chi phí hơn
flux-proBất đồng bộSiêu thực, chất lượng cao
flux-schnellĐồng bộRất nhanh
midjourneyBất đồng bộPhong cách nghệ thuật
ideogram-v3Bất đồng bộKết xuất văn bản tốt nhất
stable-diffusion-3Đồng bộMã nguồn mở, có thể tùy chỉnh
Các model async trả về status: "pending" và yêu cầu polling. Xem Get Image Status để biết cách truy xuất kết quả.

Xử lý phản hồi async

Đối với các model async, hãy kiểm tra xem phản hồi có chứa status: "pending" hay không:
import requests
import time

def generate_image(prompt, model="midjourney"):
    # Create image request
    response = requests.post(
        "https://api.lemondata.cc/v1/images/generations",
        headers={"Authorization": "Bearer sk-your-api-key"},
        json={"model": model, "prompt": prompt}
    )
    data = response.json()

    # Check if async
    if data.get("status") == "pending":
        task_id = data["task_id"]
        poll_url = data.get("poll_url")
        print(f"Async task started: {task_id}")

        # Poll for result
        while True:
            status_resp = requests.get(
                f"https://api.lemondata.cc{poll_url}" if poll_url else f"https://api.lemondata.cc/v1/tasks/{task_id}",
                headers={"Authorization": "Bearer sk-your-api-key"}
            )
            status_data = status_resp.json()

            if status_data["status"] == "completed":
                return status_data["data"][0]["url"]
            elif status_data["status"] == "failed":
                raise Exception(status_data.get("error", "Generation failed"))

            time.sleep(3)
    else:
        # Sync response
        return data["data"][0]["url"]

# Usage
url = generate_image("a beautiful sunset over mountains", model="midjourney")
print(f"Generated image: {url}")