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

Test ChatGPT

parent 89f5aff0
No related branches found
No related tags found
No related merge requests found
......@@ -14,5 +14,7 @@
}
],
"minimum-stability": "stable",
"require": {}
"require": {
"openai-php/client": "^0.3.0"
}
}
<?php
require('vendor/autoload.php');
$html;
if(empty($_POST['test']))
if(!empty($_GET['test']))
{
$html = <<<EOF
<form>
<input type="text" placeholder="Test à demander"/>
<input type="submit" value="Demander un test" />
</form>
EOF;
$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'];
}
else
{
echo <<<EOF
<form>
<input type="text" name="test" id="test" placeholder="Test">
<input type="submit" value="Envoyer">
</form>
EOF;
}
echo($html);
/**
* 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
  • Developer

    Très bonne idée d'utiliser openai-php/client, c'est toujours mieux de ne pas réinventer ce qui existe. Vous pouvez remarquer également les très nombreux tests du module : https://github.com/openai-php/client/tree/main/tests Un bel exemple de qualité logicielles. Le framework de test est PEST(https://pestphp.com) et le correcteur de code laravel/pint(https://laravel.com/docs/9.x/pint), basé sur CS-Fixer que l'on a vu rapidement.

    Edited by PIERRON Laurent
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment