Skip to content
Snippets Groups Projects
Commit f8600b81 authored by Yghore's avatar Yghore
Browse files

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
parent b109c4eb
No related branches found
No related tags found
No related merge requests found
Pipeline #10908 passed
<?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
...@@ -2,48 +2,45 @@ ...@@ -2,48 +2,45 @@
require('vendor/autoload.php'); 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); </head>
echo $result['choices'][0]['text']; HTMLEOF;
if(!empty($_POST['question']))
{
$gen = new OpenAIGenerator($_POST['question']);
echo $gen;
} }
else else
{ {
echo <<<EOF echo <<<EOF
<form> <form method="POST">
<input type="text" name="test" id="test" placeholder="Test"> <textarea id="question" name="question" rows="5" cols="33" placeholder="put your code awnser here"></textarea>
<input type="submit" value="Envoyer"> <input type="submit" value="Envoyer">
</form> </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; 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment