-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
47 lines (39 loc) · 1.31 KB
/
backend.js
File metadata and controls
47 lines (39 loc) · 1.31 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
const express = require("express");
require("dotenv").config();
const app = express();
app.use(express.json());
// ✅ Route de test
app.get("/", (req, res) => {
res.send("✅ Backend fonctionne !");
});
// ✅ Route chatbot corrigée (utilise fetch natif de Node 20)
app.post("/api/chat", async (req, res) => {
const userMessage = req.body.message;
try {
const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.GROQ_API_KEY}`
},
body: JSON.stringify({
model: "llama-3.3-70b-versatile",
messages: [{ role: "user", content: userMessage }]
})
});
const data = await response.json();
console.log("🔎 Réponse brute Groq:", JSON.stringify(data, null, 2));
if (!data.choices) {
return res.status(500).json({ reply: "Erreur API Groq", details: data });
}
res.json({ reply: data.choices[0].message.content });
} catch (err) {
console.error("❌ Erreur côté serveur :", err);
res.status(500).json({ reply: "Erreur serveur interne", error: err.message });
}
});
// ✅ Lancer le serveur
const PORT = 3000;
app.listen(PORT, () => {
console.log(`🚀 Serveur lancé sur http://localhost:${PORT}`);
});