25
25
{
26
26
public function __construct (
27
27
private HttpClientInterface $ httpClient ,
28
- private string $ hostUrl ,
29
- private string $ apiKey ,
28
+ #[\SensitiveParameter] private string $ apiKey ,
29
+ private string $ hostUrl = ' https://api.elevenlabs.io/v1 ' ,
30
30
) {
31
31
}
32
32
@@ -37,11 +37,17 @@ public function supports(Model $model): bool
37
37
38
38
public function request (Model $ model , array |string $ payload , array $ options = []): RawResultInterface
39
39
{
40
- return match ($ model ->getName ()) {
41
- ElevenLabs::SPEECH_TO_TEXT => $ this ->doSpeechToTextRequest ($ model , $ payload , $ options ),
42
- ElevenLabs::TEXT_TO_SPEECH => $ this ->doTextToSpeechRequest ($ model , $ payload , $ options ),
43
- default => throw new InvalidArgumentException (\sprintf ('The model "%s" is not supported. ' , $ model ->getName ())),
44
- };
40
+ if (\in_array ($ model ->getName (), [ElevenLabs::SCRIBE_V1 , ElevenLabs::SCRIBE_V1_EXPERIMENTAL ], true )) {
41
+ return $ this ->doSpeechToTextRequest ($ model , $ payload , $ options );
42
+ }
43
+
44
+ $ capabilities = $ this ->retrieveCapabilities ($ model );
45
+
46
+ if ([] === $ capabilities ) {
47
+ throw new InvalidArgumentException (\sprintf ('The model information could not be retrieved from the ElevenLabs API. Your model might not be supported. Try to use another one. ' ));
48
+ }
49
+
50
+ return $ this ->doTextToSpeechRequest ($ model , $ payload , $ options );
45
51
}
46
52
47
53
/**
@@ -50,23 +56,17 @@ public function request(Model $model, array|string $payload, array $options = []
50
56
*/
51
57
private function doSpeechToTextRequest (Model $ model , array |string $ payload , array $ options ): RawHttpResult
52
58
{
53
- if (!\array_key_exists ('model ' , $ model ->getOptions ())) {
54
- throw new InvalidArgumentException ('The model option is required. ' );
55
- }
56
-
57
59
if (!\is_array ($ payload )) {
58
60
throw new InvalidArgumentException (\sprintf ('The payload must be an array, received "%s". ' , get_debug_type ($ payload )));
59
61
}
60
62
61
- $ model = $ options ['model ' ] ??= $ model ->getOptions ()['model ' ];
62
-
63
63
return new RawHttpResult ($ this ->httpClient ->request ('POST ' , \sprintf ('%s/speech-to-text ' , $ this ->hostUrl ), [
64
64
'headers ' => [
65
65
'xi-api-key ' => $ this ->apiKey ,
66
66
],
67
67
'body ' => [
68
68
'file ' => fopen ($ payload ['input_audio ' ]['path ' ], 'r ' ),
69
- 'model_id ' => $ model ,
69
+ 'model_id ' => $ model-> getName () ,
70
70
],
71
71
]));
72
72
}
@@ -77,7 +77,7 @@ private function doSpeechToTextRequest(Model $model, array|string $payload, arra
77
77
*/
78
78
private function doTextToSpeechRequest (Model $ model , array |string $ payload , array $ options ): RawHttpResult
79
79
{
80
- if (!\array_key_exists ('model ' , $ model ->getOptions ())) {
80
+ if (!\array_key_exists ('voice ' , $ model ->getOptions ())) {
81
81
throw new InvalidArgumentException ('The model option is required. ' );
82
82
}
83
83
@@ -89,16 +89,31 @@ private function doTextToSpeechRequest(Model $model, array|string $payload, arra
89
89
throw new InvalidArgumentException ('The payload must contain a "text" key. ' );
90
90
}
91
91
92
- $ model = $ options ['model ' ] ??= $ model ->getOptions ()['model ' ];
92
+ $ voice = $ options ['voice ' ] ??= $ model ->getOptions ()['voice ' ];
93
93
94
- return new RawHttpResult ($ this ->httpClient ->request ('POST ' , \sprintf ('%s/text-to-speech/%s ' , $ this ->hostUrl , $ model ), [
94
+ return new RawHttpResult ($ this ->httpClient ->request ('POST ' , \sprintf ('%s/text-to-speech/%s ' , $ this ->hostUrl , $ voice ), [
95
95
'headers ' => [
96
96
'xi-api-key ' => $ this ->apiKey ,
97
97
],
98
98
'json ' => [
99
99
'text ' => $ payload ['text ' ],
100
- 'model_id ' => ' eleven_multilingual_v2 ' ,
100
+ 'model_id ' => $ model -> getName () ,
101
101
],
102
102
]));
103
103
}
104
+
105
+ private function retrieveCapabilities (Model $ model ): array
106
+ {
107
+ $ capabilityResponse = $ this ->httpClient ->request ('GET ' , sprintf ('%s/models ' , $ this ->hostUrl ), [
108
+ 'headers ' => [
109
+ 'xi-api-key ' => $ this ->apiKey ,
110
+ ],
111
+ ]);
112
+
113
+ $ models = $ capabilityResponse ->toArray ();
114
+
115
+ $ currentModelConfiguration = array_filter ($ models , static fn (array $ informations ): bool => $ informations ['model_id ' ] === $ model ->getName ());
116
+
117
+ return reset ($ currentModelConfiguration );
118
+ }
104
119
}
0 commit comments