From b7c7bbfcb1982e51cae00eb915b5d84bf7cdc31f Mon Sep 17 00:00:00 2001 From: root Date: Mon, 30 Jun 2025 11:32:19 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20Socratic=20teaching=20logi?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixed the critical issue where AI was giving direct answers instead of asking guiding questions - Now when child answers 'no' or 'don't know', system calls /api/ask for Socratic guidance - When child gives substantial answers, system calls /api/respond-to-answer for validation - Applied fix to both chat mode and step-by-step mode - Enhanced answer analysis to detect negative/unknown responses vs substantial answers - Updated response handling to work with both guidance format and simple response format This ensures the AI acts as a true Socratic teacher, guiding discovery rather than giving answers. --- html/kidsai/script-new.js | 160 +++++++++++++++++++++++++++++--------- 1 file changed, 123 insertions(+), 37 deletions(-) diff --git a/html/kidsai/script-new.js b/html/kidsai/script-new.js index df3583d..0b3f58a 100644 --- a/html/kidsai/script-new.js +++ b/html/kidsai/script-new.js @@ -415,21 +415,48 @@ class KidsAIExplorer { ? this.currentSteps[stepIndex].text : 'the current question'; - console.log('📤 Sending request to /api/respond-to-answer:', { answer, currentQuestion }); + // Analyze the answer to determine the appropriate response type + const answerLower = answer.toLowerCase().trim(); + const isNegative = answerLower === 'no' || answerLower === 'nein' || answerLower === 'nope' || answerLower === 'nei'; + const isDontKnow = answerLower.includes("don't know") || answerLower.includes('weiß nicht') || answerLower.includes('keine ahnung') || answer.trim().length < 3; - // Call server API for contextual response - const response = await fetch('/api/respond-to-answer', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - answer: answer, - question: currentQuestion, - language: this.currentLanguage, - stepIndex: stepIndex - }) - }); + let response; + + if (isNegative || isDontKnow) { + // For "no" or "don't know" answers, get Socratic guidance questions + console.log('📤 Sending guidance request to /api/ask for Socratic questioning'); + + const guidancePrompt = this.currentLanguage === 'de' + ? `Ein Kind hat "${answer}" geantwortet auf die Frage "${currentQuestion}". Führe es durch Socratic Fragen zur Entdeckung, ohne direkte Antworten zu geben.` + : `A child answered "${answer}" to the question "${currentQuestion}". Guide them through Socratic questions to discovery without giving direct answers.`; + + response = await fetch('/api/ask', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + question: guidancePrompt, + language: this.currentLanguage + }) + }); + } else { + // For substantial answers, acknowledge and validate + console.log('📤 Sending validation request to /api/respond-to-answer'); + + response = await fetch('/api/respond-to-answer', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + answer: answer, + question: currentQuestion, + language: this.currentLanguage, + stepIndex: stepIndex + }) + }); + } console.log('📥 Server response status:', response.status); @@ -440,7 +467,25 @@ class KidsAIExplorer { const data = await response.json(); console.log('✅ Server response data:', data); - if (data.success && data.response) { + let responseContent = ''; + + if (data.success) { + if (data.guidance && data.guidance.steps) { + // Response from /api/ask with Socratic guidance steps + const steps = data.guidance.steps.slice(0, 3); // Limit to 3 steps + responseContent = steps.map((step, index) => + `

${index + 1}. ${step.text}

` + ).join(''); + } else if (data.response) { + // Simple response from /api/respond-to-answer + responseContent = `

${data.response}

`; + } else { + // Fallback + responseContent = this.currentLanguage === 'de' + ? '

🤔 Das ist eine interessante Frage! Lass uns zusammen darüber nachdenken...

' + : '

🤔 That\'s an interesting question! Let\'s think about it together...

'; + } + responseDiv.innerHTML = `
@@ -448,7 +493,7 @@ class KidsAIExplorer { ${this.getTranslation('ai-teacher')}
-

${data.response}

+ ${responseContent}
`; @@ -509,26 +554,49 @@ class KidsAIExplorer { // Get the original topic for better context const originalQuestion = this.questionInput ? this.questionInput.value.trim() : ''; - console.log('📤 Sending chat request to /api/respond-to-answer:', { answer, currentQuestion, originalQuestion }); + // Analyze the answer to determine the appropriate response type + const answerLower = answer.toLowerCase().trim(); + const isNegative = answerLower === 'no' || answerLower === 'nein' || answerLower === 'nope' || answerLower === 'nei'; + const isDontKnow = answerLower.includes("don't know") || answerLower.includes('weiß nicht') || answerLower.includes('keine ahnung') || answer.trim().length < 3; - // Call server API for contextual response - const response = await fetch('/api/respond-to-answer', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - answer: answer, - question: currentQuestion, - originalTopic: originalQuestion, - language: this.currentLanguage, - stepIndex: questionIndex, - context: 'guided_learning_conversation', - instructions: this.currentLanguage === 'de' - ? 'Du bist ein geduldiger Lernbegleiter für Kinder. Gib NIEMALS direkte Antworten. Stelle stattdessen Folgefragen, die das Kind zum Nachdenken anregen. Verwende die Sokratische Methode - führe das Kind durch Fragen zur Entdeckung. Sei ermutigend und neugierig.' - : 'You are a patient learning companion for children. NEVER give direct answers. Instead, ask follow-up questions that encourage the child to think deeper. Use the Socratic method - guide the child to discovery through questions. Be encouraging and curious.' - }) - }); + let response; + + if (isNegative || isDontKnow) { + // For "no" or "don't know" answers, get Socratic guidance questions + console.log('📤 Sending guidance request to /api/ask for Socratic questioning'); + + const guidancePrompt = this.currentLanguage === 'de' + ? `Ein Kind hat "${answer}" geantwortet auf die Frage "${currentQuestion}" über das Thema "${originalQuestion}". Führe es durch Socratic Fragen zur Entdeckung, ohne direkte Antworten zu geben.` + : `A child answered "${answer}" to the question "${currentQuestion}" about the topic "${originalQuestion}". Guide them through Socratic questions to discovery without giving direct answers.`; + + response = await fetch('/api/ask', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + question: guidancePrompt, + language: this.currentLanguage + }) + }); + } else { + // For substantial answers, acknowledge and validate + console.log('📤 Sending validation request to /api/respond-to-answer'); + + response = await fetch('/api/respond-to-answer', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + answer: answer, + question: currentQuestion, + originalTopic: originalQuestion, + language: this.currentLanguage, + stepIndex: questionIndex + }) + }); + } console.log('📥 Chat server response status:', response.status); @@ -539,7 +607,25 @@ class KidsAIExplorer { const data = await response.json(); console.log('✅ Chat server response data:', data); - if (data.success && data.response) { + let responseContent = ''; + + if (data.success) { + if (data.guidance && data.guidance.steps) { + // Response from /api/ask with Socratic guidance steps + const steps = data.guidance.steps.slice(0, 3); // Limit to 3 steps + responseContent = steps.map((step, index) => + `

${index + 1}. ${step.text}

` + ).join(''); + } else if (data.response) { + // Simple response from /api/respond-to-answer + responseContent = `

${data.response}

`; + } else { + // Fallback + responseContent = this.currentLanguage === 'de' + ? '

🤔 Das ist eine interessante Frage! Lass uns zusammen darüber nachdenken...

' + : '

🤔 That\'s an interesting question! Let\'s think about it together...

'; + } + // Add AI response bubble const responseBubble = document.createElement('div'); responseBubble.className = 'chat-message ai-message'; @@ -549,7 +635,7 @@ class KidsAIExplorer { ${this.getTranslation('ai-teacher')}
-

${data.response}

+ ${responseContent}
`;