Skip to content

Commit

Permalink
Merge pull request #3 from llm-agents-php/feature/improve-prompt-inst…
Browse files Browse the repository at this point in the history
…ructions

feat: improve prompt instructions
  • Loading branch information
butschster authored Sep 15, 2024
2 parents 8b040a0 + cab872e commit 86ecea4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
26 changes: 18 additions & 8 deletions src/Interceptors/InstructionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
),
],
),
),
Expand Down
5 changes: 4 additions & 1 deletion src/Interceptors/LinkedAgentsInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public function __construct(
private AgentRepositoryInterface $agents,
private SchemaMapperInterface $schemaMapper,
private string $agentKey = 'ask_agent',
) {}

public function generate(
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions src/Interceptors/UserPromptInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
),
),
);
Expand Down

0 comments on commit 86ecea4

Please sign in to comment.