Skip to content

Commit 024fcca

Browse files
committed
ref
1 parent bd588cf commit 024fcca

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory;
14+
use Symfony\AI\Platform\Message\Content\Text;
15+
use Symfony\AI\Platform\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
18+
require_once dirname(__DIR__).'/bootstrap.php';
19+
20+
$elevenLabsPlatform = PlatformFactory::create(
21+
apiKey: env('ELEVEN_LABS_API_KEY'),
22+
httpClient: http_client(),
23+
);
24+
25+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), httpClient: http_client());
26+
27+
$agent = new Agent($platform, 'gpt-4o');
28+
$answer = $agent->call(new MessageBag(
29+
Message::ofUser('Hello'),
30+
));
31+
32+
$result = $platform->invoke('eleven_multilingual_v2', new Text('Hello world'), [
33+
'voice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
34+
]);
35+
36+
echo $answer->asVoice();

src/agent/src/Agent.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getName(): string
7777
public function call(MessageBag $messages, array $options = []): ResultInterface
7878
{
7979
$input = new Input($this->getModel(), $messages, $options);
80-
array_map(fn (InputProcessorInterface $processor) => $processor->processInput($input), $this->inputProcessors);
80+
array_map(static fn (InputProcessorInterface $processor) => $processor->processInput($input), $this->inputProcessors);
8181

8282
$model = $input->getModel();
8383
$messages = $input->getMessageBag();
@@ -86,7 +86,7 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
8686
$result = $this->platform->invoke($model, $messages, $options)->getResult();
8787

8888
$output = new Output($model, $result, $messages, $options);
89-
array_map(fn (OutputProcessorInterface $processor) => $processor->processOutput($output), $this->outputProcessors);
89+
array_map(static fn (OutputProcessorInterface $processor) => $processor->processOutput($output), $this->outputProcessors);
9090
array_map(static fn (VoiceProviderInterface $provider) => $provider->addVoice($output), $this->voiceProviders);
9191

9292
return $output->getResult();
@@ -116,14 +116,18 @@ private function initializeProcessors(iterable $processors, string $interface):
116116
/**
117117
* @param VoiceProviderInterface[] $providers
118118
*
119-
* @return InputProcessorInterface[]|OutputProcessorInterface[]
119+
* @return VoiceProviderInterface[]
120120
*/
121121
private function initializeVoiceProviders(iterable $providers): array
122122
{
123123
foreach ($providers as $provider) {
124124
if (!$provider instanceof VoiceProviderInterface) {
125125
throw new InvalidArgumentException(\sprintf('Voice provider "%s" must implement "%s".', $provider::class, VoiceProviderInterface::class));
126126
}
127+
128+
if ($provider instanceof AgentAwareInterface) {
129+
$provider->setAgent($this);
130+
}
127131
}
128132

129133
return $providers instanceof \Traversable ? iterator_to_array($providers) : $providers;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Agent\Bridge\ElevenLabs;
13+
14+
use Symfony\AI\Agent\Output;
15+
use Symfony\AI\Agent\Voice;
16+
use Symfony\AI\Agent\VoiceProviderInterface;
17+
use Symfony\AI\Platform\Bridge\ElevenLabs\ElevenLabs;
18+
use Symfony\AI\Platform\Platform;
19+
20+
/**
21+
* @author Guillaume Loulier <[email protected]>
22+
*/
23+
final readonly class VoiceProvider implements VoiceProviderInterface
24+
{
25+
public function __construct(
26+
private Platform $elevenLabsPlatform,
27+
) {
28+
}
29+
30+
public function addVoice(Output $output): void
31+
{
32+
$result = $output->getResult();
33+
34+
$voice = $this->elevenLabsPlatform->invoke($this->getName(), $result->getContent());
35+
36+
$output->setVoice(new Voice($voice->asBinary(), $this->getName()));
37+
}
38+
39+
public function getName(): string
40+
{
41+
return ElevenLabs::ELEVEN_MULTILINGUAL_V2;
42+
}
43+
}

src/agent/src/Output.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct(
2727
private ResultInterface $result,
2828
private readonly MessageBag $messageBag,
2929
private readonly array $options = [],
30+
private ?Voice $voice = null,
3031
) {
3132
}
3233

@@ -57,4 +58,14 @@ public function getOptions(): array
5758
{
5859
return $this->options;
5960
}
61+
62+
public function setVoice(?Voice $voice): void
63+
{
64+
$this->voice = $voice;
65+
}
66+
67+
public function getVoice(): ?Voice
68+
{
69+
return $this->voice;
70+
}
6071
}

src/agent/src/Voice.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Agent;
13+
14+
/**
15+
* @author Guillaume Loulier <[email protected]>
16+
*/
17+
final readonly class Voice
18+
{
19+
public function __construct(
20+
private string $input,
21+
private string $provider,
22+
) {
23+
}
24+
}

src/agent/src/VoiceProviderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
interface VoiceProviderInterface
1818
{
1919
public function addVoice(Output $output): void;
20+
21+
public function getName(): string;
2022
}

0 commit comments

Comments
 (0)