Skip to content

[Agent] ElevenLabs TTS tool #263

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions examples/.env
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ TAVILY_API_KEY=
# For using Brave (tool)
BRAVE_API_KEY=

# For using ElevenLabs (tool)
ELEVENLABS_API_KEY=

# For using MongoDB Atlas (store)
MONGODB_URI=

Expand Down
42 changes: 42 additions & 0 deletions examples/misc/text-to-speech.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Agent\Agent;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\ElevenLabs;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

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

$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$model = new Gpt(Gpt::GPT_4O_MINI);

$elevenLabs = new ElevenLabs(
http_client(),
env('ELEVENLABS_API_KEY'),
__DIR__.'/../tmp',
'eleven_multilingual_v2',
'Dslrhjl3ZpzrctukrQSN' // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
);

$toolbox = new Toolbox([$elevenLabs], logger: logger());
$toolProcessor = new AgentProcessor($toolbox);

$agent = new Agent($platform, $model, inputProcessors: [$toolProcessor], outputProcessors: [$toolProcessor]);

$messages = new MessageBag(Message::ofUser('Convert the following text to voice: "Hello world with voice!"'));
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;
4 changes: 3 additions & 1 deletion src/agent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"symfony/css-selector": "^6.4 || ^7.1",
"symfony/dom-crawler": "^6.4 || ^7.1",
"symfony/event-dispatcher": "^6.4 || ^7.1",
"symfony/http-foundation": "^6.4 || ^7.1"
"symfony/filesystem": "^6.4 || ^7.1",
"symfony/http-foundation": "^6.4 || ^7.1",
"symfony/uid": "^6.4 || ^7.1"
},
"config": {
"sort-packages": true
Expand Down
2 changes: 2 additions & 0 deletions src/agent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ messages will be added to your MessageBag::
* `Weather Tool with Event Listener`_
* `Wikipedia Tool`_
* `YouTube Transcriber Tool`_
* `ElevenLabs Text to Speech`_

Retrieval Augmented Generation (RAG)
------------------------------------
Expand Down Expand Up @@ -552,6 +553,7 @@ useful when certain interactions shouldn't be influenced by the memory context::
.. _`Weather Tool with Event Listener`: https://github.com/symfony/ai/blob/main/examples/toolbox/weather-event.php
.. _`Wikipedia Tool`: https://github.com/symfony/ai/blob/main/examples/openai/toolcall-stream.php
.. _`YouTube Transcriber Tool`: https://github.com/symfony/ai/blob/main/examples/openai/toolcall.php
.. _`ElevenLabs Text to Speech`: https://github.com/symfony/ai/blob/main/examples/misc/text-to-speech.php
.. _`Store Component`: https://github.com/symfony/ai-store
.. _`RAG with MongoDB`: https://github.com/symfony/ai/blob/main/examples/store/mongodb-similarity-search.php
.. _`RAG with Pinecone`: https://github.com/symfony/ai/blob/main/examples/store/pinecone-similarity-search.php
Expand Down
72 changes: 72 additions & 0 deletions src/agent/src/Toolbox/Tool/ElevenLabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\Agent\Toolbox\Tool;

use Symfony\AI\Agent\Exception\RuntimeException;
use Symfony\AI\Agent\Toolbox\Attribute\AsTool;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Guillaume Loulier <[email protected]>
*
* @see https://elevenlabs.io/
*/
#[AsTool('text_to_speech', description: 'Convert text to speech / voice')]
final readonly class ElevenLabs
{
public function __construct(
private HttpClientInterface $httpClient,
#[\SensitiveParameter] private string $apiKey,
private string $path,
private string $model,
private string $voice,
) {
if (!class_exists(Filesystem::class)) {
throw new RuntimeException('For using the ElevenLabs TTS tool, the symfony/filesystem package is required. Try running "composer require symfony/filesystem".');
}

if (!class_exists(Uuid::class)) {
throw new RuntimeException('For using the ElevenLabs TTS tool, the symfony/uid package is required. Try running "composer require symfony/uid".');
}
}

/**
* @return array{
* input: string,
* path: string,
* }
*/
public function __invoke(string $text): array
{
$response = $this->httpClient->request('POST', \sprintf('https://api.elevenlabs.io/v1/text-to-speech/%s?output_format=mp3_44100_128', $this->voice), [
'headers' => [
'xi-api-key' => $this->apiKey,
],
'json' => [
'text' => $text,
'model_id' => $this->model,
],
]);

$file = \sprintf('%s/%s.mp3', $this->path, Uuid::v4()->toRfc4122());

$filesystem = new Filesystem();
$filesystem->dumpFile($file, $response->getContent());

return [
'input' => $text,
'path' => $file,
];
}
}
43 changes: 43 additions & 0 deletions src/agent/tests/Toolbox/Tool/ElevenLabsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Agent\Tests\Toolbox\Tool;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Agent\Toolbox\Tool\ElevenLabs;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

#[CoversClass(ElevenLabs::class)]
final class ElevenLabsTest extends TestCase
{
public function testTextToSpeech()
{
$httpClient = new MockHttpClient(
new MockResponse(file_get_contents(__DIR__.'/../../../../../fixtures/audio.mp3'), [
'headers' => [
'Content-Type' => 'audio/mpeg',
],
'http_code' => 200,
]),
);

$elevenLabs = new ElevenLabs($httpClient, 'foo', 'bar', 'baz', 'random');

$result = $elevenLabs('Hello World');

$this->assertCount(2, $result);
$this->assertSame('Hello World', $result['input']);
$this->assertNotEmpty($result['path']);
$this->assertSame(1, $httpClient->getRequestsCount());
}
}