PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 5.4.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v5.4.2
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 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 All 533 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 5.4.2, at includes/openai/qcld-bot-openai.php

717 lines 33.7 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_response',[$this,'openai_response_callback']);
64 // add_action('wp_ajax_openai_file_list',[$this,'openai_file_list_callback']);
65 // add_action('wp_ajax_openai_file_upload',[$this,'openai_file_upload_callback']);
66 // add_action('wp_ajax_openai_file_delete',[$this,'openai_file_delete_callback']);
67 add_action('wp_ajax_nopriv_openai_response', [$this, 'openai_response_callback']);
68 // add_action('wp_ajax_qcld_openai_file_dowload',[$this,'qcld_openai_file_dowload']);
69
70 if (is_admin() && !empty($_GET["page"]) && (($_GET["page"] == "openai-panel_dashboard") || ($_GET["page"] == "openai-panel_file") || ($_GET["page"] == "openai-panel_help"))) {
71 add_action('admin_enqueue_scripts', array($this, 'qcld_wb_chatbot_admin_scripts'));
72 }
73
74
75 }
76
77
78 /**
79 * Define wpbot Constants.
80 *
81 * @return void
82 * @since 1.0.0
83 */
84 public function define_constants() {
85 if( ! defined( 'QCLD_openai_addon_VERSION' ) ){
86 define('QCLD_openai_addon_VERSION', $this->version);
87 }
88 //define('QCLD_openai_addon_REQUIRED_wpCOMMERCE_VERSION', 2.2);
89
90 if( ! defined( 'QCLD_openai_addon_PLUGIN_DIR_PATH' ) ){
91 define('QCLD_openai_addon_PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));
92 }
93 if( ! defined( 'QCLD_openai_addon_PLUGIN_URL' ) ){
94 define('QCLD_openai_addon_PLUGIN_URL', plugin_dir_url(__FILE__));
95 }
96 if( ! defined( 'QCLD_openai_addon_IMG_URL' ) ){
97 define('QCLD_openai_addon_IMG_URL', QCLD_openai_addon_PLUGIN_URL . "images/");
98 }
99 if( ! defined( 'QCLD_openai_addon_IMG_ABSOLUTE_PATH' ) ){
100 define('QCLD_openai_addon_IMG_ABSOLUTE_PATH', plugin_dir_path(__FILE__) . "images");
101 }
102
103 }
104
105
106 public function qcld_wb_chatbot_admin_scripts(){
107 // wp_register_style('qlcd-open-ai-bootstap', QCLD_openai_addon_PLUGIN_URL . 'css/openai-bootstrap.css', '', QCLD_openai_addon_VERSION, 'screen');
108 // wp_enqueue_style('qlcd-open-ai-bootstap');
109 // wp_register_style('qlcd-open-ai-admin-style', QCLD_openai_addon_PLUGIN_URL . 'css/openai-admin-style.css', '', QCLD_openai_addon_VERSION, 'screen');
110 // wp_enqueue_style('qlcd-open-ai-admin-style');
111 // wp_register_script('qlcd-openai_collapse', QCLD_openai_addon_PLUGIN_URL . 'js/collapse.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
112 // wp_enqueue_script('qlcd-openai_collapse');
113 // wp_register_script('qlcd-openai_settings', QCLD_openai_addon_PLUGIN_URL . 'js/openai_settings.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
114 // wp_enqueue_script('qlcd-openai_settings');
115
116 // wp_localize_script( 'qlcd-openai_settings', 'openai_ajax', array(
117 // 'url' => admin_url( 'admin-ajax.php' ),
118 // ) );
119
120 }
121 /**
122 * Include all required files
123 *
124 * since 1.0.0
125 *
126 * @return void
127 */
128 public function includes() {
129 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/openai/qcld_wp_OpenAI.php" );
130 require_once( QCLD_wpCHATBOT_PLUGIN_DIR_PATH . "includes/openai/OpenAi_WPBot_Menu.php" );
131
132 }
133 // public function openai_file_delete_callback(){
134 // $file_id = sanitize_text_field($_POST['file_id']);
135 // $url = 'https://api.openai.com/v1/files/'. $file_id;
136 // $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
137 // $ch = curl_init();
138 // curl_setopt($ch, CURLOPT_URL, $url);
139 // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
140 // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
141 // $headers = array(
142 // $apt_key,
143 // );
144 // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
145 // $result = curl_exec($ch);
146 // if (curl_errno($ch)) {
147 // echo 'Error:' . curl_error($ch);
148 // }
149 // curl_close($ch);
150 // wp_send_json( json_decode($result));
151 // wp_die();
152 // }
153
154 public function buildFormBody( $fields, $boundary )
155 {
156 $body = '';
157 foreach ( $fields as $name => $value ) {
158 if ( $name == 'data' ) {
159 continue;
160 }
161 $body .= "--$boundary\r\n";
162 $body .= "Content-Disposition: form-data; name=\"$name\"";
163 if ( $name == 'file' ) {
164 $body .= "; filename=\"{$value}\"\r\n";
165 $body .= "Content-Type: application/json\r\n\r\n";
166 $body .= $fields['data'] . "\r\n";
167 }else {
168 $body .= "\r\n\r\n$value\r\n";
169 }
170 }
171 $body .= "--$boundary--\r\n";
172 return $body;
173 }
174
175 // public function openai_file_list_callback(){
176 // $url = 'https://api.openai.com/v1/files';
177 // $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
178 // $curl = curl_init();
179 // curl_setopt($curl, CURLOPT_URL, $url);
180 // $headers = array(
181 // "Content-Type: application/json",
182 // $apt_key,
183 // );
184 // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
185 // curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
186 // $response = curl_exec($curl);
187 // curl_close($curl);
188 // wp_send_json( json_decode($response));
189 // wp_die();
190 // }
191 public function qcld_sanitize_text_or_array_field($array_or_string) {
192 if( is_string($array_or_string) ){
193 $array_or_string = sanitize_text_field($array_or_string);
194 }elseif( is_array($array_or_string) ){
195 foreach ( $array_or_string as $key => &$value ) {
196 if ( is_array( $value ) ) {
197 $value = $this->sanitize_text_or_array_field($value);
198 }
199 else {
200 $value = sanitize_text_field( $value );
201 }
202 }
203 }
204
205 return $array_or_string;
206 }
207
208 // public function openai_file_upload_callback(){
209 // $uploadedfile = $_FILES['file'];
210 // $url = 'https://api.openai.com/v1/files';
211 // $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
212 // $curl = curl_init($url);
213 // curl_setopt($curl, CURLOPT_URL, $url);
214 // curl_setopt($curl, CURLOPT_POST, true);
215 // $headers = array(
216 // "Content-Type: multipart/form-data",
217 // $apt_key,
218 // );
219 // if (function_exists('curl_file_create')) {
220 // $tmp_file = curl_file_create($uploadedfile['tmp_name'], 'jsonl', $uploadedfile['name']);
221 // } else {
222 // $tmp_file = open($uploadedfile['tmp_name']);
223 // }
224 // $data = array('file'=> $tmp_file,'purpose'=> 'fine-tune');
225 // $init = curl_init();
226 // //function parameteres
227 // curl_setopt($init, CURLOPT_URL,$url);
228 // curl_setopt($init, CURLOPT_HTTPHEADER, $headers);
229 // curl_setopt($init, CURLOPT_POSTFIELDS, $data);
230 // curl_setopt($init, CURLOPT_RETURNTRANSFER, true);
231 // $res = json_decode(curl_exec ($init));
232
233 // curl_close ($init);
234 // if(!empty($res->error)){
235 // $response['status'] = 'error';
236 // $response['message'] = $res->error->message;
237 // }
238
239 // if(!empty($res->status)){
240 // $response['status'] = 'success';
241 // $response['message'] = 'Successfully Created file' . $res->id ;
242
243 // }
244 // echo wp_send_json([$response]);
245 // wp_die();
246 // }
247 public function openai_finetune_create($file_id,$ft_suffix,$ft_engines){
248 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
249 $headers = array(
250 "Content-Type: application/json",
251 $apt_key,
252 );
253 $curl = curl_init();
254 $qcld_openai_suffix = isset($ft_suffix) ? $ft_suffix : get_option('qcld_openai_suffix');
255 $openai_engines = isset($ft_engines) ? $ft_engines : get_option('openai_engines');
256 $base_engine = explode('-',$openai_engines);
257 if( $base_engine[0] == 'gpt'){
258 $data = json_encode(array('training_file'=>$file_id,'model' => $openai_engines, 'suffix' => $qcld_openai_suffix ));
259 $url = "https://api.openai.com/v1/fine_tuning/jobs";
260 curl_setopt($curl, CURLOPT_URL, $url);
261 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
262 curl_setopt($curl, CURLOPT_POST, true);
263 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
264 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
265 $result = json_decode(curl_exec($curl));
266 curl_close($curl);
267 }else{
268
269 $data = json_encode(array('training_file'=>$file_id,'model' => $base_engine[1], 'suffix' => $qcld_openai_suffix ));
270 $url = "https://api.openai.com/v1/fine-tunes";
271 curl_setopt($curl, CURLOPT_URL, $url);
272 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
273 curl_setopt($curl, CURLOPT_POST, true);
274 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
275 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
276 $result = json_decode(curl_exec($curl));
277 curl_close($curl);
278 }
279 return $result;
280 }
281
282 public function openai_retrive_fine_tune($keyword){
283
284 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
285 $headers = array(
286 "Content-Type: application/json",
287 $apt_key,
288 );
289 $curl = curl_init();
290 $max_tokens = (int)get_option( 'openai_max_tokens');
291 $temp = (float)get_option( 'openai_temperature');
292 $frequency_penalty = (float)get_option( 'frequency_penalty');
293 $presence_penalty = (float)get_option( 'presence_penalty');
294 $engines = explode('-',get_option( 'openai_engines'));
295 $custom_model = get_option( 'qcld_openai_custom_model');
296 $custom_model = (explode(":",$custom_model));
297 $prompts = $this->get_prompt($keyword);
298
299 if($custom_model[1] != 'gpt-3.5-turbo-0613'){
300 $data = json_encode(array(
301 'prompt'=> $prompts,
302 'model'=> get_option( 'qcld_openai_custom_model'),
303 "max_tokens" => $max_tokens,
304 "temperature" => $temp,
305 "top_p" => 1,
306 "presence_penalty" => $frequency_penalty,
307 "frequency_penalty"=> $presence_penalty,
308 "best_of"=> 1,
309 "stop"=> ["\n###\n","###"]
310 ));
311 $url = "https://api.openai.com/v1/completions";
312
313 $ch = curl_init();
314
315 curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
316 // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
317 // curl_setopt($ch, CURLOPT_POST, 1);
318 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
319 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
320 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
321 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
322
323 $result = (curl_exec($ch));
324 $result = str_replace("#","",$result );
325
326 return $result;
327 if (curl_errno($ch)) {
328 // phpcs:ignore
329 echo 'Error:' . curl_error($ch);
330 }
331 curl_close($ch);
332 }else{
333 $data = json_encode(array(
334 'model'=> get_option( 'qcld_openai_custom_model'),
335 "messages"=> [
336
337 [
338 "role"=> "user",
339 "content"=>$keyword
340 ]
341
342 ],
343
344 ));
345 $ch = curl_init();
346
347 curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
348 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
349 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
350 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
351 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
352
353 $results = (curl_exec($ch));
354 $results = str_replace("#","",$result );
355 $result = (json_decode($results)->choices[0]->message->content);
356 return $result;
357
358
359 }
360
361 }
362 public function response_form_file($keyword){
363 $max_tokens = (int)get_option( 'openai_max_tokens');
364 $temp = (float)get_option( 'openai_temperature');
365 $frequency_penalty = (float)get_option( 'frequency_penalty');
366 $presence_penalty = (float)get_option( 'presence_penalty');
367 $engines = explode('-',get_option( 'openai_engines'));
368 if($engines[0] != 'gpt'){
369 // $prompts = $this->get_prompt($keyword);
370 }
371
372 $request_body = [
373 "prompt" => $keyword,
374 "model" => get_option( 'qcld_openai_custom_model'),
375 "max_tokens" => $max_tokens,
376 "temperature" => 0,
377 "top_p" => 1,
378 "stop" => [],
379 "presence_penalty" => 0,
380 "frequency_penalty"=> 0,
381 "best_of"=> 1,
382 ];
383 $postFields = json_encode($request_body);
384 $OpenAI = new qcld_wp_OpenAI();
385 $result = $OpenAI->get_response($postFields);
386
387 return $result;
388 }
389 public function get_prompt($keyword){
390 $openai_include_keyword = get_option( 'openai_include_keyword');
391 $openai_exclude_keyword = get_option( 'openai_exclude_keyword');
392 $qcld_openai_prompt = get_option('qcld_openai_prompt',true);
393
394 }
395 public function include_exclude_prompt($keyword){
396 $openai_include_keyword = strtolower(get_option('openai_include_keyword'));
397 $openai_exclude_keyword = strtolower(get_option('openai_exclude_keyword'));
398
399 if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') == '')){
400 $prompts = 'If the query is not relevant to one of the keywords: '.$openai_include_keyword .' then only say DUH. Provide a response only if the following query is relevant to one of the keywords: '.$openai_include_keyword .' The actual query is as follows: '. $keyword;
401 return $prompts;
402 }else if((get_option('openai_include_keyword') == '') || (get_option('openai_exclude_keyword') != '')){
403
404 $prompts = 'If the query is relevant to one of the keywords: ' .$openai_exclude_keyword . ', then do not respond and only say "DUH." The actual query is as follows: '. $keyword. '?/n';
405 return $prompts;
406 }else if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')){
407 $prompts = 'If the query is not relevant to one of the keywords: '.$openai_include_keyword .' then only say "DUH." Provide a response only if the following query is relevant to one of the keywords: '.$openai_include_keyword .' The actual query is as follows: '. $keyword;
408 return $prompts;
409 }
410 }
411 public function qcld_include_keyword_exist( $keyword ){
412 $keyword = isset($keyword) ? $keyword : '';
413 $openai_include_keywords = strtolower(get_option('openai_include_keyword'));
414 if(!empty($keyword)){
415 $openai_include_keyword = ( isset( $openai_include_keywords ) ? $openai_include_keywords : '');
416
417 if( !empty($openai_include_keyword)){
418 $include_items = explode(',', $openai_include_keyword);
419 if(!empty($include_items)){
420 foreach($include_items as $k => $item){
421 if((strpos($keyword,trim($item)) !== false) && !empty($item)){
422 return true;
423 }
424 }
425 }
426 return false;
427 }
428 }
429
430 return false;
431
432 }
433 public function openai_response_callback() {
434 // $nonce = sanitize_text_field($_POST['nonce']);
435 // if (! wp_verify_nonce($nonce,'qcsecretbotnonceval123qc')) {
436 // wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
437 // wp_die();
438
439 // }else{
440 $response['status'] = 'success';
441 $response['message'] ='A preset message';
442 $OpenAI = new qcld_wp_OpenAI();
443 $gptkeyword = [];
444 $keyword = sanitize_text_field($_POST['keyword']);
445 if(get_option( 'is_asst_enabled') != 1){
446 $response_files = $this->openai_retrive_fine_tune($keyword);
447 $response_file = json_decode($response_files, true);
448 $gptkeywords = [];
449 if((empty($response_file['choices'][0]["text"])) && empty($response_file['choices'][0]["message"]['content'])){
450
451
452 if(empty($_COOKIE["last_five_prompt"])){
453 array_push($gptkeyword, array(
454 "role" => "user",
455 "content" => $keyword
456 ));
457 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
458 }else{
459 $data = ($_COOKIE['last_five_prompt']);
460 $data = (base64_decode($data));
461 $gptkeyword = maybe_unserialize($data);
462 if(is_array($gptkeyword)){
463 array_push( $gptkeyword, array(
464 "role" => "user",
465 "content" => $keyword
466 ));
467 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
468 }
469 }
470 if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '1') ){
471 $prompts = $this->include_exclude_prompt($keyword);
472
473 $gptkeyword = [];
474 array_push($gptkeyword, array(
475 "role" => "user",
476 "content" => $prompts,
477 ));
478 }else if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '0')){
479 if($this->qcld_include_keyword_exist($keyword) == false){
480
481 $response['message'] = 'Sorry, No result found!';
482 echo json_encode($response);
483 wp_die();
484 }else{
485 array_push($gptkeyword, array(
486 "role" => "user",
487 "content" => $keyword
488 ));
489 }
490
491 }
492
493 $res = $OpenAI->gptcomplete(
494 $gptkeyword
495 );
496 $mess = json_decode($res);
497 $response['message'] = $mess->choices[0]->message->content;
498 if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
499 $response['message'] = 'Sorry, No result found!';
500 }
501 if(get_option('conversation_continuity') == 1){
502 $data = ($_COOKIE['last_five_prompt']);
503 $data = (base64_decode($data));
504 $gptkeywords = maybe_unserialize($data);
505 if(is_array($gptkeywords)){
506 array_push( $gptkeywords, array(
507 "role" => "assistant",
508 "content" => $response['message']
509 ));
510 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeywords)) , time() + (60000), "/");
511 }
512 }
513
514
515 }else if(!empty($response_file['choices'][0]["message"]['content'])){
516 $result = $response_file['choices'][0]["message"]['content'];
517 $response['message'] = $result;
518 }else{
519 $result = $response_file['choices'][0]["text"];
520 $message = explode(">",$result);
521 if(empty($message)){
522 $message = $result;
523 }elseif(empty($message[1])){
524 $message = $message[0];
525 }else{
526 $message = $message[1];
527 }
528 if(get_option('conversation_continuity') == 1){
529 $lasfivecookie = $_COOKIE["last_five_prompt"] . $message . '###';
530 setcookie('last_five_prompt', $lasfivecookie, time() + (60000), "/");
531 $response['cookie'] = $lasfivecookie;
532 }
533 $response['message'] = $message;
534 }
535 }else{
536 $url = 'https://api.openai.com/v1/threads';
537 $api_key = get_option('open_ai_api_key');
538 $engines = get_option( 'openai_engines');
539
540 $header = [
541 'Content-Type: application/json',
542 'OpenAI-Beta: assistants=v1',
543 'Authorization: Bearer ' . $api_key
544 ];
545 // $threads_id = '';
546 $threads_id_COOKIE = $_COOKIE["qcld_threads_id"];
547 $threads_id = $threads_id_COOKIE;
548 if(($threads_id == '')){
549
550 $ch = curl_init();
551 curl_setopt($ch, CURLOPT_URL, $url);
552 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
553 curl_setopt($ch, CURLOPT_POST, 1);
554 // curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
555 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
556 $threads = curl_exec($ch);
557 $threads_id = json_decode($threads)->id;
558 setcookie('qcld_threads_id',$threads_id , time() + (60000), "/");
559 if (curl_errno($ch)) {
560 // phpcs:ignore
561 echo 'Error: ' . curl_error($ch);
562 }
563 curl_close($ch);
564 }
565
566 $msg = $this->add_on_thrrads($threads_id,$keyword);
567 $response['message'] = $msg;
568
569 }
570 echo json_encode($response);
571 wp_die();
572 //}
573 }
574 public function openai_settings_option_callback() {
575 $nonce = sanitize_text_field($_POST['nonce']);
576
577 if (! wp_verify_nonce($nonce,'wp_chatbot')) {
578 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
579 wp_die();
580
581 }else{
582
583 $api_key = sanitize_text_field($_POST['api_key']);
584 $openai_engines = sanitize_text_field($_POST['openai_engines']);
585 $qcld_openai_prompt = sanitize_text_field($_POST['qcld_openai_prompt']);
586 $max_tokens = sanitize_text_field($_POST['max_tokens']);
587 $qcld_openai_suffix = (!empty($_POST['qcld_openai_suffix'])) ? sanitize_text_field($_POST['qcld_openai_suffix']) : '';
588 $qcld_openai_custom_model = sanitize_text_field($_POST['qcld_openai_custom_model']);
589 $frequency_penalty = sanitize_text_field($_POST['frequency_penalty']);
590 $presence_penalty = sanitize_text_field($_POST['presence_penalty']);
591 $temperature = sanitize_text_field($_POST['temperature']);
592 $ai_enabled = sanitize_text_field($_POST['ai_enabled']);
593
594 $is_relevant_enabled = sanitize_text_field($_POST['is_relevant_enabled']);
595
596 $ai_only_mode = sanitize_text_field($_POST['ai_only_mode']);
597 $file_id = (!empty($_POST['file_id'])) ? sanitize_text_field($_POST['file_id']) : '';
598 $qcld_openai_prompt_custom = sanitize_text_field($_POST['qcld_openai_prompt_custom']);
599 $conversation_continuity = sanitize_text_field($_POST['conversation_continuity']);
600
601 /* Customized by Kadir on 05-12-2023 : To set empty value for API field */
602
603 $disable_ss = sanitize_text_field($_POST['disable_ss']);
604
605 if($api_key != ''){
606 update_option( 'open_ai_api_key', $api_key );
607 }
608 else{
609 delete_option( 'open_ai_api_key');
610 }
611
612 /* Ends: Customized by Kadir on 05-12-2023 : To set empty value for API field */
613
614 if($openai_engines != ''){
615 update_option( 'openai_engines', $openai_engines );
616 }
617 if($conversation_continuity != ''){
618 update_option( 'conversation_continuity', $conversation_continuity );
619 }
620 update_option( 'openai_max_tokens', $max_tokens );
621
622 if($qcld_openai_suffix != ''){
623 update_option('qcld_openai_suffix', $qcld_openai_suffix);
624 }
625 if($frequency_penalty != ''){
626 update_option( 'frequency_penalty', $frequency_penalty );
627 }
628 if($presence_penalty != ''){
629 update_option( 'presence_penalty', $presence_penalty );
630 }
631 if($temperature != ''){
632 update_option( 'openai_temperature', $temperature );
633 }
634 if($qcld_openai_prompt_custom != ''){
635 update_option('qcld_openai_prompt_custom', $qcld_openai_prompt_custom );
636 }
637 update_option('qcld_openai_custom_model',$qcld_openai_custom_model);
638
639 update_option('ai_enabled',$ai_enabled);
640 update_option('qcld_openai_relevant_enabled',$is_relevant_enabled);
641
642 if($file_id != ''){
643 update_option('file_id',$file_id);
644 }
645 $openai_include_keyword = sanitize_text_field($_POST['openai_include_keyword']);
646 update_option('openai_include_keyword',$openai_include_keyword);
647 $openai_exclude_keyword = sanitize_text_field($_POST['openai_exclude_keyword']);
648 update_option('openai_exclude_keyword',$openai_exclude_keyword);
649
650
651 /* Customized by Kadir on 05-12-2023 : To Disable Site Search*/
652 //Disable Site Search
653 if( $disable_ss == 1 ){
654 update_option('disable_wp_chatbot_site_search',1);
655 }
656 /* Ends: Customized by Kadir on 05-12-2023 : To Disable Site Search*/
657
658
659
660 }
661
662 if(($ai_only_mode != '') && ($ai_only_mode == 0)){
663
664 update_option('ai_only_mode', $ai_only_mode);
665 // update_option('enable_wp_chatbot_disable_allicon', 0);
666 // update_option('qcld_disable_start_menu', 0);
667 // update_option('show_menu_after_greetings', 0);
668 // update_option('skip_wp_greetings', 0);
669 // update_option('disable_wp_chatbot_site_search',0);
670 // update_option('disable_wp_chatbot_call_gen',0);
671 // update_option('disable_wp_chatbot_feedback',0);
672 // update_option('disable_wp_chatbot_faq',0);
673 // update_option('disable_email_subscription',0);
674 // update_option('disable_str_categories',0);
675 // update_option('disable_good_bye',0);
676
677 }else if(($ai_only_mode != '') && ($ai_only_mode == 1)){
678 update_option('ai_only_mode', $ai_only_mode);
679 update_option('enable_wp_chatbot_disable_allicon', 1);
680 update_option('qcld_disable_start_menu', 1);
681 update_option('show_menu_after_greetings', 1);
682 update_option('skip_wp_greetings', 1);
683 update_option('disable_wp_chatbot_site_search',1);
684 update_option('disable_wp_chatbot_call_gen',1);
685 update_option('disable_wp_chatbot_feedback',1);
686 update_option('disable_wp_chatbot_faq',1);
687 update_option('disable_email_subscription',1);
688 update_option('disable_str_categories',1);
689 update_option('disable_good_bye',1);
690 }
691
692 if($qcld_openai_prompt != ''){
693 update_option('qcld_openai_prompt', $qcld_openai_prompt);
694 }
695 $tem = get_option( 'openai_temperature', $temperature );
696
697 echo json_encode($ai_enabled);wp_die();
698
699 }
700
701 }
702
703 /**
704 * @return qcld_wpopenai_addon
705 */
706 if(!function_exists('qcld_wpopenai_addons')){
707 function qcld_openais() {
708 $qcld_wpopenai_addon = new qcld_wpopenai_addons();
709 return $qcld_wpopenai_addon->instance();
710
711 }
712 }
713
714 //fire off the plugin
715 qcld_openais();
716
717 }