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
119 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_API { |
| 4 | public $core; |
| 5 | private $chatbot_module; |
| 6 | |
| 7 | public function __construct( $chatbot_module ) { |
| 8 | global $mwai_core; |
| 9 | $this->core = $mwai_core; |
| 10 | $this->chatbot_module = $chatbot_module; |
| 11 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 12 | } |
| 13 | |
| 14 | function rest_api_init() { |
| 15 | $public_api = $this->core->get_option( 'public_api' ); |
| 16 | if ( !$public_api ) { |
| 17 | return; |
| 18 | } |
| 19 | register_rest_route( 'mwai/v1', '/simpleTextQuery', array( |
| 20 | 'methods' => 'POST', |
| 21 | 'callback' => array( $this, 'rest_simpleTextQuery' ), |
| 22 | 'permission_callback' => function( $request ) { |
| 23 | return $this->core->can_access_public_api( 'simpleTextQuery', $request ); |
| 24 | }, |
| 25 | ) ); |
| 26 | register_rest_route( 'mwai/v1', '/moderationCheck', array( |
| 27 | 'methods' => 'POST', |
| 28 | 'callback' => array( $this, 'rest_moderationCheck' ), |
| 29 | 'permission_callback' => function( $request ) { |
| 30 | return $this->core->can_access_public_api( 'moderationCheck', $request ); |
| 31 | }, |
| 32 | ) ); |
| 33 | |
| 34 | if ( $this->chatbot_module ) { |
| 35 | register_rest_route( 'mwai/v1', '/simpleChatbotQuery', array( |
| 36 | 'methods' => 'POST', |
| 37 | 'callback' => array( $this, 'rest_simpleChatbotQuery' ), |
| 38 | 'permission_callback' => function( $request ) { |
| 39 | return $this->core->can_access_public_api( 'simpleChatbotQuery', $request ); |
| 40 | }, |
| 41 | ) ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public function rest_simpleChatbotQuery( $request ) { |
| 46 | try { |
| 47 | $params = $request->get_params(); |
| 48 | $botId = isset( $params['botId'] ) ? $params['botId'] : ''; |
| 49 | $prompt = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 50 | $clientId = isset( $params['clientId'] ) ? $params['clientId'] : null; |
| 51 | $params = null; |
| 52 | if ( !empty( $clientId ) ) { |
| 53 | $params = array( 'clientId' => $clientId ); |
| 54 | } |
| 55 | if ( empty( $botId ) || empty( $prompt ) ) { |
| 56 | throw new Exception( 'The botId and prompt are required.' ); |
| 57 | } |
| 58 | $reply = $this->simpleChatbotQuery( $botId, $prompt, $params ); |
| 59 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 60 | } |
| 61 | catch (Exception $e) { |
| 62 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function rest_simpleTextQuery( $request ) { |
| 67 | try { |
| 68 | $params = $request->get_params(); |
| 69 | $prompt = isset( $params['prompt'] ) ? $params['prompt'] : ''; |
| 70 | $options = isset( $params['options'] ) ? $params['options'] : []; |
| 71 | $env = isset( $params['env'] ) ? $params['env'] : 'public-api'; |
| 72 | if ( !empty( $env ) ) { |
| 73 | $options['env'] = $env; |
| 74 | } |
| 75 | if ( empty( $prompt ) ) { |
| 76 | throw new Exception( 'The prompt is required.' ); |
| 77 | } |
| 78 | $reply = $this->simpleTextQuery( $prompt, $options ); |
| 79 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 80 | } |
| 81 | catch (Exception $e) { |
| 82 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function rest_moderationCheck( $request ) { |
| 87 | try { |
| 88 | $params = $request->get_params(); |
| 89 | $text = $params['text']; |
| 90 | $reply = $this->moderationCheck( $text ); |
| 91 | return new WP_REST_Response([ 'success' => true, 'data' => $reply ], 200 ); |
| 92 | } |
| 93 | catch (Exception $e) { |
| 94 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public function simpleChatbotQuery( $botId, $prompt, $params = [] ) { |
| 99 | $data = $this->chatbot_module->chat_submit( $botId, $prompt, $params ); |
| 100 | return $data['reply']; |
| 101 | } |
| 102 | |
| 103 | public function simpleTextQuery( $prompt, $params = [] ) { |
| 104 | global $mwai_core; |
| 105 | $query = new Meow_MWAI_Query_Text( $prompt ); |
| 106 | $query->injectParams( $params ); |
| 107 | $reply = $mwai_core->ai->run( $query ); |
| 108 | return $reply->result; |
| 109 | } |
| 110 | |
| 111 | public function moderationCheck( $text ) { |
| 112 | global $mwai_core; |
| 113 | $openai = new Meow_MWAI_Engines_OpenAI( $mwai_core ); |
| 114 | $res = $openai->moderate( $text ); |
| 115 | if ( !empty( $res ) && !empty( $res['results'] ) ) { |
| 116 | return (bool)$res['results'][0]['flagged']; |
| 117 | } |
| 118 | } |
| 119 | } |