diff --git a/src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php b/src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php index c5626ee2d..100c05330 100644 --- a/src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php +++ b/src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php @@ -51,7 +51,7 @@ public function request(Model $model, array|string $payload, array $options = [] throw new InvalidArgumentException(\sprintf('The model "%s" does not support text-to-speech.', $model->getName())); } - return $this->doTextToSpeechRequest($model, $payload, $options); + return $this->doTextToSpeechRequest($model, $payload, array_merge($options, $model->getOptions())); } /** @@ -76,7 +76,7 @@ private function doSpeechToTextRequest(Model $model, array|string $payload): Raw */ private function doTextToSpeechRequest(Model $model, array|string $payload, array $options): RawHttpResult { - if (!\array_key_exists('voice', $model->getOptions())) { + if (!\array_key_exists('voice', $options)) { throw new InvalidArgumentException('The voice option is required.'); } @@ -84,8 +84,8 @@ private function doTextToSpeechRequest(Model $model, array|string $payload, arra throw new InvalidArgumentException('The payload must contain a "text" key.'); } - $voice = $options['voice'] ??= $model->getOptions()['voice']; - $stream = $options['stream'] ??= $model->getOptions()['stream'] ?? false; + $voice = $options['voice']; + $stream = $options['stream'] ?? false; $url = $stream ? \sprintf('%s/text-to-speech/%s/stream', $this->hostUrl, $voice) diff --git a/src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php b/src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php index 26b301a16..9fccce4d8 100644 --- a/src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php +++ b/src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php @@ -155,6 +155,35 @@ public function testClientCanPerformTextToSpeechRequest() $this->assertSame(2, $httpClient->getRequestsCount()); } + public function testClientCanPerformTextToSpeechRequestWhenVoiceKeyIsProvidedAsRequestOption() + { + $payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3'); + + $httpClient = new MockHttpClient([ + new JsonMockResponse([ + [ + 'model_id' => ElevenLabs::ELEVEN_MULTILINGUAL_V2, + 'can_do_text_to_speech' => true, + ], + ]), + new MockResponse($payload->asBinary()), + ]); + + $client = new ElevenLabsClient( + $httpClient, + 'https://api.elevenlabs.io/v1', + 'my-api-key', + ); + + $client->request(new ElevenLabs(ElevenLabs::ELEVEN_MULTILINGUAL_V2), [ + 'text' => 'foo', + ], [ + 'voice' => 'Dslrhjl3ZpzrctukrQSN', + ]); + + $this->assertSame(2, $httpClient->getRequestsCount()); + } + public function testClientCanPerformTextToSpeechRequestAsStream() { $payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3'); @@ -185,4 +214,35 @@ public function testClientCanPerformTextToSpeechRequestAsStream() $this->assertInstanceOf(RawHttpResult::class, $result); $this->assertSame(2, $httpClient->getRequestsCount()); } + + public function testClientCanPerformTextToSpeechRequestAsStreamVoiceKeyIsProvidedAsRequestOption() + { + $payload = Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3'); + + $httpClient = new MockHttpClient([ + new JsonMockResponse([ + [ + 'model_id' => ElevenLabs::ELEVEN_MULTILINGUAL_V2, + 'can_do_text_to_speech' => true, + ], + ]), + new MockResponse($payload->asBinary()), + ]); + + $client = new ElevenLabsClient( + $httpClient, + 'https://api.elevenlabs.io/v1', + 'my-api-key', + ); + + $result = $client->request(new ElevenLabs(ElevenLabs::ELEVEN_MULTILINGUAL_V2), [ + 'text' => 'foo', + ], [ + 'voice' => 'Dslrhjl3ZpzrctukrQSN', + 'stream' => true, + ]); + + $this->assertInstanceOf(RawHttpResult::class, $result); + $this->assertSame(2, $httpClient->getRequestsCount()); + } }