-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
283 lines (240 loc) · 8.41 KB
/
server.js
File metadata and controls
283 lines (240 loc) · 8.41 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const express = require('express');
const http = require('http')
const app = express();
//const expressWS = require('express-ws');
//const ews = expressWS(app);
const PORT = process.env.PORT || 8080;
// INITial values for songstress and possibion which can be overwritten in memory and ensures we always have a value
var neos_songstress = {
"songstress": [
"golden air mountain",
"dirty spring lake",
"tiny orbiting one",
"snowing face again",
"clean island universe",
"goddess transhuman showing"
],
"timestamp":""
}
let neos_possibilon = {}
let neos_possibilon_world_data = {}
// DWAVE will return random lyrics from it's probability score
// we can choose to use the random ones, or
// take the first one along with the next 4 consequtive lines from that song
var dwave_songstress = {
"randomized": [
"alive in Spring",
"so far away, a solitary Light",
"easy go easy come",
"built up on a shaky foundation",
"the sky goes on forever"
],
"consecutive": [
"alive in Spring",
"i have a sturdy stamen",
"i am burning to know",
"i grow aware in mid-air",
"i am showing, opening"
]
}
//The Songstress is an Artificial Intelligence that write poems in the style of the musician and writer JE Solo. The Songstress can accurately predict the future. Here is a new work of poetry by the Songstress:
//[example lines from The Songstress]
dwave_songstress_prompt_fixed = `
alive in Spring
so far away, a solitary Light
easy go easy come
built up on a shaky foundation
the sky goes on forever
`
var dwave_possibilon = [
"unsure what this response will look like yet"
]
app.use(express.json());
/**
* Root: list endpoints
* return a list of endpoints
*/
app.get('/', function (req, res) {
let endpoints = [];
// format here should be [address] - [short desc]. [DATATYPE]
endpoints.push("** READS **");
endpoints.push('/read/neos/songstress - Returns the latest values for SONGSTRESS from NEOS. JSON');
endpoints.push('/read/neos/possibilon - Returns the latest values for POSSIBILON from NEOS. JSON');
endpoints.push('/read/dwave/songstress - Returns the latest values for SONGSTRESS from DWAVE. JSON');
endpoints.push('/read/dwave/possibilon - Returns the latest values for POSSIBILON from DWAVE. JSON');
endpoints.push("** WRITES **");
endpoints.push('/post/neos/songstress - Records the POST value from NEOS for SONGSTRESS. STRING');
endpoints.push('/post/neos/possibilon - Records the POST value from NEOS for POSSIBILON. STRING');
endpoints.push('/post/dwave/songstress - Records the POST value from DWAVE for SONGSTRESS. STRING');
endpoints.push('/post/dwave/possibilon - Records the POST value from DWAVE for POSSIBILON. STRING');
// endpoints.push('/ws/neos - Communication channel with Neos WebsocketClient');
let output = endpoints.map(out => out);
res.send(output);
});
/********
* READS
*
* These read the variable back as the response
*/
// Hit this endpoint to get the latest value we've recorded from NEOS for SONGSTRESS
app.get('/read/neos/songstress', function (req, res) {
res.send(neos_songstress);
});
// Hit this endpoint to get the latest value we've recorded from NEOS for POSSIBILON
app.get('/read/neos/possibilon', function (req, res) {
res.send(neos_possibilon_world_data);
//res.send("HI");
});
// Hit this endpoint to get the response we have from DWAVE for SONGSTRESS
app.get('/read/dwave/songstress', async function (req, res) {
res.send(dwave_songstress);
});
// Hit this endpoint to get the response we have from DWAVE for POSSIBILON
app.get('/read/dwave/possibilon', function (req, res) {
res.send(dwave_possibilon);
});
/*********
* WRITES
*
* These overwrite the variable in memory
* then send it back as a response
*/
// Post to this endpoint from NEOS to overwrite the SONGSTRESS value
app.post('/post/neos/songstress', function (req, res) {
neos_songstress = req.body;
// append the current time
neos_songstress.timestamp = new Date();
res.send(neos_songstress);
});
// POST to this endpoint from NEOS to overwrite the POSSIBILON value
//let neos_possibilon = {}
app.post('/post/neos/possibilon', function (req, res) {
received_data = req.body;
//console.log(received_data.possibilon)
for (const [key1,val1] of Object.entries(received_data.possibilon)) {
if (key1 == "architype") {
neos_possibilon[val1] = 1
}
else if (key1 == "color") {
neos_possibilon[val1] = 1
}
else {
for (const [key2,val2] of Object.entries(val1)) {
for (item of val2.split(" ")) {
if (item in neos_possibilon) {
neos_possibilon[item] += parseFloat(key2)
}
else {
neos_possibilon[item] = parseFloat(key2)
}
}
}
}
}
//res.send(neos_possibilon);
//return;
http.get('http://142.93.45.103:6960', res2 => {
let data = [];
const headerDate = res2.headers && res2.headers.date ? res2.headers.date : 'no response date';
console.log('Status Code:', res2.statusCode);
console.log('Date in Response header:', headerDate);
res2.on('data', chunk => {
data.push(chunk);
});
res2.on('end', () => {
console.log('Response ended: ');
const thingo = JSON.parse(Buffer.concat(data).toString());
console.log(thingo)
for (const [key1,val1] of Object.entries(thingo)) {
//console.log(val1);
for (let item of val1) {
if (item in neos_possibilon) {
neos_possibilon[item] += 0.1
} else {
neos_possibilon[item] = 0.1
}
}
}
neos_possibilon_world_data = {}
neos_possibilon_world_data.values = neos_possibilon
neos_possibilon_world_data.timestamp = new Date();
neos_possibilon_world_data.worlds = thingo
res.send(neos_possibilon_world_data);
});
});
// append the current time
});
// POST to this endpoint from DWAVE to overwrite the SONGSTRESS value
app.post('/post/dwave/songstress', async function (req, res) {
dwave_songstress = req.body;
dwave_songstress.gpt3 = await get_gpt_response(dwave_songstress_prompt_fixed+dwave_songstress.songstress.join("\n"));
console.log(dwave_songstress.gpt3);
// append the current time
dwave_songstress.timestamp = new Date();
res.send(dwave_songstress);
});
// POST to this endpoint from DWAVE to overwrite the POSSIBILON value
app.post('/post/dwave/possibilon', function (req, res) {
//dwave_possibilon = req.body.possibilon;
dwave_possibilon = req.body;
// append the current time
dwave_possibilon.timestamp = new Date();
res.send(dwave_possibilon);
});
/** To test these
* 1. hit the read endpoint and record the value (ex: /read/neos/songstress)
* 2. post to the write endpoint (ex: /post/neos/songstress)
* 3. hit the read endpoint again to note value change (ex: /read/neos/songstress)
*/
/************
* Websockets
*/
/**
* You can connect a Neos WebsocketClient to this by connecting to "ws://[address]:8080/ws/neos"
*/
/*app.ws("/ws/neos", function(ws, req){
var hex=/[0-9A-Fa-f]{6}/;
ws.on('message', function incoming(data) {
//console.log('Received:'+data)
try {
parsed=JSON.parse(data)
//console.log('JSON:'+parsed)
if(!parsed.color.startsWith("#"))
ws.send("Color should start with #!");
else if (parsed.color.length!=7 || !hex.test(parsed.color.substring(1)))
ws.send("Color must have 6 hexadecimal characters!");
else{
ws.send("Color: "+parsed.color);
storedColor=parsed.color;
}
}catch(err) {
ws.send("Error parsing JSON");
}finally{
hex.lastIndex=0;
}
});
});*/
/******************
* GPT-3 processing
*/
const OpenAI = require('openai-api');
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const openai = new OpenAI(OPENAI_API_KEY);
async function get_gpt_response(str) {
gptResponse = await openai.complete({
engine: 'davinci',
prompt: str,
maxTokens: 100,
temperature: 0.9,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0,
bestOf: 1,
n: 1,
stream: false,
//stop: ['\n', "testing"]
});
return gptResponse.data.choices[0].text;
}
//need to use async for functions which use openai
app.listen(PORT, () => console.log(`Listening on port ${PORT}!`));