Skip to content

Commit dd185ed

Browse files
committed
ref
1 parent 30ffbd8 commit dd185ed

File tree

9 files changed

+40
-52
lines changed

9 files changed

+40
-52
lines changed

examples/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
"symfony/finder": "^7.4|^8.0",
9797
"symfony/http-foundation": "^7.4|^8.0",
9898
"symfony/process": "^7.4|^8.0",
99-
"symfony/var-dumper": "^7.4|^8.0"
99+
"symfony/var-dumper": "^7.4|^8.0",
100+
"symfony/rate-limiter": "^7.4|^8.0"
100101
},
101102
"require-dev": {
102103
"phpstan/phpstan": "^2.1",

examples/speech/agent-eleven-labs-speech-sts.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
httpClient: http_client(),
2828
),
2929
[
30-
'ttsModel' => 'eleven_multilingual_v2',
31-
'ttsVoice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
32-
'sttModel' => 'eleven_multilingual_v2',
30+
'tts_model' => 'eleven_multilingual_v2',
31+
'tts_voice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
32+
'stt_model' => 'scribe_v1',
3333
],
3434
);
3535

3636
$eventDispatcher = new EventDispatcher();
3737
$eventDispatcher->addSubscriber(new SpeechListener([
38-
$elevenLabsPlatform,
38+
'elevenlabs' => $elevenLabsPlatform,
3939
]));
4040

4141
$platform = OpenAiPlatformFactory::create(env('OPENAI_API_KEY'), httpClient: http_client(), eventDispatcher: $eventDispatcher);

examples/speech/agent-eleven-labs-speech-stt.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

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

24+
$elevenLabsSpeechPlatform = new ElevenLabsSpeechPlatform(
25+
PlatformFactory::create(
26+
apiKey: env('ELEVEN_LABS_API_KEY'),
27+
httpClient: http_client(),
28+
),
29+
[
30+
'stt_model' => 'scribe_v1',
31+
],
32+
);
33+
2434
$eventDispatcher = new EventDispatcher();
2535
$eventDispatcher->addSubscriber(new SpeechListener([
26-
new ElevenLabsSpeechPlatform(
27-
PlatformFactory::create(
28-
apiKey: env('ELEVEN_LABS_API_KEY'),
29-
httpClient: http_client(),
30-
),
31-
[
32-
'sttModel' => 'eleven_multilingual_v2',
33-
],
34-
),
36+
'elevenlabs' => $elevenLabsSpeechPlatform,
3537
]));
3638

3739
$platform = OpenAiPlatformFactory::create(env('OPENAI_API_KEY'), httpClient: http_client(), eventDispatcher: $eventDispatcher);

examples/speech/agent-eleven-labs-speech-tts.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
httpClient: http_client(),
2727
),
2828
[
29-
'ttsModel' => 'eleven_multilingual_v2',
30-
'ttsVoice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
29+
'tts_model' => 'eleven_multilingual_v2',
30+
'tts_voice' => 'Dslrhjl3ZpzrctukrQSN', // Brad (https://elevenlabs.io/app/voice-library?voiceId=Dslrhjl3ZpzrctukrQSN)
3131
],
3232
);
3333

3434
$eventDispatcher = new EventDispatcher();
3535
$eventDispatcher->addSubscriber(new SpeechListener([
36-
$elevenLabsPlatform,
36+
'elevenlabs' => $elevenLabsPlatform,
3737
]));
3838

3939
$platform = OpenAiPlatformFactory::create(env('OPENAI_API_KEY'), httpClient: http_client(), eventDispatcher: $eventDispatcher);

src/platform/src/Bridge/ElevenLabs/ElevenLabsSpeechPlatform.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
namespace Symfony\AI\Platform\Bridge\ElevenLabs;
1313

1414
use Symfony\AI\Platform\Exception\RuntimeException;
15-
use Symfony\AI\Platform\Message\Content\Text;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
1817
use Symfony\AI\Platform\PlatformInterface;
1918
use Symfony\AI\Platform\Result\DeferredResult;
20-
use Symfony\AI\Platform\Speech\Speech;
2119
use Symfony\AI\Platform\Speech\SpeechPlatformInterface;
2220
use Symfony\Component\OptionsResolver\OptionsResolver;
2321

@@ -48,37 +46,33 @@ public function invoke(string $model, object|array|string $input, array $options
4846
return $this->platform->invoke($model, $input, $options);
4947
}
5048

51-
public function generate(DeferredResult $result, array $options): ?Speech
49+
public function generate(DeferredResult $result, array $options): ?DeferredResult
5250
{
5351
if (!\array_key_exists('tts_model', $this->speechConfiguration) || !\array_key_exists('tts_voice', $this->speechConfiguration)) {
5452
return null;
5553
}
5654

5755
$payload = $result->asText();
5856

59-
$speechResult = $this->invoke($this->speechConfiguration['tts_model'], ['text' => $payload], [
57+
return $this->invoke($this->speechConfiguration['tts_model'], ['text' => $payload], [
6058
'voice' => $this->speechConfiguration['tts_voice'],
6159
...$this->speechConfiguration['tts_options'] ?? [],
6260
...$options,
6361
]);
64-
65-
return new Speech($payload, $speechResult, 'elevenlabs');
6662
}
6763

68-
public function listen(object|array|string $input, array $options): ?Text
64+
public function listen(object|array|string $input, array $options): ?DeferredResult
6965
{
7066
if (!\array_key_exists('stt_model', $this->speechConfiguration)) {
7167
return null;
7268
}
7369

7470
$input = ($input instanceof MessageBag && $input->containsAudio()) ? $input->getUserMessage()->getAudioContent() : $input;
7571

76-
$result = $this->platform->invoke($this->speechConfiguration['stt_model'], $input, [
72+
return $this->platform->invoke($this->speechConfiguration['stt_model'], $input, [
7773
...$options,
7874
...$this->speechConfiguration['stt_options'] ?? [],
7975
]);
80-
81-
return new Text($result->asText());
8276
}
8377

8478
public function getModelCatalog(): ModelCatalogInterface

src/platform/src/Bridge/ElevenLabs/Tests/ElevenLabsSpeechPlatformTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ public function testPlatformCanGenerate()
125125

126126
$speech = $speechPlatform->generate($deferredResult, []);
127127

128-
$this->assertSame('elevenlabs', $speech->getIdentifier());
128+
$this->assertInstanceOf(DeferredResult::class, $speech);
129129
}
130130
}

src/platform/src/Speech/Speech.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,12 @@
1818
*/
1919
final class Speech
2020
{
21-
/**
22-
* @param string|array<mixed, mixed> $payload
23-
*/
2421
public function __construct(
25-
private readonly string|array $payload,
2622
private readonly DeferredResult $result,
2723
private readonly string $identifier,
2824
) {
2925
}
3026

31-
/**
32-
* @return string|array<mixed, mixed>
33-
*/
34-
public function getPayload(): string|array
35-
{
36-
return $this->payload;
37-
}
38-
3927
public function asBinary(): string
4028
{
4129
return $this->result->asBinary();

src/platform/src/Speech/SpeechListener.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\AI\Platform\Event\InvocationEvent;
1515
use Symfony\AI\Platform\Event\ResultEvent;
16+
use Symfony\AI\Platform\Message\Content\Text;
1617
use Symfony\AI\Platform\Message\Message;
1718
use Symfony\AI\Platform\Message\MessageBag;
19+
use Symfony\AI\Platform\Result\DeferredResult;
1820
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1921

2022
/**
@@ -42,21 +44,23 @@ public function onInvocation(InvocationEvent $event): void
4244
$input = $event->getInput();
4345
$options = $event->getOptions();
4446

45-
foreach ($this->speechPlatforms as $speechToTextPlatform) {
46-
$overriddenInput = $speechToTextPlatform->listen($input, $options);
47+
foreach ($this->speechPlatforms as $platform) {
48+
$overriddenInput = $platform->listen($input, $options);
4749

48-
if (null === $overriddenInput) {
50+
if (!$overriddenInput instanceof DeferredResult) {
4951
continue;
5052
}
5153

54+
$inputAsText = new Text($overriddenInput->asText());
55+
5256
if (!$input instanceof MessageBag) {
53-
$event->setInput($overriddenInput);
57+
$event->setInput($inputAsText);
5458

5559
return;
5660
}
5761

5862
$event->setInput(new MessageBag(
59-
Message::ofUser($overriddenInput),
63+
Message::ofUser($inputAsText),
6064
));
6165
}
6266
}
@@ -66,14 +70,14 @@ public function onResult(ResultEvent $event): void
6670
$deferredResult = $event->getDeferredResult();
6771
$options = $event->getOptions();
6872

69-
foreach ($this->speechPlatforms as $textToSpeechPlatform) {
70-
$speech = $textToSpeechPlatform->generate($deferredResult, $options);
73+
foreach ($this->speechPlatforms as $name => $platform) {
74+
$payload = $platform->generate($deferredResult, $options);
7175

72-
if (null === $speech) {
76+
if (!$payload instanceof DeferredResult) {
7377
continue;
7478
}
7579

76-
$deferredResult->addSpeech($speech);
80+
$deferredResult->addSpeech(new Speech($payload, $name));
7781

7882
$event->setDeferredResult($deferredResult);
7983
}

src/platform/src/Speech/SpeechPlatformInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Speech;
1313

14-
use Symfony\AI\Platform\Message\Content\Text;
1514
use Symfony\AI\Platform\PlatformInterface;
1615
use Symfony\AI\Platform\Result\DeferredResult;
1716

@@ -23,13 +22,13 @@ interface SpeechPlatformInterface
2322
/**
2423
* @param array<string, mixed> $options
2524
*/
26-
public function generate(DeferredResult $result, array $options): ?Speech;
25+
public function generate(DeferredResult $result, array $options): ?DeferredResult;
2726

2827
/**
2928
* @param array<mixed>|string|object $input The input data
3029
* @param array<string, mixed> $options The options to customize the model invocation
3130
*
3231
* {@see PlatformInterface::invoke()}
3332
*/
34-
public function listen(object|array|string $input, array $options): ?Text;
33+
public function listen(object|array|string $input, array $options): ?DeferredResult;
3534
}

0 commit comments

Comments
 (0)