File size: 2,125 Bytes
e50b7ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 An AI flow to explain a given syllabus topic.
 *
 * - explainTopic - A function that handles the topic explanation.
 * - ExplainTopicInput - The input type for the explainTopic function.
 * - ExplainTopicOutput - The return type for the explainTopic function.
 */

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

const ExplainTopicInputSchema = z.object({
  topicText: z.string().describe('The syllabus topic text to be explained.'),
});
export type ExplainTopicInput = z.infer<typeof ExplainTopicInputSchema>;

const ExplainTopicOutputSchema = z.object({
  explanation: z.string().describe('The AI-generated explanation for the topic, formatted in simple Markdown.'),
});
export type ExplainTopicOutput = z.infer<typeof ExplainTopicOutputSchema>;

export async function explainTopic(input: ExplainTopicInput): Promise<ExplainTopicOutput> {
  return explainTopicFlow(input);
}

const explainTopicPrompt = ai.definePrompt({
  name: 'explainTopicPrompt',
  input: {schema: ExplainTopicInputSchema},
  output: {schema: ExplainTopicOutputSchema},
  prompt: `You are a helpful assistant. Explain the following syllabus topic concisely and clearly, in simple terms suitable for a university student.
Make the explanation easy to understand and focus on the core concepts.
Please use simple Markdown for formatting:
- Use '#' for main headings and '##' for subheadings (e.g., # Main Title, ## Sub-section).
- Use backticks (\`) for inline code snippets (e.g., \`String name = "Java"\`).
- Use triple backticks (\`\`\`) for code blocks, specifying the language if known (e.g., \`\`\`java ...code... \`\`\`).
- Use bullet points (-) for lists if appropriate.
- Ensure paragraphs are well-separated.

Topic: {{{topicText}}}`,
});

const explainTopicFlow = ai.defineFlow(
  {
    name: 'explainTopicFlow',
    inputSchema: ExplainTopicInputSchema,
    outputSchema: ExplainTopicOutputSchema,
  },
  async (input) => {
    const {output} = await explainTopicPrompt(input);
    if (!output) {
      throw new Error('No explanation was generated by the AI.');
    }
    return output;
  }
);