Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.74% |
42 / 62 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AiPlaygroundCommand | |
67.74% |
42 / 62 |
|
66.67% |
2 / 3 |
8.64 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| configure | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
45.95% |
17 / 37 |
|
0.00% |
0 / 1 |
8.95 | |||
| 1 | <?php |
| 2 | namespace Apie\AiInstructor; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Apie\Core\ValueObjects\NonEmptyString; |
| 6 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 7 | use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | use Symfony\Component\Console\Command\Command; |
| 9 | use Symfony\Component\Console\Input\InputInterface; |
| 10 | use Symfony\Component\Console\Input\InputOption; |
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
| 12 | use Symfony\Component\Console\Question\Question; |
| 13 | |
| 14 | #[AsCommand(name: 'apie:ai-playground', description: 'Run a playground to call an LLM.')] |
| 15 | class AiPlaygroundCommand extends Command |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly AiInstructor $aiInstructor |
| 19 | ) { |
| 20 | parent::__construct(); |
| 21 | } |
| 22 | |
| 23 | public function configure(): void |
| 24 | { |
| 25 | $this->addOption( |
| 26 | 'system', |
| 27 | null, |
| 28 | InputOption::VALUE_OPTIONAL, |
| 29 | 'system prompt to use' |
| 30 | ); |
| 31 | $this->addOption( |
| 32 | 'user', |
| 33 | null, |
| 34 | InputOption::VALUE_OPTIONAL, |
| 35 | 'user prompt to use' |
| 36 | ); |
| 37 | $this->addOption( |
| 38 | 'model', |
| 39 | null, |
| 40 | InputOption::VALUE_OPTIONAL, |
| 41 | 'LLM model picked' |
| 42 | ); |
| 43 | $this->addOption( |
| 44 | 'type', |
| 45 | null, |
| 46 | InputOption::VALUE_OPTIONAL, |
| 47 | 'PHP typehint to instruct' |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 52 | { |
| 53 | $helper = $this->getHelper('question'); |
| 54 | $systemPrompt = (string) $input->getOption('system'); |
| 55 | if (!$systemPrompt) { |
| 56 | $question = new Question( |
| 57 | 'Please enter the system prompt', |
| 58 | 'You are an API that creates instances in a specific predefined format depending on the user input.' |
| 59 | ); |
| 60 | |
| 61 | $systemPrompt = $helper->ask($input, $output, $question); |
| 62 | } |
| 63 | $userPrompt = (string) $input->getOption('user'); |
| 64 | if (!$userPrompt) { |
| 65 | $question = new Question( |
| 66 | 'Please enter the user prompt', |
| 67 | 'Please give an example with a random value.' |
| 68 | ); |
| 69 | |
| 70 | $userPrompt = $helper->ask($input, $output, $question); |
| 71 | } |
| 72 | $model = (string) $input->getOption('model'); |
| 73 | if (!$model) { |
| 74 | $question = new Question( |
| 75 | 'Please enter a llm model name', |
| 76 | 'tinyllama' |
| 77 | ); |
| 78 | |
| 79 | $model = $helper->ask($input, $output, $question); |
| 80 | } |
| 81 | $type = (string) $input->getOption('type'); |
| 82 | if (!$type) { |
| 83 | $question = new Question( |
| 84 | 'Please enter the PHP typehint you want to instruct', |
| 85 | EntityInterface::class, |
| 86 | ); |
| 87 | |
| 88 | $type = $helper->ask($input, $output, $question); |
| 89 | } |
| 90 | $response = $this->aiInstructor->instruct( |
| 91 | ReflectionTypeFactory::createReflectionType($type), |
| 92 | NonEmptyString::fromNative($model), |
| 93 | $systemPrompt, |
| 94 | $userPrompt |
| 95 | ); |
| 96 | dump($response); |
| 97 | |
| 98 | return Command::SUCCESS; |
| 99 | } |
| 100 | } |