-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverAPI.js
328 lines (279 loc) · 9.21 KB
/
serverAPI.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// server.js
import express from 'express';
import cors from 'cors';
import { OpenAI } from 'openai';
import fetch from 'node-fetch';
import { createClient } from 'redis';
const app = express();
const port = process.env.PORT || 4000;
app.use(cors());
// Middleware to parse JSON bodies
app.use(express.json());
// set redis client
const client = createClient({
url: 'redis://127.0.0.1:6379'
});
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
console.log(`connected to db: ${client.isReady}`)
await saveMessages('John', 'hiiii');
const latest = await loadLatestMessages('John')
console.log(latest)
app.post('/api/redis/save', async (req, res) => {
try {
const body = req.body
console.log('body')
console.log(body.username)
const response = await saveMessages(body.username, body.saveContainer)
res.json(response)
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error });
}
});
app.post('/api/redis/load', async (req, res) => {
try {
const body = req.body
console.log('body')
console.log(body.username)
const response = await loadLatestMessages(body.username)
// console.log('response')
// console.log(response)
res.json({saveContainer: response})
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error });
}
});
//
const openaiToken = process.env.openaiintelChainKey;
const hfToken = process.env.HF_TOKEN;
const openai = new OpenAI({apiKey: openaiToken});
// HF-api endpoint
const hfUrl8b = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct";
const hfUrl70b = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct";
const hfUrl405b = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-405B-Instruct";
// Endpoint to handle API requests
app.get("/", (req, res) => res.type('html').send(html));
// openai endpoint
app.post('/api/gpt/completions/stream', async (req, res) => {
try {
// touching
const ipAddress = req.headers['touch'] || req.socket.remoteAddress;
console.log('ipAddress')
console.log(ipAddress)
if (ipAddress === 'yes') {
return res.status(200).send('touch success');
}
// req.body is the post from client
const stream = openai.beta.chat.completions.stream({
model: "gpt-4o-mini",
messages: req.body.messages,
max_tokens: req.body.max_tokens,
stream: true,
});
res.header('Content-Type', 'text/plain');
// Sends each content stream chunk-by-chunk, such that the client
// ultimately receives a single string.
for await (const chunk of stream) {
res.write(chunk.choices[0]?.delta.content || '');
}
res.end();
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'error faced in fetching openai' });
}
});
// Adding a HEAD method for the touch
app.head('/api/gpt/completions/stream', (req, res) => {
// You can send back headers as needed
console.log('HEAD request received');
res.set('X-Last-Checked', new Date().toUTCString()); // Example header
res.sendStatus(200); // Responds with 200 OK
});
app.get('/api/gpt/completions/stream', async (req, res) => {
try {
console.log('node.js server touched')
res.json('thank you for touching')
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Get Error: Internal Server Error' });
}
});
app.post('/api/hf/8b/completions', async (req, res) => {
try {
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${hfToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
};
// console.log('options')
// console.log(options)
let response = await fetch(hfUrl8b, options)
response = await response.json()
// console.log('response')
// console.log(response)
res.json(response)
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error });
}
});
app.post('/api/hf/70b/completions', async (req, res) => {
try {
// const { inputs, parameters } = req.body;
// console.log('req.body');
// console.log(req.body);
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${hfToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
};
// console.log('options')
// console.log(options)
let response = await fetch(hfUrl70b, options)
response = await response.json()
console.log('response')
console.log(response)
res.json(response)
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/hf/completions', async (req, res) => {
try {
console.log('welcome to node.js')
res.json('welcome to njs')
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/hf/completions', async (req, res) => {
try {
// const { inputs, parameters } = req.body;
console.log('req.body');
console.log(req.body);
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${hfToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
};
console.log('options')
console.log(options)
let response = await fetch(hfUrl8b, options)
response = await response.json()
console.log('response')
console.log(response)
res.json(response)
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
async function saveMessages(username, html){
const userExists = await client.exists(username)
// if exists: 1 else 0;
const now = new Date();
console.log(userExists==1)
if (userExists===0){ // new user
console.log('new user: ', username)
// set counter
let resp = await client.json.set(username, '$', {counter: 1})
resp = await client.json.set(username, '$.1', { // set content at the counter
'time': now,
'saveContainer': html}
)
} else { // old user
console.log('old user: ', username)
// get counter
const counter = await client.json.get(username, {
path: '$.counter'
})
const counterNext = counter[0] + 1;
let resp = await client.json.set(username, '$.'+counterNext, { // set content at the counter
'time': now,
'saveContainer': html}
)
// increment the counter
resp = await client.json.numIncrBy(username, '$.counter', 1);
console.log('***saved successfully***')
return resp
}
}
async function loadLatestMessages(username){
// return undefined if user not exists
const counter = await client.json.get(username, {path: '$.counter'})
if (!counter){
console.log('undefined counter')
return undefined
} else{
const path = '$.{counter}.saveContainer'.replace('{counter}', counter[0])
const html = await client.json.get(username, {'path': path})
console.log('***loaded successfully***')
return html[0]
}
}
const html = `
<!DOCTYPE html>
<html>
<head>
<title>Hello from Davood Wadi. Welcome to HF API!</title>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.5.1/dist/confetti.browser.min.js"></script>
<script>
setTimeout(() => {
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 },
disableForReducedMotion: true
});
}, 500);
</script>
<style>
@import url("https://p.typekit.net/p.css?s=1&k=vnd5zic&ht=tk&f=39475.39476.39477.39478.39479.39480.39481.39482&a=18673890&app=typekit&e=css");
@font-face {
font-family: "neo-sans";
src: url("https://use.typekit.net/af/00ac0a/00000000000000003b9b2033/27/l?primer=7cdcb44be4a7db8877ffa5c0007b8dd865b3bbc383831fe2ea177f62257a9191&fvd=n7&v=3") format("woff2"), url("https://use.typekit.net/af/00ac0a/00000000000000003b9b2033/27/d?primer=7cdcb44be4a7db8877ffa5c0007b8dd865b3bbc383831fe2ea177f62257a9191&fvd=n7&v=3") format("woff"), url("https://use.typekit.net/af/00ac0a/00000000000000003b9b2033/27/a?primer=7cdcb44be4a7db8877ffa5c0007b8dd865b3bbc383831fe2ea177f62257a9191&fvd=n7&v=3") format("opentype");
font-style: normal;
font-weight: 700;
}
html {
font-family: neo-sans;
font-weight: 700;
font-size: calc(62rem / 16);
}
body {
background: white;
}
section {
border-radius: 1em;
padding: 1em;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<section>
Hello from Render!
</section>
</body>
</html>
`