Spaces:
Sleeping
Sleeping
File size: 2,208 Bytes
a0da743 | 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 | import { NextRequest, NextResponse } from 'next/server';
import { GoogleGenerativeAI } from '@google/generative-ai';
export async function POST(request: NextRequest) {
try {
const { question, answer, role, difficulty } = await request.json();
const headerApiKey = request.headers.get('x-gemini-api-key');
const apiKey = headerApiKey || process.env.GEMINI_API_KEY;
if (!apiKey) {
return NextResponse.json(
{ error: 'GEMINI_API_KEY is not configured. Please add it in Settings or .env.local file.' },
{ status: 500 }
);
}
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' });
const prompt = `You are an expert interview evaluator for a ${role} position (${difficulty} level).
QUESTION: "${question}"
CANDIDATE'S ANSWER: "${answer}"
Evaluate the candidate's answer thoroughly. Return ONLY a valid JSON object (no markdown, no code blocks) with these exact fields:
{
"score": <overall score 0-10>,
"relevance": <relevance to question 0-10>,
"clarity": <clarity of explanation 0-10>,
"technicalAccuracy": <technical correctness 0-10>,
"confidence": <apparent confidence level 0-10>,
"strengths": [<array of 2-3 specific strengths>],
"weaknesses": [<array of 2-3 specific areas for improvement>],
"suggestions": [<array of 2-3 actionable suggestions>],
"sampleAnswer": "<a comprehensive ideal answer in 3-4 sentences>"
}
Be fair but rigorous. Provide constructive, actionable feedback. The sample answer should demonstrate what an excellent response looks like.`;
const result = await model.generateContent(prompt);
const responseText = result.response.text();
// Parse JSON from the response
const jsonMatch = responseText.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
throw new Error('Failed to parse evaluation from AI response');
}
const evaluation = JSON.parse(jsonMatch[0]);
return NextResponse.json({ evaluation });
} catch (error) {
console.error('Error evaluating answer:', error);
return NextResponse.json(
{ error: 'Failed to evaluate answer. Please try again.' },
{ status: 500 }
);
}
}
|