-
-
Notifications
You must be signed in to change notification settings - Fork 133
[Chat] Add streaming support & persistence to storage through accumulator callback chaining #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marco-jouwweb
wants to merge
16
commits into
symfony:main
Choose a base branch
from
marco-jouwweb:feature/stream-support-chat-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−11
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
00d63ae
Added streaming support to Chat API & persistence to storage through …
33e0475
added newline
d2a3b86
reverted irrelevant test changes
9f69836
fixed phpstan chat
048b25d
Fixed metadata overwriting bug in StreamResult
82f4804
Reverted whitespace change
e40acc7
Reverted whitespace change
3949dee
Finally reverted whitespace properly
4aa44e5
Revert whole file
7efc534
Fixed changelog
64f7b98
added whitespace
8a71916
added mongodb streaminablestoreinterface
4fd4943
Fixed style issues (fabbot)
780ef0b
fix indentaiton changelog
abedd41
readded whitespace
8df829e
removed dupe line in changelog
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
|
@@ -34,7 +35,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class DoctrineDbalMessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class DoctrineDbalMessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly string $tableName, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,13 @@ | |
| use Symfony\AI\Agent\Exception\RuntimeException; | ||
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| /** | ||
| * @author Christopher Hertel <[email protected]> | ||
| */ | ||
| final class CacheStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class CacheStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly CacheItemPoolInterface $cache, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,13 @@ | |
|
|
||
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| /** | ||
| * @author Christopher Hertel <[email protected]> | ||
| */ | ||
| final class InMemoryStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class InMemoryStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| /** | ||
| * @var MessageBag[] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Clock\ClockInterface; | ||
|
|
@@ -31,7 +32,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly HttpClientInterface $httpClient, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
|
@@ -27,7 +28,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly Client $client, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
|
@@ -28,7 +29,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly HttpClientInterface $httpClient, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
|
@@ -24,7 +25,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly \Redis $redis, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use Symfony\AI\Chat\ManagedStoreInterface; | ||
| use Symfony\AI\Chat\MessageNormalizer; | ||
| use Symfony\AI\Chat\MessageStoreInterface; | ||
| use Symfony\AI\Chat\StreamableStoreInterface; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\MessageInterface; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
|
@@ -29,7 +30,7 @@ | |
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface | ||
| final class MessageStore implements ManagedStoreInterface, MessageStoreInterface, StreamableStoreInterface | ||
| { | ||
| private string $authenticationToken = ''; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\AI\Chat\Result; | ||
|
|
||
| use Symfony\AI\Agent\Toolbox\StreamResult as ToolboxStreamResult; | ||
| use Symfony\AI\Platform\Message\AssistantMessage; | ||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Metadata\Metadata; | ||
| use Symfony\AI\Platform\Result\StreamResult; | ||
| use Symfony\AI\Platform\Result\ToolCallResult; | ||
|
|
||
| /** | ||
| * @author Marco van Angeren <[email protected]> | ||
| */ | ||
| final class AccumulatingStreamResult | ||
| { | ||
| private ?\Closure $onComplete = null; | ||
|
|
||
| public function __construct( | ||
| private readonly StreamResult|ToolboxStreamResult $innerResult, | ||
| ?\Closure $onComplete = null, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't be declared as a property? |
||
| ) { | ||
| $this->onComplete = $onComplete; | ||
| } | ||
|
|
||
| public function addOnComplete(\Closure $callback): void | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would renamed it to |
||
| { | ||
| $existingCallback = $this->onComplete; | ||
|
|
||
| $this->onComplete = $existingCallback | ||
| ? function (AssistantMessage $message) use ($existingCallback, $callback) { | ||
| $existingCallback($message); | ||
| $callback($message); | ||
| } | ||
| : $callback; | ||
| } | ||
|
|
||
| public function getContent(): \Generator | ||
| { | ||
| $accumulatedContent = ''; | ||
| $toolCalls = []; | ||
|
|
||
| try { | ||
| foreach ($this->innerResult->getContent() as $value) { | ||
| if ($value instanceof ToolCallResult) { | ||
| array_push($toolCalls, ...$value->getContent()); | ||
| yield $value; | ||
| continue; | ||
| } | ||
|
|
||
| $accumulatedContent .= $value; | ||
| yield $value; | ||
| } | ||
| } finally { | ||
| if (null !== $this->onComplete) { | ||
| $assistantMessage = Message::ofAssistant( | ||
| '' === $accumulatedContent ? null : $accumulatedContent, | ||
| $toolCalls ?: null | ||
| ); | ||
|
|
||
| $assistantMessage->getMetadata()->set($this->innerResult->getMetadata()->all()); | ||
|
|
||
| ($this->onComplete)($assistantMessage); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function getMetadata(): Metadata | ||
| { | ||
| return $this->innerResult->getMetadata(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\AI\Chat; | ||
|
|
||
| /** | ||
| * @author Marco van Angeren <[email protected]> | ||
| */ | ||
| interface StreamableStoreInterface | ||
| { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be added after the list of stores IMO