Struggling to create a text summarization code using PHP and the OpenAI API

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);

Are you able to get something workable in the API Playground? Doesn’t sound like this is a PHP or code specific thing, but all in the model selection/configuration and prompt engineering.

Thank you for the response. Yes, in fact, I am trying to use the parameters that appear in Playground (model, temperature, max tokens, top p). I also ask Chatgpt to give me a summary and tell me which parameters can be used to obtain the same or similar result :slightly_frowning_face:

Edit: With tet-davinco-03 it seems to work… I have tried before, dunno why now this. Is there a way to limit the response max length without text truncate?

It is normal that it takes 10 seconds to answer?

If TD2 can’t handle it and TD3 is too expensive, have you thought about gpt-3.5-turbo?

I would set top_p to 1 and temp to like .7 too…

Yeah gpt-3.5-turbo provides great results if guided right.

Even a primitive implementation can get you quite far.

Sunco007 is struggling to create a text summarization code using PHP and the OpenAI API. 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. Sunco007 has tried various configurations in the Playground and others suggested by Chatgpt, but the API still does not consistently provide a summary. Sunco007 has tried various codes theoretically the same with some variations. Novaphil suggests that it may not be a PHP or code-specific thing, but all in the model selection/configuration and prompt engineering. Sunco007 is trying to use the parameters that appear in Playground (model, temperature, max tokens, top p). With tet-davinco-03, it seems to work. Sunco007 is asking if there is a way to limit the response max length without text truncate, and it is normal that it takes 10 seconds to answer. PaulBellow suggests using gpt-3.5-turbo if TD2 can’t handle it and TD3 is too expensive. Sunco007 has set the “temperature” to 0.5, “max_tokens” to $tokens, and “top_p” to 0.9. Sunco007 has been advised to set top_p to 1 and temp to like .7 too.

1 Like

Thanks for your answers… what do you mean with gpt-3.5-turbo ? If I set that as a model, it sends me an error saying is for chats and must change the endpoint

Asking Chatgpt, it says is the same as text-davinci-002. But when I use that model, is like 4 to 5 seconds but the answer sometimes is the original text, sometimes is paragraphs

I’m using this text: The next text is in spanish and I need a summary with a 400 characters maximun in spanish

Must specify is in spanish because sometimes the result is in english

Sorry about my poor english, this was not translated with Chatgpt

If you use the chat models, e.g. gpt-3.5-turbo or gpt-4, you have to use a different endpoint:
https://api.openai.com/v1/chat/completions

1 Like

@UncleDirkTV is correct. It might take a bit to wrap your head around it, but ChatML (chat completion endpoint with system/user/assistant messages is a lot more useful in many ways…)

1 Like

Hi again… I ended up using text-davinci-003. The thing is that gpt-3.5-turbo was taking about 2 seconds less, but the gain wasn’t that significant and the generated texts were not as good. The texts I’m sending are news articles from the newspaper where I work.

Now I’m going to see how to send longer texts because I’ve reached the token limit.

1 Like

You may also want to check out these text summarization strategies beyond “stuffing”, which appears to be what you’re doing now.

  • Building a Summarization System with LangChain and GPT-3 - Part 1 - YouTube
    • Summarization Methodologies
      • Map Reduce
        • Chunk document. Summarize each chunk, then summarize all the chunk summaries. Using this currently in embed_solr_index01.php.
      • Stuffing
        • Summarize entire document all at once, if it will fit into prompt.
      • Refine
        • Chunk document. Summarize first chunk. Summarize 2nd chunk + 1st chunk summary. Summarize 3rd chunk + 1st and 2nd chunk summary. And so on…
2 Likes