调用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
相关产品推荐
相关产品推荐

