1 服务介绍
文件保存时间:
- 上传的文件默认保存 30 天;
接口返回的文件按照文件生成的时间开始计时,默认保存 30 天。
文件要求:
格式:只支持 jsonl 文件名后缀
custom_id:全文件不能存在重复
method:只支持 POST
url:只支持 /v1/chat/completions
model:支持预置、精调、托管的文本生成类模型,取值为modelId
注意
每个请求一行,单个文件不超过5万行;body 中 model 全文件保持相同
文件单行示例:
json
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "xop1ef199ca", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "1+1=?"}],"max_tokens":2048,"top_k":4,"temperature":0.5,"patch_id":["0"]}}2 接口 Demo
部分开发语言Demo如下,其他开发语言请参照文档进行开发。
批处理API-Demo-Java
批处理API-Demo-Python
3 接口列表
3.1 接口鉴权
请先获取http服务接口认证信息中的APIPassword,假如获取到的值为123456,则请求头如下:
http
Authorization: Bearer 1234564 上传文件
4.1 接口描述
上传jsonl文件,文件格式需要符合要求,参考服务介绍。
上传的文件默认保存 30 天。
4.2 接口地址
http
POST https://spark-api-open.xf-yun.com/v1/files4.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword
Content-Type: multipart/form-data(2)请求体
| 字段名 | 类型 | 是否必传 | 描述 |
|---|---|---|---|
| purpose | string | 是 | 传 batch,表示上传文件的意图 |
| file | file | 是 | 文件(最大不超过 100 MB,默认保存30天) |
4.4 响应参数
(1)响应参数说明
id文件唯一标识符(全局唯一)。格式为:file_xxxxx
object上传类型(扩展字段,当前仅为 file)
bytes文件长度(单位:Byte)
created_at文件创建时间
filename文件名
purpose上传文件的意图(请求参数传入值)
(2)响应参数示例
json
{
"id": "file-abc123",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "mydata.jsonl",
"purpose": "batch"
}5 查询文件列表
5.1 接口描述
获取 appid 下的文件列表。
上传的文件默认保存 30 天;
接口返回的文件按照文件生成的时间开始计时,默认保存 30 天。
5.2 接口地址
http
GET https://spark-api-open.xf-yun.com/v1/files?page=1&size=20按创建时间排序,获取 [(page-1) 20 + 1, (page-1) 20 + size] 的数据
5.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword5.4 响应参数
5.4.1 响应参数示例
json
{
"data": [
{
"id": "file-abc123",
"object": "file",
"bytes": 175,
"created_at": 1613677385,
"filename": "mydata.jsonl",
"purpose": "batch"
},
{
"id": "file-abc123",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "puppy.jsonl",
"purpose": "batch"
}
],
"object": "list"
}6 查询单个文件
6.1 接口描述
根据文件的file_id获取该文件信息。
上传的文件默认保存 30 天;
接口返回的文件按照文件生成的时间开始计时,默认保存 30 天。
6.2 接口地址
http
GET https://spark-api-open.xf-yun.com/v1/files/{file_id}注意
请求参数file_id放在路径中!
6.3 请求参数
http
Authorization: Bearer $APIPassword6.4 响应参数示例
json
{
"id": "file-abc123",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "mydata.jsonl",
"purpose": "batch"
}7 删除文件
7.1 接口描述
根据文件的file_id删除该文件
7.2 接口地址
http
DELETE https://spark-api-open.xf-yun.com/v1/files/{file_id}注意
请求参数file_id放在路径中!
7.3 请求参数
请求头
http
Authorization: Bearer $APIPassword7.4 响应参数
响应参数示例
json
{
"id": "file-abc123",
"object": "file",
"deleted": true
}8 下载文件
8.1 接口描述
根据文件的file_id获取文件的详细内容;
上传的文件默认保存 30 天;
接口返回的文件按照文件生成的时间开始计时,默认保存 30 天。
8.2 接口地址
http
GET https://spark-api-open.xf-yun.com/v1/files/{file_id}/content注意
请求参数file_id放在路径中!
8.3 请求参数
请求头
http
Authorization: Bearer $APIPassword8.4 响应参数
响应参数示例
响应为文件的详细内容,可参考:
json
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "xop1ef199ca", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "1+1=?"}],"max_tokens": 1000}}9 创建Batch任务
9.1 接口描述
调用该接口前需要先上传jsonl文件,通过上传文件得到的file_id来创建Batch任务。
单个Batch任务最多包含5万个请求(一个请求对应jsonl文件的一行),每个请求的body不超过6KB
9.2 接口地址
http
POST https://spark-api-open.xf-yun.com/v1/batches9.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword
Content-Type: application/json(2)请求体
| 字段名 | 类型 | 是否必传 | 描述 |
|---|---|---|---|
| input_file_id | string | 是 | 文件标识符 (上传文件成功时返回的 file_id) |
| 格式为:file_xxxxx | |||
| endpoint | string | 是 | 处理批任务的服务的 path 路径 |
| 当前仅支持 /v1/chat/completions | |||
| completion_window | string | 是 | 批处理任务完成的时间窗口 |
| 当前仅支持 24h,在此时间内完成,超时则不再处理,未处理任务视为失败 | |||
| metadata | Map<String, String> | 否 | 批任务附加信息, 例如:"customer_id": "user_123456789" "batch_description": "Sentiment classification" |
9.4 响应参数
(1)响应参数说明
id批任务唯一标识(全局唯一)。格式为:batch_xxxxx
object结构类型,当前仅为 batch
endpoint处理批任务的服务的 path 路径,当前仅为 /v1/chat/completions
errors错误列表
object描述 data 类型 (list)
data错误项列表,每项包含:
code string、message string、param string or null、line int or nullinput_file_id文件id(用户入参)
status任务状态(枚举值)
枚举值:
queuing(排队)/ in_progress(在处理)/ finalizing(上传中)/ completed(处理成功)/ failed(处理失败)/ expired(超时)/ canceled(已取消)output_file_id结果文件id
error_file_id错误文件id
created_at任务创建时间(unix时间戳,下面的时间也均为unix时间戳)
in_progress_at任务开始处理时间
expires_at任务预期超时时间
finalizing_at任务完成开始时间(写 out_file && error_file 开始)
completed_at任务完成结束时间(写 out_file && error_file 结束)
failed_at任务失败时间(所有任务失败时返回)
expired_at任务实际超时时间
cancelling_at任务取消开始时间(收到取消请求时间)
cancelled_at任务取消结束时间(任务实际取消时间)
request_counts批任务信息
total批任务当前完成总数
completed任务当前成功数
failed任务当前失败数
metadata批任务附加信息(用户入参)
(2)status字段说明
queuing排队
in_progress在处理
finalizing上传中
completed处理成功
failed处理失败
expired超时
canceled已取消
(3)响应参数示例
json
{
"id": "batch_abc123",
"object": "batch",
"endpoint": "/v1/completions",
"errors": null,
"input_file_id": "file-abc123",
"completion_window": "24h",
"status": "completed",
"output_file_id": "file-cvaTdG",
"error_file_id": "file-HOWS94",
"created_at": 1711471533,
"in_progress_at": 1711471538,
"expires_at": 1711557933,
"finalizing_at": 1711493133,
"completed_at": 1711493163,
"failed_at": null,
"expired_at": null,
"cancelling_at": null,
"cancelled_at": null,
"request_counts": {
"total": 100,
"completed": 95,
"failed": 5
},
"metadata": {
"customer_id": "user_123456789",
"batch_description": "Sentiment classification"
}
}10 查询Batch任务
10.1 接口描述
根据batch_id获取该Batch任务的详细信息
10.2 接口地址
http
GET https://spark-api-open.xf-yun.com/v1/batches/{batch_id}注意
请求参数batch_id放在路径中!
10.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword
Content-Type: application/json10.4 响应参数
(2)响应参数示例
json
{
"id": "batch_abc123",
"object": "batch",
"endpoint": "/v1/completions",
"errors": null,
"input_file_id": "file-abc123",
"completion_window": "24h",
"status": "completed",
"output_file_id": "file-cvaTdG",
"error_file_id": "file-HOWS94",
"created_at": 1711471533,
"in_progress_at": 1711471538,
"expires_at": 1711557933,
"finalizing_at": 1711493133,
"completed_at": 1711493163,
"failed_at": null,
"expired_at": null,
"cancelling_at": null,
"cancelled_at": null,
"request_counts": {
"total": 100,
"completed": 95,
"failed": 5
},
"metadata": {
"customer_id": "user_123456789",
"batch_description": "Sentiment classification"
}
}11 取消Batch任务
11.1 接口描述
根据batch_id取消该Batch任务
11.2 接口地址
http
GET https://spark-api-open.xf-yun.com/v1/batches/{batch_id}/cancel注意
请求参数batch_id放在路径中!
11.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword
Content-Type: application/json11.4 响应参数
(1)响应参数示例
json
{
"id": "batch_abc123",
"object": "batch",
"endpoint": "/v1/completions",
"errors": null,
"input_file_id": "file-abc123",
"completion_window": "24h",
"status": "completed",
"output_file_id": "file-cvaTdG",
"error_file_id": "file-HOWS94",
"created_at": 1711471533,
"in_progress_at": 1711471538,
"expires_at": 1711557933,
"finalizing_at": 1711493133,
"completed_at": 1711493163,
"failed_at": null,
"expired_at": null,
"cancelling_at": null,
"cancelled_at": null,
"request_counts": {
"total": 100,
"completed": 95,
"failed": 5
},
"metadata": {
"customer_id": "user_123456789",
"batch_description": "Sentiment classification"
}
}12 查询Batch列表
12.1 接口描述
获取 appid 下的Batch列表
12.2 接口地址
text
GET /v1/batches?limit=10&after={batch_id}注意
请求参数batch_id放在路径中!
| 字段名 | 类型 | 是否必传 | 描述 |
|---|---|---|---|
| limit | int | 否,默认为10 | 响应中列表长度(最大为100) |
| after | string | 否,默认从批任务创建时间正序排序开始查询 | 从此 batch_id 开始查询(响应中不包括此 batch_id) |
12.3 请求参数
(1)请求头
http
Authorization: Bearer $APIPassword
Content-Type: application/json12.4 响应参数
(1)响应参数示例
json
{
"object": "list",
"data": [
{
"id": "batch_abc123",
"object": "batch",
"endpoint": "/v1/chat/completions",
"errors": null,
"input_file_id": "file-abc123",
"completion_window": "24h",
"status": "completed",
"output_file_id": "file-cvaTdG",
"error_file_id": "file-HOWS94",
"created_at": 1711471533,
"in_progress_at": 1711471538,
"expires_at": 1711557933,
"finalizing_at": 1711493133,
"completed_at": 1711493163,
"failed_at": null,
"expired_at": null,
"cancelling_at": null,
"cancelled_at": null,
"request_counts": {
"total": 100,
"completed": 95,
"failed": 5
},
"metadata": {
"customer_id": "user_123456789",
"batch_description": "Sentiment classification"
}
},
......
],
"first_id": "batch_abc123",
"last_id": "batch_abc456",
"has_more": true
}