PHPackages                             premieroctet/php-stream-protocol - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. premieroctet/php-stream-protocol

ActiveLibrary[API Development](/categories/api)

premieroctet/php-stream-protocol
================================

A PHP library for handling AI streaming protocols, particularly the Vercel AI SDK Stream Protocol

v1.0.9(8mo ago)31.4k↓50%MITPHPPHP ^8.1

Since Jun 25Pushed 8mo agoCompare

[ Source](https://github.com/premieroctet/php-stream-protocol)[ Packagist](https://packagist.org/packages/premieroctet/php-stream-protocol)[ RSS](/packages/premieroctet-php-stream-protocol/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (11)Used By (0)

PHP Stream Protocol ▲
=====================

[](#php-stream-protocol-)

A PHP library for handling the [Vercel AI SDK Stream Protocol](https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol). This package provides an easy-to-use interface for creating streaming AI responses with support for tool calls, attachments, and various AI providers.

Features
--------

[](#features)

- 🚀 **Easy Integration**: Simple, fluent API for streaming responses
- 🔧 **Tool Support**: Built-in tool calling and execution
- 📎 **Attachments**: Handle file and image attachments
- 🔄 **Multi-Provider**: Support for OpenAI and other providers
- 📊 **Protocol Compliant**: Follows Vercel AI SDK Stream Protocol specifications
- ⚡ **Symfony Integration**: Built with Symfony components

Installation
------------

[](#installation)

```
composer require premieroctet/php-stream-protocol
```

Quick Start
-----------

[](#quick-start)

### Basic Usage

[](#basic-usage)

```
use PremierOctet\PhpStreamProtocol\StreamProtocol;
use OpenAI;

// Create a new StreamProtocol instance
$protocol = StreamProtocol::create()
    ->withSystemPrompt('You are a helpful assistant.');

// Register tools
$protocol->registerTool('get_weather', [WeatherService::class, 'getCurrentWeather']);

// In your controller
public function chat(Request $request): Response
{
    // Parse incoming messages
    $messages = $protocol->parseMessages($request->getContent());

    // Convert to OpenAI format and create request
    $openaiRequest = $protocol->buildOpenAIRequest($messages, 'gpt-4');

    // Create OpenAI stream
    $client = OpenAI::client($apiKey);
    $stream = $client->chat()->createStreamed($openaiRequest);

    // Return streaming response
    return $protocol->stream($stream);
}
```

### Symfony Usage

[](#symfony-usage)

```
