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