File size: 3,363 Bytes
6379b64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { openai } from '@ai-sdk/openai';
import { generateObject } from 'ai';
import { z } from 'zod';

// Standardized question schema (same as generate-question)
const QuestionSchema = z.object({
  Question: z.string().describe('The question text'),
  Options: z.object({
    A: z.string(),
    B: z.string(),
    C: z.string(),
    D: z.string(),
  }).describe('Multiple choice options'),
  Answer: z.enum(['A', 'B', 'C', 'D']).describe('The correct answer option'),
});

export async function POST(req: Request) {
  try {
    const body = await req.json();
    const { question, direction, sourceArticle } = body as {
      question: {
        id: string;
        type: string;
        displayName?: string;
        stem: string;
        content: {
          Question: string;
          Options: { A: string; B: string; C: string; D: string };
          Answer: 'A' | 'B' | 'C' | 'D';
        };
        points: number;
        createdAt: string | Date;
      };
    
      direction: 'increase' | 'decrease';
      sourceArticle?: string;
    };

    if (!question || !direction || !['increase', 'decrease'].includes(direction)) {
      return Response.json(
        { error: 'Invalid request. Require { question, direction: "increase"|"decrease" }' },
        { status: 400 }
      );
    }

    const difficultyInstruction = direction === 'increase'
      ? 'Increase the difficulty slightly. You may use more complex vocabulary, require deeper inference, include more subtle distractors, or require multi-step reasoning. Avoid changing the topic.'
      : 'Decrease the difficulty slightly. Use simpler vocabulary, make distractors more distinguishable, and reduce inference load while keeping the same topic.';

    const systemConstraints = `
You are given a multiple-choice question with exactly 4 options (A, B, C, D) and one correct answer. ${difficultyInstruction}

Hard constraints:
- Preserve the context/topic and core learning objective.
- Keep exactly 4 options, labeled A, B, C, D.
- Ensure only one option is correct and that it matches the Answer field.
- Keep the format, return ONLY JSON matching the provided schema. No markdown.
`;

    const contextArticle = sourceArticle ? `\nSource Article (optional):\n${sourceArticle}` : '';

    const inputQuestion = `Current Question JSON:\n${JSON.stringify(
      {
        stem: question.stem,
        content: question.content,
        type: question.type,
        displayName: question.displayName,
      },
      null,
      2
    )}`;

    const prompt = `${systemConstraints}
${contextArticle}

Rewrite the question to the target difficulty. Output JSON with fields Question, Options{A,B,C,D}, Answer.

${inputQuestion}`;

    const result = await generateObject({
      model: openai('gpt-4o-mini'),
      schema: QuestionSchema,
      prompt,
    });

    return Response.json({
      type: question.type,
      displayName: question.displayName,
      stem: result.object.Question,
      content: {
        Question: result.object.Question,
        Options: result.object.Options,
        Answer: result.object.Answer,
      },
      points: question.points ?? 1,
      createdAt: new Date().toISOString(),
    });
  } catch (error) {
    console.error('Error adjusting difficulty:', error);
    return Response.json({ error: 'Failed to adjust difficulty' }, { status: 500 });
  }
}