From cab872e635286f30d227e791a304bbf0bc0cc2aa Mon Sep 17 00:00:00 2001 From: Pavel Buchnev Date: Tue, 10 Sep 2024 16:36:19 +0400 Subject: [PATCH] feat: improve prompt instructions --- src/Interceptors/InstructionGenerator.php | 26 ++++++++++++++++------- src/Interceptors/LinkedAgentsInjector.php | 5 ++++- src/Interceptors/UserPromptInjector.php | 10 +++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Interceptors/InstructionGenerator.php b/src/Interceptors/InstructionGenerator.php index b82091d..3271328 100644 --- a/src/Interceptors/InstructionGenerator.php +++ b/src/Interceptors/InstructionGenerator.php @@ -11,8 +11,12 @@ use LLM\Agents\PromptGenerator\PromptGeneratorInput; use LLM\Agents\PromptGenerator\PromptInterceptorInterface; -final class InstructionGenerator implements PromptInterceptorInterface +final readonly class InstructionGenerator implements PromptInterceptorInterface { + public function __construct( + private string $outputFormat = 'markdown', + ) {} + public function generate( PromptGeneratorInput $input, InterceptorHandler $next, @@ -24,17 +28,23 @@ public function generate( $input->prompt ->withAddedMessage( MessagePrompt::system( - prompt: <<<'PROMPT' -{prompt} -Important rules: -- always response in markdown format -- think before responding to user -PROMPT, + prompt: '{instruction}', + with: ['instruction'], + ), + ) + ->withAddedMessage( + MessagePrompt::system( + prompt: 'Output format instruction: {output_format_instruction}', + with: ['output_format_instruction'], ), ) ->withValues( values: [ - 'prompt' => $input->agent->getInstruction(), + 'instruction' => $input->agent->getInstruction(), + 'output_format_instruction' => \sprintf( + 'always response in `%s` format', + $this->outputFormat, + ), ], ), ), diff --git a/src/Interceptors/LinkedAgentsInjector.php b/src/Interceptors/LinkedAgentsInjector.php index cb803f1..5ba28cb 100644 --- a/src/Interceptors/LinkedAgentsInjector.php +++ b/src/Interceptors/LinkedAgentsInjector.php @@ -20,6 +20,7 @@ public function __construct( private AgentRepositoryInterface $agents, private SchemaMapperInterface $schemaMapper, + private string $agentKey = 'ask_agent', ) {} public function generate( @@ -52,14 +53,16 @@ public function generate( MessagePrompt::system( prompt: <<<'PROMPT' There are agents {linked_agents} associated with you. You can ask them for help if you need it. -Use the `ask_agent` tool and provide the agent key. +Use the `{ask_agent_tool}` tool and provide the agent key. Always follow rules: - Don't make up the agent key. Use only the ones from the provided list. PROMPT, + with: ['linked_agents', 'ask_agent_tool'], ), ) ->withValues( values: [ + 'ask_agent_tool' => $this->agentKey, 'linked_agents' => \implode( PHP_EOL, \array_map( diff --git a/src/Interceptors/UserPromptInjector.php b/src/Interceptors/UserPromptInjector.php index 3568e15..00749ae 100644 --- a/src/Interceptors/UserPromptInjector.php +++ b/src/Interceptors/UserPromptInjector.php @@ -22,10 +22,12 @@ public function generate( return $next( input: $input->withPrompt( $input->prompt->withAddedMessage( - MessagePrompt::user('{user_prompt}') - ->withValues([ - 'user_prompt' => (string) $input->userPrompt, - ]), + MessagePrompt::user( + prompt: '{user_prompt}', + with: ['user_prompt'], + )->withValues([ + 'user_prompt' => (string) $input->userPrompt, + ]), ), ), );