Problem with calling openai api using file ID

I am receiving an error when calling the opena ai, its not recognising ‘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 a receiving:

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

What am I doing wrong?, please advise

iv tried searching for the answer, also looking at openai documentation.

Problem 1: All Engines API endpoints are deprecated

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

Problem 2: Passing an invalid parameter to the API endpoint

You’re trying to pass file as a parameter to the Completions API endpoint, which is not a valid parameter. See the complete list of parameters you can pass to the Completions API endpoint.

Leave a Comment