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

Tham số Path

id
string
bắt buộc
ID của tác vụ được trả về từ yêu cầu tạo hình ảnh ban đầu.
Nếu phản hồi tạo bao gồm poll_url, hãy ưu tiên gọi URL chính xác đó để thực hiện polling. Một số tác vụ hình ảnh có thể hiển thị poll_url dưới /v1/tasks/{id} thay vì đường dẫn trạng thái dành riêng cho hình ảnh.If the task no longer exists or can no longer be resolved through the public async-task contract, LemonData returns async_task_not_found with the message Task not found or no longer available.

Phản hồi

created
integer
Unix timestamp lúc tạo.
task_id
string
Định danh của tác vụ.
status
string
Trạng thái tác vụ: pending, processing, completed, hoặc failed.
data
array
Mảng các hình ảnh đã tạo (được điền khi statuscompleted).Mỗi đối tượng bao gồm:
  • url (string): URL của hình ảnh đã tạo
  • revised_prompt (string): Prompt đã được chỉnh sửa dùng để tạo hình ảnh
error
string
Thông báo lỗi (chỉ xuất hiện khi statusfailed).
curl "https://api.lemondata.cc/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
  -H "Authorization: Bearer sk-your-api-key"
{
  "created": 1706000000,
  "id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "status": "pending",
  "data": [
    {
      "url": "",
      "revised_prompt": "a beautiful sunset over mountains"
    }
  ]
}

Các thực hành tốt nhất về Polling

Khoảng thời gian polling khuyến nghị: 3-5 giây. Hầu hết các tác vụ tạo hình ảnh hoàn thành trong vòng 30-120 giây tùy thuộc vào mô hình và đường dẫn nhà cung cấp được điều hướng.
import requests
import time

def poll_image_task(task_id, api_key, max_wait=300, interval=3):
    """Poll for image generation result with timeout."""
    url = f"https://api.lemondata.cc/v1/tasks/{task_id}"
    headers = {"Authorization": f"Bearer {api_key}"}

    start_time = time.time()
    while time.time() - start_time < max_wait:
        response = requests.get(url, headers=headers)
        data = response.json()

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

        time.sleep(interval)

    raise TimeoutError(f"Task {task_id} did not complete within {max_wait}s")

# Usage
image_url = poll_image_task("ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "sk-your-api-key")
print(f"Generated image: {image_url}")