-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
75 lines (58 loc) · 1.58 KB
/
app.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
import { ChatGPTAPI } from 'chatgpt';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const PORT = 3000; // the localhost/127.0.0.1 port at which the front-end is run on
const express = require('express')
const bodyParser = require('body-parser')
let data = [{
question: "Hello World!",
answer: "Hello! How can I assist you today?"
}];
const app = express()
const api = new ChatGPTAPI({
apiKey: 'YOUR_API_KEY'
});
async function gptResponse(question) {
response = await api.sendMessage(question, {
parentMessageId: response.id
});
return response.text.replace('ChatGPT', 'SpeechGPT (powered by OpenAI ChatGPT)');
}
let response = api.sendMessage('Hello World!');
console.log(response.text);
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(express.static( "public" ));
app.get("/", (req, res) => {
res.render("home", {
data: data
})
})
app.post("/add", (req, res) => {
const inputQuestion = req.body.inputQuestion;
const gptAnswer = gptResponse(inputQuestion)
gptAnswer.then(function(result) {
data.push({
question: inputQuestion,
answer: result
})
res.render("home", {
data: data
})
})
})
app.post('/delete', (req, res) => {
data = [{
question: "Hello World!",
answer: "Hello! How can I assist you today?"
}];
res.render("home", {
data: data
})
})
app.listen(PORT, (req, res) => {
console.log("App is running on port 3000")
})