Skip to content

Commit 86ecea4

Browse files
authored
Merge pull request #3 from llm-agents-php/feature/improve-prompt-instructions
feat: improve prompt instructions
2 parents 8b040a0 + cab872e commit 86ecea4

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/Interceptors/InstructionGenerator.php

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
1212
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
1313

14-
final class InstructionGenerator implements PromptInterceptorInterface
14+
final readonly class InstructionGenerator implements PromptInterceptorInterface
1515
{
16+
public function __construct(
17+
private string $outputFormat = 'markdown',
18+
) {}
19+
1620
public function generate(
1721
PromptGeneratorInput $input,
1822
InterceptorHandler $next,
@@ -24,17 +28,23 @@ public function generate(
2428
$input->prompt
2529
->withAddedMessage(
2630
MessagePrompt::system(
27-
prompt: <<<'PROMPT'
28-
{prompt}
29-
Important rules:
30-
- always response in markdown format
31-
- think before responding to user
32-
PROMPT,
31+
prompt: '{instruction}',
32+
with: ['instruction'],
33+
),
34+
)
35+
->withAddedMessage(
36+
MessagePrompt::system(
37+
prompt: 'Output format instruction: {output_format_instruction}',
38+
with: ['output_format_instruction'],
3339
),
3440
)
3541
->withValues(
3642
values: [
37-
'prompt' => $input->agent->getInstruction(),
43+
'instruction' => $input->agent->getInstruction(),
44+
'output_format_instruction' => \sprintf(
45+
'always response in `%s` format',
46+
$this->outputFormat,
47+
),
3848
],
3949
),
4050
),

src/Interceptors/LinkedAgentsInjector.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public function __construct(
2121
private AgentRepositoryInterface $agents,
2222
private SchemaMapperInterface $schemaMapper,
23+
private string $agentKey = 'ask_agent',
2324
) {}
2425

2526
public function generate(
@@ -52,14 +53,16 @@ public function generate(
5253
MessagePrompt::system(
5354
prompt: <<<'PROMPT'
5455
There are agents {linked_agents} associated with you. You can ask them for help if you need it.
55-
Use the `ask_agent` tool and provide the agent key.
56+
Use the `{ask_agent_tool}` tool and provide the agent key.
5657
Always follow rules:
5758
- Don't make up the agent key. Use only the ones from the provided list.
5859
PROMPT,
60+
with: ['linked_agents', 'ask_agent_tool'],
5961
),
6062
)
6163
->withValues(
6264
values: [
65+
'ask_agent_tool' => $this->agentKey,
6366
'linked_agents' => \implode(
6467
PHP_EOL,
6568
\array_map(

src/Interceptors/UserPromptInjector.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public function generate(
2222
return $next(
2323
input: $input->withPrompt(
2424
$input->prompt->withAddedMessage(
25-
MessagePrompt::user('{user_prompt}')
26-
->withValues([
27-
'user_prompt' => (string) $input->userPrompt,
28-
]),
25+
MessagePrompt::user(
26+
prompt: '{user_prompt}',
27+
with: ['user_prompt'],
28+
)->withValues([
29+
'user_prompt' => (string) $input->userPrompt,
30+
]),
2931
),
3032
),
3133
);

0 commit comments

Comments
 (0)