forked from mesayanroy/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
27 lines (23 loc) · 784 Bytes
/
server.js
File metadata and controls
27 lines (23 loc) · 784 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const { Configuration, OpenAIApi } = require("openai");
const app = express();
app.use(cors());
app.use(express.json());
const openai = new OpenAIApi(
new Configuration({ apiKey: process.env.OPENAI_API_KEY })
);
app.post("/chat", async (req, res) => {
const { message } = req.body;
try {
const response = await openai.createChatCompletion({
model: "gpt-4",
messages: [{ role: "user", content: message }],
});
res.json({ reply: response.data.choices[0].message.content });
} catch (error) {
res.status(500).json({ error: "API Error" });
}
});
app.listen(5000, () => console.log("Server running on port 5000"));