Skip to content

Commit

Permalink
Add audio speech endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 6, 2023
1 parent db18414 commit 2b45a03
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ foreach($stream as $response){

### `Audio` Resource

#### `speech`

Generates audio from the input text.

```php
$client->audio()->speech([
'model' => 'tts-1',
'input' => 'The quick brown fox jumped over the lazy dog.',
'voice' => 'alloy',
]); // audio file content as string
```

#### `transcribe`

Transcribes audio into the input language.
Expand Down
9 changes: 9 additions & 0 deletions src/Contracts/Resources/AudioContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

interface AudioContract
{
/**
* Generates audio from the input text.
*
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
*
* @param array<string, mixed> $parameters
*/
public function speech(array $parameters): string;

/**
* Transcribes audio into the input language.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Resources/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ final class Audio implements AudioContract
{
use Concerns\Transportable;

/**
* Generates audio from the input text.
*
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
*
* @param array<string, mixed> $parameters
*/
public function speech(array $parameters): string
{
$payload = Payload::create('audio/speech', $parameters);

return $this->transporter->requestContent($payload);
}

/**
* Transcribes audio into the input language.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Testing/Resources/AudioTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ protected function resource(): string
return Audio::class;
}

public function speech(array $parameters): string
{
return $this->record(__FUNCTION__, $parameters);
}

public function transcribe(array $parameters): TranscriptionResponse
{
return $this->record(__FUNCTION__, $parameters);
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,8 @@ function audioFileResource()
{
return fopen(__DIR__.'/audio.mp3', 'r');
}

function audioFileContent(): string
{
return file_get_contents(__DIR__.'/audio.mp3');
}
18 changes: 18 additions & 0 deletions tests/Resources/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,21 @@
expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});

test('text to speech', function () {
$client = mockContentClient('POST', 'audio/speech', [
'model' => 'tts-1',
'input' => 'Hello, how are you?',
'voice' => 'alloy',
], audioFileContent());

$result = $client->audio()->speech([
'model' => 'tts-1',
'input' => 'Hello, how are you?',
'voice' => 'alloy',
]);

expect($result)
->toBeString()
->toBe(audioFileContent());
});
21 changes: 21 additions & 0 deletions tests/Testing/Resources/AudioTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
use OpenAI\Responses\Audio\TranslationResponse;
use OpenAI\Testing\ClientFake;

it('records a speech request', function () {
$fake = new ClientFake([
'fake-mp3-content',
]);

$fake->audio()->speech([
'model' => 'tts-1',
'input' => 'Hello, how are you?',
'voice' => 'alloy',
]);

$fake->assertSent(Audio::class, function ($method, $parameters) {
return $method === 'speech' &&
$parameters === [
'model' => 'tts-1',
'input' => 'Hello, how are you?',
'voice' => 'alloy',
];
});
});

it('records an audio transcription request', function () {
$fake = new ClientFake([
TranscriptionResponse::fake(),
Expand Down

0 comments on commit 2b45a03

Please sign in to comment.