xp-forge / aws

AWS Core for the XP Framework
0 stars 0 forks source link

Support AWS event streams (`application/vnd.amazon.eventstream`) #18

Closed thekid closed 1 month ago

thekid commented 1 month ago

This pull request adds a method events() to the Response class. It returns an EventStream instance for the content type application/vnd.amazon.eventstream.

Example

Streaming responses from Bedrock AI models:

use com\amazon\aws\{ServiceEndpoint, CredentialProvider};
use util\cmd\Console;

$runtime= (new ServiceEndpoint('bedrock', CredentialProvider::default()))
  ->using('bedrock-runtime.')
  ->in('eu-central-1')
;
$events= $runtime
  ->resource('/model/anthropic.claude-3-5-sonnet-20240620-v1:0/converse-stream')
  ->transmit($payload, 'application/json')
  ->events()
;

// Streams responses from AI. 
foreach ($events as $event) { 
  Console::writeLine($event->header(':event-type'), ': ', $event->value());
}

Message format

image Source: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTSelectObjectAppendix.html

See also

thekid commented 1 month ago

Released in https://github.com/xp-forge/aws/releases/tag/v2.6.0