Add specific question detection: Detect when child asks 'woher kommt die farbe?' and provide concrete explanations instead of more abstract questions

This commit is contained in:
root
2025-06-30 13:32:06 +02:00
parent 90096bb361
commit d693ac7228
2 changed files with 71 additions and 35 deletions

View File

@@ -432,6 +432,17 @@ class KidsAIExplorer {
answerLower.includes('was heißt') || answerLower.includes('what means') ||
(answerLower.includes('?') && (answerLower.includes('ist ein') || answerLower.includes('is a')));
// Check if child is asking a specific "how/why" question that deserves explanation
const isAskingSpecificQuestion = answerLower.includes('woher kommt') || answerLower.includes('where does') ||
answerLower.includes('wie entsteht') || answerLower.includes('how does') ||
answerLower.includes('warum ist das so') || answerLower.includes('why is that') ||
answerLower.includes('ich verstehe nicht') || answerLower.includes("i don't understand") ||
(answerLower.includes('?') && (answerLower.includes('woher') || answerLower.includes('wie') || answerLower.includes('warum'))) ||
(answer.length > 30 && answerLower.includes('verstehe') && answerLower.includes('nicht'));
// Combine both definition and specific question detection
const needsExplanation = isAskingForDefinition || isAskingSpecificQuestion;
// Check if it's a substantial answer (has thinking beyond just "don't know")
const hasSubstantialThinking = answer.length > 15 && (
answerLower.includes('vielleicht') || answerLower.includes('maybe') || answerLower.includes('könnte') || answerLower.includes('could') ||
@@ -441,13 +452,18 @@ class KidsAIExplorer {
);
// Pure "don't know" without additional thinking
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isAskingForDefinition;
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !needsExplanation;
let response;
if (isAskingForDefinition) {
// Child is asking for a definition - provide simple, concrete explanation
console.log('📤 Sending definition request to /api/respond-to-answer');
if (needsExplanation) {
// Child is asking for explanation - provide simple, concrete explanation
console.log('📤 Sending explanation request to /api/respond-to-answer');
const contextType = isAskingForDefinition ? 'definition_request' : 'specific_question';
const instructionText = this.currentLanguage === 'de'
? 'Das Kind stellt eine spezifische Frage und möchte verstehen. Gib eine EINFACHE, konkrete Erklärung für Kinder. Beantworte die spezifische Frage direkt aber kindgerecht. Verwende Analogien und alltägliche Vergleiche. Für "woher kommt die farbe" erkläre einfach, dass weißes Licht alle Farben enthält, wie ein unsichtbarer Regenbogen. Keine weiteren Fragen stellen.'
: 'The child is asking a specific question and wants to understand. Give a SIMPLE, concrete explanation for children. Answer the specific question directly but age-appropriately. Use analogies and everyday comparisons. For "where does color come from" explain simply that white light contains all colors, like an invisible rainbow. Don\'t ask more questions.';
response = await fetch('/api/respond-to-answer', {
method: 'POST',
@@ -459,11 +475,9 @@ class KidsAIExplorer {
question: currentQuestion,
originalTopic: originalQuestion,
language: this.currentLanguage,
context: 'definition_request',
context: contextType,
stepIndex: questionIndex,
instructions: this.currentLanguage === 'de'
? 'Das Kind fragt nach einer Definition. Gib eine EINFACHE, konkrete Erklärung für Kinder. Verwende alltägliche Vergleiche und lade zum Ausprobieren ein. Zum Beispiel: "Ein Prisma ist wie ein spezielles Glas, das Licht in Regenbogenfarben zerlegt. Du kannst das mit einem Wasserglas am Fenster ausprobieren!" Keine weiteren Fragen stellen.'
: 'The child is asking for a definition. Give a SIMPLE, concrete explanation for children. Use everyday comparisons and invite experimentation. For example: "A prism is like a special glass that breaks light into rainbow colors. You can try this with a water glass at the window!" Don\'t ask more questions.'
instructions: instructionText
})
});
} else if (isNegative || isPureDontKnow) {
@@ -616,6 +630,17 @@ class KidsAIExplorer {
answerLower.includes('was heißt') || answerLower.includes('what means') ||
(answerLower.includes('?') && (answerLower.includes('ist ein') || answerLower.includes('is a')));
// Check if child is asking a specific "how/why" question that deserves explanation
const isAskingSpecificQuestion = answerLower.includes('woher kommt') || answerLower.includes('where does') ||
answerLower.includes('wie entsteht') || answerLower.includes('how does') ||
answerLower.includes('warum ist das so') || answerLower.includes('why is that') ||
answerLower.includes('ich verstehe nicht') || answerLower.includes("i don't understand") ||
(answerLower.includes('?') && (answerLower.includes('woher') || answerLower.includes('wie') || answerLower.includes('warum'))) ||
(answer.length > 30 && answerLower.includes('verstehe') && answerLower.includes('nicht'));
// Combine both definition and specific question detection
const needsExplanation = isAskingForDefinition || isAskingSpecificQuestion;
// Check if it's a substantial answer (has thinking beyond just "don't know")
const hasSubstantialThinking = answer.length > 15 && (
answerLower.includes('vielleicht') || answerLower.includes('maybe') || answerLower.includes('könnte') || answerLower.includes('could') ||
@@ -625,13 +650,18 @@ class KidsAIExplorer {
);
// Pure "don't know" without additional thinking
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isAskingForDefinition;
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !needsExplanation;
let response;
if (isAskingForDefinition) {
// Child is asking for a definition - provide simple, concrete explanation
console.log('📤 Sending definition request to /api/respond-to-answer');
if (needsExplanation) {
// Child is asking for explanation - provide simple, concrete explanation
console.log('📤 Sending explanation request to /api/respond-to-answer');
const contextType = isAskingForDefinition ? 'definition_request' : 'specific_question';
const instructionText = this.currentLanguage === 'de'
? 'Das Kind stellt eine spezifische Frage und möchte verstehen. Gib eine EINFACHE, konkrete Erklärung für Kinder. Beantworte die spezifische Frage direkt aber kindgerecht. Verwende Analogien und alltägliche Vergleiche. Für "woher kommt die farbe" erkläre einfach, dass weißes Licht alle Farben enthält, wie ein unsichtbarer Regenbogen. Keine weiteren Fragen stellen.'
: 'The child is asking a specific question and wants to understand. Give a SIMPLE, concrete explanation for children. Answer the specific question directly but age-appropriately. Use analogies and everyday comparisons. For "where does color come from" explain simply that white light contains all colors, like an invisible rainbow. Don\'t ask more questions.';
response = await fetch('/api/respond-to-answer', {
method: 'POST',
@@ -643,11 +673,9 @@ class KidsAIExplorer {
question: currentQuestion,
originalTopic: originalQuestion,
language: this.currentLanguage,
context: 'definition_request',
context: contextType,
stepIndex: questionIndex,
instructions: this.currentLanguage === 'de'
? 'Das Kind fragt nach einer Definition. Gib eine EINFACHE, konkrete Erklärung für Kinder. Verwende alltägliche Vergleiche und lade zum Ausprobieren ein. Zum Beispiel: "Ein Prisma ist wie ein spezielles Glas, das Licht in Regenbogenfarben zerlegt. Du kannst das mit einem Wasserglas am Fenster ausprobieren!" Keine weiteren Fragen stellen.'
: 'The child is asking for a definition. Give a SIMPLE, concrete explanation for children. Use everyday comparisons and invite experimentation. For example: "A prism is like a special glass that breaks light into rainbow colors. You can try this with a water glass at the window!" Don\'t ask more questions.'
instructions: instructionText
})
});
} else if (isNegative || isPureDontKnow) {

View File

@@ -557,17 +557,17 @@ app.post('/api/respond-to-answer', async (req, res) => {
console.log(`📝 Generating response to answer: "${answer}" for question: "${question}" with context: ${context}`);
// Handle definition requests specially
if (context === 'definition_request' && instructions) {
// For definition requests, use the provided instructions
const definitionPrompt = `${instructions}
// Handle definition requests and specific questions specially
if ((context === 'definition_request' || context === 'specific_question') && instructions) {
// For definition requests and specific questions, use the provided instructions
const explanationPrompt = `${instructions}
CHILD'S QUESTION: "${answer}"
CONTEXT: The child asked this while learning about: "${question}"
Provide a simple, concrete explanation for children using everyday comparisons and practical examples.`;
// Try OpenAI first for definitions
// Try OpenAI first for explanations
if (process.env.OPENAI_API_KEY) {
try {
const completion = await openai.chat.completions.create({
@@ -575,46 +575,54 @@ Provide a simple, concrete explanation for children using everyday comparisons a
messages: [
{
role: "system",
content: definitionPrompt
content: explanationPrompt
}
],
max_tokens: 300,
max_tokens: 400,
temperature: 0.6
});
const aiResponse = completion.choices[0]?.message?.content?.trim();
if (aiResponse && aiResponse.length > 10) {
console.log('✅ OpenAI definition response generated successfully');
console.log('✅ OpenAI explanation response generated successfully');
return res.json({
success: true,
response: aiResponse,
source: 'OpenAI GPT-3.5 (Definition)'
source: `OpenAI GPT-3.5 (${context})`
});
}
} catch (openaiError) {
console.log('❌ OpenAI error for definition:', openaiError.message);
console.log('❌ OpenAI error for explanation:', openaiError.message);
}
}
// Fallback for definitions
// Enhanced fallbacks for specific questions
const answerLower = answer.toLowerCase();
let definitionFallback;
let explanationFallback;
if (answerLower.includes('prisma') || answerLower.includes('prism')) {
definitionFallback = language === 'de' ?
if (answerLower.includes('woher kommt') && answerLower.includes('farbe')) {
explanationFallback = language === 'de' ?
'🌈 Weißes Sonnenlicht ist wie ein unsichtbarer Regenbogen! Es enthält alle Farben gleichzeitig - rot, orange, gelb, grün, blau, violett. Wenn das Licht durch Wassertropfen geht, werden diese versteckten Farben sichtbar gemacht - wie Magie! Du kannst das mit einem Gartenschlauch im Sonnenschein ausprobieren!' :
'🌈 White sunlight is like an invisible rainbow! It contains all colors at once - red, orange, yellow, green, blue, violet. When light goes through water drops, these hidden colors become visible - like magic! You can try this with a garden hose in the sunshine!';
} else if (answerLower.includes('prisma') || answerLower.includes('prism')) {
explanationFallback = language === 'de' ?
'🔍 Ein Prisma ist wie ein magisches Glas, das weißes Licht in alle Regenbogenfarben zerlegt! Du kannst das selbst ausprobieren: Halte ein Glas Wasser ins Sonnenlicht und schau, welche Farben entstehen!' :
'🔍 A prism is like a magic glass that breaks white light into all the rainbow colors! You can try this yourself: hold a glass of water in the sunlight and see what colors appear!';
} else if (answerLower.includes('verstehe nicht') || answerLower.includes("don't understand")) {
explanationFallback = language === 'de' ?
'💡 Kein Problem! Das ist wirklich faszinierend. Lass es mich einfacher erklären: Stell dir vor, weißes Licht ist wie eine Süßigkeitentüte mit vielen verschiedenen Farben drin. Normalerweise siehst du nur die Tüte (weißes Licht), aber Wasser kann die Tüte "öffnen" und alle bunten Süßigkeiten (Farben) zeigen!' :
'💡 No problem! This is really fascinating. Let me explain it simpler: Imagine white light is like a candy bag with many different colors inside. Usually you only see the bag (white light), but water can "open" the bag and show all the colorful candies (colors)!';
} else {
definitionFallback = language === 'de' ?
'🤔 Das ist eine tolle Frage! Lass uns das zusammen erforschen...' :
'🤔 That\'s a great question! Let\'s explore this together...';
explanationFallback = language === 'de' ?
'🤔 Das ist eine wirklich gute Frage! Lass mich das Schritt für Schritt erklären...' :
'🤔 That\'s a really good question! Let me explain this step by step...';
}
return res.json({
success: true,
response: definitionFallback,
source: 'Fallback Definition'
response: explanationFallback,
source: 'Enhanced Explanation Fallback'
});
}