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
rest.php
727 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest |
| 4 | { |
| 5 | private $core = null; |
| 6 | private $namespace = 'ai-engine/v1'; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 11 | } |
| 12 | |
| 13 | function rest_api_init() { |
| 14 | try { |
| 15 | register_rest_route( $this->namespace, '/update_option', array( |
| 16 | 'methods' => 'POST', |
| 17 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 18 | 'callback' => array( $this, 'rest_update_option' ) |
| 19 | ) ); |
| 20 | register_rest_route( $this->namespace, '/all_settings', array( |
| 21 | 'methods' => 'GET', |
| 22 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 23 | 'callback' => array( $this, 'rest_all_settings' ), |
| 24 | ) ); |
| 25 | register_rest_route( $this->namespace, '/make_completions', array( |
| 26 | 'methods' => 'POST', |
| 27 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 28 | 'callback' => array( $this, 'make_completions' ), |
| 29 | ) ); |
| 30 | register_rest_route( $this->namespace, '/make_images', array( |
| 31 | 'methods' => 'POST', |
| 32 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 33 | 'callback' => array( $this, 'make_images' ), |
| 34 | ) ); |
| 35 | register_rest_route( $this->namespace, '/make_titles', array( |
| 36 | 'methods' => 'POST', |
| 37 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 38 | 'callback' => array( $this, 'make_titles' ), |
| 39 | ) ); |
| 40 | register_rest_route( $this->namespace, '/make_excerpts', array( |
| 41 | 'methods' => 'POST', |
| 42 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 43 | 'callback' => array( $this, 'make_excerpts' ), |
| 44 | ) ); |
| 45 | register_rest_route( $this->namespace, '/update_post_title', array( |
| 46 | 'methods' => 'POST', |
| 47 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 48 | 'callback' => array( $this, 'update_post_title' ), |
| 49 | ) ); |
| 50 | register_rest_route( $this->namespace, '/update_post_excerpt', array( |
| 51 | 'methods' => 'POST', |
| 52 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 53 | 'callback' => array( $this, 'update_post_excerpt' ), |
| 54 | ) ); |
| 55 | register_rest_route( $this->namespace, '/create_post', array( |
| 56 | 'methods' => 'POST', |
| 57 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 58 | 'callback' => array( $this, 'create_post' ), |
| 59 | ) ); |
| 60 | register_rest_route( $this->namespace, '/create_image', array( |
| 61 | 'methods' => 'POST', |
| 62 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 63 | 'callback' => array( $this, 'create_image' ), |
| 64 | ) ); |
| 65 | register_rest_route( $this->namespace, '/openai_files', array( |
| 66 | 'methods' => 'GET', |
| 67 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 68 | 'callback' => array( $this, 'openai_files_get' ), |
| 69 | ) ); |
| 70 | register_rest_route( $this->namespace, '/openai_files', array( |
| 71 | 'methods' => 'DELETE', |
| 72 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 73 | 'callback' => array( $this, 'openai_files_delete' ), |
| 74 | ) ); |
| 75 | register_rest_route( $this->namespace, '/openai_files', array( |
| 76 | 'methods' => 'POST', |
| 77 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 78 | 'callback' => array( $this, 'openai_files_upload' ), |
| 79 | ) ); |
| 80 | register_rest_route( $this->namespace, '/openai_files_download', array( |
| 81 | 'methods' => 'POST', |
| 82 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 83 | 'callback' => array( $this, 'openai_files_download' ), |
| 84 | ) ); |
| 85 | register_rest_route( $this->namespace, '/openai_files_finetune', array( |
| 86 | 'methods' => 'POST', |
| 87 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 88 | 'callback' => array( $this, 'openai_files_finetune' ), |
| 89 | ) ); |
| 90 | register_rest_route( $this->namespace, '/openai_finetunes', array( |
| 91 | 'methods' => 'GET', |
| 92 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 93 | 'callback' => array( $this, 'openai_finetunes_get' ), |
| 94 | ) ); |
| 95 | register_rest_route( $this->namespace, '/openai_finetunes', array( |
| 96 | 'methods' => 'DELETE', |
| 97 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 98 | 'callback' => array( $this, 'openai_finetunes_delete' ), |
| 99 | ) ); |
| 100 | register_rest_route( $this->namespace, '/openai_incidents', array( |
| 101 | 'methods' => 'GET', |
| 102 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 103 | 'callback' => array( $this, 'openai_incidents' ), |
| 104 | ) ); |
| 105 | register_rest_route( $this->namespace, '/count_posts', array( |
| 106 | 'methods' => 'GET', |
| 107 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 108 | 'callback' => array( $this, 'count_posts' ), |
| 109 | ) ); |
| 110 | register_rest_route( $this->namespace, '/post_content', array( |
| 111 | 'methods' => 'GET', |
| 112 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 113 | 'callback' => array( $this, 'post_content' ), |
| 114 | ) ); |
| 115 | register_rest_route( $this->namespace, '/templates', array( |
| 116 | 'methods' => 'GET', |
| 117 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 118 | 'callback' => array( $this, 'templates_get' ), |
| 119 | ) ); |
| 120 | register_rest_route( $this->namespace, '/templates', array( |
| 121 | 'methods' => 'POST', |
| 122 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 123 | 'callback' => array( $this, 'templates_save' ), |
| 124 | ) ); |
| 125 | register_rest_route( $this->namespace, '/logs', array( |
| 126 | 'methods' => 'POST', |
| 127 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 128 | 'callback' => array( $this, 'get_logs' ), |
| 129 | ) ); |
| 130 | register_rest_route( $this->namespace, '/moderate', array( |
| 131 | 'methods' => 'POST', |
| 132 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 133 | 'callback' => array( $this, 'moderate' ), |
| 134 | ) ); |
| 135 | register_rest_route( $this->namespace, '/vectors', array( |
| 136 | 'methods' => 'POST', |
| 137 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 138 | 'callback' => array( $this, 'get_vectors' ), |
| 139 | ) ); |
| 140 | register_rest_route( $this->namespace, '/vector', array( |
| 141 | 'methods' => 'POST', |
| 142 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 143 | 'callback' => array( $this, 'add_vector' ), |
| 144 | ) ); |
| 145 | register_rest_route( $this->namespace, '/vectors_ref', array( |
| 146 | 'methods' => 'POST', |
| 147 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 148 | 'callback' => array( $this, 'get_vectors_ref' ), |
| 149 | ) ); |
| 150 | register_rest_route( $this->namespace, '/vector', array( |
| 151 | 'methods' => 'PUT', |
| 152 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 153 | 'callback' => array( $this, 'modify_vector' ), |
| 154 | ) ); |
| 155 | register_rest_route( $this->namespace, '/vectors', array( |
| 156 | 'methods' => 'DELETE', |
| 157 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 158 | 'callback' => array( $this, 'delete_vectors' ), |
| 159 | ) ); |
| 160 | register_rest_route( $this->namespace, '/transcribe', array( |
| 161 | 'methods' => 'POST', |
| 162 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 163 | 'callback' => array( $this, 'transcribe' ), |
| 164 | ) ); |
| 165 | } |
| 166 | catch ( Exception $e ) { |
| 167 | var_dump( $e ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function rest_all_settings() { |
| 172 | return new WP_REST_Response( [ |
| 173 | 'success' => true, |
| 174 | 'data' => $this->core->get_all_options() |
| 175 | ], 200 ); |
| 176 | } |
| 177 | |
| 178 | function rest_update_option( $request ) { |
| 179 | try { |
| 180 | $params = $request->get_json_params(); |
| 181 | $value = $params['options']; |
| 182 | $options = $this->core->update_options( $value ); |
| 183 | $success = !!$options; |
| 184 | $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' ); |
| 185 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 186 | } |
| 187 | catch ( Exception $e ) { |
| 188 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | function createValidationResult( $result = true, $message = null) { |
| 193 | $message = $message ? $message : __( 'OK', 'ai-engine' ); |
| 194 | return [ 'result' => $result, 'message' => $message ]; |
| 195 | } |
| 196 | |
| 197 | function validate_updated_option( $option_name ) { |
| 198 | $option_checkbox = get_option( 'mwai_option_checkbox', false ); |
| 199 | $option_text = get_option( 'mwai_option_text', 'Default' ); |
| 200 | if ( $option_checkbox === '' ) |
| 201 | update_option( 'mwai_option_checkbox', false ); |
| 202 | if ( $option_text === '' ) |
| 203 | update_option( 'mwai_option_text', 'Default' ); |
| 204 | return $this->createValidationResult(); |
| 205 | } |
| 206 | |
| 207 | function make_completions( $request ) { |
| 208 | try { |
| 209 | $params = $request->get_json_params(); |
| 210 | $prompt = $params['prompt']; |
| 211 | $query = new Meow_MWAI_QueryText( $prompt ); |
| 212 | $query->injectParams( $params ); |
| 213 | $answer = $this->core->ai->run( $query ); |
| 214 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->result, 'usage' => $answer->usage ], 200 ); |
| 215 | } |
| 216 | catch ( Exception $e ) { |
| 217 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | function make_images( $request ) { |
| 222 | try { |
| 223 | $params = $request->get_json_params(); |
| 224 | $prompt = $params['prompt']; |
| 225 | $query = new Meow_MWAI_QueryImage( $prompt ); |
| 226 | $query->injectParams( $params ); |
| 227 | $answer = $this->core->ai->run( $query ); |
| 228 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results, 'usage' => $answer->usage ], 200 ); |
| 229 | } |
| 230 | catch ( Exception $e ) { |
| 231 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | function make_titles( $request ) { |
| 236 | try { |
| 237 | $params = $request->get_json_params(); |
| 238 | $postId = intval( $params['postId'] ); |
| 239 | $text = $this->core->get_text_from_postId( $postId ); |
| 240 | $prompt = "Create a short SEO-friendly title for this article: " . $text; |
| 241 | $query = new Meow_MWAI_QueryText( $prompt, 128 ); |
| 242 | $query->setMaxResults( 5 ); |
| 243 | $query->setEnv( 'admin-tools' ); |
| 244 | $answer = $this->core->ai->run( $query ); |
| 245 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 ); |
| 246 | } |
| 247 | catch ( Exception $e ) { |
| 248 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | function make_excerpts( $request ) { |
| 253 | try { |
| 254 | $params = $request->get_json_params(); |
| 255 | $postId = intval( $params['postId'] ); |
| 256 | $text = $this->core->get_text_from_postId( $postId ); |
| 257 | $prompt = "Create a SEO-friendly introduction to this article, 120 to 170 characters max, no URLs: " . $text; |
| 258 | $query = new Meow_MWAI_QueryText( $prompt, 512 ); |
| 259 | $query->setMaxResults( 5 ); |
| 260 | $query->setEnv( 'admin-tools' ); |
| 261 | $answer = $this->core->ai->run( $query ); |
| 262 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 ); |
| 263 | } |
| 264 | catch ( Exception $e ) { |
| 265 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | function update_post_title( $request ) { |
| 270 | try { |
| 271 | $params = $request->get_json_params(); |
| 272 | $title = sanitize_text_field( $params['title'] ); |
| 273 | $postId = intval( $params['postId'] ); |
| 274 | $post = get_post( $postId ); |
| 275 | if ( !$post ) { |
| 276 | throw new Exception( 'There is no post with this ID.' ); |
| 277 | } |
| 278 | $post->post_title = $title; |
| 279 | //$post->post_name = sanitize_title( $title ); |
| 280 | wp_update_post( $post ); |
| 281 | return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 ); |
| 282 | } |
| 283 | catch ( Exception $e ) { |
| 284 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | function update_post_excerpt( $request ) { |
| 289 | try { |
| 290 | $params = $request->get_json_params(); |
| 291 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 292 | $postId = intval( $params['postId'] ); |
| 293 | $post = get_post( $postId ); |
| 294 | if ( !$post ) { |
| 295 | throw new Exception( 'There is no post with this ID.' ); |
| 296 | } |
| 297 | $post->post_excerpt = $excerpt; |
| 298 | wp_update_post( $post ); |
| 299 | return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 ); |
| 300 | } |
| 301 | catch ( Exception $e ) { |
| 302 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | function create_post( $request ) { |
| 307 | try { |
| 308 | $params = $request->get_json_params(); |
| 309 | $title = sanitize_text_field( $params['title'] ); |
| 310 | $content = sanitize_textarea_field( $params['content'] ); |
| 311 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 312 | $post = new stdClass(); |
| 313 | $post->post_title = $title; |
| 314 | $post->post_excerpt = $excerpt; |
| 315 | $post->post_content = $content; |
| 316 | $post->post_status = 'draft'; |
| 317 | $post->post_type = 'post'; |
| 318 | $post->post_content = $this->core->markdown_to_html( $post->post_content ); |
| 319 | $postId = wp_insert_post( $post ); |
| 320 | return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 ); |
| 321 | } |
| 322 | catch ( Exception $e ) { |
| 323 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | function image_download( $url ) { |
| 328 | $response = wp_remote_get( $url ); |
| 329 | $output = wp_remote_retrieve_body( $response ); |
| 330 | return $output; |
| 331 | } |
| 332 | |
| 333 | function create_image( $request ) { |
| 334 | try { |
| 335 | $params = $request->get_json_params(); |
| 336 | $title = sanitize_text_field( $params['title'] ); |
| 337 | $caption = sanitize_text_field( $params['caption'] ); |
| 338 | $alt = sanitize_text_field( $params['alt'] ); |
| 339 | $description = sanitize_text_field( $params['description'] ); |
| 340 | $url = $params['url']; |
| 341 | $filename = sanitize_text_field( $params['filename'] ); |
| 342 | $image_data = $this->image_download( $url ); |
| 343 | if ( !$image_data ) { |
| 344 | throw new Exception( 'Could not download the image.' ); |
| 345 | } |
| 346 | $upload_dir = wp_upload_dir(); |
| 347 | if ( empty( $filename ) ) { |
| 348 | $filename = basename( $url ); |
| 349 | } |
| 350 | $wp_filetype = wp_check_filetype( $filename ); |
| 351 | if ( wp_mkdir_p( $upload_dir['path'] ) ) { |
| 352 | $file = $upload_dir['path'] . '/' . $filename; |
| 353 | } |
| 354 | else { |
| 355 | $file = $upload_dir['basedir'] . '/' . $filename; |
| 356 | } |
| 357 | |
| 358 | // Make sure the file is unique, if not, add a number to the end of the file before the extension |
| 359 | $i = 1; |
| 360 | $parts = pathinfo( $file ); |
| 361 | while ( file_exists( $file ) ) { |
| 362 | $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension']; |
| 363 | $i++; |
| 364 | } |
| 365 | |
| 366 | // Write the file |
| 367 | file_put_contents( $file, $image_data ); |
| 368 | $attachment = [ |
| 369 | 'post_mime_type' => $wp_filetype['type'], |
| 370 | 'post_title' => $title, |
| 371 | 'post_content' => $description, |
| 372 | 'post_excerpt' => $caption, |
| 373 | 'post_status' => 'inherit' |
| 374 | ]; |
| 375 | // Register the file as a Media Library attachment |
| 376 | $attachmentId = wp_insert_attachment( $attachment, $file ); |
| 377 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 378 | $attachment_data = wp_generate_attachment_metadata( $attachmentId, $file ); |
| 379 | wp_update_attachment_metadata( $attachmentId, $attachment_data ); |
| 380 | update_post_meta( $attachmentId, '_wp_attachment_image_alt', $alt ); |
| 381 | return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 ); |
| 382 | } |
| 383 | catch ( Exception $e ) { |
| 384 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | function openai_files_get( $request ) { |
| 389 | try { |
| 390 | //$params = $request->get_json_params(); |
| 391 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 392 | $files = $openai->listFiles(); |
| 393 | return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 ); |
| 394 | } |
| 395 | catch ( Exception $e ) { |
| 396 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | function openai_finetunes_get( $request ) { |
| 401 | try { |
| 402 | $params = $request->get_query_params(); |
| 403 | $clean = isset( $params['clean'] ) ? true : false; |
| 404 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 405 | $finetunes = $openai->listFineTunes( $clean ); |
| 406 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 407 | } |
| 408 | catch ( Exception $e ) { |
| 409 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | function openai_files_upload( $request ) { |
| 414 | try { |
| 415 | $params = $request->get_json_params(); |
| 416 | $filename = sanitize_text_field( $params['filename'] ); |
| 417 | $data = $params['data']; |
| 418 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 419 | $file = $openai->uploadFile( $filename, $data ); |
| 420 | return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 ); |
| 421 | } |
| 422 | catch ( Exception $e ) { |
| 423 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | function x( $request ) { |
| 428 | try { |
| 429 | $params = $request->get_json_params(); |
| 430 | $filename = sanitize_text_field( $params['filename'] ); |
| 431 | $data = $params['data']; |
| 432 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 433 | $file = $openai->uploadFile( $filename, $data ); |
| 434 | return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 ); |
| 435 | } |
| 436 | catch ( Exception $e ) { |
| 437 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | function openai_files_delete( $request ) { |
| 442 | try { |
| 443 | $params = $request->get_json_params(); |
| 444 | $fileId = $params['fileId']; |
| 445 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 446 | $openai->deleteFile( $fileId ); |
| 447 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 448 | } |
| 449 | catch ( Exception $e ) { |
| 450 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | function openai_finetunes_delete( $request ) { |
| 455 | try { |
| 456 | $params = $request->get_json_params(); |
| 457 | $modelId = $params['modelId']; |
| 458 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 459 | $openai->deleteFineTune( $modelId ); |
| 460 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 461 | } |
| 462 | catch ( Exception $e ) { |
| 463 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | function openai_files_download( $request ) { |
| 468 | try { |
| 469 | $params = $request->get_json_params(); |
| 470 | $fileId = $params['fileId']; |
| 471 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 472 | $data = $openai->downloadFile( $fileId ); |
| 473 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 474 | } |
| 475 | catch ( Exception $e ) { |
| 476 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | function openai_files_finetune( $request ) { |
| 481 | try { |
| 482 | $params = $request->get_json_params(); |
| 483 | $fileId = $params['fileId']; |
| 484 | $model = $params['model']; |
| 485 | $suffix = $params['suffix']; |
| 486 | $hyperparams = [ |
| 487 | "nEpochs" => $params['nEpochs'], |
| 488 | "batchSize" => $params['batchSize'] |
| 489 | ]; |
| 490 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 491 | $finetune = $openai->fineTuneFile( $fileId, $model, $suffix, $hyperparams ); |
| 492 | return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 ); |
| 493 | } |
| 494 | catch ( Exception $e ) { |
| 495 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | function openai_incidents() { |
| 500 | try { |
| 501 | $transient = get_transient( 'mwai_openai_incidents' ); |
| 502 | if ( $transient ) { |
| 503 | return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 ); |
| 504 | } |
| 505 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 506 | $incidents = $openai->getIncidents(); |
| 507 | set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 ); |
| 508 | return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 ); |
| 509 | } |
| 510 | catch ( Exception $e ) { |
| 511 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | function count_posts( $request ) { |
| 516 | try { |
| 517 | $params = $request->get_query_params(); |
| 518 | $postType = $params['postType']; |
| 519 | $count = wp_count_posts( $postType ); |
| 520 | return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 ); |
| 521 | } |
| 522 | catch ( Exception $e ) { |
| 523 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | function post_content( $request ) { |
| 528 | try { |
| 529 | $params = $request->get_query_params(); |
| 530 | $offset = (int)$params['offset']; |
| 531 | $postType = $params['postType']; |
| 532 | $postId = (int)$params['postId']; |
| 533 | $post = null; |
| 534 | if ( !empty( $postId ) ) { |
| 535 | $post = get_post( $postId ); |
| 536 | if ( $post->post_status !== 'publish' && $post->post_status !== 'future' && $post->post_status !== 'draft' ) { |
| 537 | $post = null; |
| 538 | } |
| 539 | } |
| 540 | else { |
| 541 | $posts = get_posts( [ |
| 542 | 'posts_per_page' => 1, |
| 543 | 'post_type' => $postType, |
| 544 | 'offset' => $offset, |
| 545 | 'post_status' => 'publish' |
| 546 | ] ); |
| 547 | $post = count( $posts ) === 0 ? null : $posts[0]; |
| 548 | } |
| 549 | if ( !$post ) { |
| 550 | return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 ); |
| 551 | } |
| 552 | $language = $this->core->get_post_language( $post->ID ); |
| 553 | $content = apply_filters( 'the_content', $post->post_content ); |
| 554 | // REsolve html entities |
| 555 | $content = html_entity_decode( $content ); |
| 556 | $content = wp_strip_all_tags( $content ); |
| 557 | $content = preg_replace( '/[\r\n]+/', "\n", $content ); |
| 558 | // Remove all the non-characters except \n |
| 559 | $checksum = wp_hash( $content ); |
| 560 | $title = $post->post_title; |
| 561 | $excerpt = $post->post_excerpt; |
| 562 | $url = get_permalink( $post->ID ); |
| 563 | return new WP_REST_Response([ 'success' => true, 'content' => $content, 'checksum' => $checksum, |
| 564 | 'language' => $language, 'excerpt' => $excerpt, |
| 565 | 'postId' => $post->ID, 'title' => $title, 'url' => $url ], 200 ); |
| 566 | } |
| 567 | catch ( Exception $e ) { |
| 568 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | function templates_get( $request ) { |
| 573 | try { |
| 574 | $params = $request->get_query_params(); |
| 575 | $category = $params['category']; |
| 576 | $templates = []; |
| 577 | $templates_option = get_option( 'mwai_templates', [] ); |
| 578 | if ( !is_array( $templates_option ) ) { |
| 579 | update_option( 'mwai_templates', [] ); |
| 580 | } |
| 581 | $categories = array_column( $templates_option, 'category' ); |
| 582 | $index = array_search( $category, $categories ); |
| 583 | $templates = []; |
| 584 | if ( $index !== false ) { |
| 585 | $templates = $templates_option[$index]['templates']; |
| 586 | } |
| 587 | return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 ); |
| 588 | } |
| 589 | catch ( Exception $e ) { |
| 590 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | function templates_save( $request ) { |
| 595 | try { |
| 596 | $params = $request->get_json_params(); |
| 597 | $category = $params['category']; |
| 598 | $templates = $params['templates']; |
| 599 | $templates_option = get_option( 'mwai_templates', [] ); |
| 600 | $categories = array_column( $templates_option, 'category' ); |
| 601 | $index = array_search( $category, $categories ); |
| 602 | if ( $index !== false && $index >= 0 ) { |
| 603 | $templates_option[$index]['templates'] = $templates; |
| 604 | } |
| 605 | else { |
| 606 | $group = [ 'category' => $category, 'templates' => $templates ]; |
| 607 | $templates_option[] = $group; |
| 608 | } |
| 609 | |
| 610 | update_option( 'mwai_templates', $templates_option ); |
| 611 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 612 | } |
| 613 | catch ( Exception $e ) { |
| 614 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | function get_logs( $request ) { |
| 619 | try { |
| 620 | $params = $request->get_json_params(); |
| 621 | $offset = $params['offset']; |
| 622 | $limit = $params['limit']; |
| 623 | $filters = $params['filters']; |
| 624 | $sort = $params['sort']; |
| 625 | $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort ); |
| 626 | return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 ); |
| 627 | } |
| 628 | catch ( Exception $e ) { |
| 629 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | function moderate( $request ) { |
| 634 | try { |
| 635 | $params = $request->get_json_params(); |
| 636 | $text = $params['text']; |
| 637 | if ( !$text ) { |
| 638 | return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 ); |
| 639 | } |
| 640 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 641 | $results = $openai->moderate( $text ); |
| 642 | return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 ); |
| 643 | } |
| 644 | catch ( Exception $e ) { |
| 645 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 646 | } |
| 647 | |
| 648 | } |
| 649 | |
| 650 | function get_vectors( $request ) { |
| 651 | try { |
| 652 | $params = $request->get_json_params(); |
| 653 | $offset = $params['offset']; |
| 654 | $limit = $params['limit']; |
| 655 | $filters = $params['filters']; |
| 656 | $sort = $params['sort']; |
| 657 | $vectors = apply_filters( 'mwai_embeddings_vectors', [], $offset, $limit, $filters, $sort ); |
| 658 | return new WP_REST_Response([ 'success' => true, 'total' => $vectors['total'], 'vectors' => $vectors['rows'] ], 200 ); |
| 659 | } |
| 660 | catch ( Exception $e ) { |
| 661 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | function add_vector( $request ) { |
| 666 | try { |
| 667 | $params = $request->get_json_params(); |
| 668 | $vector = $params['vector']; |
| 669 | $success = apply_filters( 'mwai_embeddings_vectors_add', false, $vector ); |
| 670 | return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 ); |
| 671 | } |
| 672 | catch ( Exception $e ) { |
| 673 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | function get_vectors_ref( $request ) { |
| 678 | try { |
| 679 | $params = $request->get_json_params(); |
| 680 | $refId = $params['refId']; |
| 681 | $vectors = apply_filters( 'mwai_embeddings_vectors_ref', false, $refId ); |
| 682 | return new WP_REST_Response([ 'success' => true, 'vectors' => $vectors ], 200 ); |
| 683 | } |
| 684 | catch ( Exception $e ) { |
| 685 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | function modify_vector( $request ) { |
| 690 | try { |
| 691 | $params = $request->get_json_params(); |
| 692 | $vector = $params['vector']; |
| 693 | $success = apply_filters( 'mwai_embeddings_vectors_update', false, $vector ); |
| 694 | return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 ); |
| 695 | } |
| 696 | catch ( Exception $e ) { |
| 697 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | function delete_vectors( $request ) { |
| 702 | try { |
| 703 | $params = $request->get_json_params(); |
| 704 | $ids = $params['ids']; |
| 705 | $success = apply_filters( 'mwai_embeddings_vectors_delete', false, $ids ); |
| 706 | return new WP_REST_Response([ 'success' => $success ], 200 ); |
| 707 | } |
| 708 | catch ( Exception $e ) { |
| 709 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | function transcribe( $request ) { |
| 714 | try { |
| 715 | $params = $request->get_json_params(); |
| 716 | $query = new Meow_MWAI_QueryTranscribe(); |
| 717 | $query->injectParams( $params ); |
| 718 | $query->setEnv('admin-tools'); |
| 719 | $answer = $this->core->ai->run( $query ); |
| 720 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->result ], 200 ); |
| 721 | } |
| 722 | catch ( Exception $e ) { |
| 723 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 |