-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 2.08 KB
/
index.js
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
// Import necessary packages and modules
import dotenv from 'dotenv'; // For handling environment variables
dotenv.config(); // Load environment variables from a .env file
import express from 'express'; // Web framework for creating the server
import bodyParser from 'body-parser'; // Middleware for handling JSON data
import { OpenAI } from "openai"; // OpenAI API client for language models
import colors from 'colors'; // Library for adding colors to console output
// Create an Express application
const app = express();
// Set the port for the server
const port = process.env.PORT || 3000;
// Use bodyParser middleware to parse JSON data in requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Array to store chat history
const chatHistory = [];
app.get("/", (req, res) => {
res.render("home.ejs");
});
app.get("/question", (req, res) => {
// Your route handling logic here
res.render("question.ejs");
});
app.post('/question', async (req, res) => {
try {
const userInput = req.body.question;
const messages = chatHistory.map(([role, content]) => ({
role,
content,
}));
messages.push({ role: 'user', content: userInput });
const chat = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: messages,
});
const completionText = chat.choices[0].message.content;
console.log(colors.blue('Bot: ') + completionText);
chatHistory.push(['assistant', completionText]);
// Send the answer as JSON
res.json({ answer: completionText });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
// Display welcome messages
console.log(colors.bold.green('Welcome to the CIPHER CHAT - CHAT BOT!'));
console.log(colors.bold.green('You can start sending prompts'));