Skip to content

Commit cc44f81

Browse files
committed
feat(agent): chat session
1 parent d8dfb30 commit cc44f81

31 files changed

+1100
-271
lines changed

demo/config/packages/ai.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ ai:
6767
- 'Symfony\AI\Store\Document\Transformer\TextTrimTransformer'
6868
vectorizer: 'ai.vectorizer.openai'
6969
store: 'ai.store.chroma_db.symfonycon'
70+
message_store:
71+
cache:
72+
audio:
73+
service: 'cache.app'
74+
wikipedia: ~
75+
youtube: ~
76+
chat:
77+
audio:
78+
agent: 'audio'
79+
message_store: 'cache.audio'
80+
wikipedia:
81+
agent: 'wikipedia'
82+
message_store: 'cache.wikipedia'
83+
youtube:
84+
agent: 'youtube'
85+
message_store: 'cache.youtube'
7086

7187
services:
7288
_defaults:

demo/src/Audio/Chat.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

demo/src/Audio/TwigComponent.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111

1212
namespace App\Audio;
1313

14+
use Symfony\AI\Agent\AgentInterface;
15+
use Symfony\AI\Agent\Chat\MessageStoreInterface;
16+
use Symfony\AI\Agent\ChatInterface;
17+
use Symfony\AI\Platform\Bridge\OpenAi\Whisper;
18+
use Symfony\AI\Platform\Message\Content\Audio;
19+
use Symfony\AI\Platform\Message\Message;
1420
use Symfony\AI\Platform\Message\MessageInterface;
21+
use Symfony\AI\Platform\PlatformInterface;
22+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
23+
use Symfony\Component\HttpFoundation\RequestStack;
1524
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1625
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1726
use Symfony\UX\LiveComponent\Attribute\LiveArg;
@@ -23,7 +32,14 @@ final class TwigComponent
2332
use DefaultActionTrait;
2433

2534
public function __construct(
26-
private readonly Chat $chat,
35+
private readonly PlatformInterface $platform,
36+
private readonly RequestStack $requestStack,
37+
#[Autowire(service: 'ai.agent.audio')]
38+
private readonly AgentInterface $agent,
39+
#[Autowire(service: 'ai.chat.audio')]
40+
private readonly ChatInterface $chat,
41+
#[Autowire(service: 'ai.message_store.cache.audio')]
42+
private readonly MessageStoreInterface $messageStore,
2743
) {
2844
}
2945

@@ -32,18 +48,24 @@ public function __construct(
3248
*/
3349
public function getMessages(): array
3450
{
35-
return $this->chat->loadMessages()->withoutSystemMessage()->getMessages();
51+
return $this->chat->getCurrentMessageBag()->withoutSystemMessage()->getMessages();
3652
}
3753

3854
#[LiveAction]
3955
public function submit(#[LiveArg] string $audio): void
4056
{
41-
$this->chat->say($audio);
57+
// Convert base64 to temporary binary file
58+
$path = tempnam(sys_get_temp_dir(), 'audio-').'.wav';
59+
file_put_contents($path, base64_decode($audio));
60+
61+
$result = $this->platform->invoke(new Whisper(), Audio::fromFile($path));
62+
63+
$this->chat->submit(Message::ofUser($result->asText()));
4264
}
4365

4466
#[LiveAction]
4567
public function reset(): void
4668
{
47-
$this->chat->reset();
69+
$this->messageStore->clear();
4870
}
4971
}

demo/src/Wikipedia/Chat.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

demo/src/Wikipedia/TwigComponent.php

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

1212
namespace App\Wikipedia;
1313

14+
use Symfony\AI\Agent\Chat\MessageStoreInterface;
15+
use Symfony\AI\Agent\ChatInterface;
16+
use Symfony\AI\Platform\Message\Message;
1417
use Symfony\AI\Platform\Message\MessageInterface;
18+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1519
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1620
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1721
use Symfony\UX\LiveComponent\Attribute\LiveArg;
@@ -23,7 +27,10 @@ final class TwigComponent
2327
use DefaultActionTrait;
2428

2529
public function __construct(
26-
private readonly Chat $wikipedia,
30+
#[Autowire(service: 'ai.chat.wikipedia')]
31+
private readonly ChatInterface $chat,
32+
#[Autowire(service: 'ai.message_store.cache.wikipedia')]
33+
private readonly MessageStoreInterface $messageStore,
2734
) {
2835
}
2936

@@ -32,18 +39,18 @@ public function __construct(
3239
*/
3340
public function getMessages(): array
3441
{
35-
return $this->wikipedia->loadMessages()->withoutSystemMessage()->getMessages();
42+
return $this->chat->getCurrentMessageBag()->getMessages();
3643
}
3744

3845
#[LiveAction]
3946
public function submit(#[LiveArg] string $message): void
4047
{
41-
$this->wikipedia->submitMessage($message);
48+
$this->chat->submit(Message::ofUser($message));
4249
}
4350

4451
#[LiveAction]
4552
public function reset(): void
4653
{
47-
$this->wikipedia->reset();
54+
$this->messageStore->clear();
4855
}
4956
}

demo/src/YouTube/Chat.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)