Skip to content

Commit ad2d81a

Browse files
committed
[Platform][ElevenLabs] Fix option handling
1 parent 4650932 commit ad2d81a

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/Bridge/ElevenLabs/ElevenLabsClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function request(Model $model, array|string $payload, array $options = []
5151
throw new InvalidArgumentException(\sprintf('The model "%s" does not support text-to-speech.', $model->getName()));
5252
}
5353

54-
return $this->doTextToSpeechRequest($model, $payload, $options);
54+
return $this->doTextToSpeechRequest($model, $payload, array_merge($options, $model->getOptions()));
5555
}
5656

5757
/**
@@ -76,16 +76,16 @@ private function doSpeechToTextRequest(Model $model, array|string $payload): Raw
7676
*/
7777
private function doTextToSpeechRequest(Model $model, array|string $payload, array $options): RawHttpResult
7878
{
79-
if (!\array_key_exists('voice', $model->getOptions())) {
79+
if (!\array_key_exists('voice', $options)) {
8080
throw new InvalidArgumentException('The voice option is required.');
8181
}
8282

8383
if (!\array_key_exists('text', $payload)) {
8484
throw new InvalidArgumentException('The payload must contain a "text" key.');
8585
}
8686

87-
$voice = $options['voice'] ??= $model->getOptions()['voice'];
88-
$stream = $options['stream'] ??= $model->getOptions()['stream'] ?? false;
87+
$voice = $options['voice'];
88+
$stream = $options['stream'] ?? false;
8989

9090
$url = $stream
9191
? \sprintf('%s/text-to-speech/%s/stream', $this->hostUrl, $voice)

tests/Bridge/ElevenLabs/ElevenLabsClientTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,35 @@ public function testClientCanPerformTextToSpeechRequest()
155155
$this->assertSame(2, $httpClient->getRequestsCount());
156156
}
157157

158+
public function testClientCanPerformTextToSpeechRequestWhenVoiceKeyIsProvidedAsRequestOption()
159+
{
160+
$payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3');
161+
162+
$httpClient = new MockHttpClient([
163+
new JsonMockResponse([
164+
[
165+
'model_id' => ElevenLabs::ELEVEN_MULTILINGUAL_V2,
166+
'can_do_text_to_speech' => true,
167+
],
168+
]),
169+
new MockResponse($payload->asBinary()),
170+
]);
171+
172+
$client = new ElevenLabsClient(
173+
$httpClient,
174+
'https://api.elevenlabs.io/v1',
175+
'my-api-key',
176+
);
177+
178+
$client->request(new ElevenLabs(ElevenLabs::ELEVEN_MULTILINGUAL_V2), [
179+
'text' => 'foo',
180+
], [
181+
'voice' => 'Dslrhjl3ZpzrctukrQSN',
182+
]);
183+
184+
$this->assertSame(2, $httpClient->getRequestsCount());
185+
}
186+
158187
public function testClientCanPerformTextToSpeechRequestAsStream()
159188
{
160189
$payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3');
@@ -185,4 +214,35 @@ public function testClientCanPerformTextToSpeechRequestAsStream()
185214
$this->assertInstanceOf(RawHttpResult::class, $result);
186215
$this->assertSame(2, $httpClient->getRequestsCount());
187216
}
217+
218+
public function testClientCanPerformTextToSpeechRequestAsStreamVoiceKeyIsProvidedAsRequestOption()
219+
{
220+
$payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3');
221+
222+
$httpClient = new MockHttpClient([
223+
new JsonMockResponse([
224+
[
225+
'model_id' => ElevenLabs::ELEVEN_MULTILINGUAL_V2,
226+
'can_do_text_to_speech' => true,
227+
],
228+
]),
229+
new MockResponse($payload->asBinary()),
230+
]);
231+
232+
$client = new ElevenLabsClient(
233+
$httpClient,
234+
'https://api.elevenlabs.io/v1',
235+
'my-api-key',
236+
);
237+
238+
$result = $client->request(new ElevenLabs(ElevenLabs::ELEVEN_MULTILINGUAL_V2), [
239+
'text' => 'foo',
240+
], [
241+
'voice' => 'Dslrhjl3ZpzrctukrQSN',
242+
'stream' => true,
243+
]);
244+
245+
$this->assertInstanceOf(RawHttpResult::class, $result);
246+
$this->assertSame(2, $httpClient->getRequestsCount());
247+
}
188248
}

0 commit comments

Comments
 (0)