diff --git a/OpenAIGenerator.php b/OpenAIGenerator.php new file mode 100644 index 0000000000000000000000000000000000000000..3f3105a9fc636b08ccc8abc6c460021b1e0c437f --- /dev/null +++ b/OpenAIGenerator.php @@ -0,0 +1,97 @@ +<?php +class OPenAIGenerator +{ + private string $question; + private string $code; + private string $test; + + + public function __construct(string $question, bool $generateCode = true) + { + $this->question = $question; + if($generateCode) + { + $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; + } + +} \ No newline at end of file diff --git a/index.php b/index.php index 44d97b620e282c3c00a39e85c30593a19646c041..1f895f6706cef38a29820a9423b7be5a30c79d51 100644 --- a/index.php +++ b/index.php @@ -2,48 +2,45 @@ require('vendor/autoload.php'); -if(!empty($_GET['test'])) -{ +require_once('OpenAIGenerator.php'); + +echo <<<HTMLEOF + <head> + <title>ChatGPT Generator</title> + <style> + body, html { margin: 0px;} + form { margin: auto 0; display: flex; } + textarea { margin: 15px; padding: 5px; flex: 1 2; border: 0; background-color:rgb(157, 157, 157); color: white;} + input[type='submit'] { background: #28713a; color: white; border: 0; border-radius: 3px;} + input[type='submit']:hover { background: #168c35; transition-delay: 0,3s;} + </style> - $client = OpenAI::client('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'); - $result = $client->completions()->create([ - 'model' => 'text-davinci-003', - 'prompt' => 'PHP is', - ]); - var_dump($result); - echo $result['choices'][0]['text']; + </head> +HTMLEOF; +if(!empty($_POST['question'])) +{ + $gen = new OpenAIGenerator($_POST['question']); + echo $gen; } else { echo <<<EOF - - <form> - <input type="text" name="test" id="test" placeholder="Test"> + + <form method="POST"> + <textarea id="question" name="question" rows="5" cols="33" placeholder="put your code awnser here"></textarea> <input type="submit" value="Envoyer"> </form> + <div style="margin-left: 15px;"> + <h2>Example :</h2> + <p>Filter a array and exclude element if key is "chapeau"</p> + <p>Check if word is a palindromic word</p> + <p>return a number of occurence for word into a text</p> + </div> EOF; } -/** - * Response - * object(OpenAI\Responses\Completions\CreateResponse)#26 (6) { - * ["id"]=> string(34) "cmpl-6hJMVGw7Zr2wbR0LOaRGbHkSVlYg6" - * ["object"]=> string(15) "text_completion" - * ["created"]=> int(1675780551) - * ["model"]=> string(16) "text-davinci-003" - * ["choices"]=> array(1) { - * [0]=> object(OpenAI\Responses\Completions\CreateResponseChoice)#28 (4) { - * ["text"]=> string(62) " an open source language Yes, PHP is an open source, general-" - * ["index"]=> int(0) ["logprobs"]=> NULL ["finishReason"]=> string(6) "length" } } - * ["usage"]=> object(OpenAI\Responses\Completions\CreateResponseUsage)#27 (3) { - * ["promptTokens"]=> int(3) ["completionTokens"]=> int(16) - * ["totalTokens"]=> int(19) } } an open source language Yes, PHP is an open source, general- - * - * - */ - ?> \ No newline at end of file