options = get_option('aiblhewi_options'); } /** * Get chat response from AI service */ public function get_chat_response($message, $context_data = array()) { // Use Gemini API return $this->get_gemini_response($message, $context_data); } /** * Summarize article using AI service */ public function summarize_article($article_content) { $prompt = $this->build_summarization_prompt($article_content); // Use Gemini API return $this->get_gemini_response($prompt); } /** * Get response from Gemini API */ private function get_gemini_response($message, $context_data = array()) { $api_key = $this->options['gemini_api_key'] ?? ''; if (empty($api_key)) { return false; } $prompt = $this->build_prompt($message, $context_data); $url = 'https://generativelanguage.googleapis.com/v1/models/gemini-2.5-flash:generateContent?key=' . $api_key; $data = array( 'contents' => array( array( 'parts' => array( array('text' => $prompt) ) ) ) ); $response = wp_remote_post($url, array( 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => json_encode($data), 'timeout' => 30 )); // Check if the response itself is a WordPress error if (is_wp_error($response)) { $error_message = $response->get_error_message(); return false; } // Get HTTP status code $code = wp_remote_retrieve_response_code($response); // Check if status code is not 200 if ($code !== 200) { $body = wp_remote_retrieve_body($response); // Store the error for potential retrieval by AJAX handler $this->last_gemini_error = array( 'code' => $code, 'body' => $body ); return false; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (isset($data['candidates'][0]['content']['parts'][0]['text'])) { return $data['candidates'][0]['content']['parts'][0]['text']; } // Log the response body if no valid content found return false; } /** * Build prompt for AI service */ private function build_prompt($message, $context_data = array()) { $site_name = get_bloginfo('name'); $site_url = home_url(); $prompt = "You are an AI assistant for the website '{$site_name}' ({$site_url}). "; $prompt .= "Your role is to help visitors by answering questions about the website's content and providing helpful information. "; $prompt .= "Be friendly, helpful, and informative. "; // Add context if available if (!empty($context_data['current_article'])) { $article = $context_data['current_article']; $prompt .= "\n\nCurrent article context:\n"; $prompt .= "Title: {$article['title']}\n"; $prompt .= "Content: " . wp_trim_words($article['content'], 200) . "\n"; } if (!empty($context_data['related_articles'])) { $prompt .= "\n\nRelated articles from this website:\n"; foreach ($context_data['related_articles'] as $article) { $prompt .= "- {$article['title']}: {$article['excerpt']}\n"; } } $prompt .= "\n\nUser question: {$message}\n\n"; $prompt .= "Please provide a helpful response. If you reference any articles, mention their titles and that they can be found on this website."; return $prompt; } /** * Build summarization prompt */ private function build_summarization_prompt($article_content) { $site_name = get_bloginfo('name'); $prompt = "Please provide a concise summary of the following article from '{$site_name}':\n\n"; $prompt .= "Title: {$article_content['title']}\n\n"; $prompt .= "Content: {$article_content['content']}\n\n"; $prompt .= "Please provide a summary that:\n"; $prompt .= "1. Captures the main points and key information\n"; $prompt .= "2. Is 2-3 paragraphs long\n"; $prompt .= "3. Is written in a clear, engaging style\n"; $prompt .= "4. Helps readers understand what the article is about"; return $prompt; } /** * Get the last Gemini API error */ public function get_last_gemini_error() { return $this->last_gemini_error; } }