-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
104 lines (89 loc) · 2.87 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
const deepmerge = require('deepmerge');
const express = require('express');
const fetch = require('node-fetch');
const stream = require('stream');
const websocket = require('websocket');
const util = require('util');
const winston = require('winston');
const colorizer = winston.format.colorize();
const logger = winston.createLogger({
level: process.env.LOG_LEVEL,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.splat(),
winston.format.printf((info) => {
const leftBracket = colorizer.colorize(info.level, "[");
const rightBracket = colorizer.colorize(info.level, "]");
return `${info.timestamp} ${leftBracket}${info.level.toUpperCase()}${rightBracket} ${info.message}`;
}),
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'ttt.log' }),
],
});
const application = express();
if (!('INFERKIT_API_KEY' in process.env)) {
logger.error('Cannot continue without an InferKit API key.', () => {
process.exit(1);
});
}
if (!('ORIGIN' in process.env)) {
logger.error('Cannot continue without an origin.', () => {
process.exit(1);
});
}
application.set('view engine', 'pug');
application.use(express.static(`${__dirname}/static`));
application.use(express.json());
application.get('/', (request, response) => {
response.render('index', {
title: "Talk to Transformer",
});
});
logger.info("Starting server");
const server = new websocket.server({
httpServer: application.listen(8011),
});
server.on('request', (request) => {
if (request.origin !== process.env.ORIGIN) {
return;
}
logger.info("Accepting client connection from %s", request.origin);
const connection = request.accept(null, request.origin);
connection.on('message', async ({ utf8Data: rawRequestData }) => {
try {
logger.info("Sending request data: %s", rawRequestData);
const apiResponse = await fetch('https://api.inferkit.com/v1/models/standard/generate', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.INFERKIT_API_KEY}`,
'Content-Type': 'application/json',
},
body: rawRequestData,
});
for await (const rawLinesBytes of apiResponse.body) {
const rawLines = rawLinesBytes.toString('utf-8');
logger.info("Received response part: %o", rawLines);
for (const rawLine of rawLines.split('\n')) {
if (rawLine.length > 0) {
const chunk = JSON.parse(rawLine).data;
connection.sendUTF(JSON.stringify({
text: chunk.text,
}));
}
}
}
}
catch (error) {
logger.error(error);
connection.sendUTF(JSON.stringify({
error: "Server error",
}));
}
finally {
logger.info("Closing client connection from %s", request.origin);
connection.close();
}
});
});