Fix step-by-step question parsing and improve AI prompts
FIXED ISSUES: - AI was giving introductions instead of direct numbered questions - Parser wasn't correctly extracting numbered questions - Missing detailed debugging for response parsing IMPROVEMENTS: - Enhanced prompts to explicitly request ONLY numbered questions - Improved parsing function with detailed logging - Better numbered question detection with regex - Fallback question detection for non-numbered items - Comprehensive debugging output for troubleshooting RESULT: - Step-by-step mode now properly displays 3-4 Socratic questions - Questions guide children through discovery process - Better instruction following with GPT-4o-mini model
This commit is contained in:
@@ -130,12 +130,12 @@ async function getOpenAIGuidance(question, language) {
|
||||
const isGerman = language === 'de';
|
||||
|
||||
const systemPrompt = isGerman
|
||||
? "Du bist ein geduldiger Lehrer für Kinder. Anstatt direkte Antworten zu geben, führe das Kind Schritt für Schritt zum Verständnis. Stelle 3-4 aufbauende Fragen, die das Kind zum Nachdenken anregen und ihm helfen, die Antwort selbst zu entdecken. Jede Frage sollte auf der vorherigen aufbauen und dem Kind helfen, das Konzept zu verstehen. Verwende einfache Sprache und ermutigende Worte. Formatiere als nummerierte Liste."
|
||||
: "You are a patient teacher for children. Instead of giving direct answers, guide the child step by step to understanding. Ask 3-4 building questions that encourage the child to think and help them discover the answer themselves. Each question should build on the previous one and help the child understand the concept. Use simple language and encouraging words. Format as a numbered list.";
|
||||
? "Du bist ein geduldiger Lehrer für Kinder. Anstatt direkte Antworten zu geben, führe das Kind Schritt für Schritt zum Verständnis. Stelle genau 3-4 aufbauende Fragen, die das Kind zum Nachdenken anregen und ihm helfen, die Antwort selbst zu entdecken. Jede Frage sollte auf der vorherigen aufbauen und dem Kind helfen, das Konzept zu verstehen. Verwende einfache Sprache und ermutigende Worte. Formatiere IMMER als nummerierte Liste: 1. [Frage] 2. [Frage] 3. [Frage] usw. Gib NUR die nummerierten Fragen zurück, keine Einleitung."
|
||||
: "You are a patient teacher for children. Instead of giving direct answers, guide the child step by step to understanding. Ask exactly 3-4 building questions that encourage the child to think and help them discover the answer themselves. Each question should build on the previous one and help the child understand the concept. Use simple language and encouraging words. ALWAYS format as a numbered list: 1. [Question] 2. [Question] 3. [Question] etc. Return ONLY the numbered questions, no introduction.";
|
||||
|
||||
const userPrompt = isGerman
|
||||
? `Ein Kind hat gefragt: "${question}". Führe es Schritt für Schritt zum Verständnis, ohne die Antwort direkt zu verraten. Stelle aufbauende Fragen, die dem Kind helfen, selbst zu denken und die Antwort zu entdecken. Jede Frage sollte das Kind näher zur Lösung führen.`
|
||||
: `A child asked: "${question}". Guide them step by step to understanding without giving away the answer directly. Ask building questions that help the child think for themselves and discover the answer. Each question should bring the child closer to the solution.`;
|
||||
? `Ein Kind hat gefragt: "${question}". Führe es mit genau 3-4 nummerierten Fragen Schritt für Schritt zum Verständnis, ohne die Antwort direkt zu verraten. Beispiel: 1. Wie fühlt sich Wasser an, wenn du es berührst? 2. Was passiert... usw. NUR nummerierte Fragen, keine Einleitung!`
|
||||
: `A child asked: "${question}". Guide them with exactly 3-4 numbered questions step by step to understanding without giving away the answer directly. Example: 1. How does water feel when you touch it? 2. What happens... etc. ONLY numbered questions, no introduction!`;
|
||||
|
||||
try {
|
||||
const completion = await openai.chat.completions.create({
|
||||
@@ -149,7 +149,8 @@ async function getOpenAIGuidance(question, language) {
|
||||
});
|
||||
|
||||
const aiResponse = completion.choices[0]?.message?.content || '';
|
||||
console.log('✅ OpenAI response received:', aiResponse.substring(0, 100) + '...');
|
||||
console.log('✅ OpenAI response received:', aiResponse);
|
||||
console.log('🔍 Full response for debugging:', JSON.stringify(aiResponse));
|
||||
|
||||
// Parse the response into steps
|
||||
const steps = parseOpenAIResponseToSteps(aiResponse, language);
|
||||
@@ -169,27 +170,44 @@ async function getOpenAIGuidance(question, language) {
|
||||
|
||||
// Function to parse OpenAI response into thinking steps
|
||||
function parseOpenAIResponseToSteps(text, language) {
|
||||
console.log('🔧 Parsing response:', text);
|
||||
|
||||
const lines = text.split('\n').filter(line => line.trim());
|
||||
const steps = [];
|
||||
|
||||
lines.forEach((line, index) => {
|
||||
const trimmed = line.trim();
|
||||
// Look for numbered items or questions
|
||||
if (trimmed && (trimmed.match(/^\d+\./) || trimmed.includes('?') || trimmed.length > 10)) {
|
||||
// Clean up numbering and formatting
|
||||
const cleaned = trimmed.replace(/^\d+\.\s*/, '').replace(/^-\s*/, '').trim();
|
||||
if (cleaned.length > 5) {
|
||||
console.log(`🔍 Processing line ${index}: "${trimmed}"`);
|
||||
|
||||
// Look for numbered items (1. 2. 3. etc.)
|
||||
const numberedMatch = trimmed.match(/^\d+\.\s*(.+)/);
|
||||
if (numberedMatch) {
|
||||
const questionText = numberedMatch[1].trim();
|
||||
if (questionText.length > 5) {
|
||||
steps.push({
|
||||
id: steps.length + 1,
|
||||
text: cleaned,
|
||||
text: questionText,
|
||||
type: 'question'
|
||||
});
|
||||
console.log(`✅ Added step: ${questionText}`);
|
||||
}
|
||||
}
|
||||
// Also look for lines with question marks that might not be numbered
|
||||
else if (trimmed.includes('?') && trimmed.length > 10 && !trimmed.toLowerCase().includes('frage') && !trimmed.toLowerCase().includes('question')) {
|
||||
steps.push({
|
||||
id: steps.length + 1,
|
||||
text: trimmed,
|
||||
type: 'question'
|
||||
});
|
||||
console.log(`✅ Added question: ${trimmed}`);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`📋 Total steps parsed: ${steps.length}`);
|
||||
|
||||
// Ensure we have at least 2 steps
|
||||
if (steps.length < 2) {
|
||||
console.log('⚠️ Not enough steps, using fallback');
|
||||
const fallback = getFallbackGuidance('', language);
|
||||
return fallback.steps.slice(0, 3);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user