阿里雲百鍊的部分模型(如映像產生、視頻產生等)因處理時間較長,採用非同步呼叫機制,通常需要調用兩個介面完成操作:先建立任務擷取 ID,再通過該 ID 查詢結果。為了方便管理非同步任務,阿里雲百鍊提供了一組通用的非同步任務介面,支援查詢單個任務結果、批量查詢多個任務狀態、以及取消正在排隊且尚未處理的任務。
前提條件
非同步任務API通過HTTP進行調用。
在調用前,您需要準備工作:擷取與配置 API Key,再配置API Key到環境變數(準備下線,併入配置 API Key)。
查詢非同步任務結果介面
API描述:根據任務ID(task_id)查詢任務狀態和任務結果。
流量限制:20 QPS,即每秒每個帳號(含主帳號及其子帳號)最多發起 20 次請求。
支援查詢當前 API Key 所屬阿里雲主帳號下的所有任務(包括該主帳號下通過任意 API Key 提交的任務),但無法查詢其他主帳號提交的任務。
非同步任務在完成後通常保留 24 小時 (具體以對應任務的 API 文檔為準),逾時後系統將自動清理歷史任務資料。
請求介面
GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}入參描述
傳參方式 | 欄位 | 類型 | 必選 | 描述 | 樣本值 |
Header | Authorization | String | 是 | API-Key,例如:Bearer sk-xxx | Bearer sk-xxx |
Path | task_id | String | 是 | 需查詢的任務ID | a8532587-xxxx-xxxx-xxxx-0c46b17950d1 |
出參描述
欄位 | 類型 | 描述 | 樣本值 |
request_id | String | 本次請求的系統唯一碼 | 7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51 |
output | Object |
| - |
output.task_id | String | 查詢任務的 task_id | a8532587-xxxx-xxxx-xxxx-0c46b17950d1 |
output.task_status | String | 任務狀態
| 任務狀態:
|
output.submit_time | String | 任務提交時間 | 2023-12-20 21:36:31.896 |
output.scheduled_time | String | 任務調度時間,即開始執行時間 | 2023-12-20 21:36:39.009 |
output.end_time | String | 任務結束時間 | 2023-12-20 21:36:45.913 |
output.code | String | 錯誤碼,僅在任務失敗時返回 | - |
output.message | String | 錯誤資訊,僅任務失敗時返回 | - |
output.task_metrics | Object | 任務指標,包含子任務狀態的統計資訊 |
|
usage | Object | 本次請求產生的計量資訊,根據實際任務的不同,相關的計量資訊也有所不同 |
|
請求樣本
curl -X GET 'https://dashscope.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"若未配置阿里雲百鍊API Key到環境變數,請將$DASHSCOPE_API_KEY替換為實際API Key,例如:--header "Authorization: Bearer sk-xxx"。
響應樣本
{
"request_id": "45ac7f13-xxxx-xxxx-xxxx-e03c35068d83",
"output": {
"task_id": "73205176-xxxx-xxxx-xxxx-16bd5d902219",
"task_status": "SUCCEEDED",
"submit_time": "2023-12-20 21:36:31.896",
"scheduled_time": "2023-12-20 21:36:39.009",
"end_time": "2023-12-20 21:36:45.913",
"results": [
{
"url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx1.png"
},
{
"url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx2.png"
},
{
"url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx3.png"
},
{
"code": "DataInspectionFailed",
"message": "Output data may contain inappropriate content.",
}
],
"task_metrics": {
"TOTAL": 4,
"SUCCEEDED": 3,
"FAILED": 1
}
},
"usage": {
"image_count": 3
}
}批量查詢非同步任務狀態介面
API描述:支援通過組合多種查詢條件,批量擷取多個非同步任務的目前狀態。該介面適用於一次性查看多個任務的執行進度。
流量限制:20 QPS,即每秒每個帳號(含主帳號及其子帳號)最多發起 20 次請求。
支援查詢當前 API Key 所屬阿里雲主帳號下的所有任務(包括該主帳號下通過任意 API Key 提交的任務),但無法查詢到其他主帳號下的任務。
已結束的任務在逾時後將被系統自動清理,屆時將無法查詢到相關任務資料。
請求介面
GET https://dashscope.aliyuncs.com/api/v1/tasks/入參描述
傳參方式 | 欄位 | 類型 | 必選 | 描述 | 樣本值 |
Header | Authorization | String | 是 | API-Key,例如:Bearer sk-xxx | Bearer sk-xxx |
Params | task_id | String | 否 | 需要查詢任務的 task_id 若指定task_id,介面僅返回該任務的狀態資訊;若需批量查詢多個任務狀態,請勿傳入此參數 | a8532587-xxxx-xxxx-xxxx-0c46b17950d1 |
start_time | String | 否 | 任務開始時間,格式為:
| 20230420193058 代表 2023 年 4 月 20 日 19 點 30 分 58 秒 | |
end_time | String | 否 | 任務結束時間,格式為:
| ||
model_name | String | 否 | 模型名稱 | wanx-v1 | |
status | String | 否 | 任務狀態:
| ||
page_no | Integer | 否 | 當前頁,預設查詢第1頁 | - | |
page_size | Integer | 否 | 每頁查詢條數,預設查詢10條 | - |
出參描述
欄位 | 類型 | 描述 | 樣本值 |
request_id | String | 本次請求的系統唯一碼 | 7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51 |
data | Array | 查詢結果清單 | |
data[].api_key_id | String | API Key的id | |
data[].caller_parent_id | String | 阿里雲主帳號ID | |
data[].caller_uid | String | 阿里雲帳號ID | |
data[].gmt_create | Long | 任務建立時間,Date時間毫秒數 | |
data[].start_time | Long | 任務開始時間,Date時間毫秒數 | |
data[].end_time | Long | 任務結束時間,Date時間毫秒數 | |
data[].region | String | 地區,例如:cn-hangzhou | |
data[].request_id | String | 提交任務的請求id | |
data[].status | String | 任務狀態:
| |
data[].task_id | String | 任務id | |
data[].user_api_unique_key | String | API 的唯一key(提交任務時,模型API的各要素唯一索引) | |
data[].model_name | String | 模型名稱 | |
page_no | Integer | 當前頁 |
|
page_size | Integer | 每頁查詢條數 |
|
total_page | Integer | 總頁數 |
|
total | Integer | 總條數 |
|
code | String | 調用失敗的時候返回的錯誤碼 |
|
message | String | 調用失敗的時候返回的錯誤資訊 |
|
請求樣本
curl -X GET 'https://dashscope.aliyuncs.com/api/v1/tasks/?start_time=xxx&end_time=xxx&status=xxx' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"響應樣本
{
"total": 2,
"data": [
{
"api_key_id": "15xxxx",
"caller_parent_id": "xxxxxxxxx",
"caller_uid": "xxxxxxxxx",
"gmt_create": 1745568428109,
"model_name": "wanx2.1-kf2v-plus",
"region": "cn-beijing",
"request_id": "1abfc3c8-dd25-98da-ad0b-xxxxxx",
"start_time": 1745568428138,
"status": "RUNNING",
"task_id": "50e2ccea-abc4-43d7-a0dc-xxxxxx",
"user_api_unique_key": "apikey:v1:aigc:image2video:video-synthesis:wanx2.1-kf2v-plus"
},
{
"api_key_id": "15xxxx",
"caller_parent_id": "xxxxxxxxx",
"caller_uid": "xxxxxxxxx",
"end_time": 1745568302481,
"gmt_create": 1745568293253,
"model_name": "wanx2.1-t2i-turbo",
"region": "cn-beijing",
"request_id": "f6bf34d9-bf87-9e8b-9ed4-xxxxxx",
"start_time": 1745568293273,
"status": "SUCCEEDED",
"task_id": "3c777dbc-8cc6-4d80-aa90-xxxxxx",
"user_api_unique_key": "apikey:v1:aigc:text2image:image-synthesis:wanx2.1-t2i-turbo"
}
],
"total_page": 1,
"page_no": 1,
"request_id": "f6756b7e-d0bb-9b74-813a-xxxxxx",
"page_size": 10
}取消非同步任務介面
API描述:用於取消非同步任務,僅支援取消狀態為 PENDING 的任務 (即排隊中且尚未開始處理的任務),其他狀態的任務無法取消。
流量限制:20 QPS,即每秒每個帳號(含主帳號及其子帳號)最多發起 20 次請求。
支援取消當前 API Key 所屬阿里雲主帳號下的所有任務(包括該主帳號下通過任意 API Key 提交的任務),但無法取消其他主帳號提交的任務。
請求介面
POST https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}/cancel入參描述
傳參方式 | 欄位 | 類型 | 必選 | 描述 | 樣本值 |
Header | Authorization | String | 是 | API-Key,例如:Bearer sk-xxx | Bearer sk-xxx |
Path | task_id | String | 是 | 待取消的任務 task_id | a8532587-xxxx-xxxx-xxxx-0c46b17950d1 |
出參描述
欄位 | 類型 | 描述 | 樣本值 |
request_id | String | 本次請求的系統唯一碼 | 7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51 |
code | String | 調用失敗的時候返回的錯誤碼 |
|
message | String | 調用失敗的時候返回的錯誤資訊 |
|
請求樣本
curl -X POST 'https://dashscope.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219/cancel' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"響應樣本
{
"request_id": "45ac7f13-xxxx-xxxx-xxxx-e03c35068d83"
}錯誤碼
HTTP狀態代碼 | 介面錯誤碼(code) | 介面錯誤資訊(message) | 含義說明 |
400 | UnsupportedOperation | Failed to cancel the task, please confirm if the task is in PENDING status. | 取消任務失敗,請確認任務狀態為 僅 PENDING 狀態的任務可取消,其他狀態任務無法取消。 |