PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 6.9.4
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v6.9.4
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 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / includes / integration / openai / qcld-bot-openai.php

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

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