-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (82 loc) · 2.55 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Configuration, OpenAIApi } from "openai";
import express from "express";
import dotenv from "dotenv";
import { Server } from "socket.io";
import { createServer } from "http";
import { LocalStorage } from "node-localstorage";
// Our imports
import { pulseEngine } from "./pulse-engine.js";
// Set up local storage
global.localStorage = new LocalStorage("./temp");
// Clear the local storage
global.localStorage.clear();
dotenv.config();
// OpenAI API
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Express
const app = express();
// Socket.io
const server = createServer(app);
const io = new Server(server);
app.use(express.static("public"));
io.on("connection", (socket) => {
console.log(`CONNECTED ${socket.id}`);
socket.on("disconnect", (reason) => {
global.localStorage.clear();
console.log(`DISCONNECTED ${socket.id}: ${reason}`);
});
let openaiHistory = [];
socket.on("message", (data) => {
data = JSON.parse(JSON.stringify(data));
console.log(`QUESTION (${socket.id}) [${data.engine}]: ${data.question}`);
// OpenAI Engine
if (data.engine == "openai") {
let messages = [];
messages.push({
role: "system",
content: "You're a AI health assistant called Pulse that can diagnose user diseases based on symptoms that the user is having. The assistant is helpful, creative, clever, and very friendly.",
});
for (const [input_text, completion_text] of openaiHistory) {
messages.push({
role: "user",
content: input_text,
});
messages.push({
role: "assistant",
content: completion_text,
});
}
messages.push({
role: "user",
content: data.question,
});
(async () => {
try {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: messages,
});
// console.log(completion.data.choices);
openaiHistory.push([data.question, completion.data.choices[0].message.content]);
console.log(`ANSWER (${socket.id}): ${completion.data.choices[0].message.content}`);
socket.emit("answer", completion.data.choices[0].message.content);
} catch (error) {
console.log(error);
}
})();
// Pulse Engine
} else if (data.engine == "pulse") {
pulseEngine(data.question, socket.id).then((response) => {
console.log(`ANSWER (${socket.id}): ${response}`);
socket.emit("answer", response);
});
}
});
});
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log("server started at port " + port);
});