You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过APIM配置的Azure OpenAI端点使用Files及Batch API?

通过APIM调用Azure OpenAI的Files和Batch API

以下是基于你提供的Chat Completions调用方式,适配Files和Batch API的具体实现:

通用配置

先定义基础参数,和Chat Completions调用保持一致:

import requests

apim_url = "你的APIM实例URL"
subscription_key = "你的APIM订阅密钥"
api_version = "2024-02-15-preview" # 请使用与API匹配的版本

一、Files API 操作

Files API用于上传、管理批量任务所需的输入文件,常用操作如下:

1. 上传文件

用于批量任务的输入文件需符合JSONL格式,上传时使用multipart/form-data格式:

# 上传文件API路径
upload_url = f"{apim_url}/files?api-version={api_version}"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
}

# 构造请求体:指定文件用途(批量任务输入)和文件本身
files = {
    "purpose": (None, "batch"),
    "file": ("input_file.jsonl", open("input_file.jsonl", "rb"), "application/jsonl")
}

response = requests.post(upload_url, headers=headers, files=files)
file_info = response.json()
file_id = file_info["id"] # 保存文件ID,用于后续批量任务创建

2. 查询文件列表

list_url = f"{apim_url}/files?api-version={api_version}"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
}

response = requests.get(list_url, headers=headers)
files_list = response.json()

3. 查询单个文件详情

file_id = "上传文件时获取的ID"
detail_url = f"{apim_url}/files/{file_id}?api-version={api_version}"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
}

response = requests.get(detail_url, headers=headers)
file_detail = response.json()

二、Batch API 操作

Batch API用于创建批量推理任务,基于已上传的输入文件生成批量结果:

1. 创建批量任务

batch_url = f"{apim_url}/batches?api-version={api_version}"

headers = {
    "Content-Type": "application/json",
    "Ocp-Apim-Subscription-Key": subscription_key
}

# 构造批量任务请求体
json_payload = {
    "input_file_id": file_id, # 之前上传的文件ID
    "endpoint": f"/deployments/你的部署名称/chat/completions", # 指定要调用的模型部署端点
    "completion_window": "24h", # 任务完成时限
    "metadata": {
        "description": "批量处理用户问答任务"
    }
}

response = requests.post(batch_url, headers=headers, json=json_payload)
batch_info = response.json()
batch_id = batch_info["id"] # 保存批量任务ID,用于查询状态

2. 查询批量任务状态

status_url = f"{apim_url}/batches/{batch_id}?api-version={api_version}"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
}

response = requests.get(status_url, headers=headers)
batch_status = response.json()

3. 下载批量任务结果

当任务状态为completed时,可通过结果文件ID下载输出:

result_file_id = batch_status["output_file_id"]
download_url = f"{apim_url}/files/{result_file_id}/content?api-version={api_version}"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
}

response = requests.get(download_url, headers=headers)
# 保存结果到本地
with open("batch_result.jsonl", "wb") as f:
    f.write(response.content)

注意事项

  • 输入文件必须是JSONL格式,每条数据需包含custom_id和body(对应Chat Completions的请求体结构),示例条目:
    {"custom_id": "task_1", "body": {"messages": [{"role": "user", "content": "Azure OpenAI是什么?"}]}}
    
  • 确保APIM已配置Files和Batch API的转发规则,允许对应的HTTP方法(POST/GET/DELETE)通过;
  • API版本需与Azure OpenAI服务支持的版本匹配,避免兼容性问题。

内容的提问来源于stack exchange,提问作者dks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:31:12