Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/**
Expand All @@ -76,16 +76,16 @@ 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.');
}

if (!\array_key_exists('text', $payload)) {
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)
Expand Down
60 changes: 60 additions & 0 deletions src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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());
}
}