From dabe869ee931807db8fd700d5d64cc04605955ec Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Fri, 1 Nov 2024 10:36:16 +0100 Subject: [PATCH] Document realtime API --- ChangeLog.md | 6 ++++++ README.md | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9af7160..d486347 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ OpenAI APIs for XP ChangeLog ## ?.?.? / ????-??-?? +## 0.7.0 / ????-??-?? + +* Merged PR #15: Implement realtime API. This implements issue #8 in a + new `com.openai.realtime` package + (@thekid) + ## 0.6.0 / 2024-10-27 * Merged PR #9: Support uploading files, e.g. for transcribing audio. diff --git a/README.md b/README.md index 9171598..d2d7147 100755 --- a/README.md +++ b/README.md @@ -305,7 +305,38 @@ For more complex load balancing, have a look at [this blog article using Azure A Realtime API ------------ -*Coming soon* +The realtime API allows streaming audio and/or text to and from language models, see https://platform.openai.com/docs/guides/realtime + +```php +use com\openai\realtime\RealtimeApi; +use util\cmd\Console; + +$api= new RealtimeApi('wss://example.openai.azure.com/openai/realtime?'. + '?api-version=2024-10-01-preview'. + '&deployment=gpt-4o-realtime-preview' +); +$session= $api->connect(['api-key' => getenv('AZUREAI_API_KEY')]); +Console::writeLine($session); + +// Send prompt +$api->transmit([ + 'type' => 'conversation.item.create', + 'item' => [ + 'type' => 'message', + 'role' => 'user', + 'content' => $content, + ] +]); + +// Receive response(s) +$api->send(['type' => 'response.create', 'response' => ['modalities' => ['text']]]); +do { + $event= $api->receive(); + Console::writeLine($event); +} while ('response.done' !== $event['type'] && 'error' !== $event['type']); + +$api->close(); +``` See also --------