[ 'id' => 'deepseek-flash', 'name' => 'DeepSeek V4.1 Flash', 'context' => 1000000, 'input_price' => 0.30, 'output_price' => 1.20, ], 'deepseek-v4-pro' => [ 'id' => 'deepseek-v4-pro', 'name' => 'DeepSeek V4 Pro', 'context' => 1000000, 'input_price' => 1.32, 'output_price' => 3.96, ], 'deepseek-v4-flash' => [ 'id' => 'deepseek-v4-flash', 'name' => 'DeepSeek V4 Flash (Legacy alias)', 'context' => 1000000, 'input_price' => 0.30, 'output_price' => 1.20, ], ]; } public function get_api_key(): string { $options = get_option( 'aibp_settings', [] ); return $options['deepseek_api_key'] ?? ''; } public function generate( string $prompt, array $args = [] ): string { $api_key = $this->get_api_key(); if ( empty( $api_key ) ) { throw new \Exception( esc_html__( 'DeepSeek API key is not configured.', 'ai-bulk-post' ) ); } $defaults = [ 'model' => 'deepseek-flash', 'messages' => [ [ 'role' => 'user', 'content' => $prompt, ], ], 'temperature' => $args['temperature'] ?? 0.7, 'max_tokens' => $args['max_tokens'] ?? 4000, 'frequency_penalty' => 0, 'presence_penalty' => 0, ]; $request_args = wp_parse_args( $args, $defaults ); unset( $request_args['temperature'], $request_args['max_tokens'] ); $response = wp_remote_post( self::api_base_url() . '/chat/completions', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode( $request_args ), 'timeout' => 120, ] ); if ( is_wp_error( $response ) ) { throw new \Exception( esc_html( $response->get_error_message() ) ); } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code !== 200 ) { $message = $body['error']['message'] ?? esc_html__( 'Unknown API error.', 'ai-bulk-post' ); throw new \Exception( esc_html( sprintf( 'DeepSeek API error (%d): %s', $code, $message ) ) ); } return trim( $body['choices'][0]['message']['content'] ?? '' ); } public function validate_key( string $api_key ): bool { $response = wp_remote_post( self::api_base_url() . '/chat/completions', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode( [ 'model' => 'deepseek-flash', 'messages' => [['role' => 'user', 'content' => 'Hi']], 'max_tokens' => 1, ] ), 'timeout' => 15, ] ); if ( is_wp_error( $response ) ) { return false; } $code = wp_remote_retrieve_response_code( $response ); return $code === 200; } }