Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/muath-ye/stream-openai


https://github.com/muath-ye/stream-openai

Last synced: 6 days ago
JSON representation

Awesome Lists containing this project

README

        

# Streaming OpenAI Responses in Laravel with Server-Sent Events (SSE)

```sh
laravel new laravel-openai-streaming
```

```sh
composer require openai-php/laravel
```

```sh
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
```

`.env`

```.env
OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=org-...
```

`welcome.blade.php`

```blade


Laravel













Laravel Streaming OpenAI



Streaming OpenAI Responses in Laravel with Server-Sent Events (SSE).
Read tutorial here








Submit











const form = document.querySelector('form');
const result = document.getElementById("result");

form.addEventListener('submit', (event) => {
event.preventDefault();
const input = event.target.input.value
if (input === "") return;
const question = document.getElementById("question");
question.innerText = input;
event.target.input.value = "";

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

```

```sh
php artisan make:controller AskController
```

`AskController.php`

```php
query('question');
return response()->stream(function () use ($question) {
$stream = OpenAI::completions()->createStreamed([
'model' => 'gpt-3.5-turbo-instruct',
'prompt' => $question,
'max_tokens' => 1024,
]);

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

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

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

`routes/web.php`

```php