From f8600b814e0b562636b17bf03e64659593ed2c02 Mon Sep 17 00:00:00 2001 From: Yghore <Yghore@gmail.com> Date: Mon, 13 Feb 2023 21:05:40 +0100 Subject: [PATCH] Refractor of code (Use class) and other fonc OpenAIGenerator : - Add a parameter for used a existing code or note (default, generate code) - Add ToString for return of test and code Index : Include OpenAIGenerator and use this class. Style TO DOO : Class Diag UML --- OpenAIGenerator.php | 97 +++++++++++++++++++++++++++++++++++++++++++++ index.php | 57 +++++++++++++------------- 2 files changed, 124 insertions(+), 30 deletions(-) create mode 100644 OpenAIGenerator.php diff --git a/OpenAIGenerator.php b/OpenAIGenerator.php new file mode 100644 index 0000000..3f3105a --- /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 44d97b6..1f895f6 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 -- GitLab