Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AiPlaygroundCommand
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace Apie\AiInstructor;
3
4use Apie\Core\Entities\EntityInterface;
5use Apie\Core\ValueObjects\NonEmptyString;
6use Apie\TypeConverter\ReflectionTypeFactory;
7use Symfony\Component\Console\Attribute\AsCommand;
8use Symfony\Component\Console\Command\Command;
9use Symfony\Component\Console\Input\InputInterface;
10use Symfony\Component\Console\Input\InputOption;
11use Symfony\Component\Console\Output\OutputInterface;
12use Symfony\Component\Console\Question\Question;
13
14#[AsCommand(name: 'apie:ai-playground', description: 'Run a playground to call an LLM.')]
15class AiPlaygroundCommand extends Command
16{
17    public function __construct(
18        private readonly AiInstructor $aiInstructor
19    ) {
20        parent::__construct();
21    }
22
23    public function configure()
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}