'deepseek-chat', 'temperature' => 0.7, 'top_p' => 1, 'max_tokens' => 500, 'presence_penalty' => 0, 'frequency_penalty' => 0, ]; /** * Filter DeepSeek default args. * * @since 1.6.0 * * @param mixed[] $args Default args. * @return mixed[] */ $filtered_args = (array) apply_filters( 'apbe_deepseek_args', $args ); return wp_parse_args( $filtered_args, $args ); } /** * Get API URL. * * This method returns the DeepSeek API URL * endpoint. It can be filtered using the * 'apbe_deepseek_api_url' filter. * * @since 1.6.0 * * @return string */ protected function get_api_url(): string { $url = 'https://api.deepseek.com/chat/completions'; /** * Filter DeepSeek API URL. * * @since 1.6.0 * * @param string $url DeepSeek API URL. * @return string */ return esc_url( (string) apply_filters( 'apbe_deepseek_api_url', $url ) ); } /** * Get AI Response. * * @since 1.6.0 * * @param mixed[] $payload JSON Payload. * @return string|\WP_Error */ public function run( $payload ) { $api_key = get_option( Options::get_page_option(), [] )['deepseek_token'] ?? ''; if ( empty( $api_key ) ) { return $this->get_json_error( __( 'Missing DeepSeek API key.', 'ai-plus-block-editor' ) ); } // Default prompt if not passed. $prompt_text = $payload['content'] ?? ''; // Validate prompt text. if ( ! is_string( $prompt_text ) || empty( $prompt_text ) ) { return $this->get_json_error( __( 'Invalid prompt text.', 'ai-plus-block-editor' ) ); } /** * Filter DeepSeek System Prompt. * * @since 1.8.0 * * @param string $prompt DeepSeek System prompt. * @return string */ $system_prompt = apply_filters( 'apbe_deepseek_system_prompt', 'You are a helpful assistant.' ); // DeepSeek API expects a specific body structure. $body = wp_parse_args( [ 'messages' => [ [ 'role' => 'system', 'content' => $system_prompt, ], [ 'role' => 'user', 'content' => $prompt_text, ], ], ], $this->get_default_args() ); $response = wp_remote_post( $this->get_api_url(), [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ], 'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE ), 'timeout' => 20, ] ); if ( is_wp_error( $response ) ) { return $this->get_json_error( $response->get_error_message(), $body ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); // Notify user, if JSON yields null. if ( empty( $data ) || ! isset( $data['choices'][0]['message']['content'] ) ) { return $this->get_json_error( $data['error']['message'] ?? __( 'Unexpected DeepSeek API response.', 'ai-plus-block-editor' ), $body ); } // Get API response. $response = $data['choices'][0]['message']['content'] ?? ''; // Return filtered response. return $this->get_provider_response( $response, wp_json_encode( $body ) ); } /** * Get Options. * * @return array */ public static function get_options(): array { $options = [ 'heading' => esc_html__( 'DeepSeek', 'ai-plus-block-editor' ), 'controls' => [ 'deepseek_enable' => [ 'control' => esc_attr( 'checkbox' ), 'label' => esc_html__( 'Enable DeepSeek', 'ai-plus-block-editor' ), 'summary' => esc_html__( 'Use DeepSeek capabilities in Block Editor', 'ai-plus-block-editor' ), ], 'deepseek_token' => [ 'control' => esc_attr( 'password' ), 'placeholder' => esc_attr( '' ), 'label' => esc_html__( 'API Keys', 'ai-plus-block-editor' ), 'summary' => esc_html__( 'e.g. ae2kgch7ib9eqcbeveq9a923nv87392av', 'ai-plus-block-editor' ), ], ], ]; return apply_filters( 'apbe_deepseek_options', $options ); } }