I'm receiving an error when calling the OpenAI API. It's not recognizing file
argument, which I submitted to the API.
Here is my PHP code:
<?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.
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.
See the complete list of parameters you can pass to the Completions API endpoint:
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.
Use the Completions API endpoint.
Change the URL from this...
https://api.openai.com/v1/engines/davinci/completions
...to this.
https://api.openai.com/v1/completions