base_url . '/generateRandomDocId'; return $this->sendGetRequest($url); } public function generateRandomKey() { $url = $this->base_url . '/generateRandomKey'; return $this->sendGetRequest($url); } public function maskdown($text) { $Parsedown = new Parsedown(); $html = $Parsedown->text($this->removeImageTags($text)); return $html; } public function removeImageTags($inputString) { if(!empty($inputString)){ $pattern = '/\[Image[^\]]*\]/'; $replacement = ''; $outputString = preg_replace($pattern, $replacement, $inputString); return $outputString; }else{ return $inputString; } } public function bardrewrite($question, $lang = 'Vietnamese',$toneOfVoice='') { $url = $this->base_url . '/bardrewrite'; $data = ['question' => $question, 'lang' => $lang,'toneOfVoice'=>$toneOfVoice]; return $this->maskdown($this->sendPostRequest($url, $data)); } public function bardcontentfull($question, $lang = 'Vietnamese') { $url = $this->base_url . '/bardcontentfull'; $data = ['question' => $question, 'lang' => $lang]; return $this->maskdown($this->sendPostRequest($url, $data)); } public function bardcontent($question, $lang = 'Vietnamese') { $url = $this->base_url . '/bardcontent'; $data = ['question' => $question, 'lang' => $lang]; return $this->maskdown($this->sendPostRequest($url, $data)); } public function bardcontentmore($question, $lang = 'Vietnamese'){ $url = $this->base_url . '/bardcontentmore'; $data = ['question' => $question, 'lang' => $lang]; return $this->maskdown($this->sendPostRequest($url, $data)); } /** * Write Full Article - Tạo bài viết hoàn chỉnh với cấu trúc chapters * Sử dụng API hiện có và xử lý logic ở client-side * @param string $keywords - Keywords chính của bài viết * @param string $lang - Ngôn ngữ * @param int $chapters - Số lượng chapters (mặc định 5) * @param int $words - Số từ tối đa (mặc định 2000) * @return array - Mảng chứa title, article content, tags */ public function writefullarticle($keywords, $lang = 'Vietnamese', $chapters = 5, $words = 2000) { // Tạo prompt cho bài viết hoàn chỉnh $prompt = $this->generateFullArticlePrompt($keywords, $lang, $chapters, $words); // Sử dụng API bardcontentmore hiện có $url = $this->base_url . '/bardcontentmore'; $data = [ 'question' => $prompt, 'lang' => $lang ]; $result = $this->sendPostRequest($url, $data); // Xử lý kết quả để tạo cấu trúc bài viết return $this->processFullArticleResult($result, $keywords, $chapters, $words); } /** * Tạo prompt cho bài viết hoàn chỉnh */ private function generateFullArticlePrompt($keywords, $lang, $chapters, $words) { $wordsPerChapter = round($words / $chapters); $prompt = "Viết một bài viết hoàn chỉnh về chủ đề: {$keywords}\n\n"; $prompt .= "Yêu cầu:\n"; $prompt .= "- Tạo {$chapters} chapters với cấu trúc logic\n"; $prompt .= "- Mỗi chapter khoảng {$wordsPerChapter} từ\n"; $prompt .= "- Tổng cộng khoảng {$words} từ\n"; $prompt .= "- Bao gồm introduction và conclusion\n"; $prompt .= "- Sử dụng HTML formatting (h2, p, ul, li, strong, em)\n"; $prompt .= "- Viết bằng {$lang}\n"; $prompt .= "- Tối ưu SEO\n"; $prompt .= "- Nội dung chất lượng cao và hữu ích\n\n"; $prompt .= "Cấu trúc bài viết:\n"; $prompt .= "1. Introduction (giới thiệu chủ đề)\n"; for ($i = 1; $i <= $chapters; $i++) { $prompt .= ($i + 1) . ". Chapter {$i} (nội dung chính)\n"; } $prompt .= ($chapters + 2) . ". Conclusion (kết luận)\n\n"; $prompt .= "Hãy viết bài viết hoàn chỉnh với đầy đủ các phần trên."; return $prompt; } /** * Xử lý kết quả để tạo cấu trúc bài viết */ private function processFullArticleResult($result, $keywords, $chapters, $words) { // Tạo title từ keywords $title = $this->generateTitleFromKeywords($keywords); // Tạo tags từ keywords $tags = $this->generateTagsFromKeywords($keywords); return [ 'title' => $title, 'article' => $result, 'tags' => $tags, 'keywords' => $keywords, 'chapters' => $chapters, 'words' => $words ]; } /** * Tạo title từ keywords */ private function generateTitleFromKeywords($keywords) { // Tạo title đơn giản từ keywords $title = ucfirst(trim($keywords)); // Nếu keywords quá dài, cắt ngắn if (strlen($title) > 60) { $title = substr($title, 0, 57) . '...'; } return $title; } /** * Tạo tags từ keywords */ private function generateTagsFromKeywords($keywords) { // Tách keywords thành tags $keywordsArray = array_map('trim', explode(',', $keywords)); $tags = []; foreach ($keywordsArray as $keyword) { if (!empty($keyword)) { $tags[] = ucfirst(trim($keyword)); } } // Giới hạn số tags return array_slice($tags, 0, 5); } public function gentitle($question, $lang = 'Vietnamese') { $url = $this->base_url . '/gentitle'; $data = ['question' => $question, 'lang' => $lang]; return $this->sendPostRequest($url, $data); } public function searchimg($question) { $url = $this->base_url . '/searchimg'; $data = ['question' => $question]; return $this->sendPostRequest($url, $data); } private function sendGetRequest($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; } private function sendPostRequest($url, $data) { $data['info'] = rendersetting::aiautotool_getdata(); $data['domain'] = get_option('home'); //print_r($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $response = curl_exec($ch); curl_close($ch); $response = json_decode($response); // print_r($response); return $response->result; } /** * Write Full Article Advanced - Tạo bài viết hoàn chỉnh bằng cách gọi API nhiều lần * @param string $keywords - Keywords chính của bài viết * @param string $lang - Ngôn ngữ * @param int $chapters - Số lượng chapters (mặc định 5) * @param int $words - Số từ tối đa (mặc định 2000) * @return array - Mảng chứa title, article content, tags */ public function writefullarticleAdvanced($keywords, $lang = 'Vietnamese', $chapters = 5, $words = 2000) { try { // Bước 1: Tạo cấu trúc bài viết $structure = $this->generateArticleStructure($keywords, $lang, $chapters); // Bước 2: Tạo introduction $introduction = $this->generateIntroduction($keywords, $lang); // Bước 3: Tạo từng chapter $chapterContent = []; for ($i = 1; $i <= $chapters; $i++) { $chapterPrompt = $structure['chapters'][$i - 1]; $chapterContent[] = $this->generateChapter($chapterPrompt, $lang, $i); } // Bước 4: Tạo conclusion $conclusion = $this->generateConclusion($keywords, $lang); // Bước 5: Ghép tất cả lại $fullArticle = $this->assembleFullArticle($introduction, $chapterContent, $conclusion); // Bước 6: Tạo title và tags $title = $this->generateTitleFromKeywords($keywords); $tags = $this->generateTagsFromKeywords($keywords); return [ 'title' => $title, 'article' => $fullArticle, 'tags' => $tags, 'keywords' => $keywords, 'chapters' => $chapters, 'words' => $words, 'structure' => $structure ]; } catch (Exception $e) { // Fallback về method đơn giản nếu có lỗi return $this->writefullarticle($keywords, $lang, $chapters, $words); } } /** * Tạo cấu trúc bài viết */ private function generateArticleStructure($keywords, $lang, $chapters) { $prompt = "Tạo cấu trúc chi tiết cho bài viết về: {$keywords}\n\n"; $prompt .= "Yêu cầu:\n"; $prompt .= "- Tạo {$chapters} chapters với tiêu đề cụ thể\n"; $prompt .= "- Mỗi chapter cần có prompt để viết nội dung\n"; $prompt .= "- Cấu trúc logic và mạch lạc\n"; $prompt .= "- Viết bằng {$lang}\n\n"; $prompt .= "Format trả về JSON:\n"; $prompt .= '{"title": "Tiêu đề bài viết", "subtitle": "Phụ đề", "chapters": [{"title": "Tiêu đề chapter 1", "prompt": "Prompt để viết chapter 1"}, ...]}'; $url = $this->base_url . '/bardcontentmore'; $data = ['question' => $prompt, 'lang' => $lang]; $result = $this->sendPostRequest($url, $data); // Parse JSON result $structure = json_decode($result, true); if (!$structure) { // Fallback structure $structure = [ 'title' => ucfirst($keywords), 'subtitle' => 'Hướng dẫn chi tiết', 'chapters' => [] ]; for ($i = 1; $i <= $chapters; $i++) { $structure['chapters'][] = [ 'title' => "Chapter {$i}", 'prompt' => "Viết nội dung cho chapter {$i} về {$keywords}" ]; } } return $structure; } /** * Tạo introduction */ private function generateIntroduction($keywords, $lang) { $prompt = "Viết phần giới thiệu (introduction) cho bài viết về: {$keywords}\n\n"; $prompt .= "Yêu cầu:\n"; $prompt .= "- Khoảng 100-150 từ\n"; $prompt .= "- Giới thiệu chủ đề một cách hấp dẫn\n"; $prompt .= "- Sử dụng HTML formatting\n"; $prompt .= "- Viết bằng {$lang}\n"; $prompt .= "- Không cần tiêu đề 'Introduction'"; $url = $this->base_url . '/bardcontentmore'; $data = ['question' => $prompt, 'lang' => $lang]; return $this->sendPostRequest($url, $data); } /** * Tạo chapter */ private function generateChapter($chapterPrompt, $lang, $chapterNumber) { $prompt = "Viết nội dung cho chapter {$chapterNumber}:\n\n"; $prompt .= "Tiêu đề: {$chapterPrompt['title']}\n"; $prompt .= "Prompt: {$chapterPrompt['prompt']}\n\n"; $prompt .= "Yêu cầu:\n"; $prompt .= "- Khoảng 300-400 từ\n"; $prompt .= "- Sử dụng HTML formatting (h2, p, ul, li, strong, em)\n"; $prompt .= "- Viết bằng {$lang}\n"; $prompt .= "- Nội dung chất lượng cao và hữu ích"; $url = $this->base_url . '/bardcontentmore'; $data = ['question' => $prompt, 'lang' => $lang]; return $this->sendPostRequest($url, $data); } /** * Tạo conclusion */ private function generateConclusion($keywords, $lang) { $prompt = "Viết phần kết luận (conclusion) cho bài viết về: {$keywords}\n\n"; $prompt .= "Yêu cầu:\n"; $prompt .= "- Khoảng 100-150 từ\n"; $prompt .= "- Tóm tắt những điểm chính\n"; $prompt .= "- Đưa ra kết luận hoặc gợi ý\n"; $prompt .= "- Sử dụng HTML formatting\n"; $prompt .= "- Viết bằng {$lang}\n"; $prompt .= "- Không cần tiêu đề 'Conclusion'"; $url = $this->base_url . '/bardcontentmore'; $data = ['question' => $prompt, 'lang' => $lang]; return $this->sendPostRequest($url, $data); } /** * Ghép bài viết hoàn chỉnh */ private function assembleFullArticle($introduction, $chapterContent, $conclusion) { $article = ''; // Thêm introduction $article .= $introduction . "\n\n"; // Thêm từng chapter foreach ($chapterContent as $index => $content) { $article .= $content . "\n\n"; } // Thêm conclusion $article .= $conclusion; return $article; } /** * Write Full Article Simple - Tạo bài viết hoàn chỉnh với một API call * @param string $keywords - Keywords chính của bài viết * @param string $lang - Ngôn ngữ * @param int $chapters - Số lượng chapters (mặc định 5) * @param int $words - Số từ tối đa (mặc định 2000) * @return array - Mảng chứa title, article content, tags */ public function writefullarticleSimple($keywords, $lang = 'Vietnamese', $chapters = 5, $words = 2000) { // Tạo prompt tối ưu cho bài viết hoàn chỉnh $prompt = $this->createOptimizedPrompt($keywords, $lang, $chapters, $words); // Sử dụng API bardcontentmore hiện có $url = $this->base_url . '/bardcontentmore'; $data = [ 'question' => $prompt, 'lang' => $lang ]; $result = $this->sendPostRequest($url, $data); // Xử lý kết quả return $this->processSimpleResult($result, $keywords, $chapters, $words); } /** * Tạo prompt tối ưu cho bài viết hoàn chỉnh */ private function createOptimizedPrompt($keywords, $lang, $chapters, $words) { $wordsPerChapter = round($words / $chapters); $prompt = "Viết một bài viết hoàn chỉnh và chuyên nghiệp về chủ đề: {$keywords}\n\n"; $prompt .= "YÊU CẦU CHI TIẾT:\n"; $prompt .= "1. Cấu trúc bài viết:\n"; $prompt .= " - Phần giới thiệu (Introduction) - khoảng 150 từ\n"; $prompt .= " - {$chapters} chapters chính - mỗi chapter khoảng {$wordsPerChapter} từ\n"; $prompt .= " - Phần kết luận (Conclusion) - khoảng 150 từ\n"; $prompt .= " - Tổng cộng khoảng {$words} từ\n\n"; $prompt .= "2. Format và style:\n"; $prompt .= " - Sử dụng HTML tags:

cho tiêu đề chapters,

cho đoạn văn\n"; $prompt .= " - Sử dụng và cho nhấn mạnh\n"; $prompt .= " - Sử dụng