如何修复Azure OpenAI GPT-4o mini批量处理的"Invalid value for purpose"错误
修复Azure OpenAI批量任务文件上传错误
错误原因
调用client.files.create时设置的purpose="batch"是无效值,Azure OpenAI的Files API不支持该参数值,因此返回invalidPayload错误。
修复步骤
1. 修正文件purpose参数
将purpose="batch"替换为Azure支持的有效值,针对批量聊天补全任务,可设置为"fine-tune"(该值适配多数批量处理场景的文件上传需求):
file = client.files.create( file=open("./dummy.jsonl", "rb"), purpose="fine-tune" )
2. 修正JSONL文件格式
当前dummy.jsonl是多行格式化的JSON,不符合JSONL(每行一个独立JSON对象)的格式要求,需将内容压缩为单行:
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo-0125", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello world!"}], "max_tokens": 1000}}
3. 升级API版本(可选)
你使用的api_version="2023-07-01-preview"版本较早,建议升级到支持批量处理的新版本(如"2024-02-15-preview"),以获得更稳定的功能支持:
client = AzureOpenAI( api_key="[insert Azure OpenAI API key here]", api_version="2024-02-15-preview", azure_endpoint="https://[insert Azure OpenAI API endpoint here].openai.azure.com/", )
后续创建批量任务
文件上传成功后,可继续调用批量任务创建接口完成流程:
batch = client.batches.create( input_file_id=file_id, endpoint="/v1/chat/completions", completion_window="24h", metadata={"description": "批量处理聊天请求"} )
内容的提问来源于stack exchange,提问作者Franck Dernoncourt
相关产品推荐
相关产品推荐

