Skip to content

Symfony AI - Chat Component

Edit this page

The Chat component provides an API to interact with agents, it allows to store messages and retrieve them later for future chat and context-retrieving purposes.

Installation

1
$ composer require symfony/ai-chat

Basic Usage

To initiate a chat, you need to instantiate the Symfony\AI\Chat\Chat along with a Symfony\AI\Agent\AgentInterface and a Symfony\AI\Chat\MessageStoreInterface & Symfony\AI\Chat\ManagedStoreInterface:

1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\AI\Agent\Agent;
use Symfony\AI\Chat\Chat;
use Symfony\AI\Chat\InMemory\Store as InMemoryStore;
use Symfony\AI\Platform\Bridge\OpenAi\Factory;
use Symfony\AI\Platform\Message\Message;

$platform = Factory::createPlatform($apiKey);

$agent = new Agent($platform, 'gpt-4o-mini');
$chat = new Chat($agent, new InMemoryStore());

$chat->submit(Message::ofUser('Hello'));

Streaming

The Chat component supports streaming responses from the LLM in real-time using the stream() method. This returns a Generator that yields DeltaInterface deltas as they are produced by the model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Symfony\AI\Agent\Agent;
use Symfony\AI\Chat\Chat;
use Symfony\AI\Chat\InMemory\Store as InMemoryStore;
use Symfony\AI\Platform\Bridge\OpenAi\Factory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Result\Stream\Delta\TextDelta;

$platform = Factory::createPlatform($apiKey);

$agent = new Agent($platform, 'gpt-4o-mini');
$chat = new Chat($agent, new InMemoryStore());

$chat->initiate(new MessageBag(
    Message::forSystem('You are a helpful assistant.'),
));

foreach ($chat->stream(Message::ofUser('Tell me a story about the sun')) as $delta) {
    if ($delta instanceof TextDelta) {
        echo $delta;
    }
}

Once the stream is fully consumed, the assistant message is automatically persisted to the message store along with the user message. This means the conversation history is kept up-to-date without any additional code.

Note

Due to implementations limitations, using streaming with MessageStore is not recommended.

Implementing a Bridge

The main extension points of the Chat component is the MessageStoreInterface, that defines the methods for adding messages to the message store, and returning the messages from a store.

This leads to a store implementing two methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\AI\Chat\MessageStoreInterface;
use Symfony\AI\Platform\Message\MessageBag;

class MyCustomStore implements MessageStoreInterface
{
    public function save(MessageBag $messages): void
    {
        // Implementation to add a message bag to the store
    }

    public function load(): MessageBag
    {
        // Implementation to return a message bag from the store
    }
}

Managing a store

Some store might requires to create table, indexes and so on before storing messages, the ManagedStoreInterface defines the methods to setup and drop the store.

This leads to a store implementing two methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\AI\Chat\ManagedStoreInterface;
use Symfony\AI\Chat\MessageStoreInterface;

class MyCustomStore implements ManagedStoreInterface, MessageStoreInterface
{
    # ...

    public function setup(array $options = []): void
    {
        // Implementation to create the store
    }

    public function drop(): void
    {
        // Implementation to drop the store (and related messages)
    }
}

Commands

While using the Chat component in your Symfony application along with the AiBundle, you can use the bin/console ai:message-store:setup command to initialize the message store and bin/console ai:message-store:drop to clean up the message store:

1
2
3
4
5
6
7
8
# config/packages/ai.yaml
ai:
    # ...

    message_store:
        cache:
            symfonycon:
                service: 'cache.app'
1
2
$ php bin/console ai:message-store:setup symfonycon
$ php bin/console ai:message-store:drop symfonycon
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version