core = $mwai_core; if ( is_admin() ) { return; } add_shortcode( 'mwai_chat', array( $this, 'chat' ) ); add_shortcode( 'mwai_chatbot', array( $this, 'chat' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); if ( $this->core->get_option( 'shortcode_chat_inject' ) ) { add_action( 'wp_body_open', array( $this, 'inject_chat' ) ); } // Only for test now, but later we should probably import the JS/CSS if ( $this->core->get_option( 'shortcode_chat_syntax_highlighting' ) ) { wp_enqueue_script( 'mwai_chatbot', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js', [], null, false ); wp_enqueue_style( 'mwai_chatbot', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/stackoverflow-dark.min.css' ); } } function rest_api_init() { try { register_rest_route( $this->namespace, '/chat', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_chat' ), 'permission_callback' => '__return_true' ) ); } catch ( Exception $e ) { var_dump( $e ); } } function chatgpt_style( $id ) { $css = file_get_contents( MWAI_PATH . '/classes/modules/chatbot-chatgpt.css' ); $css = str_replace( '#mwai-chat-id', "#mwai-chat-{$id}", $css ); return ""; } function rest_chat( $request ) { try { $params = $request->get_json_params(); $prompt = $params['prompt']; $model = $params['model']; $temperature = $params['temperature']; $maxTokens = intval( $params['maxTokens'] ); $apiKey = $params['apiKey']; $stop = $params['stop']; $query = new Meow_MWAI_QueryText( $prompt, 1024 ); if ( $model ) { $query->setModel( $model ); } if ( $temperature ) { $query->setTemperature( $temperature ); } if ( $maxTokens ) { $query->setMaxTokens( $maxTokens ); } if ( $stop ) { $query->setStop( $stop ); } if ( $apiKey ) { $query->setApiKey( $apiKey ); } $answer = $this->core->ai->run( $query ); $rawText = $answer->result; $html = apply_filters( 'mwai_chatbot_answer', $rawText ); $html = $this->core->markdown_to_html( $rawText ); return new WP_REST_Response([ 'success' => true, 'answer' => $rawText, 'html' => $html, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } function inject_chat() { $params = $this->core->get_option( 'shortcode_chat_params' ); echo $this->chat( $params ); } function chat( $atts ) { // Use the core default parameters, or the user default parameters $override = $this->core->get_option( 'shortcode_chat_params_override' ); $defaults_params = $override ? $this->core->get_option( 'shortcode_chat_params' ) : $this->core->get_option( 'shortcode_chat_default_params' ); $defaults_params['id'] = uniqid(); // Give a chance to modify the default parameters one last time $defaults = apply_filters( 'mwai_chatbot_params_defaults', $defaults_params ); // Make sure all the mandatory params are set foreach ( $this->core->defaultChatbotParams as $key => $value ) { if ( !isset( $defaults[$key] ) ) { $defaults[$key] = $value; } } // Override with the shortcode, and before/after filters $atts = apply_filters( 'mwai_chatbot_params_before', $atts ); $atts = shortcode_atts( $defaults, $atts ); $atts = apply_filters( 'mwai_chatbot_params', $atts ); $apiUrl = get_rest_url( null, 'ai-engine/v1/chat' ); $id = $atts['id']; // Named functions $onSentClickFn = "mwai_{$id}_onSendClick"; $addReplyFn = "mwai_{$id}_addReply"; $initChatBotFn = "mwai_{$id}_initChatBot"; // UI Parameters $aiName = addslashes( trim($atts['ai_name']) ); $userName = addslashes( trim($atts['user_name']) ); $sysName = addslashes( trim($atts['sys_name']) ); $context = addslashes( $atts['context'] ); $textSend = addslashes( trim( $atts['text_send'] ) ); $textInputPlaceholder = addslashes( trim( $atts['text_input_placeholder'] ) ); $startSentence = addslashes( trim( $atts['start_sentence'] ) ); $window = filter_var( $atts['window'], FILTER_VALIDATE_BOOLEAN ); $fullscreen = filter_var( $atts['fullscreen'], FILTER_VALIDATE_BOOLEAN ); $style = $atts['style']; // Chatbot System Parameters $casuallyFineTuned = boolval( $atts['casually_fined_tuned'] ); $promptEnding = addslashes( trim( $atts['prompt_ending'] ) ); $completionEnding = addslashes( trim( $atts['completion_ending'] ) ); if ( $casuallyFineTuned ) { $promptEnding = "\\n\\n===\\n\\n"; $completionEnding = "\\n\\n"; } // OpenAI Parameters $model = $atts['model']; $temperature = $atts['temperature']; $maxTokens = $atts['max_tokens']; $apiKey = $atts['api_key']; // Variables $onGoingPrompt = "mwai_{$id}_onGoingPrompt"; $baseClasses = "mwai-chat"; $baseClasses .= ( $window ? " mwai-window" : "" ); $baseClasses .= ( $fullscreen ? " mwai-fullscreen" : "" ); // Output CSS ob_start(); $style_content = ""; if ( $style === 'chatgpt' ) { $style_content = $this->chatgpt_style( $id, $style ); } echo apply_filters( 'mwai_chatbot_style', $style_content, $id ); // Output HTML & CSS ?>