Spaces:
Sleeping
Sleeping
Commit ·
481f08a
1
Parent(s): 6c3c0aa
even for the correct answer, the system should provide a rationale as to
Browse files
src/ai/dev.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
|
|
| 1 |
import { config } from 'dotenv';
|
| 2 |
config();
|
| 3 |
|
| 4 |
import '@/ai/flows/summarize-image.ts';
|
| 5 |
import '@/ai/flows/generate-mcq.ts';
|
| 6 |
import '@/ai/flows/explain-incorrect-answer-flow.ts';
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import { config } from 'dotenv';
|
| 3 |
config();
|
| 4 |
|
| 5 |
import '@/ai/flows/summarize-image.ts';
|
| 6 |
import '@/ai/flows/generate-mcq.ts';
|
| 7 |
import '@/ai/flows/explain-incorrect-answer-flow.ts';
|
| 8 |
+
import '@/ai/flows/explain-correct-answer-flow.ts';
|
| 9 |
+
|
src/ai/flows/explain-correct-answer-flow.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
'use server';
|
| 3 |
+
/**
|
| 4 |
+
* @fileOverview Provides an explanation for why a selected MCQ answer is correct.
|
| 5 |
+
*
|
| 6 |
+
* - explainCorrectAnswer - A function that generates an explanation for a correct MCQ answer.
|
| 7 |
+
* - ExplainCorrectAnswerInput - The input type for the explainCorrectAnswer function.
|
| 8 |
+
* - ExplainCorrectAnswerOutput - The return type for the explainCorrectAnswer function.
|
| 9 |
+
*/
|
| 10 |
+
|
| 11 |
+
import {ai} from '@/ai/genkit';
|
| 12 |
+
import {z} from 'genkit';
|
| 13 |
+
|
| 14 |
+
const ExplainCorrectAnswerInputSchema = z.object({
|
| 15 |
+
question: z.string().describe('The multiple-choice question that was asked.'),
|
| 16 |
+
options: z.array(z.string()).describe('All the options provided for the question.'),
|
| 17 |
+
correctAnswer: z.string().describe('The actual correct answer to the question.'),
|
| 18 |
+
});
|
| 19 |
+
export type ExplainCorrectAnswerInput = z.infer<typeof ExplainCorrectAnswerInputSchema>;
|
| 20 |
+
|
| 21 |
+
const ExplainCorrectAnswerOutputSchema = z.object({
|
| 22 |
+
explanation: z.string().describe('The explanation of why the answer is correct.'),
|
| 23 |
+
});
|
| 24 |
+
export type ExplainCorrectAnswerOutput = z.infer<typeof ExplainCorrectAnswerOutputSchema>;
|
| 25 |
+
|
| 26 |
+
export async function explainCorrectAnswer(input: ExplainCorrectAnswerInput): Promise<ExplainCorrectAnswerOutput> {
|
| 27 |
+
return explainCorrectAnswerFlow(input);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const prompt = ai.definePrompt({
|
| 31 |
+
name: 'explainCorrectAnswerPrompt',
|
| 32 |
+
input: {schema: ExplainCorrectAnswerInputSchema},
|
| 33 |
+
output: {schema: ExplainCorrectAnswerOutputSchema},
|
| 34 |
+
prompt: `You are an expert educator. The following multiple-choice question was asked:
|
| 35 |
+
Question: "{{question}}"
|
| 36 |
+
Options:
|
| 37 |
+
{{#each options}}
|
| 38 |
+
- {{this}}
|
| 39 |
+
{{/each}}
|
| 40 |
+
The correct answer is "{{correctAnswer}}".
|
| 41 |
+
|
| 42 |
+
Explain concisely why "{{correctAnswer}}" is the correct choice for the question.
|
| 43 |
+
Your explanation should be helpful for the student to understand the concept.
|
| 44 |
+
`,
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
const explainCorrectAnswerFlow = ai.defineFlow(
|
| 48 |
+
{
|
| 49 |
+
name: 'explainCorrectAnswerFlow',
|
| 50 |
+
inputSchema: ExplainCorrectAnswerInputSchema,
|
| 51 |
+
outputSchema: ExplainCorrectAnswerOutputSchema,
|
| 52 |
+
},
|
| 53 |
+
async input => {
|
| 54 |
+
const {output} = await prompt(input);
|
| 55 |
+
return output!;
|
| 56 |
+
}
|
| 57 |
+
);
|
src/components/chatbot/chatbot.tsx
CHANGED
|
@@ -8,6 +8,7 @@ import { ScrollArea } from '@/components/ui/scroll-area';
|
|
| 8 |
import { Skeleton } from '@/components/ui/skeleton';
|
| 9 |
import { generateMCQ, type GenerateMCQOutput } from '@/ai/flows/generate-mcq';
|
| 10 |
import { explainIncorrectAnswer } from '@/ai/flows/explain-incorrect-answer-flow';
|
|
|
|
| 11 |
import type { ChatMessage as ChatMessageType } from '@/types';
|
| 12 |
import { ChatMessage } from './chat-message';
|
| 13 |
import { useToast } from '@/hooks/use-toast';
|
|
@@ -44,7 +45,7 @@ export function Chatbot({ imageDataUri, journeyTitle }: ChatbotProps) {
|
|
| 44 |
setIsAwaitingAnswer(false);
|
| 45 |
setHasAnsweredCorrectly(false);
|
| 46 |
setCurrentMCQ(null);
|
| 47 |
-
setIncorrectAttempts([]);
|
| 48 |
|
| 49 |
try {
|
| 50 |
const mcqData = await generateMCQ({ imageDataUri });
|
|
@@ -107,10 +108,24 @@ export function Chatbot({ imageDataUri, journeyTitle }: ChatbotProps) {
|
|
| 107 |
|
| 108 |
if (isCorrect) {
|
| 109 |
addMessage({ sender: 'ai', type: 'feedback', text: "That's correct! Well done.", isCorrect: true });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
setHasAnsweredCorrectly(true);
|
| 111 |
-
setIncorrectAttempts([]);
|
|
|
|
| 112 |
} else {
|
| 113 |
-
setIncorrectAttempts(prev => [...prev, option]);
|
| 114 |
setIsLoading(true);
|
| 115 |
try {
|
| 116 |
const explanationResult = await explainIncorrectAnswer({
|
|
@@ -121,7 +136,7 @@ export function Chatbot({ imageDataUri, journeyTitle }: ChatbotProps) {
|
|
| 121 |
});
|
| 122 |
addMessage({ sender: 'ai', type: 'feedback', text: explanationResult.explanation, isCorrect: false });
|
| 123 |
} catch (error) {
|
| 124 |
-
console.error("Error fetching explanation:", error);
|
| 125 |
const errorMsg = error instanceof Error ? error.message : "An unknown error occurred";
|
| 126 |
addMessage({ sender: 'ai', type: 'error', text: `Sorry, I couldn't explain that. ${errorMsg}` });
|
| 127 |
addMessage({ sender: 'ai', type: 'feedback', text: "That's not quite right. Try again!", isCorrect: false });
|
|
|
|
| 8 |
import { Skeleton } from '@/components/ui/skeleton';
|
| 9 |
import { generateMCQ, type GenerateMCQOutput } from '@/ai/flows/generate-mcq';
|
| 10 |
import { explainIncorrectAnswer } from '@/ai/flows/explain-incorrect-answer-flow';
|
| 11 |
+
import { explainCorrectAnswer } from '@/ai/flows/explain-correct-answer-flow'; // New import
|
| 12 |
import type { ChatMessage as ChatMessageType } from '@/types';
|
| 13 |
import { ChatMessage } from './chat-message';
|
| 14 |
import { useToast } from '@/hooks/use-toast';
|
|
|
|
| 45 |
setIsAwaitingAnswer(false);
|
| 46 |
setHasAnsweredCorrectly(false);
|
| 47 |
setCurrentMCQ(null);
|
| 48 |
+
setIncorrectAttempts([]);
|
| 49 |
|
| 50 |
try {
|
| 51 |
const mcqData = await generateMCQ({ imageDataUri });
|
|
|
|
| 108 |
|
| 109 |
if (isCorrect) {
|
| 110 |
addMessage({ sender: 'ai', type: 'feedback', text: "That's correct! Well done.", isCorrect: true });
|
| 111 |
+
setIsLoading(true);
|
| 112 |
+
try {
|
| 113 |
+
const explanationResult = await explainCorrectAnswer({
|
| 114 |
+
question: currentMCQ.mcq,
|
| 115 |
+
options: currentMCQ.options,
|
| 116 |
+
correctAnswer: currentMCQ.correctAnswer,
|
| 117 |
+
});
|
| 118 |
+
addMessage({ sender: 'ai', type: 'feedback', text: explanationResult.explanation, isCorrect: true });
|
| 119 |
+
} catch (error) {
|
| 120 |
+
console.error("Error fetching correct answer explanation:", error);
|
| 121 |
+
const errorMsg = error instanceof Error ? error.message : "An unknown error occurred";
|
| 122 |
+
addMessage({ sender: 'ai', type: 'error', text: `Sorry, I couldn't provide an explanation for that. ${errorMsg}` });
|
| 123 |
+
}
|
| 124 |
setHasAnsweredCorrectly(true);
|
| 125 |
+
setIncorrectAttempts([]);
|
| 126 |
+
setIsLoading(false);
|
| 127 |
} else {
|
| 128 |
+
setIncorrectAttempts(prev => [...prev, option]);
|
| 129 |
setIsLoading(true);
|
| 130 |
try {
|
| 131 |
const explanationResult = await explainIncorrectAnswer({
|
|
|
|
| 136 |
});
|
| 137 |
addMessage({ sender: 'ai', type: 'feedback', text: explanationResult.explanation, isCorrect: false });
|
| 138 |
} catch (error) {
|
| 139 |
+
console.error("Error fetching explanation for incorrect answer:", error);
|
| 140 |
const errorMsg = error instanceof Error ? error.message : "An unknown error occurred";
|
| 141 |
addMessage({ sender: 'ai', type: 'error', text: `Sorry, I couldn't explain that. ${errorMsg}` });
|
| 142 |
addMessage({ sender: 'ai', type: 'feedback', text: "That's not quite right. Try again!", isCorrect: false });
|