本文介紹如何查詢實時記錄的任務狀態和結果。
基于上一步提交任務后返回的TaskId來查詢處理結果。
如通過此方式輪詢查詢結果,注意輪詢頻率不要過高,以免被限流。比如您可以按每1分鐘或每5分鐘的頻率持續查詢。
使用回調通知的方式來獲取結果,不同于主動輪詢,您可以在提交任務后,等待服務端處理完成時主動地通知您任務狀態。
當前我們支持通過HTTP或MQ的形式將任務處理狀態通知到您。
查詢任務狀態和結果
查詢任務時,您需要將“提交任務”時返回的TaskId作為輸入,發起查詢請求,根據返回的狀態判斷任務是否已完成。
請求參數
參數名 | 類型 | 是否必填 | 說明 |
TaskId | string | 是 | 您提交任務時返回的TaskId信息 |
示例代碼
#!/usr/bin/env python
#coding=utf-8
import os
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.auth.credentials import AccessKeyCredential
def create_common_request(domain, version, protocolType, method, uri):
request = CommonRequest()
request.set_accept_format('json')
request.set_domain(domain)
request.set_version(version)
request.set_protocol_type(protocolType)
request.set_method(method)
request.set_uri_pattern(uri)
request.add_header('Content-Type', 'application/json')
return request
# TODO 請通過環境變量設置您的 AccessKeyId 和 AccessKeySecret
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
client = AcsClient(region_id='cn-beijing', credential=credentials)
uri = '/openapi/tingwu/v2/tasks' + '/' + '請輸入您提交任務時返回的TaskId'
request = create_common_request('tingwu.cn-beijing.aliyuncs.com', '2023-09-30', 'https', 'GET', uri)
response = client.do_action_with_exception(request)
print("response: \n" + json.dumps(json.loads(response), indent=4, ensure_ascii=False))
package com.alibaba.tingwu.client.demo.common;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;
/**
* @author tingwu2023
* @desc 演示了通過OpenAPI 根據TaskId查詢任務狀態和結果 的調用方式。
*/
public class GetTaskInfoTest {
@Test
public void getTaskInfo() throws ClientException {
String taskId = "請輸入創建任務(含離線轉寫、實時會議)的TaskId";
String queryUrl = String.format("/openapi/tingwu/v2/tasks" + "/%s", taskId);
CommonRequest request = createCommonRequest("tingwu.cn-beijing.aliyuncs.com", "2023-09-30", ProtocolType.HTTPS, MethodType.GET, queryUrl);
// TODO 請通過環境變量設置您的AccessKeyId、AccessKeySecret
DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}
public static CommonRequest createCommonRequest(String domain, String version, ProtocolType protocolType, MethodType method, String uri) {
// 創建API請求并設置參數
CommonRequest request = new CommonRequest();
request.setSysDomain(domain);
request.setSysVersion(version);
request.setSysProtocol(protocolType);
request.setSysMethod(method);
request.setSysUriPattern(uri);
request.setHttpContentType(FormatType.JSON);
return request;
}
}
package main
import (
"encoding/json"
"log"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
type GetTaskInfoResponse struct {
RequestId string `json:"RequestId"`
Code string `json:"Code"`
Message string `json:"Message"`
Data struct {
TaskId string `json:"TaskId"`
TaskKey string `json:"TaskKey"`
TaskStatus string `json:"TaskStatus"`
} `json:"Data"`
}
func get_task_info(taskid string, akkey string, aksecret string) (*GetTaskInfoResponse, string, error) {
client, err := sdk.NewClientWithAccessKey("cn-beijing", akkey, aksecret)
if err != nil {
return nil, "", err
}
request := requests.NewCommonRequest()
request.Method = "GET"
request.Domain = "tingwu.cn-beijing.aliyuncs.com"
request.Version = "2023-09-30"
request.PathPattern = "/openapi/tingwu/v2/tasks/" + taskid
request.SetScheme("https")
log.Default().Print("request task:", taskid)
response, err := client.ProcessCommonRequest(request)
if err != nil {
return nil, "", err
}
log.Default().Print("response body:\n", string(response.GetHttpContentBytes()))
resp := new(GetTaskInfoResponse)
err = json.Unmarshal(response.GetHttpContentBytes(), resp)
if err != nil {
return nil, response.GetHttpContentString(), err
}
return resp, response.GetHttpContentString(), nil
}
func main() {
akkey := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
aksecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
_, raw, _ := get_task_info("請輸入創建任務(含離線轉寫、實時會議)的TaskId", akkey, aksecret)
log.Default().Println("response :", raw)
}
#include <cstdlib>
#include <iostream>
#include <string>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
/**
* @author tingwu2023
* @desc 演示了通過OpenAPI 根據TaskId查詢任務狀態和結果 的調用方式。
*/
int main( int argc, char** argv ) {
std::string taskId = "請輸入創建任務(含離線轉寫、實時記錄)的TaskId";
AlibabaCloud::InitializeSdk();
AlibabaCloud::ClientConfiguration configuration( "cn-beijing" );
// specify timeout when create client.
configuration.setConnectTimeout(1500);
configuration.setReadTimeout(4000);
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
AlibabaCloud::Credentials credential( getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") );
AlibabaCloud::CommonClient client( credential, configuration );
AlibabaCloud::CommonRequest request(AlibabaCloud::CommonRequest::RequestPattern::RoaPattern);
request.setHttpMethod(AlibabaCloud::HttpRequest::Method::Get);
request.setDomain("tingwu.cn-beijing.aliyuncs.com");
request.setVersion("2023-09-30");
request.setHeaderParameter("Content-Type", "application/json");
request.setResourcePath("/openapi/tingwu/v2/tasks/" + taskId);
auto response = client.commonResponse(request);
if (response.isSuccess()) {
printf("request success.\n");
printf("result: %s\n", response.result().payload().c_str());
} else {
printf("error: %s\n", response.error().errorMessage().c_str());
printf("request id: %s\n", response.error().requestId().c_str());
}
AlibabaCloud::ShutdownSdk();
return 0;
}
示例輸出
當任務仍在運行中時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING"
},
"Message":"success",
"RequestId":"5fa32fc6-441f-4dd1-bb86-c030********"
}
當任務在運行中但有部分結果時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING",
"OutputMp3Path":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_20231101141801.mp3?ExLTAI4G4uXHLPwQHj6oX8****AccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********JBMijH7wLq0xX6aivHc%3D",
"Result":{
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"1b20e0d9-c55c-4cc3-85af-80b4********"
}
當任務已完成時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"COMPLETED",
"OutputMp3Path":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_20231101141801.mp3?ExLTAI4G4uXHLPwQHj6oX8****AccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********JBMijH7wLq0xX6aivHc%3D",
"Result":{
"AutoChapters":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_AutoChapters_20231101141955.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********Ax9FvifYAO8dj4qzWg%3D",
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"1b20e0d9-c55c-4cc3-85af-80b4********"
}
當任務失敗時:
{
"Code":"0",
"Data":{
"TaskId":"b76389677b1441fa82165cb1********",
"TaskStatus":"FAILED",
"ErrorCode":"TSC.AudioFileLink",
"ErrorMessage":"Audio file link invalid."
},
"Message":"success",
"RequestId":"d181d898-b627-4040-b7c9-9563********"
}
回調通知任務狀態和結果
如果您希望通過回調方式來獲取任務狀態和結果,需要在控制臺配置好回調類型和地址,創建實時記錄任務時將參數AppKey配置為對應的項目AppKey,同時將參數Input.ProgressiveCallbacksEnabled設為true。
當您選擇http回調方式時,我們將設置一個最大請求時間限制。這意味著,如果我們在發起請求后的超過5秒鐘內沒有收到您的回復,我們將不再等待返回結果,系統判斷回調請求發送失敗,稍后將會進行重試。
我們建議您審核和調整您的系統,以確保能夠在5秒鐘內響應我們的回調請求。這可能包括但不限于:
優化您的處理邏輯,確保高效執行;
對不必要的長操作進行異步處理,不在主回調響應路徑中等待;
實施錯誤處理和重試機制,以應對超時或失敗的情況。
回調重試機制說明:
當回調消息發送失敗時,聽悟服務端會在5秒鐘后重新發送一次回調消息,如果第二次發送仍然失敗,則會在之后的每5分鐘再發送一次,重試次數最多為3次。
示例輸出
當單個AI模型子任務完成時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING",
"TaskKey":"TingwuDemo",
"Result":{
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"5cf2b587ac6e41e6a1f06ca1********"
}
當任務完成時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"COMPLETED",
"TaskKey":"TingwuDemo",
"Result":{
"AutoChapters":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_AutoChapters_20231101141955.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********Ax9FvifYAO8dj4qzWg%3D",
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"9c60b07f152445349eaa6161********"
}
當任務失敗時:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"FAILED",
"TaskKey":"TingwuDemo",
"ErrorCode":"TSC.AudioFileLink",
"ErrorMessage":"Audio file link invalid."
},
"Message":"success",
"RequestId":"5e1c0babe36844e49f1b7bee********"
}