|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Bridge\ElevenLabs; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 15 | +use Symfony\AI\Platform\Model; |
| 16 | +use Symfony\AI\Platform\ModelClientInterface; |
| 17 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 18 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 19 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Guillaume Loulier <[email protected]> |
| 23 | + */ |
| 24 | +final readonly class ElevenLabsClient implements ModelClientInterface |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private HttpClientInterface $httpClient, |
| 28 | + private string $hostUrl, |
| 29 | + private string $apiKey, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function supports(Model $model): bool |
| 34 | + { |
| 35 | + return $model instanceof ElevenLabs; |
| 36 | + } |
| 37 | + |
| 38 | + public function request(Model $model, array|string $payload, array $options = []): RawResultInterface |
| 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('Unsupported model "%s".', $model->getName())), |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + private function doSpeechToTextRequest(Model $model, array|string $payload, array $options): RawHttpResult |
| 48 | + { |
| 49 | + if (!array_key_exists('model', $model->getOptions())) { |
| 50 | + throw new InvalidArgumentException('The model option is required.'); |
| 51 | + } |
| 52 | + |
| 53 | + if (!is_array($payload)) { |
| 54 | + throw new InvalidArgumentException('The payload must be an array, received %s.', get_debug_type($payload)); |
| 55 | + } |
| 56 | + |
| 57 | + $model = $options['model'] ??= $model->getOptions()['model']; |
| 58 | + |
| 59 | + return new RawHttpResult($this->httpClient->request('POST', sprintf('%s/speech-to-text', $this->hostUrl), [ |
| 60 | + 'headers' => [ |
| 61 | + 'xi-api-key' => $this->apiKey, |
| 62 | + ], |
| 63 | + 'body' => [ |
| 64 | + 'file' => $payload['input_audio']['resource'], |
| 65 | + 'model_id' => $model, |
| 66 | + ], |
| 67 | + ])); |
| 68 | + } |
| 69 | + |
| 70 | + private function doTextToSpeechRequest(Model $model, array|string $payload, array $options): RawHttpResult |
| 71 | + { |
| 72 | + if (!array_key_exists('model', $model->getOptions())) { |
| 73 | + throw new InvalidArgumentException('The model option is required.'); |
| 74 | + } |
| 75 | + |
| 76 | + if (is_array($payload) && !array_key_exists('text', $payload)) { |
| 77 | + throw new InvalidArgumentException('The payload must be an array, received %s.', get_debug_type($payload)); |
| 78 | + } |
| 79 | + |
| 80 | + $model = $options['model'] ??= $model->getOptions()['model']; |
| 81 | + |
| 82 | + return new RawHttpResult($this->httpClient->request('POST', sprintf('%s/text-to-speech/%s', $this->hostUrl, $model), [ |
| 83 | + 'headers' => [ |
| 84 | + 'xi-api-key' => $this->apiKey, |
| 85 | + ], |
| 86 | + 'json' => [ |
| 87 | + 'text' => $payload['text'], |
| 88 | + 'model_id' => 'eleven_multilingual_v2', |
| 89 | + ], |
| 90 | + ])); |
| 91 | + } |
| 92 | +} |
0 commit comments