Skip to content

Commit 45eab27

Browse files
committed
ref
1 parent 3810f3e commit 45eab27

File tree

6 files changed

+29
-34
lines changed

6 files changed

+29
-34
lines changed

examples/elevenlabs/text-to-speech-as-stream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
$content = '';
3030

3131
foreach ($result->asStream() as $chunk) {
32-
$content .= $chunk->getContent();
32+
echo $chunk;
3333
}
3434

35-
echo $content.\PHP_EOL;
35+
echo \PHP_EOL;

src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,15 @@ private function doTextToSpeechRequest(Model $model, array|string $payload, arra
9191
? \sprintf('%s/text-to-speech/%s/stream', $this->hostUrl, $voice)
9292
: \sprintf('%s/text-to-speech/%s', $this->hostUrl, $voice);
9393

94-
$response = $this->httpClient->request('POST', $url, [
94+
return new RawHttpResult($this->httpClient->request('POST', $url, [
9595
'headers' => [
9696
'xi-api-key' => $this->apiKey,
9797
],
9898
'json' => [
9999
'text' => $payload['text'],
100100
'model_id' => $model->getName(),
101101
],
102-
]);
103-
104-
return $stream
105-
? new RawHttpResult($this->httpClient->stream($response))
106-
: new RawHttpResult($response);
102+
]));
107103
}
108104

109105
/**

src/platform/src/Bridge/ElevenLabs/ElevenLabsResultConverter.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,49 @@
1919
use Symfony\AI\Platform\Result\StreamResult;
2020
use Symfony\AI\Platform\Result\TextResult;
2121
use Symfony\AI\Platform\ResultConverterInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223
use Symfony\Contracts\HttpClient\ResponseInterface;
23-
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
2424

2525
/**
2626
* @author Guillaume Loulier <[email protected]>
2727
*/
2828
final readonly class ElevenLabsResultConverter implements ResultConverterInterface
2929
{
30+
public function __construct(
31+
private HttpClientInterface $httpClient,
32+
) {
33+
}
34+
3035
public function supports(Model $model): bool
3136
{
3237
return $model instanceof ElevenLabs;
3338
}
3439

3540
public function convert(RawResultInterface $result, array $options = []): ResultInterface
3641
{
37-
/** @var ResponseInterface|ResponseStreamInterface $response */
42+
/** @var ResponseInterface $response */
3843
$response = $result->getObject();
3944

4045
return match (true) {
41-
$response instanceof ResponseStreamInterface => new StreamResult($this->convertToGenerator($response)),
46+
\array_key_exists('stream', $options) && $options['stream'] => new StreamResult($this->convertToGenerator($response)),
4247
str_contains($response->getInfo('url'), 'speech-to-text') => new TextResult($result->getData()['text']),
4348
str_contains($response->getInfo('url'), 'text-to-speech') => new BinaryResult($result->getObject()->getContent(), 'audio/mpeg'),
4449
default => throw new RuntimeException('Unsupported ElevenLabs response.'),
4550
};
4651
}
4752

48-
private function convertToGenerator(ResponseStreamInterface $response): \Generator
53+
private function convertToGenerator(ResponseInterface $response): \Generator
4954
{
50-
return (static function () use ($response): \Generator {
51-
foreach ($response as $chunk) {
52-
if ($chunk->isFirst()) {
53-
continue;
54-
}
55-
56-
if ('' === $chunk->getContent()) {
57-
continue;
58-
}
55+
foreach ($this->httpClient->stream($response) as $chunk) {
56+
if ($chunk->isFirst() || $chunk->isLast()) {
57+
continue;
58+
}
5959

60-
yield $chunk;
60+
if ('' === $chunk->getContent()) {
61+
continue;
6162
}
62-
})();
63+
64+
yield $chunk->getContent();
65+
}
6366
}
6467
}

src/platform/src/Bridge/ElevenLabs/PlatformFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function create(
3232

3333
return new Platform(
3434
[new ElevenLabsClient($httpClient, $apiKey, $hostUrl)],
35-
[new ElevenLabsResultConverter()],
35+
[new ElevenLabsResultConverter($httpClient)],
3636
$contract ?? ElevenLabsContract::create(),
3737
);
3838
}

src/platform/src/Result/RawHttpResult.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,23 @@
1212
namespace Symfony\AI\Platform\Result;
1313

1414
use Symfony\Contracts\HttpClient\ResponseInterface;
15-
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
1615

1716
/**
1817
* @author Christopher Hertel <[email protected]
1918
*/
2019
final readonly class RawHttpResult implements RawResultInterface
2120
{
2221
public function __construct(
23-
private ResponseInterface|ResponseStreamInterface $response,
22+
private ResponseInterface $response,
2423
) {
2524
}
2625

2726
public function getData(): array
2827
{
29-
if ($this->response instanceof ResponseStreamInterface) {
30-
return [];
31-
}
32-
3328
return $this->response->toArray(false);
3429
}
3530

36-
public function getObject(): ResponseInterface|ResponseStreamInterface
31+
public function getObject(): ResponseInterface
3732
{
3833
return $this->response;
3934
}

src/platform/tests/Bridge/ElevenLabs/ElevenLabsConverterTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\AI\Platform\Result\BinaryResult;
2121
use Symfony\AI\Platform\Result\InMemoryRawResult;
2222
use Symfony\AI\Platform\Result\TextResult;
23+
use Symfony\Component\HttpClient\MockHttpClient;
2324

2425
#[CoversClass(ElevenLabsResultConverter::class)]
2526
#[UsesClass(ElevenLabs::class)]
@@ -31,15 +32,15 @@ final class ElevenLabsConverterTest extends TestCase
3132
{
3233
public function testSupportsModel()
3334
{
34-
$converter = new ElevenLabsResultConverter();
35+
$converter = new ElevenLabsResultConverter(new MockHttpClient());
3536

3637
$this->assertTrue($converter->supports(new ElevenLabs()));
3738
$this->assertFalse($converter->supports(new Model('any-model')));
3839
}
3940

4041
public function testConvertSpeechToTextResponse()
4142
{
42-
$converter = new ElevenLabsResultConverter();
43+
$converter = new ElevenLabsResultConverter(new MockHttpClient());
4344
$rawResult = new InMemoryRawResult([
4445
'text' => 'Hello there',
4546
], new class {
@@ -57,7 +58,7 @@ public function getInfo(): string
5758

5859
public function testConvertTextToSpeechResponse()
5960
{
60-
$converter = new ElevenLabsResultConverter();
61+
$converter = new ElevenLabsResultConverter(new MockHttpClient());
6162
$rawResult = new InMemoryRawResult([], new class {
6263
public function getInfo(): string
6364
{

0 commit comments

Comments
 (0)