forked from itsaryanchauhan/ThinkDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeminiRoutes.js
More file actions
78 lines (71 loc) · 2.28 KB
/
geminiRoutes.js
File metadata and controls
78 lines (71 loc) · 2.28 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
78
// Gemini API routes for validation and feedback
import express from "express";
import fetch from "node-fetch";
const router = express.Router();
// POST /api/validate-gemini-key
// Expects: { geminiKey: string }
router.post("/validate-gemini-key", async (req, res) => {
const { geminiKey } = req.body;
if (!geminiKey) {
return res.status(400).json({ message: "Gemini API key is required." });
}
try {
// Simple validation endpoint that just checks if the key is valid
const geminiUrl =
"https://generativelanguage.googleapis.com/v1beta/models?key=" +
geminiKey;
const geminiRes = await fetch(geminiUrl);
if (!geminiRes.ok) {
const errorData = await geminiRes.json();
return res.status(400).json({
valid: false,
message: errorData.error?.message || "Invalid API key",
});
}
return res.json({ valid: true });
} catch (err) {
return res.status(500).json({
valid: false,
message: "Failed to validate API key",
});
}
});
// POST /api/gemini-feedback
// Expects: { pseudocode: string, geminiKey: string }
router.post("/gemini-feedback", async (req, res) => {
const { pseudocode, geminiKey } = req.body;
if (!pseudocode || !geminiKey) {
return res
.status(400)
.json({ message: "Pseudocode and Gemini API key are required." });
}
try {
// Gemini API endpoint (replace with the actual endpoint if different)
const geminiUrl =
"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" +
geminiKey;
const geminiRes = await fetch(geminiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ parts: [{ text: pseudocode }] }],
}),
});
const geminiData = await geminiRes.json();
if (!geminiRes.ok) {
return res
.status(500)
.json({ message: geminiData.error?.message || "Gemini API error" });
}
// Extract feedback from Gemini response
const feedback =
geminiData.candidates?.[0]?.content?.parts?.[0]?.text ||
"No feedback received.";
res.json({ feedback });
} catch (err) {
res
.status(500)
.json({ message: "Failed to get feedback from Gemini API." });
}
});
export default router;