PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 4.3.8
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v4.3.8
8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 All 540 releases
chatbot / includes / openai / qcld-bot-openai.php

qcld-bot-openai.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 4.3.8, at includes/openai/qcld-bot-openai.php

473 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit; // Exit if accessed directly
3
4 if(!class_exists('qcld_wpopenai_addons')){
5
6
7 /**
8 * Main Class.
9 */
10 final class qcld_wpopenai_addons
11 {
12 private $id = 'Open AI';
13
14 /**
15 * WPBot Pro version.
16 *
17 * @var string
18 */
19 public $version = '1.0.6';
20
21 /**
22 * WPBot Pro helper.
23 *
24 * @var object
25 */
26 public $helper;
27
28 /**
29 * The single instance of the class.
30 *
31 * @var qcld_wb_Chatbot
32 * @since 1.0.0
33 */
34 protected static $_instance = null;
35
36 /**
37 * Main wpbot Instance.
38 *
39 * Ensures only one instance of wpbot is loaded or can be loaded.
40 *
41 * @return qcld_wb_Chatbot - Main instance.
42 * @since 1.0.0
43 * @static
44 */
45 public static function instance() {
46 if ( is_null( self::$_instance ) ) {
47 self::$_instance = new self();
48 }
49
50 return self::$_instance;
51 }
52
53 public $response_list;
54
55 /**
56 * Constructor
57 */
58 public function __construct()
59 {
60 $this->define_constants();
61 $this->includes();
62 add_action('wp_ajax_openai_settings_option', [$this, 'openai_settings_option_callback']);
63 add_action('wp_ajax_openai_file_upload', [$this, 'openai_file_upload_callback']);
64 add_action('wp_ajax_openai_response',[$this,'openai_response_callback']);
65 add_action('wp_ajax_openai_file_list',[$this,'openai_file_list_callback']);
66 add_action('wp_ajax_openai_finetune_list', [$this,'openai_finetune_list']);
67 add_action('wp_ajax_openai_file_delete',[$this,'openai_file_delete_callback']);
68 add_action('wp_ajax_nopriv_openai_response', [$this, 'openai_response_callback']);
69 add_action('wp_ajax_openai_ft_model_create', [$this, 'openai_ft_model_create']);
70 add_action('wp_ajax_openai_ft_model_delete', [$this, 'openai_ft_model_delete']);
71 if (is_admin() && !empty($_GET["page"]) && (($_GET["page"] == "openai-panel_dashboard") || ($_GET["page"] == "openai-panel_file") || ($_GET["page"] == "openai-panel_help"))) {
72 add_action('admin_enqueue_scripts', array($this, 'qcld_wb_chatbot_admin_scripts'));
73 }
74
75
76 }
77
78
79 /**
80 * Define wpbot Constants.
81 *
82 * @return void
83 * @since 1.0.0
84 */
85 public function define_constants() {
86 if( ! defined( 'QCLD_openai_addon_VERSION' ) ){
87 define('QCLD_openai_addon_VERSION', $this->version);
88 }
89 //define('QCLD_openai_addon_REQUIRED_wpCOMMERCE_VERSION', 2.2);
90
91 if( ! defined( 'QCLD_openai_addon_PLUGIN_DIR_PATH' ) ){
92 define('QCLD_openai_addon_PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));
93 }
94 if( ! defined( 'QCLD_openai_addon_PLUGIN_URL' ) ){
95 define('QCLD_openai_addon_PLUGIN_URL', plugin_dir_url(__FILE__));
96 }
97 if( ! defined( 'QCLD_openai_addon_IMG_URL' ) ){
98 define('QCLD_openai_addon_IMG_URL', QCLD_openai_addon_PLUGIN_URL . "images/");
99 }
100 if( ! defined( 'QCLD_openai_addon_IMG_ABSOLUTE_PATH' ) ){
101 define('QCLD_openai_addon_IMG_ABSOLUTE_PATH', plugin_dir_path(__FILE__) . "images");
102 }
103
104 }
105
106
107 public function qcld_wb_chatbot_admin_scripts(){
108 wp_register_style('qlcd-open-ai-bootstap', QCLD_openai_addon_PLUGIN_URL . 'css/openai-bootstrap.css', '', QCLD_openai_addon_VERSION, 'screen');
109 wp_enqueue_style('qlcd-open-ai-bootstap');
110 wp_register_style('qlcd-open-ai-admin-style', QCLD_openai_addon_PLUGIN_URL . 'css/openai-admin-style.css', '', QCLD_openai_addon_VERSION, 'screen');
111 wp_enqueue_style('qlcd-open-ai-admin-style');
112 wp_register_script('qlcd-openai_collapse', QCLD_openai_addon_PLUGIN_URL . 'js/collapse.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
113 wp_enqueue_script('qlcd-openai_collapse');
114 wp_register_script('qlcd-openai_settings', QCLD_openai_addon_PLUGIN_URL . 'js/openai_settings.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
115 wp_enqueue_script('qlcd-openai_settings');
116
117 wp_localize_script( 'qlcd-openai_settings', 'openai_ajax', array(
118 'url' => admin_url( 'admin-ajax.php' ),
119 ) );
120
121 }
122 /**
123 * Include all required files
124 *
125 * since 1.0.0
126 *
127 * @return void
128 */
129 public function includes() {
130 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/openai/qcld_wp_OpenAI.php" );
131 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/openai/OpenAi_WPBot_Menu.php" );
132
133 }
134 public function openai_file_delete_callback(){
135 $file_id = sanitize_text_field($_POST['file_id']);
136 $url = 'https://api.openai.com/v1/files/'. $file_id;
137 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
138 $ch = curl_init();
139 curl_setopt($ch, CURLOPT_URL, $url);
140 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
141 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
142 $headers = array(
143 $apt_key,
144 );
145 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
146 $result = curl_exec($ch);
147 if (curl_errno($ch)) {
148 echo 'Error:' . curl_error($ch);
149 }
150 curl_close($ch);
151 wp_send_json( json_decode($result));
152 wp_die();
153 }
154 public function openai_ft_model_create(){
155 $file_id = sanitize_text_field($_POST['file_id']);
156
157 $rel = $this->openai_finetune_create($file_id);
158 print_r(wp_send_json([$rel]));wp_die();
159 echo wp_send_json([$rel]);
160 wp_die();
161 }
162 public function openai_file_list_callback(){
163 $url = 'https://api.openai.com/v1/files';
164 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
165 $curl = curl_init();
166 curl_setopt($curl, CURLOPT_URL, $url);
167 $headers = array(
168 "Content-Type: application/json",
169 $apt_key,
170 );
171 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
172 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
173 $response = curl_exec($curl);
174 curl_close($curl);
175 wp_send_json( json_decode($response));
176 wp_die();
177 }
178 public function openai_ft_model_delete(){
179 $ft_id = sanitize_text_field($_POST['ft_id']);
180 $url = 'https://api.openai.com/v1/models/' . $ft_id;
181 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
182 $curl = curl_init();
183 $headers = array(
184 "Content-Type: multipart/form-data",
185 $apt_key,
186 );
187 curl_setopt($curl, CURLOPT_URL, $url);
188 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
189 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
190 curl_setopt($curl, CURLOPT_POST, true);
191 $res = json_decode(curl_exec ($curl));
192 curl_close ($curl);
193 echo wp_send_json([$rel]);
194 wp_die();
195
196 }
197 public function openai_file_upload_callback(){
198 $uploadedfile = $_FILES['file'];
199 $url = 'https://api.openai.com/v1/files';
200 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
201 $curl = curl_init($url);
202 curl_setopt($curl, CURLOPT_URL, $url);
203 curl_setopt($curl, CURLOPT_POST, true);
204 $headers = array(
205 "Content-Type: multipart/form-data",
206 $apt_key,
207 );
208 if (function_exists('curl_file_create')) {
209 $tmp_file = curl_file_create($uploadedfile['tmp_name'], 'jsonl', $uploadedfile['name']);
210 } else {
211 $tmp_file = open($uploadedfile['tmp_name']);
212 }
213
214 $data = array('file'=> $tmp_file,'purpose'=> 'fine-tune');
215 $init = curl_init();
216 //function parameteres
217 curl_setopt($init, CURLOPT_URL,$url);
218 curl_setopt($init, CURLOPT_HTTPHEADER, $headers);
219 curl_setopt($init, CURLOPT_POSTFIELDS, $data);
220 curl_setopt($init, CURLOPT_RETURNTRANSFER, true);
221 $res = json_decode(curl_exec ($init));
222
223 curl_close ($init);
224 if(!empty($res->error)){
225 $response['status'] = 'error';
226 $response['message'] = $res->error->message;
227 }
228
229 if(!empty($res->status)){
230 $response['status'] = 'success';
231 $response['message'] = 'Successfully Created file' . $res->id ;
232
233 }
234 echo wp_send_json([$response]);
235 wp_die();
236 }
237
238
239 public function openai_finetune_create($file_id ){
240 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
241 $headers = array(
242 "Content-Type: application/json",
243 $apt_key,
244 );
245 $curl = curl_init();
246 $qcld_openai_suffix = get_option('qcld_openai_suffix');
247 $openai_engines =get_option('openai_engines');
248 $base_engine = explode('-',$openai_engines);
249 $data = json_encode(array('training_file'=>$file_id,'model' => $base_engine[1], 'suffix' => $qcld_openai_suffix ));
250 $url = "https://api.openai.com/v1/fine-tunes";
251 curl_setopt($curl, CURLOPT_URL, $url);
252 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
253 curl_setopt($curl, CURLOPT_POST, true);
254 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
255 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
256 $result = json_decode(curl_exec($curl));
257
258 curl_close($curl);
259 return $result;
260 }
261 public function openai_finetune_list(){
262
263 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
264 $headers = array(
265 "Content-Type: application/json",
266 $apt_key,
267 );
268 $curl_ft = curl_init();
269 //$data = json_encode(array('training_file'=>$file_id));
270
271 $url = "https://api.openai.com/v1/fine-tunes";
272 curl_setopt($curl_ft, CURLOPT_URL, $url);
273 curl_setopt($curl_ft, CURLOPT_HTTPHEADER, $headers);
274 curl_setopt($curl_ft, CURLOPT_RETURNTRANSFER, true);
275 $result = json_decode(curl_exec($curl_ft));
276 $ft_arry = [];
277 foreach($result->data as $value ){
278 if(($value->training_files[0]->status != 'deleted') && ($value->result_files[0]->status != 'deleted') ){
279 $ft_arry[] = [$value->id,$value->fine_tuned_model,$value->status,$value->training_files[0]->filename,$value->training_files[0]->id];
280 }
281 }
282 curl_close($curl_ft);
283 wp_send_json( $ft_arry);
284 wp_die();
285
286
287 }
288 public function openai_retrive_fine_tune(){
289 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
290 $headers = array(
291 "Content-Type: application/json",
292 $apt_key,
293 );
294 $curl = curl_init();
295 $data = json_encode(array('training_file'=>$file_id));
296
297 $url = "https://api.openai.com/v1/fine-tunes/ft-72lUhFiarH2WMFDZacdoySmU";
298 curl_setopt($curl, CURLOPT_URL, $url);
299 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
300 $result = json_decode(curl_exec($curl));
301 return $result;
302 curl_close($curl);
303 }
304 public function response_form_file($keyword){
305 $max_tokens = (int)get_option( 'openai_max_tokens');
306 $temp = (float)get_option( 'openai_temperature');
307 $frequency_penalty = (float)get_option( 'frequency_penalty');
308 $presence_penalty = (float)get_option( 'presence_penalty');
309 $prompts = $this->get_prompt($keyword);
310 $request_body = [
311 "prompt" => "\n\n" .$keyword ."\n\n",
312 "model" => get_option( 'qcld_openai_custom_model'),
313 "max_tokens" => $max_tokens,
314 "temperature" => $temp,
315 "top_p" => 1,
316 "presence_penalty" => $presence_penalty,
317 "frequency_penalty"=> $frequency_penalty,
318 "best_of"=> 1,
319 ];
320 $postFields = json_encode($request_body);
321 $OpenAI = new qcld_wp_OpenAI();
322 $result = $OpenAI->get_response($postFields);
323
324 return $result;
325 }
326 public function get_prompt($keyword){
327 $qcld_openai_prompt = get_option('qcld_openai_prompt',true);
328 switch ($qcld_openai_prompt) {
329 case "q_and_a":
330 return "Q:".$keyword."\nA:";
331 case "chat":
332 return "\nHuman:".$keyword."\nAI:";
333 case "friend_chat":
334 return "You:".$keyword."\nFriend:";
335 case "grammar_correction":
336 return "Correct this to standard English:\n\n ".$keyword." " ;
337 case "marv_sarcastic_chatbot" :
338 return "Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: ".$keyword." \nMarv:";
339 case "micro_horror":
340 return "\nTopic: ".$keyword."\n\nTwo-Sentence Horror Story:";
341 case "any_command":
342 return $keyword;
343 case "write_poem":
344 return "write a poem about". $keyword;
345 case "custom_prompt":
346 return get_option('qcld_openai_prompt_custom') ."\n" . $keyword;
347 default:
348 return "Q:".$keyword."\nA:";
349 }
350 }
351 public function openai_response_callback() {
352 $response['status'] = 'success';
353 $response['message'] ='A preset message';
354 $OpenAI = new qcld_wp_OpenAI();
355 $keyword = sanitize_text_field($_POST['keyword']);
356 $response_files = $this->response_form_file($keyword);
357
358 $response_file = ($response_files);
359 if(empty($response_file->choices[0]->text)){
360
361 $prompts = $this->get_prompt($keyword);
362 $prompt =$prompts;
363 $res = $OpenAI->complete(
364 $prompt
365 );
366 $mess = json_decode($res);
367 $response['message'] = $mess->choices[0]->text;
368 }else{
369 $response['message'] = $response_file->choices[0]->text;
370 }
371 echo json_encode($response);
372 wp_die();
373 }
374 public function openai_settings_option_callback() {
375 $api_key = sanitize_text_field($_POST['api_key']);
376 $openai_engines = sanitize_text_field($_POST['openai_engines']);
377 $qcld_openai_prompt = sanitize_text_field($_POST['qcld_openai_prompt']);
378 $max_tokens = sanitize_text_field($_POST['max_tokens']);
379 $qcld_openai_suffix = sanitize_text_field($_POST['qcld_openai_suffix']);
380 $qcld_openai_custom_model = sanitize_text_field($_POST['qcld_openai_custom_model']);
381 $frequency_penalty = sanitize_text_field($_POST['frequency_penalty']);
382 $presence_penalty = sanitize_text_field($_POST['presence_penalty']);
383 $temperature = sanitize_text_field($_POST['temperature']);
384 $ai_enabled = sanitize_text_field($_POST['ai_enabled']);
385 $ai_only_mode = sanitize_text_field($_POST['ai_only_mode']);
386 $file_id = sanitize_text_field($_POST['file_id']);
387 $qcld_openai_prompt_custom = sanitize_text_field($_POST['qcld_openai_prompt_custom']);
388 if($api_key != ''){
389 update_option( 'open_ai_api_key', $api_key );
390 }
391 if($openai_engines != ''){
392 update_option( 'openai_engines', $openai_engines );
393 }
394 if($max_tokens != ''){
395 update_option( 'openai_max_tokens', $max_tokens );
396 }
397 if($qcld_openai_suffix != ''){
398 update_option('qcld_openai_suffix', $qcld_openai_suffix);
399 }
400 if($frequency_penalty != ''){
401 update_option( 'frequency_penalty', $frequency_penalty );
402 }
403 if($presence_penalty != ''){
404 update_option( 'presence_penalty', $presence_penalty );
405 }
406 if($temperature != ''){
407 update_option( 'openai_temperature', $temperature );
408 }
409 if($qcld_openai_prompt_custom != ''){
410 update_option('qcld_openai_prompt_custom', $qcld_openai_prompt_custom );
411 }
412 if($qcld_openai_custom_model != ''){
413 update_option('qcld_openai_custom_model',$qcld_openai_custom_model);
414 }
415 if($max_tokens != ''){
416 update_option('ai_enabled',$ai_enabled);
417 }
418 if($file_id != ''){
419 update_option('file_id',$file_id);
420 }
421 if(($ai_only_mode != '') && ($ai_only_mode == 0)){
422 update_option('ai_only_mode', $ai_only_mode);
423 update_option('enable_wp_chatbot_disable_allicon', 0);
424 update_option('qcld_disable_start_menu', 0);
425 update_option('show_menu_after_greetings', 0);
426 update_option('skip_wp_greetings', 0);
427 update_option('disable_wp_chatbot_site_search',0);
428 update_option('disable_wp_chatbot_call_gen',0);
429 update_option('disable_wp_chatbot_feedback',0);
430 update_option('disable_wp_chatbot_faq',0);
431 update_option('disable_email_subscription',0);
432 update_option('disable_str_categories',0);
433 update_option('disable_good_bye',0);
434
435 }else if(($ai_only_mode != '') && ($ai_only_mode == 1)){
436 update_option('ai_only_mode', $ai_only_mode);
437 update_option('enable_wp_chatbot_disable_allicon', 1);
438 update_option('qcld_disable_start_menu', 1);
439 update_option('show_menu_after_greetings', 1);
440 update_option('skip_wp_greetings', 1);
441 update_option('disable_wp_chatbot_site_search',1);
442 update_option('disable_wp_chatbot_call_gen',1);
443 update_option('disable_wp_chatbot_feedback',1);
444 update_option('disable_wp_chatbot_faq',1);
445 update_option('disable_email_subscription',1);
446 update_option('disable_str_categories',1);
447 update_option('disable_good_bye',1);
448 }
449
450 if($qcld_openai_prompt != ''){
451 update_option('qcld_openai_prompt', $qcld_openai_prompt);
452 }
453 $tem = get_option( 'openai_temperature', $temperature );
454
455 echo json_encode($ai_enabled);wp_die();
456 }
457 }
458
459 /**
460 * @return qcld_wpopenai_addon
461 */
462 if(!function_exists('qcld_wpopenai_addons')){
463 function qcld_openais() {
464 $qcld_wpopenai_addon = new qcld_wpopenai_addons();
465 return $qcld_wpopenai_addon->instance();
466
467 }
468 }
469
470 //fire off the plugin
471 qcld_openais();
472
473 }