| @@ -78,13 +78,13 @@ | ||
| 78 | 78 | $keywords = substr( $keywords, 0, self::MAX_KEYWORDS_LENGTH ); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | $prompt = sprintf( |
| 82 | - 'Generate an FAQ answer for the question related to %s. The question: %s', | |
| 82 | + 'Write a clear, concise answer in plain text for the following FAQ question related to %s. The question: %s', | |
| 83 | 83 | $keywords, |
| 84 | 84 | $question |
| 85 | 85 | ); |
| 86 | - $system = __( 'You are a helpful assistant that writes clear, concise FAQ answers.', 'betterdocs' ); | |
| 86 | + $system = __( 'You are a helpful assistant that writes clear, concise FAQ answers in plain text. Do not use any Markdown or special formatting — no asterisks, bold, italics, headings, bullet points, backticks or code fences. Reply with a direct answer written in plain sentences.', 'betterdocs' ); | |
| 87 | 87 | |
| 88 | 88 | $result = $write_ai->generate_text( $prompt, $system ); |
| 89 | 89 | |
| 90 | 90 | if ( empty( $result['success'] ) ) { |
| @@ -119,8 +119,25 @@ | ||
| 119 | 119 | protected function clean_response( $text ) { |
| 120 | 120 | $text = trim( $text ); |
| 121 | 121 | $text = preg_replace( '/^\s*answer\s*:\s*/i', '', $text ); |
| 122 | 122 | $text = preg_replace( '/^\s*\?/', '', $text ); |
| 123 | + $text = $this->strip_markdown( $text ); | |
| 124 | + | |
| 125 | + return trim( $text ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Safety net: strip the common Markdown the model may still emit despite the | |
| 130 | + * plain-text instruction, so FAQ answers never render literal asterisks. | |
| 131 | + */ | |
| 132 | + protected function strip_markdown( $text ) { | |
| 133 | + $text = preg_replace( '/```.*?```/s', '', $text ); // fenced code blocks | |
| 134 | + $text = preg_replace( '/\*\*(.+?)\*\*/s', '$1', $text ); // **bold** | |
| 135 | + $text = preg_replace( '/__(.+?)__/s', '$1', $text ); // __bold__ | |
| 136 | + $text = preg_replace( '/`([^`]*)`/', '$1', $text ); // `inline code` | |
| 137 | + $text = preg_replace( '/^\s{0,3}#{1,6}\s+/m', '', $text ); // # headings | |
| 138 | + $text = preg_replace( '/^\s{0,3}>\s?/m', '', $text ); // > blockquotes | |
| 139 | + $text = preg_replace( '/^\s{0,3}[-*+]\s+/m', '', $text ); // -, *, + bullet markers | |
| 123 | 140 | |
| 124 | 141 | return trim( $text ); |
| 125 | 142 | } |
| 126 | 143 | } |