-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
77 lines (66 loc) · 2.27 KB
/
route.ts
File metadata and controls
77 lines (66 loc) · 2.27 KB
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
import { NextRequest, NextResponse } from "next/server";
import { GoogleGenerativeAI } from "@google/generative-ai";
// Initialize Gemini client
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY || "");
export async function POST(req: NextRequest) {
try {
const { transcript } = await req.json();
if (!transcript || typeof transcript !== "string") {
return NextResponse.json(
{ error: "Transcript is required and must be a string." },
{ status: 400 }
);
}
const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });
const prompt = `
You are an AI interview evaluator. Analyze the following transcript and provide a JSON response with:
- communication (1–10)
- confidence (1–10)
- technicalKnowledge (1–10)
- collaboration (1–10)
- hirePercentage (0–100): estimated chance of being hired
- suggestion (70 words max): brief advice for improvement
Respond only with a valid JSON object like:
{
"communication": 8,
"confidence": 7,
"technicalKnowledge": 9,
"collaboration": 6,
"hirePercentage": 85,
"suggestion": "You demonstrated good knowledge and communication. To improve further, work on giving more structured answers and showing stronger confidence when discussing your projects. Keep practicing mock interviews and refine how you present yourself."
}
Transcript:
"""
${transcript}
"""
`.trim();
const result = await model.generateContent({
contents: [{ role: "user", parts: [{ text: prompt }] }],
generationConfig: {
temperature: 0.5,
maxOutputTokens: 2048,
},
});
const response = await result.response;
const text = response.text();
try {
const cleaned = text.replace(/```json|```/g, "").trim();
const parsed = JSON.parse(cleaned);
return NextResponse.json(parsed, { status: 200 });
} catch {
console.error("Failed to parse JSON from Gemini output:", text);
return NextResponse.json(
{ error: "Gemini output is not valid JSON." },
{ status: 500 }
);
}
} catch (error) {
console.error("Unexpected Server Error:", error);
return NextResponse.json(
{
error: error instanceof Error ? error.message : "Unknown server error",
},
{ status: 500 }
);
}
}