File tree Expand file tree Collapse file tree 7 files changed +84
-0
lines changed Expand file tree Collapse file tree 7 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,18 @@ foreach($stream as $response){
292
292
293
293
### ` Audio ` Resource
294
294
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
+
295
307
#### ` transcribe `
296
308
297
309
Transcribes audio into the input language.
Original file line number Diff line number Diff line change 7
7
8
8
interface AudioContract
9
9
{
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
+
10
19
/**
11
20
* Transcribes audio into the input language.
12
21
*
Original file line number Diff line number Diff line change @@ -14,6 +14,20 @@ final class Audio implements AudioContract
14
14
{
15
15
use Concerns \Transportable;
16
16
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
+
17
31
/**
18
32
* Transcribes audio into the input language.
19
33
*
Original file line number Diff line number Diff line change @@ -17,6 +17,11 @@ protected function resource(): string
17
17
return Audio::class;
18
18
}
19
19
20
+ public function speech (array $ parameters ): string
21
+ {
22
+ return $ this ->record (__FUNCTION__ , $ parameters );
23
+ }
24
+
20
25
public function transcribe (array $ parameters ): TranscriptionResponse
21
26
{
22
27
return $ this ->record (__FUNCTION__ , $ parameters );
Original file line number Diff line number Diff line change @@ -153,3 +153,8 @@ function audioFileResource()
153
153
{
154
154
return fopen (__DIR__ .'/audio.mp3 ' , 'r ' );
155
155
}
156
+
157
+ function audioFileContent (): string
158
+ {
159
+ return file_get_contents (__DIR__ .'/audio.mp3 ' );
160
+ }
Original file line number Diff line number Diff line change 191
191
expect ($ result ->meta ())
192
192
->toBeInstanceOf (MetaInformation::class);
193
193
});
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
+ });
Original file line number Diff line number Diff line change 5
5
use OpenAI \Responses \Audio \TranslationResponse ;
6
6
use OpenAI \Testing \ClientFake ;
7
7
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
+
8
29
it ('records an audio transcription request ' , function () {
9
30
$ fake = new ClientFake ([
10
31
TranscriptionResponse::fake (),
You can’t perform that action at this time.
0 commit comments