Skip to content

Commit e55d982

Browse files
committed
[Platform][ElevenLabs] Use capability-based speech-to-text detection
Replace hardcoded model name checking with API capability detection for speech-to-text models. This makes the code more maintainable and allows for dynamic capability discovery.
1 parent e157158 commit e55d982

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function request(Model $model, array|string $payload, array $options = []
4141
throw new InvalidArgumentException(\sprintf('The payload must be an array, received "%s".', get_debug_type($payload)));
4242
}
4343

44-
if (\in_array($model->getName(), [ElevenLabs::SCRIBE_V1, ElevenLabs::SCRIBE_V1_EXPERIMENTAL], true)) {
44+
$capabilities = $this->retrieveCapabilities($model);
45+
46+
if ($capabilities['can_do_speech_to_text'] ?? false) {
4547
return $this->doSpeechToTextRequest($model, $payload);
4648
}
4749

48-
$capabilities = $this->retrieveCapabilities($model);
49-
5050
if (!$capabilities['can_do_text_to_speech']) {
5151
throw new InvalidArgumentException(\sprintf('The model "%s" does not support text-to-speech.', $model->getName()));
5252
}

src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public function testClientCannotPerformSpeechToTextRequestWithInvalidPayload()
8181
public function testClientCanPerformSpeechToTextRequest()
8282
{
8383
$httpClient = new MockHttpClient([
84+
new JsonMockResponse([
85+
[
86+
'model_id' => ElevenLabs::SCRIBE_V1,
87+
'can_do_speech_to_text' => true,
88+
],
89+
]),
8490
new JsonMockResponse([
8591
'text' => 'foo',
8692
]),
@@ -89,15 +95,43 @@ public function testClientCanPerformSpeechToTextRequest()
8995

9096
$client = new ElevenLabsClient(
9197
$httpClient,
92-
'https://api.elevenlabs.io/v1',
9398
'my-api-key',
99+
'https://api.elevenlabs.io/v1',
94100
);
95101

96102
$payload = $normalizer->normalize(Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3'));
97103

98104
$client->request(new ElevenLabs(ElevenLabs::SCRIBE_V1), $payload);
99105

100-
$this->assertSame(1, $httpClient->getRequestsCount());
106+
$this->assertSame(2, $httpClient->getRequestsCount());
107+
}
108+
109+
public function testClientCanPerformSpeechToTextRequestWithExperimentalModel()
110+
{
111+
$httpClient = new MockHttpClient([
112+
new JsonMockResponse([
113+
[
114+
'model_id' => ElevenLabs::SCRIBE_V1_EXPERIMENTAL,
115+
'can_do_speech_to_text' => true,
116+
],
117+
]),
118+
new JsonMockResponse([
119+
'text' => 'foo',
120+
]),
121+
]);
122+
$normalizer = new AudioNormalizer();
123+
124+
$client = new ElevenLabsClient(
125+
$httpClient,
126+
'my-api-key',
127+
'https://api.elevenlabs.io/v1',
128+
);
129+
130+
$payload = $normalizer->normalize(Audio::fromFile(\dirname(__DIR__, 5).'/fixtures/audio.mp3'));
131+
132+
$client->request(new ElevenLabs(ElevenLabs::SCRIBE_V1_EXPERIMENTAL), $payload);
133+
134+
$this->assertSame(2, $httpClient->getRequestsCount());
101135
}
102136

103137
public function testClientCannotPerformTextToSpeechRequestWithoutValidPayload()

0 commit comments

Comments
 (0)