| import { openai } from '@ai-sdk/openai'; |
| import { generateText } from 'ai'; |
| import { langfuse } from '@/lib/langfuse'; |
| import { AI_CONFIG } from '@/config/ai'; |
|
|
| export async function POST(req: Request) { |
| try { |
| const { |
| grade, |
| unit, |
| textbookVocab, |
| additionalVocab, |
| grammar, |
| topic, |
| inputArticle |
| } = await req.json(); |
|
|
| |
| const langfusePrompt = await langfuse.prompt.get('article/generation'); |
| |
| |
| const prompt = langfusePrompt.prompt |
| .replace(/\{\{grade_values\}\}/g, grade.join(', ')) |
| .replace(/\{\{unit_values\}\}/g, unit.join(', ')) |
| .replace(/\{\{textbook_vocab_values\}\}/g, textbookVocab) |
| .replace(/\{\{additional_vocab_values\}\}/g, additionalVocab) |
| .replace(/\{\{grammar_values\}\}/g, grammar.join(', ')) |
| .replace(/\{\{topic_values\}\}/g, topic.join(', ')); |
|
|
| const result = await generateText({ |
| model: openai(AI_CONFIG.model), |
| prompt: prompt, |
| }); |
|
|
| return Response.json({ |
| article: result.text, |
| createdAt: new Date().toISOString(), |
| }); |
|
|
| } catch (error) { |
| console.error('Error generating article:', error); |
| return Response.json( |
| { error: 'Failed to generate article' }, |
| { status: 500 } |
| ); |
| } |
| } |