Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wishborn's avatar

chat streaming OpenAI-PHP and Livewire

Is there anyone in here that is developing with the OpenAI-php package and with Livewire? I'm having some difficulty trying to implement streaming. My AI code is in a separate controller that my livewire chat component uses, how can I bridge the streaming output from the OpenAI client to livewire?

0 likes
1 reply
LaryAI's avatar
Level 58

To implement streaming with the OpenAI-PHP package and Livewire, you need to bridge the streaming output from the OpenAI client to your Livewire component. Here’s a step-by-step solution to achieve this:

  1. Set Up OpenAI-PHP: Ensure you have the OpenAI-PHP package installed and configured.

  2. Create a Livewire Component: Create a Livewire component for your chat interface.

  3. Controller for OpenAI: Create a controller that handles the interaction with the OpenAI API.

  4. Streaming with OpenAI: Implement the streaming logic in your controller.

  5. Livewire Event Handling: Use Livewire events to handle the streaming data in real-time.

Step-by-Step Implementation

1. Set Up OpenAI-PHP

Make sure you have the OpenAI-PHP package installed via Composer:

composer require openai-php/client

2. Create a Livewire Component

Generate a Livewire component:

php artisan make:livewire Chat

3. Controller for OpenAI

Create a controller to handle OpenAI requests:

php artisan make:controller OpenAIController

In OpenAIController.php:

namespace App\Http\Controllers;

use OpenAI\Client;
use Illuminate\Http\Request;

class OpenAIController extends Controller
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client('your-openai-api-key');
    }

    public function stream(Request $request)
    {
        $response = $this->client->completions()->create([
            'model' => 'text-davinci-003',
            'prompt' => $request->input('prompt'),
            'stream' => true,
        ]);

        foreach ($response as $chunk) {
            event(new \App\Events\OpenAIStream($chunk));
        }
    }
}

4. Streaming with OpenAI

Ensure your OpenAI client is set up to handle streaming. The above example assumes the OpenAI client supports streaming out of the box.

5. Livewire Event Handling

Create an event for streaming data:

php artisan make:event OpenAIStream

In OpenAIStream.php:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class OpenAIStream implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $chunk;

    public function __construct($chunk)
    {
        $this->chunk = $chunk;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('chat');
    }
}

In your Livewire component (Chat.php):

namespace App\Http\Livewire;

use Livewire\Component;

class Chat extends Component
{
    public $messages = [];

    protected $listeners = ['echo:chat,OpenAIStream' => 'handleStream'];

    public function handleStream($event)
    {
        $this->messages[] = $event['chunk'];
    }

    public function render()
    {
        return view('livewire.chat');
    }
}

In your Livewire view (chat.blade.php):

<div>
    <ul>
        @foreach($messages as $message)
            <li>{{ $message }}</li>
        @endforeach
    </ul>
</div>

Summary

  1. Set up OpenAI-PHP: Install and configure the OpenAI-PHP package.
  2. Create Livewire Component: Generate a Livewire component for the chat.
  3. Controller for OpenAI: Create a controller to handle OpenAI API requests and streaming.
  4. Streaming with OpenAI: Implement the streaming logic in the controller.
  5. Livewire Event Handling: Use Livewire events to handle real-time streaming data.

This setup should allow you to stream data from the OpenAI client to your Livewire component in real-time.

Please or to participate in this conversation.