ahmadrosid / laravel-openai-streaming

Streaming OpenAI Responses in Laravel with Server-Sent Events (SSE).
14 stars 4 forks source link

Laravel Streaming OpenAI is not working. please support sir. i am kindly request #3

Open SingaravelC opened 8 months ago

SingaravelC commented 8 months ago

Screenshot (48)

SingaravelC commented 8 months ago

Please Reply Me Sir

ahmadrosid commented 8 months ago

Hi @SingaravelC the completion model is deprecated, you can use chat instead. So you can change the controller like this instead.


class AskController extends Controller
{
    public function __invoke(Request $request)
    {
        $question = $request->query('question');
        return response()->stream(function () use ($question) {
            $stream = OpenAI::chat()->createStreamed([
                'model' => 'gpt-3.5-turbo',
                'temperature' => 0.8,
                'messages' => [
                    [
                        'role' => 'user',
                        'content' => $question
                    ]
                ],
                'max_tokens' => 1024,
            ]);

            foreach ($stream as $response) {
                $text = $response->choices[0]->delta->content;
                if (connection_aborted()) {
                    break;
                }

                echo "event: update\n";
                echo 'data: ' . $text;
                echo "\n\n";
                ob_flush();
                flush();
            }

            echo "event: update\n";
            echo 'data: <END_STREAMING_SSE>';
            echo "\n\n";
            ob_flush();
            flush();
        }, 200, [
            'Cache-Control' => 'no-cache',
            'X-Accel-Buffering' => 'no',
            'Content-Type' => 'text/event-stream',
        ]);
    }
}
SingaravelC commented 8 months ago

Thank you for your supporting sir

SingaravelC commented 8 months ago

Screenshot (49)

i am configuration to paid openai api key. but do not working. please support sir. i am kindly request