|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LLM\Agents\AgentExecutor\Interceptor; |
| 6 | + |
| 7 | +use LLM\Agents\Agent\Execution; |
| 8 | +use LLM\Agents\AgentExecutor\ExecutionInput; |
| 9 | +use LLM\Agents\AgentExecutor\ExecutorInterceptorInterface; |
| 10 | +use LLM\Agents\AgentExecutor\InterceptorHandler; |
| 11 | +use LLM\Agents\LLM\Exception\FormatterException; |
| 12 | +use LLM\Agents\LLM\Output\EnumFormatter; |
| 13 | +use LLM\Agents\LLM\Output\FormatterInterface; |
| 14 | +use LLM\Agents\LLM\Output\JsonSchemaFormatter; |
| 15 | +use LLM\Agents\LLM\Prompt\PromptInterface; |
| 16 | +use LLM\Agents\LLM\Response\ChatResponse; |
| 17 | + |
| 18 | +/** |
| 19 | + * This interceptor is responsible for formatting the output of the agent based on the provided output formatter. |
| 20 | + */ |
| 21 | +final readonly class OutputFormatterInterceptor implements ExecutorInterceptorInterface |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private JsonSchemaFormatter $jsonSchemaFormatter, |
| 25 | + ) {} |
| 26 | + |
| 27 | + public function execute(ExecutionInput $input, InterceptorHandler $next): Execution |
| 28 | + { |
| 29 | + $outputFormatter = $input->options->get('output_formatter'); |
| 30 | + |
| 31 | + if ($outputFormatter === null) { |
| 32 | + return $next($input); |
| 33 | + } |
| 34 | + |
| 35 | + if (!$input->prompt instanceof PromptInterface) { |
| 36 | + throw new FormatterException('Prompt must implement PromptInterface'); |
| 37 | + } |
| 38 | + |
| 39 | + if (!$outputFormatter instanceof FormatterInterface) { |
| 40 | + $outputFormatter = $this->createFormatter($outputFormatter); |
| 41 | + } |
| 42 | + |
| 43 | + $input = $input->withPrompt( |
| 44 | + $input->prompt->withValues( |
| 45 | + ['output_format_instruction' => $outputFormatter->getInstruction()], |
| 46 | + ), |
| 47 | + ); |
| 48 | + |
| 49 | + return $this->formatResponse($next($input), $outputFormatter); |
| 50 | + } |
| 51 | + |
| 52 | + private function formatResponse(Execution $execution, FormatterInterface $outputFormatter): Execution |
| 53 | + { |
| 54 | + $result = $execution->result; |
| 55 | + |
| 56 | + if (!$result instanceof ChatResponse) { |
| 57 | + return $execution; |
| 58 | + } |
| 59 | + |
| 60 | + return new Execution( |
| 61 | + result: new ChatResponse( |
| 62 | + content: $outputFormatter->format($result->content), |
| 63 | + ), |
| 64 | + prompt: $execution->prompt, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param non-empty-string|class-string $schema |
| 70 | + */ |
| 71 | + private function createFormatter(string $schema): FormatterInterface |
| 72 | + { |
| 73 | + // If the schema is an existing class, check if it is an enum. |
| 74 | + if (\class_exists($schema)) { |
| 75 | + $refl = new \ReflectionClass($schema); |
| 76 | + if ($refl->isEnum()) { |
| 77 | + return new EnumFormatter($refl->getName()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $this->jsonSchemaFormatter->withJsonSchema($schema); |
| 82 | + } |
| 83 | +} |
0 commit comments