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