Fix repeated 'don't know' detection and infinite questioning loop

PROBLEM SOLVED:
- AI was stuck in infinite questioning loop when child repeatedly said 'I don't know'
- Both chat and step-by-step modes weren't properly detecting repeated frustration
- System should switch to explanations after 2+ 'don't know' responses

FIXES IMPLEMENTED:
- Enhanced conversation history tracking with better selectors
- Added more comprehensive 'don't know' phrase detection (German variations)
- Improved server-side logic to detect repeated scenarios
- Added specific context handling for repeated_dont_know scenarios
- Better debugging logs to track conversation patterns
- Fixed variable name conflicts in server.js

RESULT:
- Children now get helpful explanations instead of endless questions
- System respects when child needs direct help vs. discovery guidance
- More natural conversation flow that adapts to child's needs
- Both German and English patterns properly detected
This commit is contained in:
root
2025-06-30 16:08:37 +02:00
parent c451be7be9
commit 1e8149fdcc
2 changed files with 66 additions and 5 deletions

View File

@@ -658,16 +658,22 @@ class KidsAIExplorer {
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isExpressingConfusion && !isAskingForHelp;
// Check conversation history for repeated "don't know" responses
const conversationHistory = Array.from(this.conversationContainer.querySelectorAll('.user-message .message-content p'))
.map(p => p.textContent.toLowerCase().trim())
.slice(-3); // Last 3 user responses
const conversationHistory = Array.from(this.conversationContainer.querySelectorAll('.user-message .message-content'))
.map(el => el.textContent.toLowerCase().trim())
.slice(-4); // Last 4 user responses
console.log('🔍 Recent conversation history:', conversationHistory);
const recentDontKnowCount = conversationHistory.filter(msg =>
msg.includes('weiß nicht') || msg.includes('weis nicht') || msg.includes('weiss nicht') ||
msg.includes("don't know") || msg.includes('i dont know') || msg.includes('keine ahnung')
msg.includes("don't know") || msg.includes('i dont know') || msg.includes('keine ahnung') ||
msg.includes('das weiß ich nicht') || msg.includes('das weiss ich nicht') ||
msg.includes('das weis ich nicht') || msg.includes('weiß ich auch nicht')
).length;
// If child has said "don't know" multiple times recently, they need help
console.log(`🔢 Recent "don't know" count: ${recentDontKnowCount}`);
// If child has said "don't know" 2 or more times recently, they need help
const needsExplanationDueToRepeatedDontKnow = recentDontKnowCount >= 2;
// Check if child is asking for a definition or explanation

View File

@@ -706,6 +706,61 @@ The child is asking for a definition, explanation, or clarification about locati
// Create contextual prompt for responding to the user's answer (existing logic)
const isGerman = language === 'de';
// Check if this looks like a repeated "don't know" scenario
const answerLowerChat = answer.toLowerCase().trim();
const isDontKnowResponse = answerLowerChat.includes("don't know") || answerLowerChat.includes("weiß nicht") ||
answerLowerChat.includes("weis nicht") || answerLowerChat.includes("weiss nicht") ||
answerLowerChat.includes("keine ahnung") || answerLowerChat.includes("ich weiß es nicht") ||
answerLowerChat.includes("das weiß ich nicht") || answerLowerChat.includes("weiß ich auch nicht");
console.log(`🔍 Answer analysis - isDontKnow: ${isDontKnowResponse}, answer: "${answer}"`);
// If this is a "don't know" response and we're in a loop, provide explanation
if (isDontKnowResponse && (context === 'repeated_dont_know' || instructions.includes('mehrmals') || instructions.includes('multiple times'))) {
console.log('🎯 Detected repeated "don\'t know" - providing explanation instead of more questions');
const explanationPrompt = isGerman ?
`Das Kind braucht jetzt eine klare Erklärung, da es mehrmals "weiß nicht" gesagt hat.
URSPRÜNGLICHE FRAGE: "${originalTopic || question}"
CONTEXT: Wiederholte "weiß nicht" Antworten
Gib eine einfache, verständliche Erklärung für Kinder. Verwende Beispiele aus dem Alltag. Erkläre Schritt für Schritt. Keine weiteren Fragen - das Kind braucht jetzt Wissen!` :
`The child needs a clear explanation now since they've said "don't know" multiple times.
ORIGINAL QUESTION: "${originalTopic || question}"
CONTEXT: Repeated "don't know" responses
Give a simple, understandable explanation for children. Use everyday examples. Explain step by step. No more questions - the child needs knowledge now!`;
try {
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: explanationPrompt
}
],
max_tokens: 300,
temperature: 0.6
});
const aiResponse = completion.choices[0]?.message?.content?.trim();
if (aiResponse && aiResponse.length > 10) {
console.log('✅ OpenAI explanation for repeated "don\'t know" generated successfully');
return res.json({
success: true,
response: aiResponse,
source: 'OpenAI GPT-4o-mini (Repeated Don\'t Know Explanation)'
});
}
} catch (openaiError) {
console.log('❌ OpenAI error for repeated don\'t know explanation:', openaiError.message);
}
}
const contextualPrompt = isGerman ?
`Du bist ein Socratic Lernbegleiter für Kinder. Ein Kind hat gerade auf eine Frage geantwortet.