Hello, I’m trying to write a code that summarizes texts from a local newspaper using PHP and the completion API. The problem is that the API sometimes returns the text in its original form, sometimes it returns only 50% of the text without any summary, and sometimes it returns the text (Spanish) in English.
I have tried various configurations in the Playground and others suggested by Chatgpt, but the API still does not consistently provide a summary. I start the input with “Make a summary of the following text:” (without quotes) and also tried adding “Tl;dr” at the end, as indicated in OpenAI API.
I set the max_token manually, although sometimes I calculate it based on the length of the text. The texts I send are dynamic and range from 500 to 2000 characters.
These are some of the codes I have tried, which are theoretically the same with some variations.
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.openai.com/v1/completions', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.$api_key
],
'json' => [
'prompt' => $texto,
'temperature' => 0,
'max_tokens' => $tokens,
'model' => 'text-davinci-002',
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0
]
]);
$json_response = json_decode($response->getBody(), true);
-----------
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$postFields = '{
"model": "text-davinci-002",
"prompt": "'.$texto.'",
"temperature": 0,
"max_tokens": $tokens,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer ' . $api_key;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }
curl_close($ch);
-----------
$ch = curl_init();
$data = array(
"model" => "text-davinci-002",
"prompt" => $texto,
"temperature" => 0.5,
"max_tokens" => $tokens,
"top_p" => 0.9
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:Bearer ' . $api_key));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
curl_close($ch);