-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (30 loc) · 918 Bytes
/
server.js
File metadata and controls
39 lines (30 loc) · 918 Bytes
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
import express from "express";
import 'dotenv/config';
import OpenAI from "openai";
import cors from "cors";
const app = express();
app.use(cors());
app.use(express.json());
console.log("API KEY:", process.env.OPENAI_API_KEY);
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// ======= ROTA DO CHATBOT =======
app.post("/chat", async (req, res) => {
try {
const { message } = req.body;
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: message }],
});
res.json({ reply: completion.choices[0].message.content });
} catch (err) {
console.error("Erro no chat:", err);
res.status(500).json({ reply: "Erro no servidor." });
}
});
// ======= INICIAR SERVIDOR =======
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Servidor rodando na porta ${PORT}`);
});