跳到主要内容

异步作业生命周期

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")

相关文档