| 1 |
<?php |
| 2 |
/** |
| 3 |
* LLMs.txt Prompts Class |
| 4 |
* |
| 5 |
* Handles LLMs.txt generation AI prompts. |
| 6 |
* Preserves exact functionality from original Prompt_Builder implementation. |
| 7 |
* |
| 8 |
* @package ThinkRank |
| 9 |
* @subpackage AI\Prompts |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace ThinkRank\AI\Prompts; |
| 16 |
|
| 17 |
// Prevent direct access |
| 18 |
if (!defined('ABSPATH')) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* LLMs.txt Prompts Class |
| 24 |
* |
| 25 |
* Centralized LLMs.txt prompt building for consistent AI optimization across all providers. |
| 26 |
* Handles LLMs.txt content generation and optimization. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class LLMs_Txt_Prompts { |
| 31 |
|
| 32 |
/** |
| 33 |
* Build LLMs.txt optimization prompt |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @param array $website_data Website data to optimize |
| 38 |
* @param array $options Optimization options |
| 39 |
* @param string $provider AI provider ('openai', 'claude', etc.) |
| 40 |
* @return string Formatted prompt |
| 41 |
*/ |
| 42 |
public function build_llms_txt_prompt(array $website_data, array $options = [], string $provider = 'openai'): string { |
| 43 |
// Extract and sanitize data |
| 44 |
$data = $this->prepare_llms_txt_data($website_data, $options); |
| 45 |
|
| 46 |
// Get provider-specific prompt introduction |
| 47 |
$intro = $this->get_llms_txt_intro($provider); |
| 48 |
|
| 49 |
// Build the main prompt content |
| 50 |
$prompt_content = $this->build_llms_txt_content($data); |
| 51 |
|
| 52 |
// Get provider-specific output format instructions |
| 53 |
$format_instructions = $this->get_llms_txt_format_instructions($provider); |
| 54 |
|
| 55 |
return $intro . "\n\n" . $prompt_content . "\n\n" . $format_instructions; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Prepare and sanitize LLMs.txt data |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
* |
| 63 |
* @param array $website_data Raw website data |
| 64 |
* @param array $options Optimization options |
| 65 |
* @return array Prepared data |
| 66 |
*/ |
| 67 |
private function prepare_llms_txt_data(array $website_data, array $options): array { |
| 68 |
return [ |
| 69 |
'site_name' => sanitize_text_field($website_data['site_name'] ?? ''), |
| 70 |
'website_description' => sanitize_textarea_field($website_data['website_description'] ?? ''), |
| 71 |
'key_features' => sanitize_textarea_field($website_data['key_features'] ?? ''), |
| 72 |
'technical_stack' => sanitize_textarea_field($website_data['technical_stack'] ?? ''), |
| 73 |
'development_approach' => sanitize_textarea_field($website_data['development_approach'] ?? ''), |
| 74 |
'business_type' => sanitize_text_field($options['business_type'] ?? 'website'), |
| 75 |
'target_audience' => sanitize_text_field($options['target_audience'] ?? 'general'), |
| 76 |
'tone' => sanitize_text_field($options['tone'] ?? 'professional') |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get provider-specific LLMs.txt prompt introduction |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* |
| 85 |
* @param string $provider AI provider |
| 86 |
* @return string Prompt introduction |
| 87 |
*/ |
| 88 |
private function get_llms_txt_intro(string $provider): string { |
| 89 |
switch ($provider) { |
| 90 |
case 'claude': |
| 91 |
return "As an expert technical writer, generate comprehensive llms.txt content for this website:"; |
| 92 |
|
| 93 |
case 'openai': |
| 94 |
default: |
| 95 |
return "Generate comprehensive llms.txt content for this website:"; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Build main LLMs.txt prompt content |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @param array $data Prepared website data |
| 105 |
* @return string Prompt content |
| 106 |
*/ |
| 107 |
private function build_llms_txt_content(array $data): string { |
| 108 |
return "Website Title: {$data['site_name']} |
| 109 |
Website Description: {$data['website_description']} |
| 110 |
Key Features: {$data['key_features']} |
| 111 |
Business Type: {$data['business_type']} |
| 112 |
Target Audience: {$data['target_audience']} |
| 113 |
Technical Stack: {$data['technical_stack']} |
| 114 |
Development Approach: {$data['development_approach']} |
| 115 |
Desired Tone: {$data['tone']} |
| 116 |
|
| 117 |
Create structured content that helps AI assistants understand this website better. Optimize and improve all provided content including: |
| 118 |
1. Website title (make it more compelling and SEO-friendly) |
| 119 |
2. Clear project overview |
| 120 |
3. Key features and functionality |
| 121 |
4. Technical architecture details |
| 122 |
5. Development guidelines |
| 123 |
6. Context for AI assistance |
| 124 |
|
| 125 |
Requirements: |
| 126 |
- Make the content informative and well-structured |
| 127 |
- Optimize all text for clarity and professionalism |
| 128 |
- Ensure the tone matches the specified style: {$data['tone']} |
| 129 |
- Target the content for: {$data['target_audience']} |
| 130 |
- Focus on {$data['business_type']} context |
| 131 |
- Return key_features as a single string with exactly one feature per line, separated by newline characters (\\n). Do not separate features with commas, and do not return an array. A comma inside a feature is fine, for example \"Collect reviews from Trustpilot, Google and Etsy\" is one feature. |
| 132 |
|
| 133 |
Focus on creating content that is informative, well-structured, and helpful for AI assistants to understand the website's purpose and functionality."; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get provider-specific output format instructions |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* |
| 141 |
* @param string $provider AI provider |
| 142 |
* @return string Format instructions |
| 143 |
*/ |
| 144 |
private function get_llms_txt_format_instructions(string $provider): string { |
| 145 |
$base_format = 'Provide the response in this exact JSON format: |
| 146 |
{ |
| 147 |
"optimized_data": { |
| 148 |
"site_name": "improved website title", |
| 149 |
"website_description": "enhanced description", |
| 150 |
"key_features": "Feature one\\nFeature two\\nFeature three", |
| 151 |
"technical_stack": "improved technical details", |
| 152 |
"development_approach": "enhanced development info" |
| 153 |
}, |
| 154 |
"suggestions": [ |
| 155 |
"additional suggestions for the website" |
| 156 |
] |
| 157 |
}'; |
| 158 |
|
| 159 |
switch ($provider) { |
| 160 |
case 'claude': |
| 161 |
return $base_format . "\n\nEnsure the JSON is valid and properly formatted."; |
| 162 |
|
| 163 |
case 'openai': |
| 164 |
default: |
| 165 |
return $base_format . "\n\nMake sure to return only valid JSON without any additional text or formatting."; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|