[\w-]+):text-to-speech'; const METHODS = WP_REST_Server::CREATABLE; /** * Returns the route base. * * @since 0.7.0 * * @return string Route base. */ protected function base(): string { return self::BASE; } /** * Returns the route methods, as a comma-separated string. * * @since 0.7.0 * * @return string Route methods, as a comma-separated string. */ protected function methods(): string { return self::METHODS; } /** * Generates content using the given service and model. * * @since 0.7.0 * * @param Generative_AI_Service $service The service instance. * @param Generative_AI_Model $model The model instance. * @param string|Parts|Content|Content[] $content The content prompt. * @return Candidates The generated content candidates. * * @throws REST_Exception Thrown when the model does not support text to speech. */ protected function generate_content( Generative_AI_Service $service, Generative_AI_Model $model, $content ): Candidates { if ( ! $model instanceof With_Text_To_Speech ) { throw $this->create_missing_text_to_speech_exception(); } return $model->text_to_speech( $content ); } /** * Retrieves the (text-based) model with the given slug and parameters. * * @since 0.7.0 * * @param Generative_AI_Service $service The service instance to get the model from. * @param array $model_params The model parameters. * @return Generative_AI_Model The model. * * @throws REST_Exception Thrown when the model cannot be retrieved or invalid parameters are provided. */ protected function get_model( Generative_AI_Service $service, array $model_params ): Generative_AI_Model { $model = parent::get_model( $service, $model_params ); if ( ! $model instanceof With_Text_To_Speech ) { throw $this->create_missing_text_to_speech_exception(); } return $model; } /** * Processes the model parameters before retrieving the model with them. * * @since 0.7.0 * * @param array $model_params The model parameters. * @return array The processed model parameters. */ protected function process_model_params( array $model_params ): array { // At the very least, text to speech capabilities need to be supported by the model. if ( ! isset( $model_params['capabilities'] ) ) { $model_params['capabilities'] = array( AI_Capability::TEXT_TO_SPEECH ); } // Replace parent method implementation because parameters are quite different for text to speech. if ( isset( $model_params['generationConfig'] ) && is_array( $model_params['generationConfig'] ) ) { $model_params['generationConfig'] = Text_To_Speech_Config::from_array( $model_params['generationConfig'] ); } // Tools are not supported for text to speech. unset( $model_params['tools'], $model_params['toolConfig'] ); return $model_params; } /** * Creates a REST exception for a missing text to speech support of a model. * * @since 0.7.0 * * @return REST_Exception The REST exception. */ protected function create_missing_text_to_speech_exception(): REST_Exception { return REST_Exception::create( 'rest_model_lacks_support', esc_html__( 'The model does not support text to speech.', 'ai-services' ), 400 ); } /** * Returns the route specific arguments. * * @since 0.7.0 * * @return array Route arguments. */ protected function args(): array { $args = parent::args(); // Override generation config to be specific to text to speech. $args['modelParams']['generationConfig'] = array_merge( array( 'description' => __( 'Model generation configuration options.', 'ai-services' ) ), Text_To_Speech_Config::get_json_schema() ); // Tools are not supported for text to speech. unset( $args['modelParams']['tools'], $args['modelParams']['toolConfig'] ); return $args; } }