-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (31 loc) · 1.14 KB
/
server.js
File metadata and controls
41 lines (31 loc) · 1.14 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";
dotenv.config();
const app = express();
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); // gemini key
app.use(cors());
app.use(express.json());
app.post("/hint", async (req, res) => {
const { question } = req.body;
if (!question) {
return res.status(400).json({ error: "No question provided" });
}
try {
// init gemini
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
const prompt = `You are a helpful math tutor. The student is stuck on: "${question}".
Instead of giving the answer, provide a short, guiding hint.`;
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
res.json({ hint: text });
} catch (error) {
console.error("Gemini Error:", error);
res
.status(500)
.json({ hint: "I'm having trouble connecting to my AI brain." });
}
});
app.listen(5000, () => console.log("🚀 Gemini Tutor running on port 5000"));