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
299 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 | register_rest_route( 'mwai/v1', '/simpleTextQuery', array( |
| 23 | 'methods' => 'POST', |
| 24 | 'callback' => array( $this, 'rest_simpleTextQuery' ), |
| 25 | 'permission_callback' => function( $request ) { |
| 26 | return $this->core->can_access_public_api( 'simpleTextQuery', $request ); |
| 27 | }, |
| 28 | ) ); |
| 29 | register_rest_route( 'mwai/v1', '/simpleVisionQuery', array( |
| 30 | 'methods' => 'POST', |
| 31 | 'callback' => array( $this, 'rest_simpleVisionQuery' ), |
| 32 | 'permission_callback' => function( $request ) { |
| 33 | return $this->core->can_access_public_api( 'simpleVisionQuery', $request ); |
| 34 | }, |
| 35 | ) ); |
| 36 | register_rest_route( 'mwai/v1', '/simpleJsonQuery', array( |
| 37 | 'methods' => 'POST', |
| 38 | 'callback' => array( $this, 'rest_simpleJsonQuery' ), |
| 39 | 'permission_callback' => function( $request ) { |
| 40 | return $this->core->can_access_public_api( 'simpleJsonQuery', $request ); |
| 41 | }, |
| 42 | ) ); |
| 43 | register_rest_route( 'mwai/v1', '/moderationCheck', array( |
| 44 | 'methods' => 'POST', |
| 45 | 'callback' => array( $this, 'rest_moderationCheck' ), |
| 46 | 'permission_callback' => function( $request ) { |
| 47 | return $this->core->can_access_public_api( 'moderationCheck', $request ); |
| 48 | }, |
| 49 | ) ); |
| 50 | |
| 51 | if ( $this->chatbot_module ) { |
| 52 | register_rest_route( 'mwai/v1', '/simpleChatbotQuery', array( |
| 53 | 'methods' => 'POST', |
| 54 | 'callback' => array( $this, 'rest_simpleChatbotQuery' ), |
| 55 | 'permission_callback' => function( $request ) { |
| 56 | return $this->core->can_access_public_api( 'simpleChatbotQuery', $request ); |
| 57 | }, |
| 58 | ) ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public function rest_simpleChatbotQuery( $request ) { |
| 63 | try { |
| 64 | $params = $request->get_params(); |
| 65 | $botId = isset( $params['botId'] ) ? $params['botId'] : ''; |
| 66 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 67 | $chatId = isset( $params['chatId'] ) ? $params['chatId'] : null; |
| 68 | $params = null; |
| 69 | if ( !empty( $chatId ) ) { |
| 70 | $params = array( 'chatId' => $chatId ); |
| 71 | } |
| 72 | if ( empty( $botId ) || empty( $message ) ) { |
| 73 | throw new Exception( 'The botId and prompt are required.' ); |
| 74 | } |
| 75 | $reply = $this->simpleChatbotQuery( $botId, $message, $params ); |
| 76 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 77 | } |
| 78 | catch (Exception $e) { |
| 79 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function rest_simpleTextQuery( $request ) { |
| 84 | try { |
| 85 | $params = $request->get_params(); |
| 86 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 87 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 88 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 89 | if ( !empty( $scope ) ) { |
| 90 | $options['scope'] = $scope; |
| 91 | } |
| 92 | if ( empty( $message ) ) { |
| 93 | throw new Exception( 'The prompt is required.' ); |
| 94 | } |
| 95 | $reply = $this->simpleTextQuery( $message, $options ); |
| 96 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 97 | } |
| 98 | catch (Exception $e) { |
| 99 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public function rest_simpleVisionQuery( $request ) { |
| 104 | try { |
| 105 | $params = $request->get_params(); |
| 106 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 107 | $url = isset( $params['url'] ) ? $params['url'] : ''; |
| 108 | $path = isset( $params['path'] ) ? $params['path'] : ''; |
| 109 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 110 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 111 | if ( !empty( $scope ) ) { |
| 112 | $options['scope'] = $scope; |
| 113 | } |
| 114 | if ( empty( $message ) ) { |
| 115 | throw new Exception( 'The prompt is required.' ); |
| 116 | } |
| 117 | if ( empty( $url ) && empty( $path ) ) { |
| 118 | throw new Exception( 'The url or path is required.' ); |
| 119 | } |
| 120 | $reply = $this->simpleVisionQuery( $message, $url, $path, $options ); |
| 121 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 122 | } |
| 123 | catch (Exception $e) { |
| 124 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public function rest_simpleJsonQuery( $request ) { |
| 129 | try { |
| 130 | $params = $request->get_params(); |
| 131 | $message = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 132 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 133 | $scope = isset( $params['scope'] ) ? $params['scope'] : 'public-api'; |
| 134 | if ( !empty( $scope ) ) { |
| 135 | $options['scope'] = $scope; |
| 136 | } |
| 137 | if ( empty( $message ) ) { |
| 138 | throw new Exception( 'The prompt is required.' ); |
| 139 | } |
| 140 | $reply = $this->simpleJsonQuery( $message, $options ); |
| 141 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 142 | } |
| 143 | catch (Exception $e) { |
| 144 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function rest_moderationCheck( $request ) { |
| 149 | try { |
| 150 | $params = $request->get_params(); |
| 151 | $text = $params['text']; |
| 152 | $reply = $this->moderationCheck( $text ); |
| 153 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 154 | } |
| 155 | catch (Exception $e) { |
| 156 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 157 | } |
| 158 | } |
| 159 | #endregion |
| 160 | |
| 161 | #region Simple API |
| 162 | /** |
| 163 | * Executes a vision query.` |
| 164 | * |
| 165 | * @param string $message The prompt for the AI. |
| 166 | * @param string $url The URL of the image to analyze. |
| 167 | * @param string|null $path The path to the image file. If provided, the image data will be read from this file. |
| 168 | * @param array $params Additional parameters for the AI query. |
| 169 | * |
| 170 | * @return string The result of the AI query. |
| 171 | */ |
| 172 | public function simpleVisionQuery( $message, $url, $path = null, $params = [] ) { |
| 173 | global $mwai_core; |
| 174 | $ai_vision_default_env = $this->core->get_option( 'ai_vision_default_env' ); |
| 175 | $ai_vision_default_model = $this->core->get_option( 'ai_vision_default_model' ); |
| 176 | if ( empty( $ai_vision_default_model ) ) { |
| 177 | $ai_vision_default_model = MWAI_FALLBACK_MODEL_VISION; |
| 178 | } |
| 179 | $query = new Meow_MWAI_Query_Text( $message ); |
| 180 | if ( !empty( $ai_vision_default_env ) ) { |
| 181 | $query->set_env_id( $ai_vision_default_env ); |
| 182 | } |
| 183 | if ( !empty( $ai_vision_default_model ) ) { |
| 184 | $query->set_model( $ai_vision_default_model ); |
| 185 | } |
| 186 | $query->inject_params( $params ); |
| 187 | $remote_upload = $this->core->get_option( 'image_remote_upload' ); |
| 188 | $preferURL = $remote_upload === 'url'; |
| 189 | |
| 190 | if ( $preferURL && $url ) { |
| 191 | $query->set_image( $url ); |
| 192 | } |
| 193 | else if ( !$preferURL && !empty( $path ) ) { |
| 194 | $data = base64_encode( file_get_contents( $path ) ); |
| 195 | $query->set_image_data( $data ); |
| 196 | } |
| 197 | else if ( $url ) { |
| 198 | $query->set_image( $url ); |
| 199 | } |
| 200 | else if ( !empty($path ) ) { |
| 201 | $data = base64_encode( file_get_contents( $path ) ); |
| 202 | $query->set_image_data( $data ); |
| 203 | } |
| 204 | |
| 205 | $reply = $mwai_core->run_query( $query ); |
| 206 | return $reply->result; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Executes a chatbot query. |
| 211 | * It will use the discussion if chatId is provided in the parameters. |
| 212 | * |
| 213 | * @param string $botId The ID of the chatbot. |
| 214 | * @param string $message The prompt for the AI. |
| 215 | * @param array $params Additional parameters for the AI query. |
| 216 | * |
| 217 | * @return string The result of the AI query. |
| 218 | */ |
| 219 | public function simpleChatbotQuery( $botId, $message, $params = [] ) { |
| 220 | if ( !isset( $params['messages'] ) && isset( $params['chatId'] ) ) { |
| 221 | $discussion = $this->discussions_module->get_discussion( $botId, $params['chatId'] ); |
| 222 | if ( !empty( $discussion ) ) { |
| 223 | $params['messages'] = $discussion->messages; |
| 224 | } |
| 225 | } |
| 226 | $data = $this->chatbot_module->chat_submit( $botId, $message, $params ); |
| 227 | return $data['reply']; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Executes a text query. |
| 232 | * |
| 233 | * @param string $message The prompt for the AI. |
| 234 | * @param array $params Additional parameters for the AI query. |
| 235 | * |
| 236 | * @return string The result of the AI query. |
| 237 | */ |
| 238 | public function simpleTextQuery( $message, $params = [] ) { |
| 239 | global $mwai_core; |
| 240 | $query = new Meow_MWAI_Query_Text( $message ); |
| 241 | $query->inject_params( $params ); |
| 242 | $reply = $mwai_core->run_query( $query ); |
| 243 | return $reply->result; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Executes a query that will have to return a JSON result. |
| 248 | * |
| 249 | * @param string $message The prompt for the AI. |
| 250 | * @param array $params Additional parameters for the AI query. |
| 251 | * |
| 252 | * @return array The result of the AI query. |
| 253 | */ |
| 254 | public function simpleJsonQuery( $message, $url = null, $path = null, $params = [] ) { |
| 255 | if ( !empty( $url ) || !empty( $path ) ) { |
| 256 | throw new Exception( 'The url and path are not supported yet by the simpleJsonQuery.' ); |
| 257 | } |
| 258 | global $mwai_core; |
| 259 | $query = new Meow_MWAI_Query_Text( $message . "\nYour reply must be a formatted JSON." ); |
| 260 | $query->inject_params( $params ); |
| 261 | $query->set_response_format( 'json' ); |
| 262 | $ai_json_default_env = $mwai_core->get_option( 'ai_json_default_env' ); |
| 263 | $ai_json_default_model = $mwai_core->get_option( 'ai_json_default_model' ); |
| 264 | if ( !empty( $ai_json_default_env ) ) { |
| 265 | $query->set_env_id( $ai_json_default_env ); |
| 266 | } |
| 267 | if ( !empty( $ai_json_default_model ) ) { |
| 268 | $query->set_model( $ai_json_default_model ); |
| 269 | } |
| 270 | else { |
| 271 | $query->set_model( MWAI_FALLBACK_MODEL_JSON ); |
| 272 | } |
| 273 | $reply = $mwai_core->run_query( $query ); |
| 274 | try { |
| 275 | $json = json_decode( $reply->result, true ); |
| 276 | return $json; |
| 277 | } |
| 278 | catch ( Exception $e ) { |
| 279 | throw new Exception( 'The result is not a valid JSON.' ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Checks if a text is safe or not. |
| 285 | * |
| 286 | * @param string $text The text to check. |
| 287 | * |
| 288 | * @return bool True if the text is safe, false otherwise. |
| 289 | */ |
| 290 | public function moderationCheck( $text ) { |
| 291 | global $mwai_core; |
| 292 | $openai = Meow_MWAI_Engines_Factory::get_openai( $mwai_core ); |
| 293 | $res = $openai->moderate( $text ); |
| 294 | if ( !empty( $res ) && !empty( $res['results'] ) ) { |
| 295 | return (bool)$res['results'][0]['flagged']; |
| 296 | } |
| 297 | } |
| 298 | #endregion |
| 299 | } |