1

In my Laravel application, I ran into a strange problem. I get a response from the API, but everything is in spaces in Russian, and everything is OK in English. Please tell me how to fix it?

chatcontroller.php:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\MessageRequest;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use App\Models\Message;
use OpenAI\Laravel\Facades\OpenAI;
use Illuminate\Support\Facades\Auth;

class ChatController extends Controller
{
    private $openAIRequest;

    public function __construct()
    {
        $this->openAIRequest = Http::withoutVerifying()
            ->withOptions(['timeout' => 120])
            ->withHeaders([
                'Authorization' => 'Bearer ' . config('openai.api_key'),
                'Content-Type' => 'application/json',
            ]);
    }

    public function messages(Request $request, MessageRequest $messageRequest)
    {
        
        $prompt = $messageRequest->input('prompt');
        $proglang = $messageRequest->input('proglang');
        $framework = $messageRequest->input('framework');
        $size = $messageRequest->input('size');
        $lang = $messageRequest->input('lang');
        $message = 'Задокументируйте код на языке программирования: ' . $proglang . ' с использованием фреймворка: ' . $framework .
            ' Документация должна быть в формате: ' . $size . ' Объект - предназначение' . ' и написана на ' . $lang . ' языке.' .
            ' Код, который необходимо документировать: ' . $prompt . ' Если в поле для кода будет короткий код, то мы его всё равно описываем, а если код отсутствует, то сообщи об этом. Текже не повторяй в своем ответе мой запрос, сконкретизируся на документации';

       Message::create([
            'user_id' => Auth::user()->id,
            'prompt' => $prompt,
            'proglang' => $proglang,
            'framework' => $framework,
            'size' => $size,
            'lang' => $lang,
        ]);

        return $this->askToChatGPT($message);
    }

    private function askToChatGPT($message)
    {
        $question = $message;
        return response()->stream(function () use ($question) {
            $stream = OpenAI::completions()->createStreamed([
                'model' => 'gpt-3.5-turbo-instruct',
                'prompt' => $question,
                'max_tokens' => 2000,
                'temperature' => 0.3,
            ]);
    
            foreach ($stream as $response) {
                $text = trim($response->choices[0]->text);
                if (connection_aborted()) {
                    break;
                }
    
                echo "\n";
                echo $text;
                echo "\n\n";
                ob_flush();
                flush();
            }
        }, 200, [
            'Cache-Control' => 'no-cache',
            'X-Accel-Buffering' => 'no',
            'Content-Type' => 'text/event-stream; charset=utf-8',
        ]);
    }    

}

А также скрипт страницы с API:

$(document).on("click", "#submit-btn", function (e) {
    e.preventDefault();

    var form     = $("#ask-chatgpt-form");
    var formData = form.serialize();
    var textarea = form.find('textarea[name="prompt"]');

    var selectedproglang = $('select[name="proglang"]').val();
    formData += '&proglang=' + selectedproglang;

    var selectedframework = $('select[name="framework"]').val();
    formData += '&framework=' + selectedframework;

    var selectedsize = $('select[name="size"]').val();
    formData += '&size=' + selectedsize;

    var selectedlang = $('select[name="lang"]').val();
    formData += '&lang=' + selectedlang;

    generateChatBoxMessage(false, textarea.val());
    textarea.val('');

    function generateChatBoxMessage(isChatGPTMessage, messageText) {
        var template   = isChatGPTMessage ? $("#chatgpt-message-box-template").html() : $("#user-message-box-template").html();
        var messageBox = $(template);
        messageBox.find('#message-box-text').text(messageText);
        $('#chat-box').append(messageBox);
    }

    $.ajax({
        type: "GET",
        url: 'chatgpt/ask',
        data: formData,
        success: function (answer) {
            generateChatBoxMessage(true, answer);
        },
    });
});

const source = new EventSource('chatgpt/ask' + encodeURIComponent(input));
source.addEventListener('update', function (event) {
    if (event.data === "<END_STREAMING_SSE>") {
        source.close();
        return;
    }
    result.innerText += event.data
});

// Обновляем скрытое поле при выборе значения в с-електоре
$('select').change(function () {
    var selectedValue = $(this).val();
    $(this).siblings('input[type="hidden"]').val(selectedValue);
});

P.S. If you don’t have enough information or code, then write in the comments and I will definitely supplement the question!

I tried to output the response to the console and also in a simple div. The response also goes to the console, only not separated by a space, but through a line, and in an empty div also separated by a space.

enter image description here

enter image description here

2
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Oct 26, 2023 at 13:56
  • Explain the foreach on $streams? just $response['choices'][0]['message']['content'] and by this your response from the chat should have '\n'. Can you dump the text please. Commented Oct 27, 2023 at 15:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.