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