-
-
Notifications
You must be signed in to change notification settings - Fork 607
feat(OpenAI): Realtime Ephermal Tokens #591
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
Open
iBotPeaches
wants to merge
8
commits into
main
Choose a base branch
from
realtime-tokens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae13f07
feat: initial work on realtime tokens
iBotPeaches dc5a524
feat: more work on realtime tokens
iBotPeaches 9ee14d7
fix: add realtime class to client
iBotPeaches 67b7bc3
fix: threshold is float, not int
iBotPeaches 6e5bdfb
fix: modalities can be null on transcribe
iBotPeaches 61f4b60
feat: test build out for realtime
iBotPeaches d25c6f5
chore: correct docblock
iBotPeaches 38ebf3b
test: integration tests for realtime
iBotPeaches File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Contracts\Resources; | ||
|
||
use OpenAI\Responses\Realtime\SessionResponse; | ||
use OpenAI\Responses\Realtime\TranscriptionSessionResponse; | ||
|
||
interface RealtimeContract | ||
{ | ||
/** | ||
* Create an ephemeral API token for real time sessions. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function token(array $parameters = []): SessionResponse; | ||
|
||
/** | ||
* Create an ephemeral API token for real time transcription sessions. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create-transcription | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function transcribeToken(array $parameters = []): TranscriptionSessionResponse; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Resources; | ||
|
||
use OpenAI\Contracts\Resources\RealtimeContract; | ||
use OpenAI\Responses\Realtime\SessionResponse; | ||
use OpenAI\Responses\Realtime\TranscriptionSessionResponse; | ||
use OpenAI\ValueObjects\Transporter\Payload; | ||
use OpenAI\ValueObjects\Transporter\Response; | ||
|
||
/** | ||
* @phpstan-import-type SessionType from SessionResponse | ||
* @phpstan-import-type TranscriptionSessionType from TranscriptionSessionResponse | ||
*/ | ||
final class Realtime implements RealtimeContract | ||
{ | ||
use Concerns\Transportable; | ||
|
||
/** | ||
* Create an ephemeral API token for real time sessions. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function token(array $parameters = []): SessionResponse | ||
{ | ||
$payload = Payload::create('realtime/sessions', $parameters); | ||
|
||
/** @var Response<SessionType> $response */ | ||
$response = $this->transporter->requestObject($payload); | ||
|
||
return SessionResponse::from($response->data()); | ||
} | ||
|
||
/** | ||
* Create an ephemeral API token for real time transcription sessions. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create-transcription | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function transcribeToken(array $parameters = []): TranscriptionSessionResponse | ||
{ | ||
$payload = Payload::create('realtime/transcription_sessions', $parameters); | ||
|
||
/** @var Response<TranscriptionSessionType> $response */ | ||
$response = $this->transporter->requestObject($payload); | ||
|
||
return TranscriptionSessionResponse::from($response->data()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Responses\Realtime\Session; | ||
|
||
use OpenAI\Contracts\ResponseContract; | ||
use OpenAI\Responses\Concerns\ArrayAccessible; | ||
use OpenAI\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* @phpstan-type ClientSecretType array{expires_at: int, value: string} | ||
* | ||
* @implements ResponseContract<ClientSecretType> | ||
*/ | ||
final class ClientSecret implements ResponseContract | ||
{ | ||
/** | ||
* @use ArrayAccessible<ClientSecretType> | ||
*/ | ||
use ArrayAccessible; | ||
|
||
use Fakeable; | ||
|
||
private function __construct( | ||
public readonly int $expiresAt, | ||
public readonly string $value, | ||
) {} | ||
|
||
/** | ||
* @param ClientSecretType $attributes | ||
*/ | ||
public static function from(array $attributes): self | ||
{ | ||
return new self( | ||
expiresAt: $attributes['expires_at'], | ||
value: $attributes['value'], | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'expires_at' => $this->expiresAt, | ||
'value' => $this->value, | ||
]; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Responses/Realtime/Session/InputAudioTranscription.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Responses\Realtime\Session; | ||
|
||
use OpenAI\Contracts\ResponseContract; | ||
use OpenAI\Responses\Concerns\ArrayAccessible; | ||
use OpenAI\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* @phpstan-type InputAudioTranscriptionType array{model: 'whisper-1'} | ||
* | ||
* @implements ResponseContract<InputAudioTranscriptionType> | ||
*/ | ||
final class InputAudioTranscription implements ResponseContract | ||
{ | ||
/** | ||
* @use ArrayAccessible<InputAudioTranscriptionType> | ||
*/ | ||
use ArrayAccessible; | ||
|
||
use Fakeable; | ||
|
||
/** | ||
* @param "whisper-1" $model | ||
*/ | ||
private function __construct( | ||
public readonly string $model | ||
) {} | ||
|
||
/** | ||
* @param InputAudioTranscriptionType $attributes | ||
*/ | ||
public static function from(array $attributes): self | ||
{ | ||
return new self( | ||
model: $attributes['model'], | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'model' => $this->model, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Responses\Realtime\Session; | ||
|
||
use OpenAI\Contracts\ResponseContract; | ||
use OpenAI\Responses\Concerns\ArrayAccessible; | ||
use OpenAI\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* @phpstan-type TurnDetectionType array{prefix_padding_ms: int, silence_duration_ms: int, threshold: float, type: 'server_vad'} | ||
* | ||
* @implements ResponseContract<TurnDetectionType> | ||
*/ | ||
final class TurnDetection implements ResponseContract | ||
{ | ||
/** | ||
* @use ArrayAccessible<TurnDetectionType> | ||
*/ | ||
use ArrayAccessible; | ||
|
||
use Fakeable; | ||
|
||
/** | ||
* @param 'server_vad' $type | ||
*/ | ||
private function __construct( | ||
public readonly int $prefixPaddingMs, | ||
public readonly int $silenceDurationMs, | ||
public readonly float $threshold, | ||
public readonly string $type, | ||
) {} | ||
|
||
/** | ||
* @param TurnDetectionType $attributes | ||
*/ | ||
public static function from(array $attributes): self | ||
{ | ||
return new self( | ||
prefixPaddingMs: $attributes['prefix_padding_ms'], | ||
silenceDurationMs: $attributes['silence_duration_ms'], | ||
threshold: $attributes['threshold'], | ||
type: $attributes['type'], | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'prefix_padding_ms' => $this->prefixPaddingMs, | ||
'silence_duration_ms' => $this->silenceDurationMs, | ||
'threshold' => $this->threshold, | ||
'type' => $this->type, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.