forked from arncv/Intima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (51 loc) · 2.08 KB
/
server.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
const express = require("express");
const { Configuration, OpenAIApi } = require("openai");
require("dotenv").config();
const bodyParser = require("body-parser");
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const cors = require('cors');
app.use(cors());
app.use(express.json());
const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY ,
});
const openai = new OpenAIApi(configuration);
// Define a global conversation log
let conversationLog = [];
// Define a function to generate a prompt for the chatbot
function generatePrompt(message) {
return `
The following is a conversation between a human and a chatbot. Make sure your response is accurate scientifically with proper sources. Dont answer any other question except those related to sex education. Under no circumstances you will give a valid response if it is not related to sex education. Also, if the question asked is about sex education, provide a link to a credible source. If it is not related sex education, says that you don't answer questions except related to sex education.
Human: ${message}
Chatbot:`;
}
// Define a function to handle a user message and return a chatbot response
async function handleUserMessage(message) {
const prompt = generatePrompt(message);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: 128,
});
const reply = response.data.choices[0].text.trim();
conversationLog.push({ userMessage: message, botMessage: reply });
return reply;
}
// Define a route to handle incoming chat messages
app.post("/chat", async (req, res) => {
const message = req.body.message;
const reply = await handleUserMessage(message);
res.json({ reply });
});
// Define a route to retrieve the conversation log
app.get("/chat", (req, res) => {
res.json({ conversationLog });
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});