forked from RasikDhage0825/Virtual-Lab-Using-AI-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (62 loc) · 2.51 KB
/
server.js
File metadata and controls
77 lines (62 loc) · 2.51 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const fetch = require('node-fetch');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(cors({ origin: '*' })); // Allow Vercel to access
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// =======================================================
// 🔄 API KEY ROTATION LOGIC
// =======================================================
// 1. Load keys from Environment Variables (Safer for Render)
const allKeys = [
process.env.GROQ_API_KEY_1,
process.env.GROQ_API_KEY_2,
process.env.GROQ_API_KEY_3
].filter(key => key); // This removes any undefined keys if you only have 1 or 2
app.post('/gemini', async (req, res) => {
try {
const userMessage = req.body.message;
// 2. CHECK IF WE HAVE KEYS
if (allKeys.length === 0) {
return res.status(500).json({ reply: "Server Error: No API Keys configured." });
}
// 3. RANDOMLY PICK ONE KEY
const randomKey = allKeys[Math.floor(Math.random() * allKeys.length)];
// (Optional) Print which key is being used to the console logs
console.log(`Using Key ending in: ...${randomKey.slice(-5)}`);
// 4. CALL THE API WITH THE RANDOM KEY
const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${randomKey}` // ⬅️ USE THE SELECTED KEY
},
body: JSON.stringify({
model: "llama-3.1-8b-instant",
messages: [
{ role: "system", content: "You are a helpful electronics tutor. Keep answers short." },
{ role: "user", content: userMessage }
]
})
});
const data = await response.json();
if (data.error) {
console.error("Groq Error:", data.error);
return res.json({ reply: "Error from AI provider." });
}
res.json({ reply: data.choices[0].message.content });
} catch (error) {
console.error("Server Error:", error);
res.status(500).json({ reply: "Internal Server Error" });
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});