Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement realtime API #15

Merged
merged 8 commits into from
Nov 1, 2024
Merged

Implement realtime API #15

merged 8 commits into from
Nov 1, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 27, 2024

This pull request implements #8 in a new com.openai.realtime package. Quoting the OpenAI platform docs:

The Realtime API enables you to build low-latency, multi-modal conversational experiences. It currently supports text and audio as both input and output, as well as function calling.

The Azure API also supports images by passing ['type' => 'ms_image', 'image' => 'data:image/webp;base64,...'], which is not documented but can be reversed engineered from the API's error messages.

Example

This example uses Azure OpenAI version:

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

@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

The structure for the tools element differs from that used in the REST API in that it has no function substructure:

Realtime

[
  [
    'type'        => 'function',
    'name'        => 'get_person',
    'description' => 'Gets a person',
    'parameters'  => [...]
  ]
]

REST

[
  [
    'type'        => 'function',
    'function'    => [
      'name'        => 'get_person',
      'description' => 'Gets a person',
      'parameters'  => [...]
    ]
  ]
]

This means the com.openai.Tools class cannot completely be used in a generic way, and we might need to specialize it for the two APIs.

@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

$api->connect(['api-key' => getenv('AZUREAI_API_KEY')]);

// Initial handshake: Server sends session.created
Console::writeLine($api->receive());

Should be return the session.created event from the connect() call? The documentation states this event is sent as soon as the connection is successfully established. Provides a connection-specific ID that may be useful for debugging or logging (source).

@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

@thekid thekid merged commit bc8bdb3 into main Nov 1, 2024
12 checks passed
@thekid thekid deleted the feature/realtime-api branch November 1, 2024 09:33
@thekid
Copy link
Member Author

thekid commented Nov 1, 2024

This means the com.openai.Tools class cannot completely be used in a generic way, and we might need to specialize it for the two APIs.

...or we make use of something like xp-forge/marshalling#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant