Facing an issue in spanish text when api call.. and response time is too long

Hi
I have calling API but when asked in spanish text then it disturb some UI in long text and also point 10th comes within point 9th . image below you see


and in english it is ok no ui disturb

and the last thing is its take response time too long like one mint even two mint

here it is my code

        $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);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"model\": \"text-davinci-003\",\n  \"prompt\": \"$request->aitopic\",\n  \"temperature\": 0.6,\n  \"max_tokens\": 2048,\n  \"top_p\": 1,\n  \"frequency_penalty\": 1,\n  \"presence_penalty\": 1\n}");


        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Authorization: Bearer sk-ZrDc9eizI*****************************';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        /*if (curl_errno($ch)) {
            return response()->json();
        }*/
        $data = json_decode($result,true);
        

        $ai = nl2br($data['choices'][0]['text']) ?? NULL;
        curl_close($ch);

        if(is_null()){
            return response()->json($ai);
        }
        return response()->json([$ai,$request->aitopic]);

Thanks

¿Qué pregunta le haces? Yo estoy haciendo resumen y le tengo que poner así, de lo contrario me arrojaba distintas cosas

El siguiente texto es en español y ocupo un resumen en español: [TEXTO AQUI]

Al intentar tomar puntos principales le tuve que poner así:

Ocupo un lista de 3 o 4 puntos principales del siguiente texto y cada punto debe comenzar con un guión (-) y espacio en blanco y debe haber 2 saltos de línea entre cada punto: [TEXTO AQUI]

En tu caso no se si tenga que ver con los max_token que estás enviando. Los que yo envío los hago con un cálculo:

$longitud_texto = strlen($texto); 
$porcentaje_max_tokens = 0.12;
$tokens = (int)($longitud_texto * $porcentaje_max_tokens);

if ($tokens < 200) { $tokens = 200; }

This actually happens to me quite a bit in english as well. What I have been doing is just making sure that it responds in some sort of format, like XML, JSON, etc, and then I parse it with a simple function. Cuts down on the formatting issues.

As a tip, make sure to add an example of the format you want along with the instructions and prompt.