Skip to content

Commit ec7e181

Browse files
committed
Add config options for keeping tool call messages and sources + expose metadata in profiler
1 parent 0eeb101 commit ec7e181

File tree

9 files changed

+50
-10
lines changed

9 files changed

+50
-10
lines changed

.github/workflows/code-quality.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Configure environment
3030
run: |
3131
echo COLUMNS=120 >> $GITHUB_ENV
32-
echo COMPOSER_UP='composer update --no-progress --no-interaction --ansi --ignore-platform-req=ext-mongodb' >> $GITHUB_ENV
32+
echo COMPOSER_UP='composer update --no-progress --no-interaction --no-scripts --ansi --ignore-platform-req=ext-mongodb' >> $GITHUB_ENV
3333
echo PHPSTAN='vendor/bin/phpstan' >> $GITHUB_ENV
3434
3535
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' ' ')

demo/config/packages/ai.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ ai:
2525
options:
2626
temperature: 0.5
2727
prompt:
28-
text: 'Please answer the users question based on Wikipedia and provide a link to the article.'
28+
text: 'Please answer the users question based on Wikipedia, only use information provided in the messages.'
2929
include_tools: true
3030
tools:
3131
- 'Symfony\AI\Agent\Toolbox\Tool\Wikipedia'
32+
keep_tool_sources: true
3233
audio:
3334
model: 'gpt-4o-mini?temperature=1.0'
3435
prompt: 'You are a friendly chatbot that likes to have a conversation with users and asks them some questions.'
@@ -89,7 +90,7 @@ services:
8990

9091
Symfony\AI\Store\Document\Loader\RssFeedLoader: ~
9192
Symfony\AI\Store\Document\Transformer\TextTrimTransformer: ~
92-
93+
9394
app.filter.week_of_symfony:
9495
class: 'Symfony\AI\Store\Document\Filter\TextContainsFilter'
9596
arguments:

demo/src/Wikipedia/Chat.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function submitMessage(string $message): void
4343

4444
\assert($result instanceof TextResult);
4545

46-
$messages->add(Message::ofAssistant($result->getContent()));
46+
$response = Message::ofAssistant($result->getContent());
47+
$response->getMetadata()->add('sources', $result->getMetadata()->get('sources', []));
48+
$messages->add($response);
4749

4850
$this->saveMessages($messages);
4951
}

demo/templates/components/wikipedia.html.twig

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% import "_message.html.twig" as message %}
1+
{% import "_message.html.twig" as msg %}
22

33
<div class="card mx-auto shadow-lg" {{ attributes.defaults(stimulus_controller('wikipedia')) }}>
44
<div class="card-header p-2">
@@ -9,6 +9,16 @@
99
<div id="chat-body" class="card-body p-4 overflow-auto">
1010
{% for message in this.messages %}
1111
{% include '_message.html.twig' with { message, latest: loop.last } %}
12+
{% if message.metadata.has('sources') %}
13+
<div class="ms-5">
14+
<h6>Sources:</h6>
15+
<ol>
16+
{% for source in message.metadata.get('sources') %}
17+
<li><a class="text-black-50" href="{{ source.reference }}" target="_blank" rel="noopener">{{ source.name }}</a></li>
18+
{% endfor %}
19+
</ol>
20+
</div>
21+
{% endif %}
1222
{% else %}
1323
<div id="welcome" class="text-center mt-5 py-5 bg-white rounded-5 shadow-sm w-75 mx-auto">
1424
{{ ux_icon('mdi:wikipedia', { height: '200px', width: '200px' }) }}
@@ -17,8 +27,8 @@
1727
</div>
1828
{% endfor %}
1929
<div id="loading-message" class="d-none">
20-
{{ message.user([{text:''}]) }}
21-
{{ message.bot('The Wikipedia Bot is doing some research ...', true) }}
30+
{{ msg.user([{text:''}]) }}
31+
{{ msg.bot('The Wikipedia Bot is doing some research ...', true) }}
2232
</div>
2333
</div>
2434
<div class="card-footer p-2">

src/ai-bundle/config/options.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,18 @@
409409
->end()
410410
->end()
411411
->end()
412-
->booleanNode('fault_tolerant_toolbox')->defaultTrue()->end()
412+
->booleanNode('keep_tool_messages')
413+
->info('Keep tool messages in the conversation history')
414+
->defaultFalse()
415+
->end()
416+
->booleanNode('keep_tool_sources')
417+
->info('Keep tool sources as part of the tool result metadata')
418+
->defaultFalse()
419+
->end()
420+
->booleanNode('fault_tolerant_toolbox')
421+
->info('Continue the agent run even if a tool call fails')
422+
->defaultTrue()
423+
->end()
413424
->end()
414425
->end()
415426
->end()

src/ai-bundle/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
service('ai.tool_result_converter'),
158158
service('event_dispatcher')->nullOnInvalid(),
159159
false,
160+
false,
160161
])
161162
->set('ai.security.is_granted_attribute_listener', IsGrantedToolAttributeListener::class)
162163
->args([

src/ai-bundle/src/AiBundle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,9 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
613613
}
614614

615615
$toolProcessorDefinition = (new ChildDefinition('ai.tool.agent_processor.abstract'))
616-
->replaceArgument(0, new Reference('ai.toolbox.'.$name));
616+
->replaceArgument(0, new Reference('ai.toolbox.'.$name))
617+
->replaceArgument(3, $config['keep_tool_messages'])
618+
->replaceArgument(4, $config['keep_tool_sources']);
617619

618620
$container->setDefinition('ai.tool.agent_processor.'.$name, $toolProcessorDefinition)
619621
->addTag('ai.agent.input_processor', ['agent' => $agentId, 'priority' => -10])

src/ai-bundle/src/Profiler/DataCollector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\AiBundle\Profiler;
1313

1414
use Symfony\AI\Agent\Toolbox\ToolResult;
15+
use Symfony\AI\Platform\Metadata\Metadata;
1516
use Symfony\AI\Platform\Tool\Tool;
1617
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
1718
use Symfony\Component\HttpFoundation\Request;
@@ -103,7 +104,8 @@ private function getAllTools(): array
103104
* model: string,
104105
* input: array<mixed>|string|object,
105106
* options: array<string, mixed>,
106-
* result: string|iterable<mixed>|object|null
107+
* result: string|iterable<mixed>|object|null,
108+
* metadata: Metadata,
107109
* }[]
108110
*/
109111
private function awaitCallResults(TraceablePlatform $platform): array
@@ -118,6 +120,8 @@ private function awaitCallResults(TraceablePlatform $platform): array
118120
$call['result'] = $result->getContent();
119121
}
120122

123+
$call['metadata'] = $result->getMetadata();
124+
121125
$calls[$key] = $call;
122126
}
123127

src/ai-bundle/templates/data_collector.html.twig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@
164164
{% else %}
165165
{{ call.result }}
166166
{% endif %}
167+
{% if call.metadata|length > 0 %}
168+
<br />
169+
<strong>Metadata</strong>
170+
<ul>
171+
{% for key, value in call.metadata.all %}
172+
<li>{{ key }}:<br />{{ dump(value) }}</li>
173+
{% endfor %}
174+
</ul>
175+
{% endif %}
167176
</td>
168177
</tr>
169178
</tbody>

0 commit comments

Comments
 (0)