Skip to content

Commit 2b45a03

Browse files
committed
Add audio speech endpoint
1 parent db18414 commit 2b45a03

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ foreach($stream as $response){
292292

293293
### `Audio` Resource
294294

295+
#### `speech`
296+
297+
Generates audio from the input text.
298+
299+
```php
300+
$client->audio()->speech([
301+
'model' => 'tts-1',
302+
'input' => 'The quick brown fox jumped over the lazy dog.',
303+
'voice' => 'alloy',
304+
]); // audio file content as string
305+
```
306+
295307
#### `transcribe`
296308

297309
Transcribes audio into the input language.

src/Contracts/Resources/AudioContract.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77

88
interface AudioContract
99
{
10+
/**
11+
* Generates audio from the input text.
12+
*
13+
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
14+
*
15+
* @param array<string, mixed> $parameters
16+
*/
17+
public function speech(array $parameters): string;
18+
1019
/**
1120
* Transcribes audio into the input language.
1221
*

src/Resources/Audio.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ final class Audio implements AudioContract
1414
{
1515
use Concerns\Transportable;
1616

17+
/**
18+
* Generates audio from the input text.
19+
*
20+
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
21+
*
22+
* @param array<string, mixed> $parameters
23+
*/
24+
public function speech(array $parameters): string
25+
{
26+
$payload = Payload::create('audio/speech', $parameters);
27+
28+
return $this->transporter->requestContent($payload);
29+
}
30+
1731
/**
1832
* Transcribes audio into the input language.
1933
*

src/Testing/Resources/AudioTestResource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ protected function resource(): string
1717
return Audio::class;
1818
}
1919

20+
public function speech(array $parameters): string
21+
{
22+
return $this->record(__FUNCTION__, $parameters);
23+
}
24+
2025
public function transcribe(array $parameters): TranscriptionResponse
2126
{
2227
return $this->record(__FUNCTION__, $parameters);

tests/Fixtures/Audio.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,8 @@ function audioFileResource()
153153
{
154154
return fopen(__DIR__.'/audio.mp3', 'r');
155155
}
156+
157+
function audioFileContent(): string
158+
{
159+
return file_get_contents(__DIR__.'/audio.mp3');
160+
}

tests/Resources/Audio.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,21 @@
191191
expect($result->meta())
192192
->toBeInstanceOf(MetaInformation::class);
193193
});
194+
195+
test('text to speech', function () {
196+
$client = mockContentClient('POST', 'audio/speech', [
197+
'model' => 'tts-1',
198+
'input' => 'Hello, how are you?',
199+
'voice' => 'alloy',
200+
], audioFileContent());
201+
202+
$result = $client->audio()->speech([
203+
'model' => 'tts-1',
204+
'input' => 'Hello, how are you?',
205+
'voice' => 'alloy',
206+
]);
207+
208+
expect($result)
209+
->toBeString()
210+
->toBe(audioFileContent());
211+
});

tests/Testing/Resources/AudioTestResource.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55
use OpenAI\Responses\Audio\TranslationResponse;
66
use OpenAI\Testing\ClientFake;
77

8+
it('records a speech request', function () {
9+
$fake = new ClientFake([
10+
'fake-mp3-content',
11+
]);
12+
13+
$fake->audio()->speech([
14+
'model' => 'tts-1',
15+
'input' => 'Hello, how are you?',
16+
'voice' => 'alloy',
17+
]);
18+
19+
$fake->assertSent(Audio::class, function ($method, $parameters) {
20+
return $method === 'speech' &&
21+
$parameters === [
22+
'model' => 'tts-1',
23+
'input' => 'Hello, how are you?',
24+
'voice' => 'alloy',
25+
];
26+
});
27+
});
28+
829
it('records an audio transcription request', function () {
930
$fake = new ClientFake([
1031
TranscriptionResponse::fake(),

0 commit comments

Comments
 (0)