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

738 lines 34.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 switch ($qcld_openai_prompt) {
393 case "q_and_a":
394 if(get_option('conversation_continuity') == 1){
395 if(empty($_COOKIE["last_five_prompt"])){
396 setcookie("last_five_prompt", "Q:".$keyword."\nA:", time() + (60000), "/");
397 return "Q:".$keyword."\nA:";
398 }else{
399 $last_five = $_COOKIE["last_five_prompt"];
400 setcookie("last_five_prompt", $last_five. "\nQ:".$keyword."\nA:", time() + (60000), "/");
401 return $last_five . "Q:".$keyword."\nA:";
402 }
403 }else{
404 return "Q:".$keyword."\nA:";
405 }
406 case "chat":
407 if(get_option('conversation_continuity') == 1){
408 if(empty($_COOKIE["last_five_prompt"])){
409 setcookie("last_five_prompt", "Human:".$keyword."\nAI:", time() + (60000), "/");
410 return "Human:".$keyword."\nAI:";
411 }else{
412 $last_five = $_COOKIE["last_five_prompt"];
413 setcookie("last_five_prompt", $last_five. "\nQ:".$keyword."\nA:", time() + (60000), "/");
414 return $last_five . "\nHuman:".$keyword."\nAI:";
415 }
416 }else{
417 return "\nHuman:".$keyword."\nAI:";
418 }
419
420 case "friend_chat":
421 if(get_option('conversation_continuity') == 1){
422 if(empty($_COOKIE["last_five_prompt"])){
423 setcookie("last_five_prompt", "You:".$keyword."\nFriend:", time() + (60000), "/");
424 return "You:".$keyword."\nFriend:";
425 }else{
426 $last_five = $_COOKIE["last_five_prompt"];
427 setcookie("last_five_prompt", $last_five. "\nQ:".$keyword."\nA:", time() + (60000), "/");
428 return $last_five . "\nYou:".$keyword."\nFriend:";
429 }
430 }else{
431 return "You:".$keyword."\nFriend:";
432 }
433 case "grammar_correction":
434 return "Correct this to standard English:\n\n ".$keyword." " ;
435 case "marv_sarcastic_chatbot" :
436 return "Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: ".$keyword." \nMarv:";
437 case "micro_horror":
438 return "\nTopic: ".$keyword."\n\nTwo-Sentence Horror Story:";
439 case "any_command":
440 return $keyword;
441 case "write_poem":
442 return "write a poem about". $keyword;
443 case "custom_prompt":
444 return get_option('qcld_openai_prompt_custom') ."\n" . $keyword;
445 default:
446 return "Q:".$keyword."\nA:";
447 }
448 }
449 public function include_exclude_prompt($keyword){
450 $openai_include_keyword = strtolower(get_option('openai_include_keyword'));
451 $openai_exclude_keyword = strtolower(get_option('openai_exclude_keyword'));
452
453 if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') == '')){
454 $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;
455 return $prompts;
456 }else if((get_option('openai_include_keyword') == '') || (get_option('openai_exclude_keyword') != '')){
457
458 $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';
459 return $prompts;
460 }else if((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')){
461 $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;
462 return $prompts;
463 }
464 }
465 public function qcld_include_keyword_exist( $keyword ){
466 $keyword = isset($keyword) ? $keyword : '';
467 $openai_include_keywords = strtolower(get_option('openai_include_keyword'));
468 if(!empty($keyword)){
469 $openai_include_keyword = ( isset( $openai_include_keywords ) ? $openai_include_keywords : '');
470
471 if( !empty($openai_include_keyword)){
472 $include_items = explode(',', $openai_include_keyword);
473 if(!empty($include_items)){
474 foreach($include_items as $k => $item){
475 if((strpos($keyword,trim($item)) !== false) && !empty($item)){
476 return true;
477 }
478 }
479 }
480 return false;
481 }
482 }
483
484 return false;
485
486 }
487 public function openai_response_callback() {
488 $response['status'] = 'success';
489 $response['message'] ='A preset message';
490 $OpenAI = new qcld_wp_OpenAI();
491 $gptkeyword = [];
492 $keyword = sanitize_text_field($_POST['keyword']);
493 $response_files = $this->openai_retrive_fine_tune($keyword);
494 $response_file = json_decode($response_files, true);
495
496 $gptkeywords = [];
497 if(empty($response_file['choices'][0]["text"])){
498
499 $engines = explode('-',get_option( 'openai_engines'));
500 if($engines[0] == 'gpt'){
501
502 if(empty($_COOKIE["last_five_prompt"])){
503 array_push($gptkeyword, array(
504 "role" => "user",
505 "content" => $keyword
506 ));
507 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
508 }else{
509 $data = ($_COOKIE['last_five_prompt']);
510 $data = (base64_decode($data));
511 $gptkeyword = maybe_unserialize($data);
512 if(is_array($gptkeyword)){
513 array_push( $gptkeyword, array(
514 "role" => "user",
515 "content" => $keyword
516 ));
517 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeyword)) , time() + (60000), "/");
518 }
519 }
520 if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '1') ){
521 $prompts = $this->include_exclude_prompt($keyword);
522
523 $gptkeyword = [];
524 array_push($gptkeyword, array(
525 "role" => "user",
526 "content" => $prompts,
527 ));
528 }else if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '0')){
529 if($this->qcld_include_keyword_exist($keyword) == false){
530
531 $response['message'] = 'Sorry, No result found!';
532 echo json_encode($response);
533 wp_die();
534 }else{
535 array_push($gptkeyword, array(
536 "role" => "user",
537 "content" => $keyword
538 ));
539 }
540
541 }
542
543 $res = $OpenAI->gptcomplete(
544 $gptkeyword
545 );
546 $mess = json_decode($res);
547 $response['message'] = $mess->choices[0]->message->content;
548 if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
549 $response['message'] = 'Sorry, No result found!';
550 }
551 if(get_option('conversation_continuity') == 1){
552 $data = ($_COOKIE['last_five_prompt']);
553 $data = (base64_decode($data));
554 $gptkeywords = maybe_unserialize($data);
555 if(is_array($gptkeywords)){
556 array_push( $gptkeywords, array(
557 "role" => "assistant",
558 "content" => $response['message']
559 ));
560 setcookie('last_five_prompt', base64_encode(maybe_serialize($gptkeywords)) , time() + (60000), "/");
561 }
562 }
563
564 }else{
565
566 if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '1') ){
567 $prompts = $this->include_exclude_prompt($keyword);
568
569 }else if(((get_option('openai_include_keyword') != '') || (get_option('openai_exclude_keyword') != '')) && (get_option('qcld_openai_relevant_enabled') == '0')){
570 if($this->qcld_include_keyword_exist($keyword) == false){
571 $response['message'] = "Sorry, No result found!";
572 echo json_encode($response);
573 wp_die();
574 }else{
575 $prompts = $this->get_prompt($keyword);
576 }
577 }else{
578 $prompts = $this->get_prompt($keyword);
579 }
580 $prompt =$prompts;
581 $res = $OpenAI->complete(
582 $prompt
583 );
584
585 $mess = json_decode($res);
586 $response['message'] = $mess->choices[0]->text;
587 $response['message'] = str_replace(array("\n", "\r"), '', $response['message']);
588 if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
589 $response['message'] = 'Sorry, No result found!';
590 }
591 if(get_option('conversation_continuity') == 1){
592 $lasfivecookie = $_COOKIE["last_five_prompt"] . $response['message'] . '###';
593 $response['cookie'] = $_COOKIE["last_five_prompt"];
594 }
595 }
596 }else{
597 $result = $response_file['choices'][0]["text"];
598 $message = explode(">",$result);
599 if(empty($message)){
600 $message = $result;
601 }elseif(empty($message[1])){
602 $message = $message[0];
603 }else{
604 $message = $message[1];
605 }
606 if(get_option('conversation_continuity') == 1){
607 $lasfivecookie = $_COOKIE["last_five_prompt"] . $message . '###';
608 setcookie('last_five_prompt', $lasfivecookie, time() + (60000), "/");
609 $response['cookie'] = $lasfivecookie;
610 }
611 $response['message'] = $message;
612 }
613 echo json_encode($response);
614 wp_die();
615 }
616 public function openai_settings_option_callback() {
617 $nonce = sanitize_text_field($_POST['nonce']);
618
619 if (! wp_verify_nonce($nonce,'wp_chatbot')) {
620 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'sm')));
621 wp_die();
622
623 }else{
624
625 $api_key = sanitize_text_field($_POST['api_key']);
626 $openai_engines = sanitize_text_field($_POST['openai_engines']);
627 $qcld_openai_prompt = sanitize_text_field($_POST['qcld_openai_prompt']);
628 $max_tokens = sanitize_text_field($_POST['max_tokens']);
629 $qcld_openai_suffix = (!empty($_POST['qcld_openai_suffix'])) ? sanitize_text_field($_POST['qcld_openai_suffix']) : '';
630 $qcld_openai_custom_model = sanitize_text_field($_POST['qcld_openai_custom_model']);
631 $frequency_penalty = sanitize_text_field($_POST['frequency_penalty']);
632 $presence_penalty = sanitize_text_field($_POST['presence_penalty']);
633 $temperature = sanitize_text_field($_POST['temperature']);
634 $ai_enabled = sanitize_text_field($_POST['ai_enabled']);
635
636 $is_relevant_enabled = sanitize_text_field($_POST['is_relevant_enabled']);
637
638 $ai_only_mode = sanitize_text_field($_POST['ai_only_mode']);
639 $file_id = (!empty($_POST['file_id'])) ? sanitize_text_field($_POST['file_id']) : '';
640 $qcld_openai_prompt_custom = sanitize_text_field($_POST['qcld_openai_prompt_custom']);
641 $conversation_continuity = sanitize_text_field($_POST['conversation_continuity']);
642 if($api_key != ''){
643 update_option( 'open_ai_api_key', $api_key );
644 }
645 if($openai_engines != ''){
646 update_option( 'openai_engines', $openai_engines );
647 }
648 if($conversation_continuity != ''){
649 update_option( 'conversation_continuity', $conversation_continuity );
650 }
651 update_option( 'openai_max_tokens', $max_tokens );
652
653 if($qcld_openai_suffix != ''){
654 update_option('qcld_openai_suffix', $qcld_openai_suffix);
655 }
656 if($frequency_penalty != ''){
657 update_option( 'frequency_penalty', $frequency_penalty );
658 }
659 if($presence_penalty != ''){
660 update_option( 'presence_penalty', $presence_penalty );
661 }
662 if($temperature != ''){
663 update_option( 'openai_temperature', $temperature );
664 }
665 if($qcld_openai_prompt_custom != ''){
666 update_option('qcld_openai_prompt_custom', $qcld_openai_prompt_custom );
667 }
668 update_option('qcld_openai_custom_model',$qcld_openai_custom_model);
669
670 update_option('ai_enabled',$ai_enabled);
671 update_option('qcld_openai_relevant_enabled',$is_relevant_enabled);
672
673 if($file_id != ''){
674 update_option('file_id',$file_id);
675 }
676 $openai_include_keyword = sanitize_text_field($_POST['openai_include_keyword']);
677 update_option('openai_include_keyword',$openai_include_keyword);
678 $openai_exclude_keyword = sanitize_text_field($_POST['openai_exclude_keyword']);
679 update_option('openai_exclude_keyword',$openai_exclude_keyword);
680
681 }
682
683 if(($ai_only_mode != '') && ($ai_only_mode == 0)){
684
685 update_option('ai_only_mode', $ai_only_mode);
686 // update_option('enable_wp_chatbot_disable_allicon', 0);
687 // update_option('qcld_disable_start_menu', 0);
688 // update_option('show_menu_after_greetings', 0);
689 // update_option('skip_wp_greetings', 0);
690 // update_option('disable_wp_chatbot_site_search',0);
691 // update_option('disable_wp_chatbot_call_gen',0);
692 // update_option('disable_wp_chatbot_feedback',0);
693 // update_option('disable_wp_chatbot_faq',0);
694 // update_option('disable_email_subscription',0);
695 // update_option('disable_str_categories',0);
696 // update_option('disable_good_bye',0);
697
698 }else if(($ai_only_mode != '') && ($ai_only_mode == 1)){
699 update_option('ai_only_mode', $ai_only_mode);
700 update_option('enable_wp_chatbot_disable_allicon', 1);
701 update_option('qcld_disable_start_menu', 1);
702 update_option('show_menu_after_greetings', 1);
703 update_option('skip_wp_greetings', 1);
704 update_option('disable_wp_chatbot_site_search',1);
705 update_option('disable_wp_chatbot_call_gen',1);
706 update_option('disable_wp_chatbot_feedback',1);
707 update_option('disable_wp_chatbot_faq',1);
708 update_option('disable_email_subscription',1);
709 update_option('disable_str_categories',1);
710 update_option('disable_good_bye',1);
711 }
712
713 if($qcld_openai_prompt != ''){
714 update_option('qcld_openai_prompt', $qcld_openai_prompt);
715 }
716 $tem = get_option( 'openai_temperature', $temperature );
717
718 echo json_encode($ai_enabled);wp_die();
719
720 }
721
722 }
723
724 /**
725 * @return qcld_wpopenai_addon
726 */
727 if(!function_exists('qcld_wpopenai_addons')){
728 function qcld_openais() {
729 $qcld_wpopenai_addon = new qcld_wpopenai_addons();
730 return $qcld_wpopenai_addon->instance();
731
732 }
733 }
734
735 //fire off the plugin
736 qcld_openais();
737
738 }