core = $core; add_shortcode( 'mwai_chat', array( $this, 'chat' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); // If we apply ChatGPT styles add_filter( 'mwai_chat_html', array( $this, 'chatgpt_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 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']; $temperature = $params['temperature']; $query = new Meow_MWAI_Query( $prompt, 1024 ); if ( $model ) { $query->setModel( $model ); } if ( $temperature ) { $query->setTemperature( $temperature ); } $answer = $this->core->ai->run( $query ); return new WP_REST_Response([ 'success' => true, 'data' => $answer->result, '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', [ 'id' => uniqid(), 'context' => "Converse as if you were an AI assistant. Be friendly, creative.", 'ai_name' => "AI: ", 'user_name' => "You: ", 'sys_name' => "System: ", 'start_sentence' => "Hi! How can I help you?", 'model' => 'text-davinci-003', 'temperature' => 0.8 ] ); $atts = shortcode_atts( $defaults, $atts, 'meow_ai' ); $id = $atts['id']; $apiUrl = get_rest_url( null, 'ai-engine/v1/chat' ); $onSentClickFn = "mwai_{$id}_onSendClick"; $addReplyFn = "mwai_{$id}_addReply"; $convertToHtmlFn = "mwai_{$id}_convertToHtml"; $aiName = trim($atts['ai_name']); $userName = trim($atts['user_name']); ob_start(); ?>