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