Skip to content

Commit 9ff8d83

Browse files
committed
[AI Bundle] Restructure system_prompt configuration to nest include_tools
- Move include_tools configuration from agent level to system_prompt level - Support both simple string format and advanced array format for system_prompt - Add comprehensive test coverage for new configuration structure - Update demo configuration to showcase new array format - Update documentation with clear examples for both formats - Maintain full backward compatibility with existing string configurations String format (simple usage): system_prompt: "You are a helpful assistant." Array format (advanced usage): system_prompt: prompt: "You are a helpful assistant." include_tools: true Benefits: - Ergonomic design: simple string for basic use, array for advanced features - Logical grouping: tool inclusion now grouped with system prompt - Future extensibility: array format allows for future enhancements - Clear intent: configuration structure shows prompt-tool relationship
1 parent ca49b53 commit 9ff8d83

File tree

5 files changed

+347
-23
lines changed

5 files changed

+347
-23
lines changed

demo/config/packages/ai.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ ai:
3535
name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
3636
options:
3737
temperature: 0.5
38-
system_prompt: 'Please answer the users question based on Wikipedia and provide a link to the article.'
39-
include_tools: true
38+
system_prompt:
39+
prompt: 'Please answer the users question based on Wikipedia and provide a link to the article.'
40+
include_tools: true
4041
tools:
4142
- 'Symfony\AI\Agent\Toolbox\Tool\Wikipedia'
4243
audio:

src/ai-bundle/config/options.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,48 @@
130130
->end()
131131
->end()
132132
->booleanNode('structured_output')->defaultTrue()->end()
133-
->scalarNode('system_prompt')
133+
->variableNode('system_prompt')
134134
->validate()
135-
->ifTrue(fn ($v) => null !== $v && '' === trim($v))
136-
->thenInvalid('The default system prompt must not be an empty string')
135+
->ifTrue(function ($v) {
136+
// Allow null (no system prompt)
137+
if (null === $v) {
138+
return false;
139+
}
140+
141+
// Allow string (simple usage - just a prompt)
142+
if (\is_string($v)) {
143+
return '' === trim($v);
144+
}
145+
146+
// Allow array with prompt key (advanced usage with include_tools)
147+
if (\is_array($v)) {
148+
return !isset($v['prompt']) || (isset($v['prompt']) && '' === trim($v['prompt']));
149+
}
150+
151+
// Reject other types
152+
return true;
153+
})
154+
->thenInvalid('The system prompt must be a string (simple prompt), an array with "prompt" key (advanced configuration), or null. If provided, the prompt text must not be an empty string.')
155+
->end()
156+
->beforeNormalization()
157+
->ifString()
158+
->then(function (string $v) {
159+
return ['prompt' => $v, 'include_tools' => false];
160+
})
161+
->end()
162+
->beforeNormalization()
163+
->ifArray()
164+
->then(function (array $v) {
165+
// Ensure include_tools defaults to false if not provided
166+
if (!isset($v['include_tools'])) {
167+
$v['include_tools'] = false;
168+
}
169+
170+
return $v;
171+
})
137172
->end()
138173
->defaultNull()
139-
->info('The default system prompt of the agent')
140-
->end()
141-
->booleanNode('include_tools')
142-
->info('Include tool definitions at the end of the system prompt')
143-
->defaultFalse()
174+
->info('The system prompt configuration. Use a string for simple prompts, or an array with "prompt" and "include_tools" keys for advanced configuration.')
144175
->end()
145176
->arrayNode('tools')
146177
->addDefaultsIfNotSet()

src/ai-bundle/doc/index.rst

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ Configuration
7171
model:
7272
class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
7373
name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
74-
system_prompt: 'You are a helpful assistant that can answer questions.' # The default system prompt of the agent
75-
include_tools: true # Include tool definitions at the end of the system prompt
74+
system_prompt: # The system prompt configuration
75+
prompt: 'You are a helpful assistant that can answer questions.' # The prompt text
76+
include_tools: true # Include tool definitions at the end of the system prompt
7677
tools:
7778
# Referencing a service with #[AsTool] attribute
7879
- 'Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch'
@@ -144,6 +145,46 @@ Configuration
144145
vectorizer: 'ai.vectorizer.mistral_embeddings'
145146
store: 'ai.store.memory.research'
146147
148+
System Prompt Configuration
149+
----------------------------
150+
151+
The ``system_prompt`` configuration supports two formats to provide flexibility for different use cases:
152+
153+
**Simple Format (String)**
154+
155+
For basic usage when you only need to set a prompt text:
156+
157+
.. code-block:: yaml
158+
159+
ai:
160+
agent:
161+
my_agent:
162+
model:
163+
class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
164+
name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
165+
system_prompt: 'You are a helpful assistant.'
166+
167+
**Advanced Format (Array)**
168+
169+
For advanced usage when you need additional configuration like tool inclusion:
170+
171+
.. code-block:: yaml
172+
173+
ai:
174+
agent:
175+
my_agent:
176+
model:
177+
class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
178+
name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
179+
system_prompt:
180+
prompt: 'You are a helpful assistant that can answer questions.'
181+
include_tools: true # Include tool definitions at the end of the system prompt
182+
183+
**Configuration Options**
184+
185+
* ``prompt`` (string): The system prompt text that will be sent to the AI model
186+
* ``include_tools`` (boolean, default: false): Whether to automatically append tool definitions to the system prompt
187+
147188
Usage
148189
-----
149190

src/ai-bundle/src/AiBundle.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -604,16 +604,28 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
604604
}
605605

606606
// SYSTEM PROMPT
607-
if (\is_string($config['system_prompt'])) {
608-
$systemPromptInputProcessorDefinition = (new Definition(SystemPromptInputProcessor::class))
609-
->setArguments([
610-
$config['system_prompt'],
611-
$config['include_tools'] ? new Reference('ai.toolbox.'.$name) : null,
612-
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
613-
])
614-
->addTag('ai.agent.input_processor', ['agent' => $agentId, 'priority' => -30]);
607+
if (null !== $config['system_prompt']) {
608+
$systemPromptConfig = $config['system_prompt'];
609+
610+
if (\is_array($systemPromptConfig)) {
611+
$promptText = $systemPromptConfig['prompt'] ?? '';
612+
$includeTools = $systemPromptConfig['include_tools'] ?? false;
613+
} else {
614+
$promptText = (string) $systemPromptConfig;
615+
$includeTools = $config['include_tools'] ?? false;
616+
}
615617

616-
$container->setDefinition('ai.agent.'.$name.'.system_prompt_processor', $systemPromptInputProcessorDefinition);
618+
if (!empty(trim($promptText))) {
619+
$systemPromptInputProcessorDefinition = (new Definition(SystemPromptInputProcessor::class))
620+
->setArguments([
621+
$promptText,
622+
$includeTools ? new Reference('ai.toolbox.'.$name) : null,
623+
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
624+
])
625+
->addTag('ai.agent.input_processor', ['agent' => $agentId, 'priority' => -30]);
626+
627+
$container->setDefinition('ai.agent.'.$name.'.system_prompt_processor', $systemPromptInputProcessorDefinition);
628+
}
617629
}
618630

619631
$agentDefinition

0 commit comments

Comments
 (0)