modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
core.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | require_once( MWAI_PATH . '/vendor/autoload.php' ); |
| 4 | |
| 5 | #region Constants |
| 6 | |
| 7 | // Price as of January 2023: https://openai.com/api/pricing/ |
| 8 | define( 'MWAI_OPENAI_PRICING', [ |
| 9 | // Base models: |
| 10 | [ "model" => "davinci", "price" => 0.02, "type" => "token", "unit" => 1 / 1000 ], |
| 11 | [ "model" => "curie", "price" => 0.002, "type" => "token", "unit" => 1 / 1000 ], |
| 12 | [ "model" => "babbage", "price" => 0.0005, "type" => "token", "unit" => 1 / 1000 ], |
| 13 | [ "model" => "ada", "price" => 0.0004, "type" => "token", "unit" => 1 / 1000 ], |
| 14 | // Image models: |
| 15 | [ "model" => "dall-e", "type" => "image", "unit" => 1, "options" => [ |
| 16 | [ "option" => "1024x1024", "price" => 0.02 ], |
| 17 | [ "option" => "512x512", "price" => 0.018 ], |
| 18 | [ "option" => "256x256", "price" => 0.016 ] |
| 19 | ], |
| 20 | ], |
| 21 | // Fine-tuned models: |
| 22 | [ "model" => "fn-davinci", "price" => 0.12, "type" => "token", "unit" => 1 / 1000 ], |
| 23 | [ "model" => "fn-curie", "price" => 0.012, "type" => "token", "unit" => 1 / 1000 ], |
| 24 | [ "model" => "fn-babbage", "price" => 0.0024, "type" => "token", "unit" => 1 / 1000 ], |
| 25 | [ "model" => "fn-ada", "price" => 0.0016, "type" => "token", "unit" => 1 / 1000 ], |
| 26 | ]); |
| 27 | |
| 28 | define( 'MWAI_CHATBOT_PARAMS', [ |
| 29 | // UI Parameters |
| 30 | 'id' => '', |
| 31 | 'env' => 'chatbot', |
| 32 | 'mode' => 'chat', |
| 33 | 'context' => "Converse as if you were an AI assistant. Be friendly, creative.", |
| 34 | 'ai_name' => "AI: ", |
| 35 | 'user_name' => "User: ", |
| 36 | 'sys_name' => "System: ", |
| 37 | 'start_sentence' => "Hi! How can I help you?", |
| 38 | 'text_send' => 'Send', |
| 39 | 'text_clear' => 'Clear', |
| 40 | 'text_input_placeholder' => 'Type your message...', |
| 41 | 'style' => 'chatgpt', |
| 42 | 'window' => false, |
| 43 | 'icon' => null, |
| 44 | 'icon_position' => 'bottom-right', |
| 45 | 'fullscreen' => false, |
| 46 | // Chatbot System Parameters |
| 47 | 'casually_fine_tuned' => false, |
| 48 | 'content_aware' => false, |
| 49 | 'prompt_ending' => null, |
| 50 | 'completion_ending' => null, |
| 51 | // AI Parameters |
| 52 | 'model' => 'text-davinci-003', |
| 53 | 'temperature' => 0.8, |
| 54 | 'max_tokens' => 1024, |
| 55 | 'max_results' => 3, |
| 56 | 'api_key' => null |
| 57 | ] ); |
| 58 | |
| 59 | define( 'MWAI_LANGUAGES', [ |
| 60 | 'en' => 'English', |
| 61 | 'es' => 'Spanish', |
| 62 | 'fr' => 'French', |
| 63 | 'de' => 'German', |
| 64 | 'it' => 'Italian', |
| 65 | 'pt' => 'Portuguese', |
| 66 | 'ru' => 'Russian', |
| 67 | 'ja' => 'Japanese', |
| 68 | 'zh' => 'Chinese', |
| 69 | ] ); |
| 70 | |
| 71 | define( 'MWAI_OPTIONS', [ |
| 72 | 'module_titles' => true, |
| 73 | 'module_excerpts' => true, |
| 74 | 'module_woocommerce' => true, |
| 75 | 'module_forms' => false, |
| 76 | 'module_blocks' => false, |
| 77 | 'module_statistics' => false, |
| 78 | 'shortcode_chat' => true, |
| 79 | 'shortcode_chat_params' => MWAI_CHATBOT_PARAMS, |
| 80 | 'shortcode_chat_params_override' => false, |
| 81 | 'shortcode_chat_html' => true, |
| 82 | 'shortcode_chat_formatting' => true, |
| 83 | 'shortcode_chat_syntax_highlighting' => false, |
| 84 | 'shortcode_chat_inject' => false, |
| 85 | 'openai_apikey' => false, |
| 86 | 'openai_usage' => [], |
| 87 | 'openai_finetunes' => [], |
| 88 | 'openai_finetunes_deleted' => [], |
| 89 | 'extra_models' => "", |
| 90 | 'languages' => MWAI_LANGUAGES |
| 91 | ]); |
| 92 | #endregion |
| 93 | |
| 94 | class Meow_MWAI_Core |
| 95 | { |
| 96 | public $admin = null; |
| 97 | public $is_rest = false; |
| 98 | public $is_cli = false; |
| 99 | public $site_url = null; |
| 100 | public $ai = null; |
| 101 | private $option_name = 'mwai_options'; |
| 102 | public $defaultChatbotParams = MWAI_CHATBOT_PARAMS; |
| 103 | |
| 104 | public function __construct() { |
| 105 | $this->site_url = get_site_url(); |
| 106 | $this->is_rest = MeowCommon_Helpers::is_rest(); |
| 107 | $this->is_cli = defined( 'WP_CLI' ) && WP_CLI; |
| 108 | $this->ai = new Meow_MWAI_AI( $this ); |
| 109 | add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 110 | } |
| 111 | |
| 112 | function init() { |
| 113 | if ( $this->is_rest ) { |
| 114 | new Meow_MWAI_Rest( $this ); |
| 115 | } |
| 116 | if ( is_admin() ) { |
| 117 | new Meow_MWAI_Admin( $this ); |
| 118 | new Meow_MWAI_Modules_Assistants( $this ); |
| 119 | } |
| 120 | else { |
| 121 | //new Meow_MWAI_UI( $this ); |
| 122 | if ( $this->get_option( 'shortcode_chat' ) ) { |
| 123 | new Meow_MWAI_Modules_Chatbot(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Advanced core |
| 128 | if ( class_exists( 'MeowPro_MWAI_Core' ) ) { |
| 129 | new MeowPro_MWAI_Core( $this ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #region Helpers |
| 134 | function can_access_settings() { |
| 135 | return apply_filters( 'mwai_allow_setup', current_user_can( 'manage_options' ) ); |
| 136 | } |
| 137 | |
| 138 | function can_access_features() { |
| 139 | $editor_or_admin = current_user_can( 'editor' ) || current_user_can( 'administrator' ); |
| 140 | return apply_filters( 'mwai_allow_usage', $editor_or_admin ); |
| 141 | } |
| 142 | |
| 143 | function isUrl( $url ) { |
| 144 | return strpos( $url, 'http' ) === 0 ? true : false; |
| 145 | } |
| 146 | |
| 147 | function get_text_from_postId( $postId ) { |
| 148 | $post = get_post( $postId ); |
| 149 | if ( !$post ) { |
| 150 | return false; |
| 151 | } |
| 152 | $post->post_content = apply_filters( 'the_content', $post->post_content ); |
| 153 | $text = strip_tags( $post->post_content ); |
| 154 | $text = preg_replace( '/^\h*\v+/m', '', $text ); |
| 155 | $text = html_entity_decode( $text ); |
| 156 | return $text; |
| 157 | } |
| 158 | |
| 159 | function get_session_id() { |
| 160 | if ( !isset( $_SESSION ) ) { |
| 161 | error_log("AI Engine: There is no session."); |
| 162 | return uniqid(); |
| 163 | } |
| 164 | if ( isset( $_SESSION['mwai_session_id'] ) ) { |
| 165 | return $_SESSION['mwai_session_id']; |
| 166 | } |
| 167 | else { |
| 168 | $session_id = uniqid(); |
| 169 | $_SESSION['mwai_session_id'] = $session_id; |
| 170 | return $session_id; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function markdown_to_html( $content ) { |
| 175 | $Parsedown = new Parsedown(); |
| 176 | $content = $Parsedown->text( $content ); |
| 177 | return $content; |
| 178 | } |
| 179 | #endregion |
| 180 | |
| 181 | #region Options |
| 182 | function get_all_options() { |
| 183 | $options = get_option( $this->option_name, null ); |
| 184 | foreach ( MWAI_OPTIONS as $key => $value ) { |
| 185 | if ( !isset( $options[$key] ) ) { |
| 186 | $options[$key] = $value; |
| 187 | } |
| 188 | if ( $key === 'languages' ) { |
| 189 | $options[$key] = apply_filters( 'mwai_languages', $options[$key] ); |
| 190 | } |
| 191 | } |
| 192 | $options['shortcode_chat_default_params'] = MWAI_CHATBOT_PARAMS; |
| 193 | return $options; |
| 194 | } |
| 195 | |
| 196 | // Validate and keep the options clean and logical. |
| 197 | function sanitize_options() { |
| 198 | $options = $this->get_all_options(); |
| 199 | $needs_update = false; |
| 200 | |
| 201 | // We can sanitize our future options here, let's always remember it. |
| 202 | // Now, it is empty... |
| 203 | |
| 204 | if ( $needs_update ) { |
| 205 | update_option( $this->option_name, $options, false ); |
| 206 | } |
| 207 | return $options; |
| 208 | } |
| 209 | |
| 210 | function update_options( $options ) { |
| 211 | if ( !update_option( $this->option_name, $options, false ) ) { |
| 212 | return false; |
| 213 | } |
| 214 | $options = $this->sanitize_options(); |
| 215 | return $options; |
| 216 | } |
| 217 | |
| 218 | function update_option( $option, $value ) { |
| 219 | $options = $this->get_all_options(); |
| 220 | $options[$option] = $value; |
| 221 | return $this->update_options( $options ); |
| 222 | } |
| 223 | |
| 224 | function get_option( $option, $default = null ) { |
| 225 | $options = $this->get_all_options(); |
| 226 | return $options[$option] ?? $default; |
| 227 | } |
| 228 | #endregion |
| 229 | } |
| 230 | |
| 231 | ?> |