题意:OpenAI API 错误:‘提供了无法识别的请求参数’
问题背景:
I'm receiving an error when calling the OpenAI API. It's not recognizing file
argument, which I submitted to the API.
我在调用 OpenAI API 时遇到错误。API 不识别我提交的 file
参数。
Here is my PHP code: 这是我的 PHP 代码
<?php
// Define your OpenAI API key and the endpoint
$apiKey = 'sk-TOh**********************************';
$endpoint = 'https://api.openai.com/v1/engines/davinci/completions';
// File ID of the uploaded data
$fileId = 'file-FlW6jPfNuuq1lTak91AjMj2j';
// Product name
$productName = '6 pack fresh grannies apples';
// Prompt to use the file ID as a reference
$prompt = "Given the following data from the uploaded file $fileId, categorize the product '$productName':";
// Prepare the cURL request data
$data = [
'prompt' => $prompt,
'max_tokens' => 1, // Adjust the token limit as needed
'file' => $fileId // Reference the file by ID
];
// Prepare the cURL request
$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));
// Execute the cURL request
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
// Parse the API response as JSON
$responseData = json_decode($response, true);
echo "<pre>",print_r($responseData),"</pre>";
// Extract and display the category
$category = $responseData['choices'][0]['text'];
echo "Product '$productName' belongs to the category: $category";
}
// Close the cURL session
curl_close($ch);
?>
Here is the data of the file I uploaded:
这是我上传的文件的数据:
{"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"}
Here is the error I'm receiving: 这是我收到的错误信息:
[error] => Array
(
[message] => Unrecognized request argument supplied: file
[type] => invalid_request_error
[param] =>
[code] =>
)
What am I doing wrong? I've tried searching for the answer and also looking at OpenAI documentation.
我哪里做错了?我已经尝试过搜索答案,也查阅了 OpenAI 的文档。
问题解决:
Problem 问题
You're trying to pass file
as a parameter to the Completions API endpoint, which is not a valid parameter. You can't pass any parameter you make up to the Completions API endpoint.
你正在尝试将 file
作为参数传递给 Completions API 端点,但这不是一个有效的参数。你不能向 Completions API 端点传递你自己定义的参数。
Solution 解决方案
See the complete list of parameters you can pass to the Completions API endpoint:
请参阅可以传递给 Completions API 端点的完整参数列表:
model
prompt
suffix
max_tokens
temperature
top_p
n
stream
logprobs
echo
stop
presence_penalty
frequency_penalty
best_of
logit_bias
user
Also, all Engines API endpoints are deprecated.
此外,所有 Engines API
端点都已弃用。
Use the Completions API endpoint.
请使用 Completions API
端点。
Change the URL from this... 将以下url
https://api.openai.com/v1/engines/davinci/completions
...to this. 修改成以下
https://api.openai.com/v1/completions