Skip to content

Commit a6c0db1

Browse files
committed
Add stream example command
1 parent 3fd202f commit a6c0db1

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 App\Blog\Command;
13+
14+
use Symfony\AI\Agent\AgentInterface;
15+
use Symfony\AI\Platform\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
use Symfony\Component\Console\Attribute\AsCommand;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
19+
20+
#[AsCommand('app:blog:stream', 'An example command to demonstrate streaming output.')]
21+
final readonly class StreamCommand
22+
{
23+
public function __construct(
24+
private AgentInterface $blogAgent,
25+
) {
26+
}
27+
28+
public function __invoke(SymfonyStyle $io): int
29+
{
30+
$io->title('Stream Example Command');
31+
$io->text('This command demonstrates streaming output in the console.');
32+
33+
$io->comment('Make sure to have ChromaDB running and the blog indexed, see README.');
34+
$io->comment('You can use -vvv or --profile to get more insights into the execution.');
35+
36+
$question = $io->ask(
37+
'Ask a question about the content of the Symfony blog',
38+
'Tell me about the latest Symfony features.',
39+
);
40+
$messages = new MessageBag(Message::ofUser($question));
41+
42+
$io->section('Agent Response:');
43+
$result = $this->blogAgent->call($messages, ['stream' => true]);
44+
45+
foreach ($result->getContent() as $word) {
46+
$io->write($word);
47+
}
48+
49+
$io->newLine(2);
50+
$io->success('The command has completed successfully.');
51+
52+
return 0;
53+
}
54+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 App\Tests\Blog\Command;
13+
14+
use App\Blog\Command\StreamCommand;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\AI\Agent\AgentInterface;
17+
use Symfony\AI\Platform\Message\MessageBag;
18+
use Symfony\AI\Platform\Metadata\Metadata;
19+
use Symfony\AI\Platform\Result\RawResultInterface;
20+
use Symfony\AI\Platform\Result\ResultInterface;
21+
use Symfony\Component\Console\Input\ArrayInput;
22+
use Symfony\Component\Console\Output\BufferedOutput;
23+
use Symfony\Component\Console\Style\SymfonyStyle;
24+
25+
class StreamCommandTest extends TestCase
26+
{
27+
public function testStreamCommandOutputsStreamedContentAndSuccess()
28+
{
29+
$mockAgent = $this->createMock(AgentInterface::class);
30+
$mockAgent
31+
->method('call')
32+
->with($this->isInstanceOf(MessageBag::class), ['stream' => true])
33+
->willReturn(new class implements ResultInterface {
34+
public function getContent(): iterable
35+
{
36+
yield 'Hello';
37+
yield ' ';
38+
yield 'world';
39+
yield '!';
40+
}
41+
42+
public function getMetadata(): Metadata
43+
{
44+
}
45+
46+
public function getRawResult(): ?RawResultInterface
47+
{
48+
}
49+
50+
public function setRawResult(RawResultInterface $rawResult): void
51+
{
52+
}
53+
});
54+
55+
$input = new ArrayInput([]);
56+
$input->setInteractive(false);
57+
$io = new SymfonyStyle($input, $buffer = new BufferedOutput());
58+
$command = new StreamCommand($mockAgent);
59+
$command->__invoke($io);
60+
61+
$output = $buffer->fetch();
62+
63+
$this->assertStringContainsString('Stream Example Command', $output);
64+
$this->assertStringContainsString('This command demonstrates streaming output', $output);
65+
$this->assertStringContainsString('Agent Response:', $output);
66+
$this->assertStringContainsString('Hello world!', $output);
67+
$this->assertStringContainsString('The command has completed successfully.', $output);
68+
}
69+
}

0 commit comments

Comments
 (0)