Skip to content

[Platform] Add ElevenLabs as platform #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025
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
4 changes: 4 additions & 0 deletions examples/.env
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ HUGGINGFACE_KEY=
# For using OpenRouter
OPENROUTER_KEY=

# For using ElevenLabs
ELEVEN_LABS_URL=https://api.elevenlabs.io/v1
ELEVEN_LABS_API_KEY=

# For using SerpApi (tool)
SERP_API_KEY=

Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.transformers-cache
composer.lock
vendor
tmp
1 change: 1 addition & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"config": {
"allow-plugins": {
"codewithkyrian/platform-package-installer": true,
"codewithkyrian/transformers-libsloader": true,
"php-http/discovery": true
},
"sort-packages": true
Expand Down
26 changes: 26 additions & 0 deletions examples/elevenlabs/speech-to-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\ElevenLabs\ElevenLabs;
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory;
use Symfony\AI\Platform\Message\Content\Audio;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(
apiKey: env('ELEVEN_LABS_API_KEY'),
httpClient: http_client()
);
$model = new ElevenLabs(ElevenLabs::SCRIBE_V1);

$result = $platform->invoke($model, Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3'));

echo $result->asText().\PHP_EOL;
28 changes: 28 additions & 0 deletions examples/elevenlabs/text-to-speech.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\ElevenLabs\ElevenLabs;
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory;
use Symfony\AI\Platform\Message\Content\Text;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(
apiKey: env('ELEVEN_LABS_API_KEY'),
httpClient: http_client(),
);
$model = new ElevenLabs(options: [
'voice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
]);

$result = $platform->invoke($model, new Text('Hello world'));

echo $result->asBinary().\PHP_EOL;
6 changes: 6 additions & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
->end()
->end()
->end()
->arrayNode('eleven_labs')
->children()
->scalarNode('host')->end()
->scalarNode('api_key')->isRequired()->end()
->end()
->end()
->arrayNode('gemini')
->children()
->scalarNode('api_key')->isRequired()->end()
Expand Down
12 changes: 11 additions & 1 deletion src/ai-bundle/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Configuration
class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI

**Advanced Example with Anthropic, Azure, Gemini and multiple agents**
**Advanced Example with Anthropic, Azure, ElevenLabs, Gemini, Ollama multiple agents**

.. code-block:: yaml

Expand All @@ -50,6 +50,10 @@ Configuration
deployment: '%env(AZURE_OPENAI_GPT)%'
api_key: '%env(AZURE_OPENAI_KEY)%'
api_version: '%env(AZURE_GPT_VERSION)%'
eleven_labs:
host: '%env(ELEVEN_LABS_HOST)%'
api_key: '%env(ELEVEN_LABS_API_KEY)%'
output_path: '%env(ELEVEN_LABS_OUTPUT_PATH)%'
gemini:
api_key: '%env(GEMINI_API_KEY)%'
ollama:
Expand Down Expand Up @@ -85,6 +89,12 @@ Configuration
tools: # If undefined, all tools are injected into the agent, use "tools: false" to disable tools.
- 'Symfony\AI\Agent\Toolbox\Tool\Wikipedia'
fault_tolerant_toolbox: false # Disables fault tolerant toolbox, default is true
audio:
platform: 'ai.platform.eleven_labs'
model:
class: 'Symfony\AI\Platform\Bridge\ElevenLabs'
name: !php/const Symfony\AI\Platform\Bridge\ElevenLabs::TEXT_TO_SPEECH
tools: false
store:
# also azure_search, meilisearch, memory, mongodb, pinecone, qdrant and surrealdb are supported as store type
chroma_db:
Expand Down
20 changes: 20 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory as AnthropicPlatformFactory;
use Symfony\AI\Platform\Bridge\Azure\OpenAi\PlatformFactory as AzureOpenAiPlatformFactory;
use Symfony\AI\Platform\Bridge\Cerebras\PlatformFactory as CerebrasPlatformFactory;
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory as ElevenLabsPlatformFactory;
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory as GeminiPlatformFactory;
use Symfony\AI\Platform\Bridge\LmStudio\PlatformFactory as LmStudioPlatformFactory;
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory as MistralPlatformFactory;
Expand Down Expand Up @@ -204,6 +205,25 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
return;
}

if ('eleven_labs' === $type) {
$platformId = 'ai.platform.eleven_labs';
$definition = (new Definition(Platform::class))
->setFactory(ElevenLabsPlatformFactory::class.'::create')
->setLazy(true)
->addTag('proxy', ['interface' => PlatformInterface::class])
->setArguments([
$platform['api_key'],
$platform['host'],
new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE),
new Reference('ai.platform.contract.default'),
])
->addTag('ai.platform');

$container->setDefinition($platformId, $definition);

return;
}

if ('gemini' === $type) {
$platformId = 'ai.platform.gemini';
$definition = (new Definition(Platform::class))
Expand Down
4 changes: 4 additions & 0 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private function getFullConfig(): array
'api_version' => '2024-02-15-preview',
],
],
'eleven_labs' => [
'host' => 'https://api.elevenlabs.io/v1',
'api_key' => 'eleven_labs_key_full',
],
'gemini' => [
'api_key' => 'gemini_key_full',
],
Expand Down
2 changes: 1 addition & 1 deletion src/platform/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"phpstan/phpstan": "^2.1.17",
"phpstan/phpstan-symfony": "^2.0.6",
"phpunit/phpunit": "^11.5",
"symfony/ai-agent": "@dev",
"symfony/console": "^6.4 || ^7.1",
"symfony/dotenv": "^6.4 || ^7.1",
"symfony/ai-agent": "@dev",
"symfony/event-dispatcher": "^6.4 || ^7.1",
"symfony/finder": "^6.4 || ^7.1",
"symfony/process": "^6.4 || ^7.1",
Expand Down
58 changes: 58 additions & 0 deletions src/platform/src/Bridge/ElevenLabs/Contract/AudioNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\ElevenLabs\Contract;

use Symfony\AI\Platform\Message\Content\Audio;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Guillaume Loulier <[email protected]>
*/
final readonly class AudioNormalizer implements NormalizerInterface
{
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Audio;
}

public function getSupportedTypes(?string $format): array
{
return [
Audio::class => true,
];
}

/**
* @param Audio $data
*
* @return array{type: 'input_audio', input_audio: array{
* data: string,
* path: string,
* format: 'mp3'|'wav'|string,
* }}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'input_audio',
'input_audio' => [
'data' => $data->asBase64(),
'path' => $data->asPath(),
'format' => match ($data->getFormat()) {
'audio/mpeg' => 'mp3',
'audio/wav' => 'wav',
default => $data->getFormat(),
},
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\ElevenLabs\Contract;

use Symfony\AI\Platform\Contract;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Guillaume Loulier <[email protected]>
*/
final readonly class ElevenLabsContract extends Contract
{
public static function create(NormalizerInterface ...$normalizer): Contract
{
return parent::create(
new AudioNormalizer(),
...$normalizer,
);
}
}
40 changes: 40 additions & 0 deletions src/platform/src/Bridge/ElevenLabs/ElevenLabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\ElevenLabs;

use Symfony\AI\Platform\Model;

/**
* @author Guillaume Loulier <[email protected]>
*/
final class ElevenLabs extends Model
{
public const ELEVEN_V3 = 'eleven_v3';
public const ELEVEN_TTV_V3 = 'eleven_ttv_v3';
public const ELEVEN_MULTILINGUAL_V2 = 'eleven_multilingual_v2';
public const ELEVEN_FLASH_V250 = 'eleven_flash_v2_5';
public const ELEVEN_FLASH_V2 = 'eleven_flashv2';
public const ELEVEN_TURBO_V2_5 = 'eleven_turbo_v2_5';
public const ELEVEN_TURBO_v2 = 'eleven_turbo_v2';
public const ELEVEN_MULTILINGUAL_STS_V2 = 'eleven_multilingual_sts_v2';
public const ELEVEN_MULTILINGUAL_ttv_V2 = 'eleven_multilingual_ttv_v2';
public const ELEVEN_ENGLISH_STS_V2 = 'eleven_english_sts_v2';
public const SCRIBE_V1 = 'scribe_v1';
public const SCRIBE_V1_EXPERIMENTAL = 'scribe_v1_experimental';

public function __construct(
string $name = self::ELEVEN_MULTILINGUAL_V2,
array $options = [],
) {
parent::__construct($name, [], $options);
}
}
Loading