From cbf06298b7013c7c33b4d398edecf66d7aa6aa91 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Fri, 10 Oct 2025 00:36:38 +0200 Subject: [PATCH] Add config options for keeping tool call messages and sources + expose metadata in profiler --- .github/workflows/code-quality.yaml | 2 +- demo/config/packages/ai.yaml | 5 +++-- demo/src/Wikipedia/Chat.php | 4 +++- demo/templates/components/wikipedia.html.twig | 16 +++++++++++++--- docs/components/agent.rst | 4 ++-- examples/anthropic/toolcall.php | 2 +- src/agent/src/Toolbox/AgentProcessor.php | 4 ++-- src/agent/tests/Toolbox/AgentProcessorTest.php | 6 +++--- src/ai-bundle/config/options.php | 13 ++++++++++++- src/ai-bundle/config/services.php | 1 + src/ai-bundle/src/AiBundle.php | 4 +++- src/ai-bundle/src/Profiler/DataCollector.php | 6 +++++- src/ai-bundle/templates/data_collector.html.twig | 9 +++++++++ 13 files changed, 58 insertions(+), 18 deletions(-) diff --git a/.github/workflows/code-quality.yaml b/.github/workflows/code-quality.yaml index 058574ba0..a43fc344c 100644 --- a/.github/workflows/code-quality.yaml +++ b/.github/workflows/code-quality.yaml @@ -29,7 +29,7 @@ jobs: - name: Configure environment run: | echo COLUMNS=120 >> $GITHUB_ENV - echo COMPOSER_UP='composer update --no-progress --no-interaction --ansi --ignore-platform-req=ext-mongodb' >> $GITHUB_ENV + echo COMPOSER_UP='composer update --no-progress --no-interaction --no-scripts --ansi --ignore-platform-req=ext-mongodb' >> $GITHUB_ENV echo PHPSTAN='vendor/bin/phpstan' >> $GITHUB_ENV PACKAGES=$(find src/ -mindepth 2 -type f -name composer.json -not -path "*/vendor/*" -printf '%h\n' | sed 's/^src\///' | grep -Ev "examples" | sort | tr '\n' ' ') diff --git a/demo/config/packages/ai.yaml b/demo/config/packages/ai.yaml index ed37f2a33..d7117a68f 100644 --- a/demo/config/packages/ai.yaml +++ b/demo/config/packages/ai.yaml @@ -25,10 +25,11 @@ ai: options: temperature: 0.5 prompt: - text: 'Please answer the users question based on Wikipedia and provide a link to the article.' + text: 'Please answer the users question based on Wikipedia, only use information provided in the messages.' include_tools: true tools: - 'Symfony\AI\Agent\Toolbox\Tool\Wikipedia' + include_sources: true audio: model: 'gpt-4o-mini?temperature=1.0' prompt: 'You are a friendly chatbot that likes to have a conversation with users and asks them some questions.' @@ -89,7 +90,7 @@ services: Symfony\AI\Store\Document\Loader\RssFeedLoader: ~ Symfony\AI\Store\Document\Transformer\TextTrimTransformer: ~ - + app.filter.week_of_symfony: class: 'Symfony\AI\Store\Document\Filter\TextContainsFilter' arguments: diff --git a/demo/src/Wikipedia/Chat.php b/demo/src/Wikipedia/Chat.php index cd9736f7a..19ec60f5c 100644 --- a/demo/src/Wikipedia/Chat.php +++ b/demo/src/Wikipedia/Chat.php @@ -43,7 +43,9 @@ public function submitMessage(string $message): void \assert($result instanceof TextResult); - $messages->add(Message::ofAssistant($result->getContent())); + $response = Message::ofAssistant($result->getContent()); + $response->getMetadata()->add('sources', $result->getMetadata()->get('sources', [])); + $messages->add($response); $this->saveMessages($messages); } diff --git a/demo/templates/components/wikipedia.html.twig b/demo/templates/components/wikipedia.html.twig index a187a1706..2e78014a9 100644 --- a/demo/templates/components/wikipedia.html.twig +++ b/demo/templates/components/wikipedia.html.twig @@ -1,4 +1,4 @@ -{% import "_message.html.twig" as message %} +{% import "_message.html.twig" as msg %}
@@ -9,6 +9,16 @@
{% for message in this.messages %} {% include '_message.html.twig' with { message, latest: loop.last } %} + {% if message.metadata.has('sources') %} +
+
Sources:
+
    + {% for source in message.metadata.get('sources') %} +
  1. {{ source.name }}
  2. + {% endfor %} +
+
+ {% endif %} {% else %}
{{ ux_icon('mdi:wikipedia', { height: '200px', width: '200px' }) }} @@ -17,8 +27,8 @@
{% endfor %}
- {{ message.user([{text:''}]) }} - {{ message.bot('The Wikipedia Bot is doing some research ...', true) }} + {{ msg.user([{text:''}]) }} + {{ msg.bot('The Wikipedia Bot is doing some research ...', true) }}