1. Go to this page and download the library: Download halilcosdu/laravel-chatbot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use HalilCosdu\ChatBot\Facades\ChatBot;
$thread = ChatBot::createThread(
'Explain why the sky is blue.',
ownerId: auth()->id(),
);
$assistantMessage = ChatBot::updateThread(
'Why does it turn red at sunset?',
id: $thread->getKey(),
ownerId: auth()->id(),
);
echo $assistantMessage->content;
$stream = ChatBot::createThreadStreamed(
'Write a short welcome message.',
ownerId: auth()->id(),
);
$threadId = $stream->thread->getKey();
foreach ($stream as $delta) {
echo $delta;
}
// Available only after the stream has completed.
$savedMessage = $stream->assistantMessage();
$completed = $stream->completed();
use HalilCosdu\ChatBot\Facades\ChatBot;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::post('/chat', function (Request $request) {
$stream = ChatBot::createThreadStreamed(
subject: $request->string('message')->toString(),
ownerId: $request->user()->getAuthIdentifier(),
);
return response()->stream(function () use ($stream): void {
foreach ($stream as $delta) {
echo $delta;
if (ob_get_level() > 0) {
ob_flush();
}
flush();
}
}, 200, [
'Cache-Control' => 'no-cache, no-transform',
'Content-Type' => 'text/plain; charset=utf-8',
'X-Accel-Buffering' => 'no',
'X-Chatbot-Thread' => (string) $stream->thread->getKey(),
]);
});