Skip to content
Snippets Groups Projects
Select Git revision
  • 3dbdebc043e375824e36c80e57e995846c9e34cc
  • main default protected
  • Iteration_1
3 results

OpenAIGenerator.php

Blame
  • user avatar
    Yghore authored
    53123413
    History
    OpenAIGenerator.php 2.32 KiB
    <?php
    class OPenAIGenerator
    {
        private string $question;
        private string $code;
        private string $test;
    
    
        public function __construct(string $question)
        {
            $this->question = $question;
    
        }
    
        public function generate()
        {
                
            $this->code = $this->genCode();
            $this->test = $this->genTest();
        }
    
    
        public function genCode(): string
        {
            $client = OpenAI::client('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');
            $result = $client->completions()->create([
                'model' => 'text-davinci-003',
                'prompt' => "Create code for". $this->question . " in the java development language",
                'max_tokens' => 10,
            ]);
            return $result['choices'][0]['text'];
    
    
          
        }
    
        public function genTest(): string
        {
    
            $client = OpenAI::client('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');
            $result = $client->completions()->create([
                'model' => 'text-davinci-003',
                'prompt' => "Create test for this code :" . $this->code . ", without import",
            ]);
    
            return $result['choices'][0]['text'];
            
        }
    
    
        public function getQuestion(): string
        {
            return $this->question;
        }
    
        public function setQuestion(string $question): void
        {
            $this->question = $question;
        }
    
        public function getCode(): string
        {
            return $this->code;
        }
    
        public function setCode(string $code): void
        {
            $this->code = $code;
        }
    
        public function getTest(): string
        {
            return $this->test;
        }
    
        public function setTest(string $test): void
        {
            $this->test = $test;
        }
    
        public function __toString()
        {
            return <<<EOF
                <div class="response">
                    <h1>Request :</h2>
                    <p>{$this->question}</p>
                    <h2>Code : </h2>
                    <div class="code">
                    <textarea id="code" name="code" rows="5" cols="33">
                        {$this->code}
                    </textarea>
                    </div>
                    <h2>Test for code :</h2>
                    <div class="test">
                    <textarea id="test" name="test" rows="5" cols="33">
                        {$this->test}
                    </textarea>
                    </div>
                </div>
    
            EOF;
        }
    
    }