Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/muath-ye/stream-openai
https://github.com/muath-ye/stream-openai
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/muath-ye/stream-openai
- Owner: muath-ye
- Created: 2024-01-21T12:17:11.000Z (10 months ago)
- Default Branch: father
- Last Pushed: 2024-01-21T13:14:31.000Z (10 months ago)
- Last Synced: 2024-05-19T04:21:01.340Z (6 months ago)
- Language: Blade
- Size: 84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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