- Is the toolbox for internal or external use?
external use
- There is no official API for ChatGPT, but the Completions API is available
yes i know, that’s the API that I try to use
- Can you clarify the issue you have with the proposed extension?
yes, i don’t receive the answer from OpenAI
Here is the code of the extension PHP for WP
<?php
/*
* Plugin Name: Extension OpenAI
* Plugin URI: https://www.exemple.com
* Description: Extension qui permet d'utiliser OpenAI par le biais de son API
* Version: 1.0
* Author: John Doe
* Author URI: https://www.exemple.com
* License: GPL2
*/
/*
* Shortcode
*/
function openAI_shortcode(){
// Affichage du formulaire
$html = '';
// Champ de la clé API
$html .= '
Entrez votre clé API OpenAI
';
$html .= '';
// Champ du prompt à executer
$html .= '
Entrez le prompt à executer
';
$html .= '';
// Bouton GO
$html .= '';
// Affichage du résultat
if( isset($_POST['openAI_go']) ) {
$openAI_api_key = esc_attr($_POST['openAI_api_key']);
$openAI_prompt = esc_attr($_POST['openAI_prompt']);
// Appel API OpenAI
$result = openAI_call($openAI_api_key, $openAI_prompt);
// Affichage du résultat
$html .= '
Résultat du prompt :
';
$html .= '
' . $result . '
';
}
$html .= '';
return $html;
}
add_shortcode('openAI', 'openAI_shortcode');
/*
* Appel API OpenAI
*/
function openAI_call($openAI_api_key, $openAI_prompt){
// Requête
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.openai.com/v1/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 200,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"prompt": "' . $openAI_prompt . '","max_tokens": 50,"temperature": 0.9,"top_p": 1.0,"n": 1,"stream": false,"logprobs": null,"stop": [" "],"engine": "davinci","frequency_penalty": 0.0,"presence_penalty": 0.0}',
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $openAI_api_key
),
));
// Récupération du résultat
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response);
$result = $response->choices[0]->text;
return $result;
}