PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 5.3.1
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v5.3.1
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 5.3.1, at includes/openai/qcld-bot-openai.php

715 lines 33.4 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 echo 'Error:' . curl_error($ch);
329 }
330 curl_close($ch);
331 }else{
332 $data = json_encode(array(
333 'model'=> get_option( 'qcld_openai_custom_model'),
334 "messages"=> [
335
336 [
337 "role"=> "user",
338 "content"=>$keyword
339 ]
340
341 ],
342
343 ));
344 $ch = curl_init();
345
346 curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
347 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
348 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
349 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
350 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
351
352 $results = (curl_exec($ch));
353 $results = str_replace("#","",$result );
354 $result = (json_decode($results)->choices[0]->message->content);
355 return $result;
356
357
358 }
359
360 }
361 public function response_form_file($keyword){
362 $max_tokens = (int)get_option( 'openai_max_tokens');
363 $temp = (float)get_option( 'openai_temperature');
364 $frequency_penalty = (float)get_option( 'frequency_penalty');
365 $presence_penalty = (float)get_option( 'presence_penalty');
366 $engines = explode('-',get_option( 'openai_engines'));
367 if($engines[0] != 'gpt'){
368 // $prompts = $this->get_prompt($keyword);
369 }
370
371 $request_body = [
372 "prompt" => $keyword,
373 "model" => get_option( 'qcld_openai_custom_model'),
374 "max_tokens" => $max_tokens,
375 "temperature" => 0,
376 "top_p" => 1,
377 "stop" => [],
378 "presence_penalty" => 0,
379 "frequency_penalty"=> 0,
380 "best_of"=> 1,
381 ];
382 $postFields = json_encode($request_body);
383 $OpenAI = new qcld_wp_OpenAI();
384 $result = $OpenAI->get_response($postFields);
385
386 return $result;
387 }
388 public function get_prompt($keyword){
389 $openai_include_keyword = get_option( 'openai_include_keyword');
390 $openai_exclude_keyword = get_option( 'openai_exclude_keyword');
391 $qcld_openai_prompt = get_option('qcld_openai_prompt',true);
392
393 }
394 public function include_exclude_prompt($keyword){
395 $openai_include_keyword = strtolower(get_option('openai_include_keyword'));
396 $openai_exclude_keyword = strtolower(get_option('openai_exclude_keyword'));
397
398 if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') == '')){
399 $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;
400 return $prompts;
401 }else if((get_option('openai_include_keyword') == '') || (get_option('openai_exclude_keyword') != '')){
402
403 $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';
404 return $prompts;
405 }else if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')){
406 $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;
407 return $prompts;
408 }
409 }
410 public function qcld_include_keyword_exist( $keyword ){
411 $keyword = isset($keyword) ? $keyword : '';
412 $openai_include_keywords = strtolower(get_option('openai_include_keyword'));
413 if(!empty($keyword)){
414 $openai_include_keyword = ( isset( $openai_include_keywords ) ? $openai_include_keywords : '');
415
416 if( !empty($openai_include_keyword)){
417 $include_items = explode(',', $openai_include_keyword);
418 if(!empty($include_items)){
419 foreach($include_items as $k => $item){
420 if((strpos($keyword,trim($item)) !== false) && !empty($item)){
421 return true;
422 }
423 }
424 }
425 return false;
426 }
427 }
428
429 return false;
430
431 }
432 public function openai_response_callback() {
433 // $nonce = sanitize_text_field($_POST['nonce']);
434 // if (! wp_verify_nonce($nonce,'qcsecretbotnonceval123qc')) {
435 // wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
436 // wp_die();
437
438 // }else{
439 $response['status'] = 'success';
440 $response['message'] ='A preset message';
441 $OpenAI = new qcld_wp_OpenAI();
442 $gptkeyword = [];
443 $keyword = sanitize_text_field($_POST['keyword']);
444 if(get_option( 'is_asst_enabled') != 1){
445 $response_files = $this->openai_retrive_fine_tune($keyword);
446 $response_file = json_decode($response_files, true);
447 $gptkeywords = [];
448 if((empty($response_file['choices'][0]["text"])) && empty($response_file['choices'][0]["message"]['content'])){
449
450
451 if(empty($_COOKIE["last_five_prompt"])){
452 array_push($gptkeyword, array(
453 "role" => "user",
454 "content" => $keyword
455 ));
456 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
457 }else{
458 $data = ($_COOKIE['last_five_prompt']);
459 $data = (base64_decode($data));
460 $gptkeyword = maybe_unserialize($data);
461 if(is_array($gptkeyword)){
462 array_push( $gptkeyword, array(
463 "role" => "user",
464 "content" => $keyword
465 ));
466 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
467 }
468 }
469 if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '1') ){
470 $prompts = $this->include_exclude_prompt($keyword);
471
472 $gptkeyword = [];
473 array_push($gptkeyword, array(
474 "role" => "user",
475 "content" => $prompts,
476 ));
477 }else if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '0')){
478 if($this->qcld_include_keyword_exist($keyword) == false){
479
480 $response['message'] = 'Sorry, No result found!';
481 echo json_encode($response);
482 wp_die();
483 }else{
484 array_push($gptkeyword, array(
485 "role" => "user",
486 "content" => $keyword
487 ));
488 }
489
490 }
491
492 $res = $OpenAI->gptcomplete(
493 $gptkeyword
494 );
495 $mess = json_decode($res);
496 $response['message'] = $mess->choices[0]->message->content;
497 if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
498 $response['message'] = 'Sorry, No result found!';
499 }
500 if(get_option('conversation_continuity') == 1){
501 $data = ($_COOKIE['last_five_prompt']);
502 $data = (base64_decode($data));
503 $gptkeywords = maybe_unserialize($data);
504 if(is_array($gptkeywords)){
505 array_push( $gptkeywords, array(
506 "role" => "assistant",
507 "content" => $response['message']
508 ));
509 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeywords)) , time() + (60000), "/");
510 }
511 }
512
513
514 }else if(!empty($response_file['choices'][0]["message"]['content'])){
515 $result = $response_file['choices'][0]["message"]['content'];
516 $response['message'] = $result;
517 }else{
518 $result = $response_file['choices'][0]["text"];
519 $message = explode(">",$result);
520 if(empty($message)){
521 $message = $result;
522 }elseif(empty($message[1])){
523 $message = $message[0];
524 }else{
525 $message = $message[1];
526 }
527 if(get_option('conversation_continuity') == 1){
528 $lasfivecookie = $_COOKIE["last_five_prompt"] . $message . '###';
529 setcookie('last_five_prompt', $lasfivecookie, time() + (60000), "/");
530 $response['cookie'] = $lasfivecookie;
531 }
532 $response['message'] = $message;
533 }
534 }else{
535 $url = 'https://api.openai.com/v1/threads';
536 $api_key = get_option('open_ai_api_key');
537 $engines = get_option( 'openai_engines');
538
539 $header = [
540 'Content-Type: application/json',
541 'OpenAI-Beta: assistants=v1',
542 'Authorization: Bearer ' . $api_key
543 ];
544 // $threads_id = '';
545 $threads_id_COOKIE = $_COOKIE["qcld_threads_id"];
546 $threads_id = $threads_id_COOKIE;
547 if(($threads_id == '')){
548
549 $ch = curl_init();
550 curl_setopt($ch, CURLOPT_URL, $url);
551 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
552 curl_setopt($ch, CURLOPT_POST, 1);
553 // curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
554 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
555 $threads = curl_exec($ch);
556 $threads_id = json_decode($threads)->id;
557 setcookie('qcld_threads_id',$threads_id , time() + (60000), "/");
558 if (curl_errno($ch)) {
559 echo 'Error: ' . curl_error($ch);
560 }
561 curl_close($ch);
562 }
563
564 $msg = $this->add_on_thrrads($threads_id,$keyword);
565 $response['message'] = $msg;
566
567 }
568 echo json_encode($response);
569 wp_die();
570 //}
571 }
572 public function openai_settings_option_callback() {
573 $nonce = sanitize_text_field($_POST['nonce']);
574
575 if (! wp_verify_nonce($nonce,'wp_chatbot')) {
576 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
577 wp_die();
578
579 }else{
580
581 $api_key = sanitize_text_field($_POST['api_key']);
582 $openai_engines = sanitize_text_field($_POST['openai_engines']);
583 $qcld_openai_prompt = sanitize_text_field($_POST['qcld_openai_prompt']);
584 $max_tokens = sanitize_text_field($_POST['max_tokens']);
585 $qcld_openai_suffix = (!empty($_POST['qcld_openai_suffix'])) ? sanitize_text_field($_POST['qcld_openai_suffix']) : '';
586 $qcld_openai_custom_model = sanitize_text_field($_POST['qcld_openai_custom_model']);
587 $frequency_penalty = sanitize_text_field($_POST['frequency_penalty']);
588 $presence_penalty = sanitize_text_field($_POST['presence_penalty']);
589 $temperature = sanitize_text_field($_POST['temperature']);
590 $ai_enabled = sanitize_text_field($_POST['ai_enabled']);
591
592 $is_relevant_enabled = sanitize_text_field($_POST['is_relevant_enabled']);
593
594 $ai_only_mode = sanitize_text_field($_POST['ai_only_mode']);
595 $file_id = (!empty($_POST['file_id'])) ? sanitize_text_field($_POST['file_id']) : '';
596 $qcld_openai_prompt_custom = sanitize_text_field($_POST['qcld_openai_prompt_custom']);
597 $conversation_continuity = sanitize_text_field($_POST['conversation_continuity']);
598
599 /* Customized by Kadir on 05-12-2023 : To set empty value for API field */
600
601 $disable_ss = sanitize_text_field($_POST['disable_ss']);
602
603 if($api_key != ''){
604 update_option( 'open_ai_api_key', $api_key );
605 }
606 else{
607 delete_option( 'open_ai_api_key');
608 }
609
610 /* Ends: Customized by Kadir on 05-12-2023 : To set empty value for API field */
611
612 if($openai_engines != ''){
613 update_option( 'openai_engines', $openai_engines );
614 }
615 if($conversation_continuity != ''){
616 update_option( 'conversation_continuity', $conversation_continuity );
617 }
618 update_option( 'openai_max_tokens', $max_tokens );
619
620 if($qcld_openai_suffix != ''){
621 update_option('qcld_openai_suffix', $qcld_openai_suffix);
622 }
623 if($frequency_penalty != ''){
624 update_option( 'frequency_penalty', $frequency_penalty );
625 }
626 if($presence_penalty != ''){
627 update_option( 'presence_penalty', $presence_penalty );
628 }
629 if($temperature != ''){
630 update_option( 'openai_temperature', $temperature );
631 }
632 if($qcld_openai_prompt_custom != ''){
633 update_option('qcld_openai_prompt_custom', $qcld_openai_prompt_custom );
634 }
635 update_option('qcld_openai_custom_model',$qcld_openai_custom_model);
636
637 update_option('ai_enabled',$ai_enabled);
638 update_option('qcld_openai_relevant_enabled',$is_relevant_enabled);
639
640 if($file_id != ''){
641 update_option('file_id',$file_id);
642 }
643 $openai_include_keyword = sanitize_text_field($_POST['openai_include_keyword']);
644 update_option('openai_include_keyword',$openai_include_keyword);
645 $openai_exclude_keyword = sanitize_text_field($_POST['openai_exclude_keyword']);
646 update_option('openai_exclude_keyword',$openai_exclude_keyword);
647
648
649 /* Customized by Kadir on 05-12-2023 : To Disable Site Search*/
650 //Disable Site Search
651 if( $disable_ss == 1 ){
652 update_option('disable_wp_chatbot_site_search',1);
653 }
654 /* Ends: Customized by Kadir on 05-12-2023 : To Disable Site Search*/
655
656
657
658 }
659
660 if(($ai_only_mode != '') && ($ai_only_mode == 0)){
661
662 update_option('ai_only_mode', $ai_only_mode);
663 // update_option('enable_wp_chatbot_disable_allicon', 0);
664 // update_option('qcld_disable_start_menu', 0);
665 // update_option('show_menu_after_greetings', 0);
666 // update_option('skip_wp_greetings', 0);
667 // update_option('disable_wp_chatbot_site_search',0);
668 // update_option('disable_wp_chatbot_call_gen',0);
669 // update_option('disable_wp_chatbot_feedback',0);
670 // update_option('disable_wp_chatbot_faq',0);
671 // update_option('disable_email_subscription',0);
672 // update_option('disable_str_categories',0);
673 // update_option('disable_good_bye',0);
674
675 }else if(($ai_only_mode != '') && ($ai_only_mode == 1)){
676 update_option('ai_only_mode', $ai_only_mode);
677 update_option('enable_wp_chatbot_disable_allicon', 1);
678 update_option('qcld_disable_start_menu', 1);
679 update_option('show_menu_after_greetings', 1);
680 update_option('skip_wp_greetings', 1);
681 update_option('disable_wp_chatbot_site_search',1);
682 update_option('disable_wp_chatbot_call_gen',1);
683 update_option('disable_wp_chatbot_feedback',1);
684 update_option('disable_wp_chatbot_faq',1);
685 update_option('disable_email_subscription',1);
686 update_option('disable_str_categories',1);
687 update_option('disable_good_bye',1);
688 }
689
690 if($qcld_openai_prompt != ''){
691 update_option('qcld_openai_prompt', $qcld_openai_prompt);
692 }
693 $tem = get_option( 'openai_temperature', $temperature );
694
695 echo json_encode($ai_enabled);wp_die();
696
697 }
698
699 }
700
701 /**
702 * @return qcld_wpopenai_addon
703 */
704 if(!function_exists('qcld_wpopenai_addons')){
705 function qcld_openais() {
706 $qcld_wpopenai_addon = new qcld_wpopenai_addons();
707 return $qcld_wpopenai_addon->instance();
708
709 }
710 }
711
712 //fire off the plugin
713 qcld_openais();
714
715 }