args = $this->request->get_json_params(); // Bail out, if it does NOT exists. if ( empty( $this->args['text'] ?? '' ) ) { return $this->get_error_response( sprintf( 'API Request does not contain a text. Post ID: %s', $this->args['id'] ?? '' ) ); } return $this->get_response(); } /** * Get Response for valid WP REST request. * * This method obtains the REST response using * the custom prompt. * * @since 1.0.0 * * @return \WP_REST_Response|\WP_Error */ protected function get_response() { // Get Args. $prompt_tone = $this->args['tone'] ?? ''; $prompt_text = $this->args['text'] ?? ''; $placeholder = 'Using a %s tone, generate a text I can use to substitute the following text: %s. Do not include any explanation, commentary, or alternative suggestions.'; // Get Prompt. $prompt = sprintf( $placeholder, $prompt_tone, $prompt_text ); /** * Filter Tone prompt. * * @since 1.0.0 * * @param string $prompt Prompt sent to AI LLM endpoint. * @param string $prompt_tone Tone sent. * @param string $prompt_text Text sent. * * @return string */ $prompt = apply_filters( 'apbe_tone_prompt', $prompt, $prompt_tone, $prompt_text ); return rest_ensure_response( $this->ai->run( [ 'content' => $prompt, ] ) ); } }