Skip to content

Commit 3406794

Browse files
authored
feat: make system prompt for chain configurable (#57)
1 parent e427048 commit 3406794

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ llm_chain:
4545
model:
4646
name: 'GPT'
4747
version: 'gpt-4o-mini'
48+
system_prompt: 'You are a helpful assistant that can answer questions.' # The default system prompt of the chain
4849
tools:
4950
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch'
5051
research:

src/DependencyInjection/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function getConfigTreeBuilder(): TreeBuilder
6464
->end()
6565
->end()
6666
->booleanNode('structured_output')->defaultTrue()->end()
67+
->scalarNode('system_prompt')
68+
->validate()
69+
->ifTrue(fn ($v) => null !== $v && '' === trim($v))
70+
->thenInvalid('The default system prompt must not be an empty string')
71+
->end()
72+
->defaultNull()
73+
->info('The default system prompt of the chain')
74+
->end()
6775
->arrayNode('tools')
6876
->addDefaultsIfNotSet()
6977
->treatFalseLike(['enabled' => false])

src/DependencyInjection/LlmChainExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpLlm\LlmChain\Bridge\Voyage\Voyage;
1919
use PhpLlm\LlmChain\Chain;
2020
use PhpLlm\LlmChain\Chain\InputProcessor;
21+
use PhpLlm\LlmChain\Chain\InputProcessor\SystemPromptInputProcessor;
2122
use PhpLlm\LlmChain\Chain\OutputProcessor;
2223
use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor as StructureOutputProcessor;
2324
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
@@ -259,6 +260,17 @@ private function processChainConfig(string $name, array $config, ContainerBuilde
259260
$inputProcessors[] = new Reference(StructureOutputProcessor::class);
260261
$outputProcessors[] = new Reference(StructureOutputProcessor::class);
261262
}
263+
264+
// SYSTEM PROMPT
265+
if (is_string($config['system_prompt'])) {
266+
$systemPromptInputProcessorDefinition = new Definition(SystemPromptInputProcessor::class);
267+
$systemPromptInputProcessorDefinition
268+
->setAutowired(true)
269+
->setArgument('$systemPrompt', $config['system_prompt']);
270+
271+
$inputProcessor[] = $systemPromptInputProcessorDefinition;
272+
}
273+
262274
$chainDefinition
263275
->setArgument('$inputProcessors', $inputProcessors)
264276
->setArgument('$outputProcessors', $outputProcessors);

0 commit comments

Comments
 (0)