Replace hard-coded question patterns with AI-based question detection
- Removed rigid pattern matching for 'was ist', 'was bedeutet', etc. - Added intelligent AI-based question detection using GPT-4o-mini - Server now analyzes child responses to determine if they're asking questions - More flexible and natural - can handle any way children might ask questions - Falls back gracefully if AI detection fails - Maintains all existing functionality while being much more adaptable
This commit is contained in:
@@ -717,18 +717,17 @@ class KidsAIExplorer {
|
||||
// If child has said "don't know" 2 or more times recently, OR asked a question then said don't know, they need help
|
||||
const needsExplanationDueToRepeatedDontKnow = recentDontKnowCount >= 2 || isAskingQuestionThenDontKnowStep;
|
||||
|
||||
// Check if child is asking for a definition or explanation
|
||||
const isAskingForDefinition = answerLower.startsWith('was ist') || answerLower.startsWith('what is') ||
|
||||
answerLower.includes('was bedeutet') || answerLower.includes('what does') ||
|
||||
answerLower.includes('was heißt') || answerLower.includes('what means') ||
|
||||
answerLower.includes('was hat') || answerLower.includes('what has') ||
|
||||
answerLower.includes('wo ist') || answerLower.includes('where is') ||
|
||||
answerLower.includes('wo sind') || answerLower.includes('where are') ||
|
||||
answerLower.includes('wie funktioniert') || answerLower.includes('how does') ||
|
||||
answerLower.includes('warum ist') || answerLower.includes('why is') ||
|
||||
answerLower.includes('was passiert') || answerLower.includes('what happens') ||
|
||||
answerLower.includes('was geschieht') || answerLower.includes('what occurs') ||
|
||||
(answerLower.includes('?') && (answerLower.includes('ist ein') || answerLower.includes('is a')));
|
||||
// Use AI to determine if child is asking for information/explanation
|
||||
let isAskingForDefinition = false;
|
||||
|
||||
// Quick check: if it contains a question mark and isn't just expressing uncertainty
|
||||
if (answer.includes('?') && !isPureDontKnow && !isExpressingConfusion) {
|
||||
isAskingForDefinition = true;
|
||||
} else {
|
||||
// For non-obvious cases, we'll let the AI determine this in the server response
|
||||
// The server can detect question patterns better than hard-coded rules
|
||||
isAskingForDefinition = false;
|
||||
}
|
||||
|
||||
let response;
|
||||
|
||||
|
||||
@@ -779,6 +779,114 @@ Give a simple, understandable explanation for children. Use everyday examples. E
|
||||
}
|
||||
}
|
||||
|
||||
// Use AI to intelligently detect if child is asking a question that needs a direct answer
|
||||
if (process.env.OPENAI_API_KEY && !isDontKnowResponse && context !== 'repeated_dont_know') {
|
||||
console.log('🤖 Using AI to analyze if child is asking a question...');
|
||||
|
||||
try {
|
||||
const questionDetectionPrompt = isGerman ?
|
||||
`Analysiere diese Antwort eines Kindes: "${answer}"
|
||||
|
||||
Kontext: Das Kind wurde gefragt: "${question}"
|
||||
|
||||
Bestimme: Stellt das Kind eine direkte Frage, die eine Erklärung braucht?
|
||||
|
||||
Beispiele für JA:
|
||||
- "Was passiert denn da?"
|
||||
- "Wie geht das?"
|
||||
- "Warum ist das so?"
|
||||
- "Was bedeutet das?"
|
||||
- "Wo ist das?"
|
||||
|
||||
Beispiele für NEIN:
|
||||
- "Ich weiß nicht"
|
||||
- "Vielleicht wegen der Temperatur"
|
||||
- "Ist es wegen dem Wetter?"
|
||||
- "Hmm, keine Ahnung"
|
||||
|
||||
Antworte nur mit "JA" oder "NEIN".` :
|
||||
`Analyze this child's response: "${answer}"
|
||||
|
||||
Context: The child was asked: "${question}"
|
||||
|
||||
Determine: Is the child asking a direct question that needs an explanation?
|
||||
|
||||
Examples of YES:
|
||||
- "What happens there?"
|
||||
- "How does that work?"
|
||||
- "Why is that?"
|
||||
- "What does that mean?"
|
||||
- "Where is that?"
|
||||
|
||||
Examples of NO:
|
||||
- "I don't know"
|
||||
- "Maybe because of temperature"
|
||||
- "Is it because of weather?"
|
||||
- "Hmm, no idea"
|
||||
|
||||
Reply only with "YES" or "NO".`;
|
||||
|
||||
const detectionCompletion = await openai.chat.completions.create({
|
||||
model: "gpt-4o-mini",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: questionDetectionPrompt
|
||||
}
|
||||
],
|
||||
max_tokens: 10,
|
||||
temperature: 0.1
|
||||
});
|
||||
|
||||
const detectionResult = detectionCompletion.choices[0]?.message?.content?.trim().toUpperCase();
|
||||
console.log(`🔍 AI question detection result: "${detectionResult}"`);
|
||||
|
||||
if (detectionResult === 'JA' || detectionResult === 'YES') {
|
||||
console.log('📤 AI detected a question - providing explanation');
|
||||
|
||||
const explanationPrompt = isGerman ?
|
||||
`Das Kind stellt eine direkte Frage und braucht eine Erklärung.
|
||||
|
||||
KIND'S FRAGE: "${answer}"
|
||||
KONTEXT: Wir haben über "${originalTopic || question}" gesprochen
|
||||
|
||||
Gib eine klare, kindgerechte Antwort. Verwende einfache Sprache und Beispiele. Lade das Kind ein, mehr zu fragen oder selbst zu experimentieren. Beantworte die spezifische Frage des Kindes direkt.` :
|
||||
`The child is asking a direct question and needs an explanation.
|
||||
|
||||
CHILD'S QUESTION: "${answer}"
|
||||
CONTEXT: We were discussing "${originalTopic || question}"
|
||||
|
||||
Give a clear, child-friendly answer. Use simple language and examples. Invite the child to ask more or experiment themselves. Answer the child's specific question directly.`;
|
||||
|
||||
const explanationCompletion = await openai.chat.completions.create({
|
||||
model: "gpt-4o-mini",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: explanationPrompt
|
||||
}
|
||||
],
|
||||
max_tokens: 300,
|
||||
temperature: 0.6
|
||||
});
|
||||
|
||||
const explanationResponse = explanationCompletion.choices[0]?.message?.content?.trim();
|
||||
|
||||
if (explanationResponse && explanationResponse.length > 10) {
|
||||
console.log('✅ AI-detected question explanation generated successfully');
|
||||
return res.json({
|
||||
success: true,
|
||||
response: explanationResponse,
|
||||
source: 'OpenAI GPT-4o-mini (AI-Detected Question)'
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (aiError) {
|
||||
console.log('❌ AI question detection error:', aiError.message);
|
||||
// Continue with normal flow if AI detection fails
|
||||
}
|
||||
}
|
||||
|
||||
const contextualPrompt = isGerman ?
|
||||
`Du bist ein Socratic Lernbegleiter für Kinder. Ein Kind hat gerade auf eine Frage geantwortet.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user