engines
3 years ago
modules
3 years ago
queries
3 years ago
tests
3 years ago
admin.php
3 years ago
api.php
3 years ago
core.php
3 years ago
init.php
3 years ago
reply.php
3 years ago
rest.php
3 years ago
api.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_API { |
| 4 | public $core; |
| 5 | |
| 6 | public function __construct() { |
| 7 | global $mwai_core; |
| 8 | $this->core = $mwai_core; |
| 9 | } |
| 10 | |
| 11 | function rest_api_init() { |
| 12 | register_rest_route( 'mwai/v1', '/simpleTextQuery', array( |
| 13 | 'methods' => 'POST', |
| 14 | 'callback' => array( $this, 'rest_simpleTextQuery' ), |
| 15 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 16 | ) ); |
| 17 | register_rest_route( 'mwai/v1', '/moderationCheck', array( |
| 18 | 'methods' => 'POST', |
| 19 | 'callback' => array( $this, 'rest_moderationCheck' ), |
| 20 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 21 | ) ); |
| 22 | } |
| 23 | |
| 24 | public function rest_simpleTextQuery( $request ) { |
| 25 | try { |
| 26 | $params = $request->get_params(); |
| 27 | $prompt = $params['prompt']; |
| 28 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 29 | $reply = $this->simpleTextQuery( $prompt, $options ); |
| 30 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 31 | } |
| 32 | catch (Exception $e) { |
| 33 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function rest_moderationCheck( $request ) { |
| 38 | try { |
| 39 | $params = $request->get_params(); |
| 40 | $text = $params['text']; |
| 41 | $reply = $this->moderationCheck( $text ); |
| 42 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 43 | } |
| 44 | catch (Exception $e) { |
| 45 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public function simpleTextQuery( $prompt, $options = [] ) { |
| 50 | global $mwai_core; |
| 51 | $query = new Meow_MWAI_Query_Text( $prompt ); |
| 52 | $query->injectParams( $options ); |
| 53 | $reply = $mwai_core->ai->run( $query ); |
| 54 | return $reply->result; |
| 55 | } |
| 56 | |
| 57 | public function moderationCheck( $text ) { |
| 58 | global $mwai_core; |
| 59 | $openai = new Meow_MWAI_Engines_OpenAI( $mwai_core ); |
| 60 | $res = $openai->moderate( $text ); |
| 61 | if ( !empty( $res ) && !empty( $res['results'] ) ) { |
| 62 | return (bool)$res['results'][0]['flagged']; |
| 63 | } |
| 64 | } |
| 65 | } |