testchat / src /ai /flows /explain-incorrect-answer-flow.ts
aakashbansal's picture
on the chat screen, when a user gives a wrong answer, don't say what the
72c115f
'use server';
/**
* @fileOverview Provides an explanation for why a selected MCQ answer is incorrect.
*
* - explainIncorrectAnswer - A function that generates an explanation for an incorrect MCQ answer.
* - ExplainIncorrectAnswerInput - The input type for the explainIncorrectAnswer function.
* - ExplainIncorrectAnswerOutput - The return type for the explainIncorrectAnswer function.
*/
import {ai} from '@/ai/genkit';
import {z} from 'genkit';
const ExplainIncorrectAnswerInputSchema = z.object({
question: z.string().describe('The multiple-choice question that was asked.'),
options: z.array(z.string()).describe('All the options provided for the question.'),
selectedAnswer: z.string().describe('The answer incorrectly selected by the user.'),
correctAnswer: z.string().describe('The actual correct answer to the question.'),
});
export type ExplainIncorrectAnswerInput = z.infer<typeof ExplainIncorrectAnswerInputSchema>;
const ExplainIncorrectAnswerOutputSchema = z.object({
explanation: z.string().describe('The explanation of why the selected answer is incorrect.'),
});
export type ExplainIncorrectAnswerOutput = z.infer<typeof ExplainIncorrectAnswerOutputSchema>;
export async function explainIncorrectAnswer(input: ExplainIncorrectAnswerInput): Promise<ExplainIncorrectAnswerOutput> {
return explainIncorrectAnswerFlow(input);
}
const prompt = ai.definePrompt({
name: 'explainIncorrectAnswerPrompt',
input: {schema: ExplainIncorrectAnswerInputSchema},
output: {schema: ExplainIncorrectAnswerOutputSchema},
prompt: `You are an expert educator. A student was asked the following multiple-choice question:
Question: "{{question}}"
Options:
{{#each options}}
- {{this}}
{{/each}}
The student incorrectly chose: "{{selectedAnswer}}".
The correct answer is "{{correctAnswer}}".
Explain concisely why "{{selectedAnswer}}" is not the correct choice for the question.
Focus only on the incorrectness of the chosen option.
Do NOT reveal what the correct answer is in your explanation.
Your explanation should be helpful for the student to learn from their mistake and try again.
For example, if the question is "What is the capital of France?" with options "Paris, London, Berlin, Rome", the correct answer is "Paris", and the student chose "London", you should explain why London is not the capital of France (e.g., "London is the capital of the United Kingdom, not France."), without stating that "The correct answer is Paris."
`,
});
const explainIncorrectAnswerFlow = ai.defineFlow(
{
name: 'explainIncorrectAnswerFlow',
inputSchema: ExplainIncorrectAnswerInputSchema,
outputSchema: ExplainIncorrectAnswerOutputSchema,
},
async input => {
const {output} = await prompt(input);
return output!;
}
);