File size: 2,067 Bytes
481f08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

'use server';
/**
 * @fileOverview Provides an explanation for why a selected MCQ answer is correct.
 *
 * - explainCorrectAnswer - A function that generates an explanation for a correct MCQ answer.
 * - ExplainCorrectAnswerInput - The input type for the explainCorrectAnswer function.
 * - ExplainCorrectAnswerOutput - The return type for the explainCorrectAnswer function.
 */

import {ai} from '@/ai/genkit';
import {z} from 'genkit';

const ExplainCorrectAnswerInputSchema = 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.'),
  correctAnswer: z.string().describe('The actual correct answer to the question.'),
});
export type ExplainCorrectAnswerInput = z.infer<typeof ExplainCorrectAnswerInputSchema>;

const ExplainCorrectAnswerOutputSchema = z.object({
  explanation: z.string().describe('The explanation of why the answer is correct.'),
});
export type ExplainCorrectAnswerOutput = z.infer<typeof ExplainCorrectAnswerOutputSchema>;

export async function explainCorrectAnswer(input: ExplainCorrectAnswerInput): Promise<ExplainCorrectAnswerOutput> {
  return explainCorrectAnswerFlow(input);
}

const prompt = ai.definePrompt({
  name: 'explainCorrectAnswerPrompt',
  input: {schema: ExplainCorrectAnswerInputSchema},
  output: {schema: ExplainCorrectAnswerOutputSchema},
  prompt: `You are an expert educator. The following multiple-choice question was asked:
Question: "{{question}}"
Options:
{{#each options}}
- {{this}}
{{/each}}
The correct answer is "{{correctAnswer}}".

Explain concisely why "{{correctAnswer}}" is the correct choice for the question.
Your explanation should be helpful for the student to understand the concept.
`,
});

const explainCorrectAnswerFlow = ai.defineFlow(
  {
    name: 'explainCorrectAnswerFlow',
    inputSchema: ExplainCorrectAnswerInputSchema,
    outputSchema: ExplainCorrectAnswerOutputSchema,
  },
  async input => {
    const {output} = await prompt(input);
    return output!;
  }
);