Spaces:
Running
Running
fix: clean AI responses to remove JSON artifacts and markdown formatting
Browse files- Remove leading/trailing quotes and colons from responses
- Strip markdown formatting (asterisks, underscores, hash symbols)
- Normalize whitespace to prevent display issues
- Apply cleaning to both hints and contextualization responses
- src/aiService.js +25 -2
src/aiService.js
CHANGED
|
@@ -72,7 +72,19 @@ class OpenRouterService {
|
|
| 72 |
return 'Unable to generate hint at this time';
|
| 73 |
}
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
} catch (error) {
|
| 77 |
console.error('Error generating contextual hint:', error);
|
| 78 |
return 'Unable to generate hint at this time';
|
|
@@ -195,7 +207,18 @@ Passage: "${passage}"`
|
|
| 195 |
}
|
| 196 |
|
| 197 |
const data = await response.json();
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
console.log('Contextualization received:', content);
|
| 200 |
return content;
|
| 201 |
} catch (error) {
|
|
|
|
| 72 |
return 'Unable to generate hint at this time';
|
| 73 |
}
|
| 74 |
|
| 75 |
+
let content = data.choices[0].message.content.trim();
|
| 76 |
+
|
| 77 |
+
// Clean up AI response artifacts
|
| 78 |
+
content = content
|
| 79 |
+
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 80 |
+
.replace(/^\s*:+\s*/, '') // Remove leading colons
|
| 81 |
+
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 82 |
+
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 83 |
+
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
| 84 |
+
.replace(/\s+/g, ' ') // Normalize whitespace
|
| 85 |
+
.trim();
|
| 86 |
+
|
| 87 |
+
return content;
|
| 88 |
} catch (error) {
|
| 89 |
console.error('Error generating contextual hint:', error);
|
| 90 |
return 'Unable to generate hint at this time';
|
|
|
|
| 207 |
}
|
| 208 |
|
| 209 |
const data = await response.json();
|
| 210 |
+
let content = data.choices[0].message.content.trim();
|
| 211 |
+
|
| 212 |
+
// Clean up AI response artifacts
|
| 213 |
+
content = content
|
| 214 |
+
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 215 |
+
.replace(/^\s*:+\s*/, '') // Remove leading colons
|
| 216 |
+
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 217 |
+
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 218 |
+
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
| 219 |
+
.replace(/\s+/g, ' ') // Normalize whitespace
|
| 220 |
+
.trim();
|
| 221 |
+
|
| 222 |
console.log('Contextualization received:', content);
|
| 223 |
return content;
|
| 224 |
} catch (error) {
|