1. Go to this page and download the library: Download sferra/delta 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 Sferra\Delta\Facades\Delta;
$shouldCancel = false;
$response = Delta::stream(
messages: [['role' => 'user', 'content' => 'Tell me a long story.']],
onChunk: fn(string $chunk) => echo $chunk,
shouldCancel: fn () => $shouldCancel
);
// Later, from another part of the app (e.g. a Livewire cancel button):
$shouldCancel = true;
// SideChat.php (Livewire component)
public bool $shouldCancel = false;
public function cancelStream(): void
{
$this->shouldCancel = true;
}
#[On('sendToAI')]
public function streamToAI(): void
{
$this->shouldCancel = false;
Delta::stream(
messages: $this->messages,
onChunk: fn (string $chunk) => $this->stream('response', $chunk),
shouldCancel: fn () => $this->shouldCancel
);
}