assistants.php
3 years ago
chatbot.php
3 years ago
chatbot_legacy.php
3 years ago
chatbot_logs.php
3 years ago
discussions.php
3 years ago
chatbot.php
212 lines
| 1 | <?php |
| 2 | |
| 3 | define( 'MWAI_CHATBOT_FRONT_PARAMS', [ 'aiName', 'userName', 'guestName', 'textSend', 'textClear', |
| 4 | 'textInputPlaceholder', 'textInputMaxLength', 'textCompliance', 'startSentence', |
| 5 | 'themeId', 'window', 'icon', 'iconText', 'iconAlt', 'iconPosition', 'fullscreen', 'copyButton' |
| 6 | ] ); |
| 7 | |
| 8 | class Meow_MWAI_Modules_Chatbot { |
| 9 | private $core = null; |
| 10 | private $namespace = 'mwai-bot/v1'; |
| 11 | private $isEnqueued = false; |
| 12 | private $siteWideChatId = null; |
| 13 | |
| 14 | public function __construct() { |
| 15 | global $mwai_core; |
| 16 | $this->core = $mwai_core; |
| 17 | add_shortcode( 'mwai_chatbot_v2', array( $this, 'chat' ) ); |
| 18 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 19 | $this->siteWideChatId = $this->core->get_option( 'chatId' ); |
| 20 | if ( !empty( $this->siteWideChatId ) && $this->siteWideChatId !== 'none' ) { |
| 21 | $this->registerChatbotScripts(); |
| 22 | add_action( 'wp_footer', array( $this, 'inject_chat' ) ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public function registerChatbotScripts() { |
| 27 | if ( $this->isEnqueued ) { |
| 28 | return; |
| 29 | } |
| 30 | $this->isEnqueued = true; |
| 31 | // $physical_file = MWAI_PATH . '/app/chatbot-vendor.js'; |
| 32 | // $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MWAI_VERSION; |
| 33 | // wp_register_script( 'mwai_chatbot_vendor', MWAI_URL . '/app/chatbot-vendor.js', [ 'wp-element' ], $cache_buster, false ); |
| 34 | $physical_file = MWAI_PATH . '/app/chatbot.js'; |
| 35 | $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MWAI_VERSION; |
| 36 | wp_enqueue_script( 'mwai_chatbot', MWAI_URL . '/app/chatbot.js', [ 'wp-element' ], $cache_buster, false ); |
| 37 | } |
| 38 | |
| 39 | public function rest_api_init() { |
| 40 | register_rest_route( $this->namespace, '/chat', array( |
| 41 | 'methods' => 'POST', |
| 42 | 'callback' => array( $this, 'rest_chat' ), |
| 43 | 'permission_callback' => '__return_true' |
| 44 | ) ); |
| 45 | register_rest_route( $this->namespace, '/images', array( |
| 46 | 'methods' => 'POST', |
| 47 | 'callback' => array( $this, 'rest_images' ), |
| 48 | 'permission_callback' => '__return_true' |
| 49 | ) ); |
| 50 | } |
| 51 | |
| 52 | public function basics_security_check( $params ) { |
| 53 | if ( empty( $params['newMessage'] ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | if ( empty( $params['chatId'] ) ) { |
| 57 | return false; |
| 58 | } |
| 59 | $length = strlen( trim( $params['newMessage'] ) ); |
| 60 | if ( $length < 1 || $length > ( 4096 - 512 ) ) { |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | public function rest_chat( $request ) { |
| 67 | try { |
| 68 | $params = $request->get_json_params(); |
| 69 | if ( !$this->basics_security_check( $params )) { |
| 70 | return new WP_REST_Response( [ |
| 71 | 'success' => false, |
| 72 | 'message' => 'Sorry, your query has been rejected.' ], 403 |
| 73 | ); |
| 74 | } |
| 75 | $chatbot = $this->core->getChatbot( $params['chatId'] ); |
| 76 | if ( !$chatbot ) { |
| 77 | return new WP_REST_Response( [ |
| 78 | 'success' => false, |
| 79 | 'message' => 'Sorry, your query has been rejected.' ], 403 |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | // Create the QueryText |
| 84 | $query = new Meow_MWAI_QueryText( $params['newMessage'], 1024 ); |
| 85 | $query->setIsChat( true ); |
| 86 | |
| 87 | // Take care of the parameters |
| 88 | $newParams = []; |
| 89 | foreach ( $chatbot as $key => $value ) { |
| 90 | $newParams[$key] = $value; |
| 91 | } |
| 92 | foreach ( $params as $key => $value ) { |
| 93 | $newParams[$key] = $value; |
| 94 | } |
| 95 | $params = apply_filters( 'mwai_chatbot_params', $newParams ); |
| 96 | $query->injectParams( $params ); |
| 97 | |
| 98 | $takeoverAnswer = apply_filters( 'mwai_chatbot_takeover', null, $query, $params ); |
| 99 | if ( !empty( $takeoverAnswer ) ) { |
| 100 | return new WP_REST_Response( [ 'success' => true, 'answer' => $takeoverAnswer, |
| 101 | 'html' => $takeoverAnswer, 'usage' => null ], 200 ); |
| 102 | } |
| 103 | |
| 104 | // Moderation |
| 105 | if ( $this->core->get_option( 'shortcode_chat_moderation' ) ) { |
| 106 | global $mwai; |
| 107 | $isFlagged = $mwai->moderationCheck( $query->prompt ); |
| 108 | if ( $isFlagged ) { |
| 109 | return new WP_REST_Response( [ |
| 110 | 'success' => false, |
| 111 | 'message' => 'Sorry, your message has been rejected by moderation.' ], 403 |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Awareness & Embeddings |
| 117 | $context = null; |
| 118 | $embeddingsIndex = $params['embeddingsIndex']; |
| 119 | if ( $query->mode === 'chat' && !empty( $embeddingsIndex ) ) { |
| 120 | $context = apply_filters( 'mwai_context_search', $query, $embeddingsIndex ); |
| 121 | if ( !empty( $context ) ) { |
| 122 | $query->injectContext( $context['content'] ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Query the AI |
| 127 | $answer = $this->core->ai->run( $query ); |
| 128 | $rawText = $answer->result; |
| 129 | $extra = []; |
| 130 | if ( $context ) { |
| 131 | $extra = [ 'embeddings' => $context['embeddings'] ]; |
| 132 | } |
| 133 | $html = apply_filters( 'mwai_chatbot_reply', $rawText, $query, $params, $extra ); |
| 134 | if ( $this->core->get_option( 'shortcode_chat_formatting' ) ) { |
| 135 | $html = $this->core->markdown_to_html( $html ); |
| 136 | } |
| 137 | return new WP_REST_Response( [ 'success' => true, 'answer' => $rawText, |
| 138 | 'html' => $html, 'usage' => $answer->usage ], 200 ); |
| 139 | } |
| 140 | catch ( Exception $e ) { |
| 141 | return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public function rest_images( $request ) { |
| 146 | try { |
| 147 | $params = $request->get_json_params(); |
| 148 | $query = new Meow_MWAI_QueryImage( $params['prompt'] ); |
| 149 | $query->injectParams( $params ); |
| 150 | $answer = $this->core->ai->run( $query ); |
| 151 | return new WP_REST_Response( [ 'success' => true, 'images' => $answer->results, 'usage' => $answer->usage ], 200 ); |
| 152 | } |
| 153 | catch ( Exception $e ) { |
| 154 | return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public function inject_chat() { |
| 159 | $params = $this->core->getChatbot( $this->siteWideChatId ); |
| 160 | if ( !empty( $params ) ) { |
| 161 | $params['window'] = true; |
| 162 | $params['id'] = $this->siteWideChatId; |
| 163 | echo $this->chat( $params ); |
| 164 | } |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | public function imageschat( $atts ) { |
| 169 | $atts['mode'] = 'images'; |
| 170 | return $this->chat( $atts ); |
| 171 | } |
| 172 | |
| 173 | public function chat( $atts ) { |
| 174 | $chatId = isset( $atts['id'] ) ? $atts['id'] : 'default'; |
| 175 | $chatbot = $this->core->getChatbot( $chatId ); |
| 176 | if ( !$chatbot ) { |
| 177 | return "Chatbot not found."; |
| 178 | } |
| 179 | |
| 180 | $frontParams = []; |
| 181 | foreach ( MWAI_CHATBOT_FRONT_PARAMS as $param ) { |
| 182 | if ( isset( $atts[$param] ) ) { |
| 183 | $frontParams[$param] = $atts[$param]; |
| 184 | } |
| 185 | else if ( isset( $chatbot[$param] ) ) { |
| 186 | $frontParams[$param] = $chatbot[$param]; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | $frontSystem = [ |
| 191 | 'chatId' => $chatId, |
| 192 | 'userData' => $this->core->getUserData(), |
| 193 | 'sessionId' => $this->core->get_session_id(), |
| 194 | 'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 195 | 'contextId' => get_the_ID(), |
| 196 | 'pluginUrl' => MWAI_URL, |
| 197 | 'restUrl' => untrailingslashit( rest_url() ), |
| 198 | 'debugMode' => $this->core->get_option( 'debug_mode' ), |
| 199 | 'typewriter' => $this->core->get_option( 'shortcode_chat_typewriter' ) |
| 200 | ]; |
| 201 | |
| 202 | $theme = isset( $frontParams['themeId'] ) ? $this->core->getTheme( $frontParams['themeId'] ) : null; |
| 203 | $jsonFrontParams = htmlspecialchars(json_encode($frontParams), ENT_QUOTES, 'UTF-8'); |
| 204 | $jsonFrontSystem = htmlspecialchars(json_encode($frontSystem), ENT_QUOTES, 'UTF-8'); |
| 205 | $jsonFrontTheme = htmlspecialchars(json_encode($theme), ENT_QUOTES, 'UTF-8'); |
| 206 | |
| 207 | $this->registerChatbotScripts(); |
| 208 | return "<div class='mwai-chatbot-container' data-params='{$jsonFrontParams}' data-system='{$jsonFrontSystem}' data-theme='{$jsonFrontTheme}'></div>"; |
| 209 | } |
| 210 | |
| 211 | } |
| 212 |