diff --git a/README.md b/README.md index e877566..763044f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ llm_chain: model: name: 'GPT' version: 'gpt-4o-mini' + system_prompt: 'You are a helpful assistant that can answer questions.' # The default system prompt of the chain tools: - 'PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch' research: diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3c88960..b33c071 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -64,6 +64,14 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->booleanNode('structured_output')->defaultTrue()->end() + ->scalarNode('system_prompt') + ->validate() + ->ifTrue(fn ($v) => null !== $v && '' === trim($v)) + ->thenInvalid('The default system prompt must not be an empty string') + ->end() + ->defaultNull() + ->info('The default system prompt of the chain') + ->end() ->arrayNode('tools') ->addDefaultsIfNotSet() ->treatFalseLike(['enabled' => false]) diff --git a/src/DependencyInjection/LlmChainExtension.php b/src/DependencyInjection/LlmChainExtension.php index e03b8e8..063f496 100644 --- a/src/DependencyInjection/LlmChainExtension.php +++ b/src/DependencyInjection/LlmChainExtension.php @@ -18,6 +18,7 @@ use PhpLlm\LlmChain\Bridge\Voyage\Voyage; use PhpLlm\LlmChain\Chain; use PhpLlm\LlmChain\Chain\InputProcessor; +use PhpLlm\LlmChain\Chain\InputProcessor\SystemPromptInputProcessor; use PhpLlm\LlmChain\Chain\OutputProcessor; use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor as StructureOutputProcessor; use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool; @@ -259,6 +260,17 @@ private function processChainConfig(string $name, array $config, ContainerBuilde $inputProcessors[] = new Reference(StructureOutputProcessor::class); $outputProcessors[] = new Reference(StructureOutputProcessor::class); } + + // SYSTEM PROMPT + if (is_string($config['system_prompt'])) { + $systemPromptInputProcessorDefinition = new Definition(SystemPromptInputProcessor::class); + $systemPromptInputProcessorDefinition + ->setAutowired(true) + ->setArgument('$systemPrompt', $config['system_prompt']); + + $inputProcessor[] = $systemPromptInputProcessorDefinition; + } + $chainDefinition ->setArgument('$inputProcessors', $inputProcessors) ->setArgument('$outputProcessors', $outputProcessors);