| 1 |
<?php |
| 2 |
include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 3 |
if(!class_exists('qcld_wp_OpenAI')){ |
| 4 |
class qcld_wp_OpenAI{ |
| 5 |
public $baseURL = "https://api.openai.com/v1/"; |
| 6 |
private $defaultEngine = "davinci"; |
| 7 |
public function setDefaultEngine($defaultEngine){ |
| 8 |
$this->defaultEngine = $defaultEngine; |
| 9 |
} |
| 10 |
|
| 11 |
public function get_response($postFields){ |
| 12 |
$url = "https://api.openai.com/v1/completions"; |
| 13 |
$apt_key = "Authorization: Bearer ". get_option('open_ai_api_key'); |
| 14 |
|
| 15 |
$curl = curl_init($url); |
| 16 |
curl_setopt($curl, CURLOPT_URL, $url); |
| 17 |
curl_setopt($curl, CURLOPT_POST, true); |
| 18 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 19 |
|
| 20 |
$headers = array( |
| 21 |
"Content-Type: application/json", |
| 22 |
$apt_key , |
| 23 |
); |
| 24 |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
| 25 |
|
| 26 |
|
| 27 |
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); |
| 28 |
|
| 29 |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
| 30 |
$response = curl_exec($curl); |
| 31 |
curl_close($curl); |
| 32 |
|
| 33 |
|
| 34 |
return json_decode($response); |
| 35 |
} |
| 36 |
public function complete($prompt) { |
| 37 |
$max_tokens = (int)get_option( 'openai_max_tokens'); |
| 38 |
$temp = (float)get_option( 'openai_temperature'); |
| 39 |
$frequency_penalty = (float)get_option( 'frequency_penalty'); |
| 40 |
$presence_penalty = (float)get_option( 'presence_penalty'); |
| 41 |
$request_body = [ |
| 42 |
"prompt" => $prompt, |
| 43 |
"model" => get_option( 'openai_engines'), |
| 44 |
"max_tokens" => $max_tokens, |
| 45 |
"temperature" => $temp, |
| 46 |
"top_p" => 1, |
| 47 |
"presence_penalty" => $presence_penalty, |
| 48 |
"frequency_penalty"=> $frequency_penalty, |
| 49 |
"best_of"=> 1, |
| 50 |
"stream" => false, |
| 51 |
]; |
| 52 |
$postFields = json_encode($request_body); |
| 53 |
$result = self::get_response($postFields); |
| 54 |
return json_encode($result); |
| 55 |
} |
| 56 |
public function gptcomplete($keyword){ |
| 57 |
$ch = curl_init(); |
| 58 |
$url = 'https://api.openai.com/v1/responses'; |
| 59 |
$api_key = get_option('open_ai_api_key'); |
| 60 |
|
| 61 |
$post_fields = array( |
| 62 |
"model" => get_option( 'openai_engines'), |
| 63 |
"input" => $keyword, |
| 64 |
// "max_tokens" => $max_tokens, |
| 65 |
"temperature" => 0 |
| 66 |
); |
| 67 |
$header = [ |
| 68 |
'Content-Type: application/json', |
| 69 |
'Authorization: Bearer ' . $api_key |
| 70 |
]; |
| 71 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 72 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 73 |
curl_setopt($ch, CURLOPT_POST, 1); |
| 74 |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields)); |
| 75 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
| 76 |
$result = curl_exec($ch); |
| 77 |
if (curl_errno($ch)) { |
| 78 |
// phpcs:ignore |
| 79 |
echo 'Error: ' . curl_error($ch); |
| 80 |
} |
| 81 |
curl_close($ch); |
| 82 |
// $response = json_decode($result); |
| 83 |
return $result; |
| 84 |
} |
| 85 |
} |
| 86 |
} |