取消批处理任务
curl --request POST \
--url https://api.moonshot.cn/v1/batches/{batch_id}/cancel \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.cn/v1/batches/{batch_id}/cancel"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.cn/v1/batches/{batch_id}/cancel', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.cn/v1/batches/{batch_id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.cn/v1/batches/{batch_id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/batches/{batch_id}/cancel")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.cn/v1/batches/{batch_id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "batch",
"endpoint": "<string>",
"input_file_id": "<string>",
"completion_window": "<string>",
"created_at": 123,
"request_counts": {
"completed": 123,
"failed": 123,
"total": 123
},
"output_file_id": "<string>",
"error_file_id": "<string>",
"in_progress_at": 123,
"expires_at": 123,
"finalizing_at": 123,
"completed_at": 123,
"failed_at": 123,
"cancelling_at": 123,
"cancelled_at": 123,
"metadata": {}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}取消批处理任务
取消一个正在进行的批处理任务。取消后,任务状态将先变为 cancelling,最终变为 cancelled。仅 validating、in_progress、finalizing 状态的任务可以取消。
POST
/
v1
/
batches
/
{batch_id}
/
cancel
取消批处理任务
curl --request POST \
--url https://api.moonshot.cn/v1/batches/{batch_id}/cancel \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.cn/v1/batches/{batch_id}/cancel"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.cn/v1/batches/{batch_id}/cancel', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.cn/v1/batches/{batch_id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.cn/v1/batches/{batch_id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/batches/{batch_id}/cancel")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.cn/v1/batches/{batch_id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "batch",
"endpoint": "<string>",
"input_file_id": "<string>",
"completion_window": "<string>",
"created_at": 123,
"request_counts": {
"completed": 123,
"failed": 123,
"total": 123
},
"output_file_id": "<string>",
"error_file_id": "<string>",
"in_progress_at": 123,
"expires_at": 123,
"finalizing_at": 123,
"completed_at": 123,
"failed_at": 123,
"cancelling_at": 123,
"cancelled_at": 123,
"metadata": {}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}取消一个正在进行的批处理任务。取消后,任务状态将先变为
完整的调用示例和状态流转说明,请参考 Batch API 指南。
cancelling,最终变为 cancelled。仅 validating、in_progress、finalizing 状态的任务可以取消。
调用示例
调用示例
import os
from openai import OpenAI
from openai.types import Batch
client = OpenAI(
api_key=os.environ.get("MOONSHOT_API_KEY"),
base_url=os.environ.get("MOONSHOT_BASE_URL", "https://api.moonshot.cn/v1"),
)
batch: Batch = client.batches.cancel("your_batch_id")
print(f"状态: {batch.status}") # cancelling
curl -X POST ${MOONSHOT_BASE_URL:-https://api.moonshot.cn/v1}/batches/your_batch_id/cancel \
-H "Authorization: Bearer $MOONSHOT_API_KEY"
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: process.env.MOONSHOT_BASE_URL || "https://api.moonshot.cn/v1",
});
async function main() {
const batch = await client.batches.cancel("your_batch_id");
console.log(`状态: ${batch.status}`); // cancelling
}
main();
响应字段说明
响应字段说明
调用成功后,接口返回一个
BatchObject 对象,包含以下字段:| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 批处理任务的唯一标识符 |
object | string | 对象类型,固定为 batch |
endpoint | string | 请求端点 |
input_file_id | string | 输入文件 ID |
completion_window | string | 任务处理时间窗口 |
status | string | 当前状态。取消请求成功后通常为 cancelling,最终变为 cancelled |
output_file_id | string | null | 处理成功的结果文件 ID |
error_file_id | string | null | 处理失败的错误文件 ID |
created_at | integer | 创建时间(Unix 时间戳) |
in_progress_at | integer | null | 开始执行时间(Unix 时间戳) |
expires_at | integer | null | 过期时间(Unix 时间戳) |
finalizing_at | integer | null | 开始准备结果的时间(Unix 时间戳) |
completed_at | integer | null | 完成时间(Unix 时间戳) |
failed_at | integer | null | 校验失败时间(Unix 时间戳) |
cancelling_at | integer | null | 发起取消时间(Unix 时间戳) |
cancelled_at | integer | null | 取消完成时间(Unix 时间戳) |
request_counts | object | 请求计数,包含 completed(已完成)、failed(失败)、total(总数) |
metadata | object | null | 自定义元数据 |
仅
validating、in_progress、finalizing 状态的任务可以取消。若任务已处于 completed、failed、expired 或 cancelled 状态,调用此接口将返回 400 错误。常见错误说明
- 400 请求错误:任务状态不允许取消,或请求参数无效。请确认任务状态后再发起取消。
- 401 未授权:API Key 无效或缺失。请检查
Authorization: Bearer <key>是否正确。 - 404 未找到:指定的
batch_id不存在。请确认 ID 拼写正确且该任务属于当前组织。 - 500 服务器错误:服务端内部错误,请稍后重试;若持续出现,请附带
request_id联系支持团队。
授权
路径参数
批处理任务的 ID
响应
已取消的批处理任务
批处理任务的唯一标识符
对象类型,固定为 batch
示例:
"batch"
请求端点
输入文件 ID
任务处理时间窗口
当前状态:validating(校验中)、failed(校验失败)、in_progress(执行中)、finalizing(准备结果中)、completed(已完成)、expired(已过期)、cancelling(取消中)、cancelled(已取消)
可用选项:
validating, failed, in_progress, finalizing, completed, expired, cancelling, cancelled 创建时间(Unix 时间戳)
Show child attributes
Show child attributes
处理成功的结果文件 ID
处理失败的错误文件 ID
开始执行时间(Unix 时间戳)
过期时间(Unix 时间戳)
开始准备结果的时间(Unix 时间戳)
完成时间(Unix 时间戳)
校验失败时间(Unix 时间戳)
发起取消时间(Unix 时间戳)
取消完成时间(Unix 时间戳)
自定义元数据
Show child attributes
Show child attributes
此页面对您有帮助吗?
⌘I