engines
2 years ago
modules
2 years ago
queries
2 years ago
admin.php
2 years ago
api.php
2 years ago
core.php
2 years ago
init.php
3 years ago
reply.php
2 years ago
rest.php
2 years ago
api.php
429 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_API { |
| 4 | public $core; |
| 5 | private $chatbot_module; |
| 6 | private $discussions_module; |
| 7 | private $bearer_token; |
| 8 | |
| 9 | public function __construct( $chatbot_module, $discussions_module ) { |
| 10 | global $mwai_core; |
| 11 | $this->core = $mwai_core; |
| 12 | $this->chatbot_module = $chatbot_module; |
| 13 | $this->discussions_module = $discussions_module; |
| 14 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 15 | } |
| 16 | |
| 17 | #region REST API |
| 18 | function rest_api_init() { |
| 19 | $public_api = $this->core->get_option( 'public_api' ); |
| 20 | if ( !$public_api ) { return; } |
| 21 | $this->bearer_token = $this->core->get_option( 'public_api_bearer_token' ); |
| 22 | if ( !empty( $this->bearer_token ) ) { |
| 23 | add_filter( 'mwai_allow_public_api', [ $this, 'auth_via_bearer_token' ], 10, 3 ); |
| 24 | } |
| 25 | |
| 26 | register_rest_route( 'mwai/v1', '/simpleAuthCheck', array( |
| 27 | 'methods' => 'GET', |
| 28 | 'callback' => array( $this, 'rest_simpleAuthCheck' ), |
| 29 | 'permission_callback' => function( $request ) { |
| 30 | return $this->core->can_access_public_api( 'simpleAuthCheck', $request ); |
| 31 | }, |
| 32 | ) ); |
| 33 | register_rest_route( 'mwai/v1', '/simpleTextQuery', array( |
| 34 | 'methods' => 'POST', |
| 35 | 'callback' => array( $this, 'rest_simpleTextQuery' ), |
| 36 | 'permission_callback' => function( $request ) { |
| 37 | return $this->core->can_access_public_api( 'simpleTextQuery', $request ); |
| 38 | }, |
| 39 | ) ); |
| 40 | register_rest_route( 'mwai/v1', '/simpleImageQuery', array( |
| 41 | 'methods' => 'POST', |
| 42 | 'callback' => array( $this, 'rest_simpleImageQuery' ), |
| 43 | 'permission_callback' => function( $request ) { |
| 44 | return $this->core->can_access_public_api( 'simpleImageQuery', $request ); |
| 45 | }, |
| 46 | ) ); |
| 47 | register_rest_route( 'mwai/v1', '/simpleVisionQuery', array( |
| 48 | 'methods' => 'POST', |
| 49 | 'callback' => array( $this, 'rest_simpleVisionQuery' ), |
| 50 | 'permission_callback' => function( $request ) { |
| 51 | return $this->core->can_access_public_api( 'simpleVisionQuery', $request ); |
| 52 | }, |
| 53 | ) ); |
| 54 | register_rest_route( 'mwai/v1', '/simpleJsonQuery', array( |
| 55 | 'methods' => 'POST', |
| 56 | 'callback' => array( $this, 'rest_simpleJsonQuery' ), |
| 57 | 'permission_callback' => function( $request ) { |
| 58 | return $this->core->can_access_public_api( 'simpleJsonQuery', $request ); |
| 59 | }, |
| 60 | ) ); |
| 61 | register_rest_route( 'mwai/v1', '/moderationCheck', array( |
| 62 | 'methods' => 'POST', |
| 63 | 'callback' => array( $this, 'rest_moderationCheck' ), |
| 64 | 'permission_callback' => function( $request ) { |
| 65 | return $this->core->can_access_public_api( 'moderationCheck', $request ); |
| 66 | }, |
| 67 | ) ); |
| 68 | |
| 69 | if ( $this->chatbot_module ) { |
| 70 | register_rest_route( 'mwai/v1', '/simpleChatbotQuery', array( |
| 71 | 'methods' => 'POST', |
| 72 | 'callback' => array( $this, 'rest_simpleChatbotQuery' ), |
| 73 | 'permission_callback' => function( $request ) { |
| 74 | return $this->core->can_access_public_api( 'simpleChatbotQuery', $request ); |
| 75 | }, |
| 76 | ) ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public function rest_simpleAuthCheck( $request ) { |
| 81 | try { |
| 82 | $params = $request->get_params(); |
| 83 | $current_user = wp_get_current_user(); |
| 84 | $current_email = $current_user->user_email; |
| 85 | return new WP_REST_Response([ 'success' => true, 'data' => [ |
| 86 | 'type' => 'email', |
| 87 | 'value' => $current_email |
| 88 | ] ], 200 ); |
| 89 | } |
| 90 | catch (Exception $e) { |
| 91 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function auth_via_bearer_token( $allow, $feature, $extra ) { |
| 96 | if ( !empty( $extra ) && !empty( $extra->get_header( 'Authorization' ) ) ) { |
| 97 | $token = $extra->get_header( 'Authorization' ); |
| 98 | $token = str_replace( 'Bearer ', '', $token ); |
| 99 | if ( $token === $this->bearer_token ) { |
| 100 | // We set the current user to the first admin. |
| 101 | $admin = $this->core->get_admin_user(); |
| 102 | wp_set_current_user( $admin->ID, $admin->user_login ); |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return $allow; |
| 107 | } |
| 108 | |
| 109 | public function rest_simpleChatbotQuery( $request ) { |
| 110 | try { |
| 111 | $params = $request->get_params(); |
| 112 | $botId = isset( $params['botId'] ) ? $params['botId'] : ''; |
| 113 | $message = isset( $params['message'] ) ? $params['message'] : ''; |
| 114 | if ( empty( $message ) ) { |
| 115 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 116 | } |
| 117 | $chatId = isset( $params['chatId'] ) ? $params['chatId'] : null; |
| 118 | $params = null; |
| 119 | if ( !empty( $chatId ) ) { |
| 120 | $params = array( 'chatId' => $chatId ); |
| 121 | } |
| 122 | if ( empty( $botId ) || empty( $message ) ) { |
| 123 | throw new Exception( 'The botId and message are required.' ); |
| 124 | } |
| 125 | $reply = $this->simpleChatbotQuery( $botId, $message, $params ); |
| 126 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 127 | } |
| 128 | catch (Exception $e) { |
| 129 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | public function rest_simpleTextQuery( $request ) { |
| 135 | try { |
| 136 | $params = $request->get_params(); |
| 137 | $message = isset( $params['message'] ) ? $params['message'] : ''; |
| 138 | if ( empty( $message ) ) { |
| 139 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 140 | } |
| 141 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 142 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 143 | if ( !empty( $scope ) ) { |
| 144 | $options['scope'] = $scope; |
| 145 | } |
| 146 | if ( empty( $message ) ) { |
| 147 | throw new Exception( 'The message is required.' ); |
| 148 | } |
| 149 | $reply = $this->simpleTextQuery( $message, $options ); |
| 150 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 151 | } |
| 152 | catch (Exception $e) { |
| 153 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public function rest_simpleImageQuery( $request ) { |
| 158 | try { |
| 159 | $params = $request->get_params(); |
| 160 | $message = isset( $params['message'] ) ? $params['message'] : ''; |
| 161 | if ( empty( $message ) ) { |
| 162 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 163 | } |
| 164 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 165 | $resolution = isset( $params['resolution'] ) ? $params['resolution'] : ''; |
| 166 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 167 | if ( !empty( $scope ) ) { |
| 168 | $options['scope'] = $scope; |
| 169 | } |
| 170 | if ( empty( $message ) ) { |
| 171 | throw new Exception( 'The message is required.' ); |
| 172 | } |
| 173 | if ( !empty( $resolution ) ) { |
| 174 | $options['resolution'] = $resolution; |
| 175 | } |
| 176 | $reply = $this->simpleImageQuery( $message, $options ); |
| 177 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 178 | } |
| 179 | catch (Exception $e) { |
| 180 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function rest_simpleVisionQuery( $request ) { |
| 185 | try { |
| 186 | $params = $request->get_params(); |
| 187 | $message = isset( $params['message'] ) ? $params['message'] : ''; |
| 188 | if ( empty( $message ) ) { |
| 189 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 190 | } |
| 191 | $url = isset( $params['url'] ) ? $params['url'] : ''; |
| 192 | $path = isset( $params['path'] ) ? $params['path'] : ''; |
| 193 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 194 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 195 | if ( !empty( $scope ) ) { |
| 196 | $options['scope'] = $scope; |
| 197 | } |
| 198 | if ( empty( $message ) ) { |
| 199 | throw new Exception( 'The message is required.' ); |
| 200 | } |
| 201 | if ( empty( $url ) && empty( $path ) ) { |
| 202 | throw new Exception( 'The url or path is required.' ); |
| 203 | } |
| 204 | $reply = $this->simpleVisionQuery( $message, $url, $path, $options ); |
| 205 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 206 | } |
| 207 | catch (Exception $e) { |
| 208 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public function rest_simpleJsonQuery( $request ) { |
| 213 | try { |
| 214 | $params = $request->get_params(); |
| 215 | $message = isset( $params['message'] ) ? $params['message'] : ''; |
| 216 | if ( empty( $message ) ) { |
| 217 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 218 | } |
| 219 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 220 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 221 | if ( !empty( $scope ) ) { |
| 222 | $options['scope'] = $scope; |
| 223 | } |
| 224 | if ( empty( $message ) ) { |
| 225 | throw new Exception( 'The message is required.' ); |
| 226 | } |
| 227 | $reply = $this->simpleJsonQuery( $message, $options ); |
| 228 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 229 | } |
| 230 | catch (Exception $e) { |
| 231 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public function rest_moderationCheck( $request ) { |
| 236 | try { |
| 237 | $params = $request->get_params(); |
| 238 | $text = $params['text']; |
| 239 | $reply = $this->moderationCheck( $text ); |
| 240 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 241 | } |
| 242 | catch (Exception $e) { |
| 243 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 244 | } |
| 245 | } |
| 246 | #endregion |
| 247 | |
| 248 | #region Simple API |
| 249 | /** |
| 250 | * Executes a vision query.` |
| 251 | * |
| 252 | * @param string $message The prompt for the AI. |
| 253 | * @param string $url The URL of the image to analyze. |
| 254 | * @param string|null $path The path to the image file. If provided, the image data will be read from this file. |
| 255 | * @param array $params Additional parameters for the AI query. |
| 256 | * |
| 257 | * @return string The result of the AI query. |
| 258 | */ |
| 259 | public function simpleVisionQuery( $message, $url, $path = null, $params = [] ) { |
| 260 | global $mwai_core; |
| 261 | $ai_vision_default_env = $this->core->get_option( 'ai_vision_default_env' ); |
| 262 | $ai_vision_default_model = $this->core->get_option( 'ai_vision_default_model' ); |
| 263 | if ( empty( $ai_vision_default_model ) ) { |
| 264 | $ai_vision_default_model = MWAI_FALLBACK_MODEL_VISION; |
| 265 | } |
| 266 | $query = new Meow_MWAI_Query_Text( $message ); |
| 267 | if ( !empty( $ai_vision_default_env ) ) { |
| 268 | $query->set_env_id( $ai_vision_default_env ); |
| 269 | } |
| 270 | if ( !empty( $ai_vision_default_model ) ) { |
| 271 | $query->set_model( $ai_vision_default_model ); |
| 272 | } |
| 273 | $query->inject_params( $params ); |
| 274 | $remote_upload = $this->core->get_option( 'image_remote_upload' ); |
| 275 | $preferURL = $remote_upload === 'url'; |
| 276 | |
| 277 | if ( $preferURL && $url ) { |
| 278 | $query->set_file( $url, 'url', 'vision' ); |
| 279 | } |
| 280 | else if ( !$preferURL && !empty( $path ) ) { |
| 281 | $binary = file_get_contents( $path ); |
| 282 | if ( $binary === false ) { |
| 283 | throw new Exception( 'The file could not be read.' ); |
| 284 | } |
| 285 | $data = base64_encode( $binary ); |
| 286 | $mimeType = $this->core->get_mime_type( $path ); |
| 287 | $query->set_file( $data, 'data', 'vision', $mimeType ); |
| 288 | } |
| 289 | else if ( $url ) { |
| 290 | $query->set_file( $url, 'url', 'vision' ); |
| 291 | } |
| 292 | else if ( !empty($path ) ) { |
| 293 | $data = base64_encode( file_get_contents( $path ) ); |
| 294 | $mimeType = $this->core->get_mime_type( $path ); |
| 295 | $query->set_file( $data, 'data', 'vision', $mimeType ); |
| 296 | } |
| 297 | |
| 298 | $reply = $mwai_core->run_query( $query ); |
| 299 | return $reply->result; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Executes a chatbot query. |
| 304 | * It will use the discussion if chatId is provided in the parameters. |
| 305 | * |
| 306 | * @param string $botId The ID of the chatbot. |
| 307 | * @param string $message The prompt for the AI. |
| 308 | * @param array $params Additional parameters for the AI query. |
| 309 | * |
| 310 | * @return string The result of the AI query. |
| 311 | */ |
| 312 | public function simpleChatbotQuery( $botId, $message, $params = [] ) { |
| 313 | if ( !isset( $params['messages'] ) && isset( $params['chatId'] ) ) { |
| 314 | $discussion = $this->discussions_module->get_discussion( $botId, $params['chatId'] ); |
| 315 | if ( !empty( $discussion ) ) { |
| 316 | $params['messages'] = $discussion->messages; |
| 317 | } |
| 318 | } |
| 319 | $data = $this->chatbot_module->chat_submit( $botId, $message, null, $params ); |
| 320 | return $data['reply']; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Executes a text query. |
| 325 | * |
| 326 | * @param string $message The prompt for the AI. |
| 327 | * @param array $params Additional parameters for the AI query. |
| 328 | * |
| 329 | * @return string The result of the AI query. |
| 330 | */ |
| 331 | public function simpleTextQuery( $message, $params = [] ) { |
| 332 | global $mwai_core; |
| 333 | $query = new Meow_MWAI_Query_Text( $message ); |
| 334 | $query->inject_params( $params ); |
| 335 | $reply = $mwai_core->run_query( $query ); |
| 336 | return $reply->result; |
| 337 | } |
| 338 | |
| 339 | public function simpleImageQuery( $message, $params = [] ) { |
| 340 | global $mwai_core; |
| 341 | $query = new Meow_MWAI_Query_Image( $message ); |
| 342 | $query->inject_params( $params ); |
| 343 | $reply = $mwai_core->run_query( $query ); |
| 344 | return $reply->result; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Executes a query that will have to return a JSON result. |
| 349 | * |
| 350 | * @param string $message The prompt for the AI. |
| 351 | * @param array $params Additional parameters for the AI query. |
| 352 | * |
| 353 | * @return array The result of the AI query. |
| 354 | */ |
| 355 | public function simpleJsonQuery( $message, $url = null, $path = null, $params = [] ) { |
| 356 | if ( !empty( $url ) || !empty( $path ) ) { |
| 357 | throw new Exception( 'The url and path are not supported yet by the simpleJsonQuery.' ); |
| 358 | } |
| 359 | global $mwai_core; |
| 360 | $query = new Meow_MWAI_Query_Text( $message . "\nYour reply must be a formatted JSON." ); |
| 361 | $query->inject_params( $params ); |
| 362 | $query->set_response_format( 'json' ); |
| 363 | $ai_json_default_env = $mwai_core->get_option( 'ai_json_default_env' ); |
| 364 | $ai_json_default_model = $mwai_core->get_option( 'ai_json_default_model' ); |
| 365 | if ( !empty( $ai_json_default_env ) ) { |
| 366 | $query->set_env_id( $ai_json_default_env ); |
| 367 | } |
| 368 | if ( !empty( $ai_json_default_model ) ) { |
| 369 | $query->set_model( $ai_json_default_model ); |
| 370 | } |
| 371 | else { |
| 372 | $query->set_model( MWAI_FALLBACK_MODEL_JSON ); |
| 373 | } |
| 374 | $reply = $mwai_core->run_query( $query ); |
| 375 | try { |
| 376 | $json = json_decode( $reply->result, true ); |
| 377 | return $json; |
| 378 | } |
| 379 | catch ( Exception $e ) { |
| 380 | throw new Exception( 'The result is not a valid JSON.' ); |
| 381 | } |
| 382 | } |
| 383 | #endregion |
| 384 | |
| 385 | #region Standard API |
| 386 | /** |
| 387 | * Checks if a text is safe or not. |
| 388 | * |
| 389 | * @param string $text The text to check. |
| 390 | * |
| 391 | * @return bool True if the text is safe, false otherwise. |
| 392 | */ |
| 393 | public function moderationCheck( $text ) { |
| 394 | global $mwai_core; |
| 395 | $openai = Meow_MWAI_Engines_Factory::get_openai( $mwai_core ); |
| 396 | $res = $openai->moderate( $text ); |
| 397 | if ( !empty( $res ) && !empty( $res['results'] ) ) { |
| 398 | return (bool)$res['results'][0]['flagged']; |
| 399 | } |
| 400 | } |
| 401 | #endregion |
| 402 | |
| 403 | #region Standard API (No REST API) |
| 404 | |
| 405 | /** |
| 406 | * Checks the status of the AI environments. |
| 407 | * |
| 408 | * @return array The types of environments that are available. |
| 409 | */ |
| 410 | public function checkStatus() { |
| 411 | $env_types = []; |
| 412 | $ai_envs = $this->core->get_option( 'ai_envs' ); |
| 413 | if ( empty( $ai_envs ) ) { |
| 414 | throw new Exception( 'There are no AI environments yet.' ); |
| 415 | } |
| 416 | foreach ( $ai_envs as $env ) { |
| 417 | if ( !empty( $env['apikey'] ) ) { |
| 418 | if ( !in_array( $env['type'], $env_types ) ) { |
| 419 | $env_types[] = $env['type']; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | if ( empty( $env_types ) ) { |
| 424 | throw new Exception( 'There are no AI environments with an API key yet.' ); |
| 425 | } |
| 426 | return $env_types; |
| 427 | } |
| 428 | #endregion |
| 429 | } |