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