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

调用OpenAI API遇'Unrecognized request argument supplied'错误求助

OpenAI API调用错误:file参数不被识别

调用OpenAI API时出现错误,API无法识别我提交的file参数。

我的PHP代码

<?php

// 定义OpenAI API密钥和接口地址
$apiKey = 'sk-TOh**********************************';
$endpoint = 'https://api.openai.com/v1/engines/davinci/completions';

// 已上传数据的文件ID
$fileId = 'file-FlW6jPfNuuq1lTak91AjMj2j';

// 产品名称
$productName = '6 pack fresh grannies apples';

// 提示语,要求以上传文件为参考进行分类
$prompt = "Given the following data from the uploaded file $fileId, categorize the product '$productName':";

// 准备cURL请求数据
$data = [
    'prompt' => $prompt,
    'max_tokens' => 1, // 根据需要调整token限制
    'file' => $fileId // 通过ID引用文件
];

// 初始化cURL请求
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $apiKey,
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// 执行cURL请求
$response = curl_exec($ch);

// 检查cURL错误
if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
} else {
    // 解析API响应为JSON格式
    $responseData = json_decode($response, true);
echo "<pre>",print_r($responseData),"</pre>";
    // 提取并显示分类结果
    $category = $responseData['choices'][0]['text'];
    echo "Product '$productName' belongs to the category: $category";
}

// 关闭cURL会话
curl_close($ch);

?>

上传的文件数据

{"prompt": "fruits", "completion": "apples, bananas, oranges, grapes, strawberries"}
{"prompt": "vegetables", "completion": "carrots, broccoli, spinach, lettuce, tomatoes"}
{"prompt": "dairy", "completion": "milk, cheese, yogurt, butter, cream"}
{"prompt": "meat", "completion": "chicken, beef, pork, lamb, turkey"}
{"prompt": "bakery", "completion": "bread, muffins, cookies, cakes, pies"}

收到的错误信息

[error] => Array
(
  [message] => Unrecognized request argument supplied: file
  [type] => invalid_request_error
  [param] => 
  [code] => 
)

我已经搜索过相关答案并查阅了OpenAI文档,但问题仍未解决,请问我哪里操作有误?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 01:57:46