Commit ·
034f126
1
Parent(s): 8ec90be
Update prompt management
Browse files- src/app/api/generate-question/route.ts +42 -19
- src/app/page.tsx +5 -11
- src/config/questionTypes.ts +6 -45
- src/prompts/prompt-management.ts +2 -1
src/app/api/generate-question/route.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import { openai } from '@ai-sdk/openai';
|
| 2 |
import { generateObject } from 'ai';
|
| 3 |
import { z } from 'zod';
|
| 4 |
-
import {
|
| 5 |
|
| 6 |
// Standardized question schema - all questions use this format
|
| 7 |
const QuestionSchema = z.object({
|
|
@@ -18,24 +18,47 @@ const QuestionSchema = z.object({
|
|
| 18 |
export async function POST(req: Request) {
|
| 19 |
try {
|
| 20 |
const { type, parameters, sourceArticle } = await req.json();
|
| 21 |
-
|
| 22 |
-
//
|
| 23 |
-
const
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
// All questions use the same JSON format
|
| 41 |
const result = await generateObject({
|
|
|
|
| 1 |
import { openai } from '@ai-sdk/openai';
|
| 2 |
import { generateObject } from 'ai';
|
| 3 |
import { z } from 'zod';
|
| 4 |
+
import { langfuse } from '@/lib/langfuse';
|
| 5 |
|
| 6 |
// Standardized question schema - all questions use this format
|
| 7 |
const QuestionSchema = z.object({
|
|
|
|
| 18 |
export async function POST(req: Request) {
|
| 19 |
try {
|
| 20 |
const { type, parameters, sourceArticle } = await req.json();
|
| 21 |
+
console.log(sourceArticle)
|
| 22 |
+
// Get prompt from Langfuse
|
| 23 |
+
const langfusePrompt = await langfuse.prompt.get(`question/${type}`);
|
| 24 |
+
|
| 25 |
+
// Detect all variables in the prompt ({{variableName}})
|
| 26 |
+
const variableRegex = /\{\{(\w+)\}\}/g;
|
| 27 |
+
const detectedVariables = new Set<string>();
|
| 28 |
+
let match;
|
| 29 |
+
|
| 30 |
+
while ((match = variableRegex.exec(langfusePrompt.prompt)) !== null) {
|
| 31 |
+
detectedVariables.add(match[1]);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
console.log(`Detected variables in prompt:`, Array.from(detectedVariables));
|
| 35 |
+
|
| 36 |
+
// Build parameter replacements dynamically
|
| 37 |
+
let prompt = langfusePrompt.prompt;
|
| 38 |
+
|
| 39 |
+
// Replace each detected variable with its corresponding parameter value
|
| 40 |
+
for (const variable of detectedVariables) {
|
| 41 |
+
const regex = new RegExp(`\\{\\{${variable}\\}\\}`, 'g');
|
| 42 |
+
let replacement = '';
|
| 43 |
+
|
| 44 |
+
// Handle special cases
|
| 45 |
+
if (variable === 'article') {
|
| 46 |
+
replacement = sourceArticle || '';
|
| 47 |
+
} else if (variable === 'examples') {
|
| 48 |
+
// Skip examples for now since we don't have them
|
| 49 |
+
replacement = '';
|
| 50 |
+
} else {
|
| 51 |
+
// Check if variable exists in parameters
|
| 52 |
+
if (variable in parameters) {
|
| 53 |
+
replacement = parameters[variable] || '';
|
| 54 |
+
} else {
|
| 55 |
+
console.warn(`Variable '${variable}' not found in parameters, using empty string`);
|
| 56 |
+
replacement = '';
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
prompt = prompt.replace(regex, replacement);
|
| 61 |
+
}
|
| 62 |
|
| 63 |
// All questions use the same JSON format
|
| 64 |
const result = await generateObject({
|
src/app/page.tsx
CHANGED
|
@@ -11,13 +11,13 @@ import { Input } from '@/components/ui/input';
|
|
| 11 |
import { ChevronRight, ChevronLeft } from 'lucide-react';
|
| 12 |
|
| 13 |
const QUESTION_TYPES: QuestionType[] = [
|
| 14 |
-
{ id: 'cloze', name: 'Cloze (Fill-in-the-blank)', icon: '🔤', description: 'Fill in missing words or phrases' },
|
| 15 |
-
{ id: 'word-comprehension', name: 'Word Comprehension', icon: '📚', description: 'Test vocabulary understanding in context' },
|
| 16 |
-
{ id: 'grammatical-structure', name: 'Grammar Structure', icon: '📝', description: 'Identify grammatical patterns and structures' },
|
| 17 |
-
{ id: 'paragraph-structure', name: 'Paragraph Structure', icon: '📋', description: 'Analyze paragraph organization and flow' },
|
| 18 |
{ id: 'paragraph-summary', name: 'Paragraph Summary', icon: '📄', description: 'Create summaries of text passages' },
|
| 19 |
{ id: 'paragraph-details', name: 'Paragraph Details', icon: '🔍', description: 'Identify key details in paragraphs' },
|
| 20 |
-
{ id: '
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
];
|
| 22 |
|
| 23 |
export default function QuestionBuilder() {
|
|
@@ -204,12 +204,6 @@ export default function QuestionBuilder() {
|
|
| 204 |
|
| 205 |
{currentStep === 'parameters' && selectedQuestionType && (
|
| 206 |
<div className="space-y-4">
|
| 207 |
-
<div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-700 rounded-lg p-4">
|
| 208 |
-
<p className="text-blue-800 dark:text-blue-200">
|
| 209 |
-
Great choice! Let's configure your {selectedQuestionType.name} question.
|
| 210 |
-
</p>
|
| 211 |
-
</div>
|
| 212 |
-
|
| 213 |
<QuestionParameterForm
|
| 214 |
questionType={selectedQuestionType}
|
| 215 |
parameters={questionParameters}
|
|
|
|
| 11 |
import { ChevronRight, ChevronLeft } from 'lucide-react';
|
| 12 |
|
| 13 |
const QUESTION_TYPES: QuestionType[] = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
{ id: 'paragraph-summary', name: 'Paragraph Summary', icon: '📄', description: 'Create summaries of text passages' },
|
| 15 |
{ id: 'paragraph-details', name: 'Paragraph Details', icon: '🔍', description: 'Identify key details in paragraphs' },
|
| 16 |
+
// { id: 'cloze', name: 'Cloze (Fill-in-the-blank)', icon: '🔤', description: 'Fill in missing words or phrases' },
|
| 17 |
+
// { id: 'word-comprehension', name: 'Word Comprehension', icon: '📚', description: 'Test vocabulary understanding in context' },
|
| 18 |
+
// { id: 'grammatical-structure', name: 'Grammar Structure', icon: '📝', description: 'Identify grammatical patterns and structures' },
|
| 19 |
+
// { id: 'paragraph-structure', name: 'Paragraph Structure', icon: '📋', description: 'Analyze paragraph organization and flow' },
|
| 20 |
+
// { id: 'textual-inference', name: 'Textual Inference', icon: '🧠', description: 'Make inferences from text passages' },
|
| 21 |
];
|
| 22 |
|
| 23 |
export default function QuestionBuilder() {
|
|
|
|
| 204 |
|
| 205 |
{currentStep === 'parameters' && selectedQuestionType && (
|
| 206 |
<div className="space-y-4">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
<QuestionParameterForm
|
| 208 |
questionType={selectedQuestionType}
|
| 209 |
parameters={questionParameters}
|
src/config/questionTypes.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { QuestionTypeConfig } from '@/types/questionConfig';
|
|
| 2 |
|
| 3 |
// Configuration for each question type
|
| 4 |
export const questionTypeConfigs: Record<string, QuestionTypeConfig> = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
'article-generation': {
|
| 6 |
id: 'article-generation',
|
| 7 |
parameters: [
|
|
@@ -225,54 +230,10 @@ export const questionTypeConfigs: Record<string, QuestionTypeConfig> = {
|
|
| 225 |
]
|
| 226 |
},
|
| 227 |
|
| 228 |
-
'paragraph-summary': {
|
| 229 |
-
id: 'paragraph-summary',
|
| 230 |
-
parameters: [
|
| 231 |
-
{
|
| 232 |
-
key: 'gradeValues',
|
| 233 |
-
label: 'Grade Level',
|
| 234 |
-
type: 'select',
|
| 235 |
-
required: true,
|
| 236 |
-
options: [
|
| 237 |
-
{ value: '七上七下', label: '七上七下' },
|
| 238 |
-
{ value: '八上八下', label: '八上八下' },
|
| 239 |
-
{ value: '九上九下', label: '九上九下' },
|
| 240 |
-
],
|
| 241 |
-
defaultValue: '七上七下'
|
| 242 |
-
},
|
| 243 |
-
{
|
| 244 |
-
key: 'unitValues',
|
| 245 |
-
label: 'Unit Range',
|
| 246 |
-
type: 'text',
|
| 247 |
-
required: false,
|
| 248 |
-
placeholder: 'Unit 1-3'
|
| 249 |
-
}
|
| 250 |
-
]
|
| 251 |
-
},
|
| 252 |
|
| 253 |
'paragraph-details': {
|
| 254 |
id: 'paragraph-details',
|
| 255 |
-
parameters: [
|
| 256 |
-
{
|
| 257 |
-
key: 'gradeValues',
|
| 258 |
-
label: 'Grade Level',
|
| 259 |
-
type: 'select',
|
| 260 |
-
required: true,
|
| 261 |
-
options: [
|
| 262 |
-
{ value: '七上七下', label: '七上七下' },
|
| 263 |
-
{ value: '八上八下', label: '八上八下' },
|
| 264 |
-
{ value: '九上九下', label: '九上九下' },
|
| 265 |
-
],
|
| 266 |
-
defaultValue: '七上七下'
|
| 267 |
-
},
|
| 268 |
-
{
|
| 269 |
-
key: 'unitValues',
|
| 270 |
-
label: 'Unit Range',
|
| 271 |
-
type: 'text',
|
| 272 |
-
required: false,
|
| 273 |
-
placeholder: 'Unit 1-3'
|
| 274 |
-
}
|
| 275 |
-
]
|
| 276 |
},
|
| 277 |
|
| 278 |
'textual-inference': {
|
|
|
|
| 2 |
|
| 3 |
// Configuration for each question type
|
| 4 |
export const questionTypeConfigs: Record<string, QuestionTypeConfig> = {
|
| 5 |
+
'paragraph-summary': {
|
| 6 |
+
id: 'paragraph-summary',
|
| 7 |
+
parameters: []
|
| 8 |
+
},
|
| 9 |
+
|
| 10 |
'article-generation': {
|
| 11 |
id: 'article-generation',
|
| 12 |
parameters: [
|
|
|
|
| 230 |
]
|
| 231 |
},
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
'paragraph-details': {
|
| 235 |
id: 'paragraph-details',
|
| 236 |
+
parameters: []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
},
|
| 238 |
|
| 239 |
'textual-inference': {
|
src/prompts/prompt-management.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { ARTICLE_FORMAT_PROMPT } from './article-format';
|
|
| 13 |
import { ARTICLE_REVISION_PROMPT } from './article-revision';
|
| 14 |
|
| 15 |
// Simple interface for prompt parameters
|
| 16 |
-
export interface QuizPromptParams extends Record<string,
|
| 17 |
topic?: string;
|
| 18 |
difficulty?: string;
|
| 19 |
numQuestions?: number;
|
|
@@ -27,6 +27,7 @@ export interface QuizPromptParams extends Record<string, string | number | boole
|
|
| 27 |
topicValues?: string;
|
| 28 |
grammarValues?: string;
|
| 29 |
inputArticleValue?: string;
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
// All available prompts
|
|
|
|
| 13 |
import { ARTICLE_REVISION_PROMPT } from './article-revision';
|
| 14 |
|
| 15 |
// Simple interface for prompt parameters
|
| 16 |
+
export interface QuizPromptParams extends Record<string, any> {
|
| 17 |
topic?: string;
|
| 18 |
difficulty?: string;
|
| 19 |
numQuestions?: number;
|
|
|
|
| 27 |
topicValues?: string;
|
| 28 |
grammarValues?: string;
|
| 29 |
inputArticleValue?: string;
|
| 30 |
+
examples?: Array<{題幹: string; 選項: string}> | string;
|
| 31 |
}
|
| 32 |
|
| 33 |
// All available prompts
|