跳至主要內容

非同步作業生命週期

AetherAI API 的所有生成作業皆以非同步方式處理。建立作業後,必須透過輪詢確認狀態。

作業建立

發送 POST 請求後會立即回傳 job_id。影像處理於背景進行。

請求範例

curl -X POST "https://api.aetherforgeai.com/public/v1/generate/image" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"prompt": "A beautiful sunset over mountains",
"ai_model": "GPT Image 1.5"
}'

回應範例

{
"job_id": "550e8400-e29b-41d4-a716-446655440000"
}

作業狀態輪詢

使用回傳的 job_id 發送 GET 請求以確認作業狀態。

curl -X GET "https://api.aetherforgeai.com/public/v1/job/{job_id}" \
-H "Authorization: Bearer your_api_key_here"

狀態值

狀態說明
Pending作業等待中或處理中
Succeed作業已成功完成
Failed作業處理時發生錯誤

各狀態回應範例

Pending

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "Pending",
"image_urls": []
}

Succeed

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "Succeed",
"image_urls": [
"https://cdn.aetherforgeai.com/images/abc123.png",
"https://cdn.aetherforgeai.com/images/abc123.gif"
]
}

Failed

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "Failed",
"image_urls": []
}

輪詢策略

建議依以下間隔進行輪詢以確認作業完成:

  • 初始輪詢:作業建立後 1 秒
  • 後續間隔:2~5 秒
  • 最大輪詢時間:1 分鐘(依作業類型而異)

Python 範例

import time
import requests

def poll_job_status(job_id, api_key, max_attempts=60):
headers = {"Authorization": f"Bearer {api_key}"}

for attempt in range(max_attempts):
response = requests.get(
f"https://api.aetherforgeai.com/public/v1/job/{job_id}",
headers=headers
)
data = response.json()

if data["status"] == "Succeed":
return data["image_urls"]
elif data["status"] == "Failed":
raise Exception(f"Job failed: {data.get('error_message', 'Unknown error')}")

time.sleep(2)

raise TimeoutError("Job polling timed out")

相關文件