core = $mwai_core; add_shortcode( 'mwai_chat', array( $this, 'chat' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); // If we apply ChatGPT styles if ( $this->core->get_option( 'shortcode_chat_style' ) ) { add_filter( 'mwai_chat_html', array( $this, 'chatgpt_style' ), 10, 2 ); add_filter( 'mwai_chat_html', array( $this, 'spinner_style' ), 10, 2 ); } } 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 spinner_style( $html, $atts ) { // If needed, we can use the $id to apply styles $id = $atts['id']; // This could be replace by the ID of a specific chatbot return " " . $html; } function chatgpt_style( $html, $atts ) { // If needed, we can use the $id to apply styles $id = $atts['id']; // This could be replace by the ID of a specific chatbot return " " . $html; } function rest_chat( $request ) { try { $params = $request->get_json_params(); $prompt = $params['prompt']; $model = $params['model']; //$userName = $params['userName']; //$aiName = $params['aiName']; $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 = $this->core->markdown_to_html( $answer->result ); 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 chat( $atts ) { $defaults = apply_filters( 'mwai_chat_atts', [ // UI Parameters 'id' => uniqid(), 'context' => "Converse as if you were an AI assistant. Be friendly, creative.", 'ai_name' => "AI: ", 'user_name' => "User: ", 'sys_name' => "System: ", 'start_sentence' => "Hi! How can I help you?", 'text_send' => 'Send', 'text_input_placeholder' => 'Type your message...', // Chatbot System Parameters 'casually_fined_tuned' => 'false', 'prompt_ending' => '', 'completion_ending' => '', // AI Parameters 'model' => 'text-davinci-003', 'temperature' => 0.8, 'max_tokens' => 1024, 'api_key' => '' ] ); $atts = shortcode_atts( $defaults, $atts, 'mwai_chat_atts' ); $id = $atts['id']; $apiUrl = get_rest_url( null, 'ai-engine/v1/chat' ); // Functions $onSentClickFn = "mwai_{$id}_onSendClick"; $addReplyFn = "mwai_{$id}_addReply"; $initChatBotFn = "mwai_{$id}_initChatBot"; $convertToHtmlFn = "mwai_{$id}_convertToHtml"; // UI Parameters $aiName = addslashes( trim($atts['ai_name']) ); $userName = addslashes( trim($atts['user_name']) ); $sysName = addslashes( trim($atts['sys_name']) ); $context = addslashes( trim( $atts['context'] ) ); $textSend = addslashes( trim( $atts['text_send'] ) ); $textInputPlaceholder = addslashes( trim( $atts['text_input_placeholder'] ) ); // Chatbot System Parameters $casuallyFineTuned = $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']; $onGoingPrompt = "mwai_{$id}_onGoingPrompt"; ob_start(); ?>