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.1.6 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / core.php
ai-engine / classes Last commit date
modules 3 years ago admin.php 3 years ago ai.php 3 years ago answer.php 3 years ago api.php 3 years ago core.php 3 years ago init.php 3 years ago openai.php 3 years ago query.php 3 years ago queryembed.php 3 years ago queryimage.php 3 years ago querytext.php 3 years ago querytranscribe.php 3 years ago rest.php 3 years ago ui.php 3 years ago
core.php
526 lines
1 <?php
2
3 require_once( MWAI_PATH . '/vendor/autoload.php' );
4 require_once( MWAI_PATH . '/constants/init.php' );
5
6 use Rahul900day\Gpt3Encoder\Encoder;
7
8 class Meow_MWAI_Core
9 {
10 public $admin = null;
11 public $is_rest = false;
12 public $is_cli = false;
13 public $site_url = null;
14 public $ai = null;
15 private $option_name = 'mwai_options';
16 public $defaultChatbotParams = MWAI_CHATBOT_PARAMS;
17
18 public function __construct() {
19 $this->site_url = get_site_url();
20 $this->is_rest = MeowCommon_Helpers::is_rest();
21 $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
22 $this->ai = new Meow_MWAI_AI( $this );
23 add_action( 'plugins_loaded', array( $this, 'init' ) );
24 }
25
26 function init() {
27 global $mwai;
28 $mwai = new Meow_MWAI_API();
29 if ( $this->is_rest ) {
30 new Meow_MWAI_Rest( $this );
31 }
32 if ( is_admin() ) {
33 new Meow_MWAI_Admin( $this );
34 new Meow_MWAI_Modules_Assistants( $this );
35 }
36 else {
37 //new Meow_MWAI_UI( $this );
38 if ( $this->get_option( 'shortcode_chat' ) ) {
39 new Meow_MWAI_Modules_Chatbot();
40 }
41 }
42
43 // Advanced core
44 if ( class_exists( 'MeowPro_MWAI_Core' ) ) {
45 new MeowPro_MWAI_Core( $this );
46 }
47
48 // Dynamic max tokens
49 if ( $this->get_option( 'dynamic_max_tokens' ) ) {
50 add_filter( 'mwai_estimate_tokens', array( $this, 'dynamic_max_tokens' ), 10, 2 );
51 }
52 }
53
54 #region Roles & Capabilities
55
56 function can_access_settings() {
57 return apply_filters( 'mwai_allow_setup', current_user_can( 'manage_options' ) );
58 }
59
60 function can_access_features() {
61 $editor_or_admin = current_user_can( 'editor' ) || current_user_can( 'administrator' );
62 return apply_filters( 'mwai_allow_usage', $editor_or_admin );
63 }
64
65 #endregion
66
67 #region Text-Related Helpers
68
69 // Clean the text perfectly, resolve shortcodes, etc, etc.
70 function clean_text( $rawText = "" ) {
71 $text = strip_tags( $rawText );
72 $text = strip_shortcodes( $text );
73 $text = html_entity_decode( $text );
74 $text = str_replace( array( "\r", "\n" ), "", $text );
75 $sentences = preg_split( '/(?<=[.?!])(?=[a-zA-Z ])/', $text );
76 foreach ( $sentences as $key => $sentence ) {
77 $sentences[$key] = trim( $sentence );
78 }
79 $text = implode( " ", $sentences );
80 $text = preg_replace( '/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text );
81 return $text . " ";
82 }
83
84 // Make sure there are no duplicate sentences, and keep the length under a maximum length.
85 function clean_sentences( $text, $maxLength = 1024 ) {
86 $sentences = preg_split( '/(?<=[.?!])(?=[a-zA-Z ])/', $text );
87 $hashes = array();
88 $uniqueSentences = array();
89 $length = 0;
90 foreach ( $sentences as $sentence ) {
91 $sentence = preg_replace( '/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $sentence );
92 $hash = md5( $sentence );
93 if ( !in_array( $hash, $hashes ) ) {
94 if ( $length + strlen( $sentence ) > $maxLength ) {
95 continue;
96 }
97 $hashes[] = $hash;
98 $uniqueSentences[] = $sentence;
99 $length += strlen( $sentence );
100 }
101 }
102 $text = implode( " ", $uniqueSentences );
103 $text = preg_replace( '/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text );
104 return $text;
105 }
106
107 function get_text_from_postId( $postId ) {
108 $post = get_post( $postId );
109 if ( !$post ) {
110 return false;
111 }
112 $post->post_content = apply_filters( 'the_content', $post->post_content );
113 $text = $this->clean_text( $post->post_content );
114 $text = $this->clean_sentences( $text );
115 return $text;
116 }
117
118 function markdown_to_html( $content ) {
119 $Parsedown = new Parsedown();
120 $content = $Parsedown->text( $content );
121 return $content;
122 }
123
124 function get_post_language( $postId ) {
125 $locale = get_locale();
126 $code = strtolower( substr( $locale, 0, 2 ) );
127 $lang_codes = array(
128 'aa' => 'Afar',
129 'ab' => 'Abkhazian',
130 'af' => 'Afrikaans',
131 'ak' => 'Akan',
132 'sq' => 'Albanian',
133 'am' => 'Amharic',
134 'ar' => 'Arabic',
135 'an' => 'Aragonese',
136 'hy' => 'Armenian',
137 'as' => 'Assamese',
138 'av' => 'Avaric',
139 'ae' => 'Avestan',
140 'ay' => 'Aymara',
141 'az' => 'Azerbaijani',
142 'ba' => 'Bashkir',
143 'bm' => 'Bambara',
144 'eu' => 'Basque',
145 'be' => 'Belarusian',
146 'bn' => 'Bengali',
147 'bh' => 'Bihari',
148 'bi' => 'Bislama',
149 'bs' => 'Bosnian',
150 'br' => 'Breton',
151 'bg' => 'Bulgarian',
152 'my' => 'Burmese',
153 'ca' => 'Catalan; Valencian',
154 'ch' => 'Chamorro',
155 'ce' => 'Chechen',
156 'zh' => 'Chinese',
157 'cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic',
158 'cv' => 'Chuvash',
159 'kw' => 'Cornish',
160 'co' => 'Corsican',
161 'cr' => 'Cree',
162 'cs' => 'Czech',
163 'da' => 'Danish',
164 'dv' => 'Divehi; Dhivehi; Maldivian',
165 'nl' => 'Dutch; Flemish',
166 'dz' => 'Dzongkha',
167 'en' => 'English',
168 'eo' => 'Esperanto',
169 'et' => 'Estonian',
170 'ee' => 'Ewe',
171 'fo' => 'Faroese',
172 'fj' => 'Fijjian',
173 'fi' => 'Finnish',
174 'fr' => 'French',
175 'fy' => 'Western Frisian',
176 'ff' => 'Fulah',
177 'ka' => 'Georgian',
178 'de' => 'German',
179 'gd' => 'Gaelic; Scottish Gaelic',
180 'ga' => 'Irish',
181 'gl' => 'Galician',
182 'gv' => 'Manx',
183 'el' => 'Greek, Modern',
184 'gn' => 'Guarani',
185 'gu' => 'Gujarati',
186 'ht' => 'Haitian; Haitian Creole',
187 'ha' => 'Hausa',
188 'he' => 'Hebrew',
189 'hz' => 'Herero',
190 'hi' => 'Hindi',
191 'ho' => 'Hiri Motu',
192 'hu' => 'Hungarian',
193 'ig' => 'Igbo',
194 'is' => 'Icelandic',
195 'io' => 'Ido',
196 'ii' => 'Sichuan Yi',
197 'iu' => 'Inuktitut',
198 'ie' => 'Interlingue',
199 'ia' => 'Interlingua (International Auxiliary Language Association)',
200 'id' => 'Indonesian',
201 'ik' => 'Inupiaq',
202 'it' => 'Italian',
203 'jv' => 'Javanese',
204 'ja' => 'Japanese',
205 'kl' => 'Kalaallisut; Greenlandic',
206 'kn' => 'Kannada',
207 'ks' => 'Kashmiri',
208 'kr' => 'Kanuri',
209 'kk' => 'Kazakh',
210 'km' => 'Central Khmer',
211 'ki' => 'Kikuyu; Gikuyu',
212 'rw' => 'Kinyarwanda',
213 'ky' => 'Kirghiz; Kyrgyz',
214 'kv' => 'Komi',
215 'kg' => 'Kongo',
216 'ko' => 'Korean',
217 'kj' => 'Kuanyama; Kwanyama',
218 'ku' => 'Kurdish',
219 'lo' => 'Lao',
220 'la' => 'Latin',
221 'lv' => 'Latvian',
222 'li' => 'Limburgan; Limburger; Limburgish',
223 'ln' => 'Lingala',
224 'lt' => 'Lithuanian',
225 'lb' => 'Luxembourgish; Letzeburgesch',
226 'lu' => 'Luba-Katanga',
227 'lg' => 'Ganda',
228 'mk' => 'Macedonian',
229 'mh' => 'Marshallese',
230 'ml' => 'Malayalam',
231 'mi' => 'Maori',
232 'mr' => 'Marathi',
233 'ms' => 'Malay',
234 'mg' => 'Malagasy',
235 'mt' => 'Maltese',
236 'mo' => 'Moldavian',
237 'mn' => 'Mongolian',
238 'na' => 'Nauru',
239 'nv' => 'Navajo; Navaho',
240 'nr' => 'Ndebele, South; South Ndebele',
241 'nd' => 'Ndebele, North; North Ndebele',
242 'ng' => 'Ndonga',
243 'ne' => 'Nepali',
244 'nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian',
245 'nb' => 'Bokmål, Norwegian, Norwegian Bokmål',
246 'no' => 'Norwegian',
247 'ny' => 'Chichewa; Chewa; Nyanja',
248 'oc' => 'Occitan, Provençal',
249 'oj' => 'Ojibwa',
250 'or' => 'Oriya',
251 'om' => 'Oromo',
252 'os' => 'Ossetian; Ossetic',
253 'pa' => 'Panjabi; Punjabi',
254 'fa' => 'Persian',
255 'pi' => 'Pali',
256 'pl' => 'Polish',
257 'pt' => 'Portuguese',
258 'ps' => 'Pushto',
259 'qu' => 'Quechua',
260 'rm' => 'Romansh',
261 'ro' => 'Romanian',
262 'rn' => 'Rundi',
263 'ru' => 'Russian',
264 'sg' => 'Sango',
265 'sa' => 'Sanskrit',
266 'sr' => 'Serbian',
267 'hr' => 'Croatian',
268 'si' => 'Sinhala; Sinhalese',
269 'sk' => 'Slovak',
270 'sl' => 'Slovenian',
271 'se' => 'Northern Sami',
272 'sm' => 'Samoan',
273 'sn' => 'Shona',
274 'sd' => 'Sindhi',
275 'so' => 'Somali',
276 'st' => 'Sotho, Southern',
277 'es' => 'Spanish; Castilian',
278 'sc' => 'Sardinian',
279 'ss' => 'Swati',
280 'su' => 'Sundanese',
281 'sw' => 'Swahili',
282 'sv' => 'Swedish',
283 'ty' => 'Tahitian',
284 'ta' => 'Tamil',
285 'tt' => 'Tatar',
286 'te' => 'Telugu',
287 'tg' => 'Tajik',
288 'tl' => 'Tagalog',
289 'th' => 'Thai',
290 'bo' => 'Tibetan',
291 'ti' => 'Tigrinya',
292 'to' => 'Tonga (Tonga Islands)',
293 'tn' => 'Tswana',
294 'ts' => 'Tsonga',
295 'tk' => 'Turkmen',
296 'tr' => 'Turkish',
297 'tw' => 'Twi',
298 'ug' => 'Uighur; Uyghur',
299 'uk' => 'Ukrainian',
300 'ur' => 'Urdu',
301 'uz' => 'Uzbek',
302 've' => 'Venda',
303 'vi' => 'Vietnamese',
304 'vo' => 'Volapük',
305 'cy' => 'Welsh',
306 'wa' => 'Walloon',
307 'wo' => 'Wolof',
308 'xh' => 'Xhosa',
309 'yi' => 'Yiddish',
310 'yo' => 'Yoruba',
311 'za' => 'Zhuang; Chuang',
312 'zu' => 'Zulu',
313 );
314 $language = strtr( $code, $lang_codes );
315 $lang = apply_filters( 'wpml_post_language_details', null, $postId );
316 if ( !empty( $lang ) ) {
317 $language = $lang['display_name'];
318 }
319 return strtolower( $language );
320 }
321 #endregion
322
323 #region Users/Sessions Helpers
324
325 function get_session_id() {
326 if ( isset( $_COOKIE['mwai_session_id'] ) ) {
327 return $_COOKIE['mwai_session_id'];
328 }
329 return "N/A";
330 }
331
332 // Get the UserID from the data, or from the current user
333 function get_user_id( $data = null ) {
334 if ( isset( $data ) && isset( $data['userId'] ) ) {
335 return (int)$data['userId'];
336 }
337 if ( is_user_logged_in() ) {
338 $current_user = wp_get_current_user();
339 if ( $current_user->ID > 0 ) {
340 return $current_user->ID;
341 }
342 }
343 return null;
344 }
345
346 function get_ip_address( $data = null ) {
347 if ( isset( $data ) && isset( $data['ip'] ) ) {
348 $data['ip'] = (string)$data['ip'];
349 }
350 else {
351 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
352 $data['ip'] = sanitize_text_field( $_SERVER['REMOTE_ADDR'] );
353 }
354 else if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
355 $data['ip'] = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] );
356 }
357 else if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
358 $data['ip'] = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] );
359 }
360 }
361 return $data['ip'];
362 }
363
364 #endregion
365
366 #region Other Helpers
367
368 function isUrl( $url ) {
369 return strpos( $url, 'http' ) === 0 ? true : false;
370 }
371
372 #endregion
373
374 #region Usage & Costs
375
376 public function dynamic_max_tokens( $tokens, $text ) {
377 // Approximation (fast, no lib)
378 $asciiCount = 0;
379 $nonAsciiCount = 0;
380 for ( $i = 0; $i < mb_strlen( $text ); $i++ ) {
381 $char = mb_substr( $text, $i, 1 );
382 if ( ord( $char ) < 128 ) {
383 $asciiCount++;
384 }
385 else {
386 $nonAsciiCount++;
387 }
388 }
389 $asciiTokens = $asciiCount / 3.5;
390 $nonAsciiTokens = $nonAsciiCount * 2.5;
391 $tokens = $asciiTokens + $nonAsciiTokens;
392
393 // More exact (slower, and lib)
394 if ( PHP_VERSION_ID >= 70400 ) {
395 try {
396 $token_array = Encoder::encode( $text );
397 if ( !empty( $token_array ) ) {
398 $tokens = count( $token_array );
399 }
400 }
401 catch ( Exception $e ) {
402 error_log( $e->getMessage() );
403 }
404 }
405
406 $tokens = $tokens;
407 return (int)$tokens;
408 }
409
410 public function record_tokens_usage( $model, $prompt_tokens, $completion_tokens = 0 ) {
411 if ( !is_numeric( $prompt_tokens ) ) {
412 throw new Exception( 'Record usage: prompt_tokens is not a number.' );
413 }
414 if ( !is_numeric( $completion_tokens ) ) {
415 $completion_tokens = 0;
416 }
417 if ( !$model ) {
418 throw new Exception( 'Record usage: model is missing.' );
419 }
420 $usage = $this->get_option( 'openai_usage' );
421 $month = date( 'Y-m' );
422 if ( !isset( $usage[$month] ) ) {
423 $usage[$month] = array();
424 }
425 if ( !isset( $usage[$month][$model] ) ) {
426 $usage[$month][$model] = array(
427 'prompt_tokens' => 0,
428 'completion_tokens' => 0,
429 'total_tokens' => 0
430 );
431 }
432 $usage[$month][$model]['prompt_tokens'] += $prompt_tokens;
433 $usage[$month][$model]['completion_tokens'] += $completion_tokens;
434 $usage[$month][$model]['total_tokens'] += $prompt_tokens + $completion_tokens;
435 $this->update_option( 'openai_usage', $usage );
436 return [
437 'prompt_tokens' => $prompt_tokens,
438 'completion_tokens' => $completion_tokens,
439 'total_tokens' => $prompt_tokens + $completion_tokens
440 ];
441 }
442
443 public function record_images_usage( $model, $resolution, $images ) {
444 if ( !$model || !$resolution || !$images ) {
445 throw new Exception( 'Missing parameters for record_image_usage.' );
446 }
447 $usage = $this->get_option( 'openai_usage' );
448 $month = date( 'Y-m' );
449 if ( !isset( $usage[$month] ) ) {
450 $usage[$month] = array();
451 }
452 if ( !isset( $usage[$month][$model] ) ) {
453 $usage[$month][$model] = array(
454 'resolution' => array(),
455 'images' => 0
456 );
457 }
458 if ( !isset( $usage[$month][$model]['resolution'][$resolution] ) ) {
459 $usage[$month][$model]['resolution'][$resolution] = 0;
460 }
461 $usage[$month][$model]['resolution'][$resolution] += $images;
462 $usage[$month][$model]['images'] += $images;
463 $this->update_option( 'openai_usage', $usage );
464 return [
465 'resolution' => $resolution,
466 'images' => $images
467 ];
468 }
469
470 #endregion
471
472 #region Options
473 function get_all_options() {
474 $options = get_option( $this->option_name, null );
475 foreach ( MWAI_OPTIONS as $key => $value ) {
476 if ( !isset( $options[$key] ) ) {
477 $options[$key] = $value;
478 }
479 if ( $key === 'languages' ) {
480 // TODO: If we decide to make a set of options for languages, we can keep it in the settings
481 $options[$key] = MWAI_LANGUAGES;
482 $options[$key] = apply_filters( 'mwai_languages', $options[$key] );
483 }
484 }
485 $options['shortcode_chat_default_params'] = MWAI_CHATBOT_PARAMS;
486 $options['default_limits'] = MWAI_LIMITS;
487 $options['openai_models'] = MWAI_OPENAI_MODELS;
488 return $options;
489 }
490
491 // Validate and keep the options clean and logical.
492 function sanitize_options() {
493 $options = $this->get_all_options();
494 $needs_update = false;
495
496 // We can sanitize our future options here, let's always remember it.
497 // Now, it is empty...
498
499 if ( $needs_update ) {
500 update_option( $this->option_name, $options, false );
501 }
502 return $options;
503 }
504
505 function update_options( $options ) {
506 if ( !update_option( $this->option_name, $options, false ) ) {
507 return false;
508 }
509 $options = $this->sanitize_options();
510 return $options;
511 }
512
513 function update_option( $option, $value ) {
514 $options = $this->get_all_options();
515 $options[$option] = $value;
516 return $this->update_options( $options );
517 }
518
519 function get_option( $option, $default = null ) {
520 $options = $this->get_all_options();
521 return $options[$option] ?? $default;
522 }
523 #endregion
524 }
525
526 ?>