forked from Night-N3twork/NextGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (76 loc) · 3.44 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import express from 'express';
import * as dotenv from 'dotenv';
import cors from 'cors';
import { Configuration, OpenAIApi } from 'openai';
import http from "http";
import path from "path";
import chalk from "chalk";
dotenv.config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const app = express();
const __dirname = process.cwd();
const PORT = process.env.PORT || 8080;
app.use(cors());
app.use(express.json());
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
// Send your frontend's index.html file
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/api/gpt', async (req, res) => {
try {
const prompt = req.body.prompt;
const response = await openai.createCompletion({
model: "gpt-3.5-turbo",
prompt: `${prompt}`,
temperature: 0,
max_tokens: 3000,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0,
});
res.status(200).send({
bot: response.data.choices[0].text
});
} catch (error) {
console.error(error);
res.status(500).send('Something went wrong');
}
});
const server = app.listen(PORT, () => {
const address = server.address();
const theme = chalk.hex("#D91E56");
const host = chalk.hex("0d52bd");
console.log(chalk.bold(theme(`
███╗ ██╗███████╗██╗ ██╗████████╗ ██████╗ ██████╗ ████████╗
████╗ ██║██╔════╝╚██╗██╔╝╚══██╔══╝██╔════╝ ██╔══██╗╚══██╔══╝
██╔██╗ ██║█████╗ ╚███╔╝ ██║ ██║ ███╗██████╔╝ ██║
██║╚██╗██║██╔══╝ ██╔██╗ ██║ ██║ ██║██╔═══╝ ██║
██║ ╚████║███████╗██╔╝ ██╗ ██║ ╚██████╔╝██║ ██║
╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
`)));
console.log(` ${chalk.bold(host("Local System:"))} http://localhost:${chalk.bold(address.port)}`);
if (process.env.REPL_SLUG && process.env.REPL_OWNER) {
console.log(` ${chalk.bold(host("Replit:"))} https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`);
}
if (process.env.HOSTNAME && process.env.GITPOD_WORKSPACE_CLUSTER_HOST) {
console.log(` ${chalk.bold(host("Gitpod:"))} https://${PORT}-${process.env.HOSTNAME}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}`);
}
if (process.env.CODESPACE_NAME && process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN) {
console.log(` ${chalk.bold(host("Github Codespaces:"))} https://${process.env.CODESPACE_NAME}-${address.port}.${process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`);
}
});
// Handle shutdown gracefully
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close(() => {
console.log("HTTP server closed");
process.exit(0);
});
}