-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (105 loc) · 3.69 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const http = require('http');
const https = require('https');
const config = require('./config.json');
const apiKeys = config.apiKeys;
let currentApiKeyIndex = 0;
let attemptCount = 0;
function getNextApiKey() {
const numKeys = apiKeys.length;
currentApiKeyIndex = (currentApiKeyIndex + 1) % numKeys;
return apiKeys[currentApiKeyIndex];
}
function forwardToOpenAI(apiKeyInfo, url, data, res) {
const { key, url: apiUrl } = apiKeyInfo;
const openaiUrl = apiUrl + url;
const parsedUrl = new URL(openaiUrl);
const transport = parsedUrl.protocol === 'https:' ? https : http;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${encodeURIComponent(key)}`,
},
};
const req = transport.request(parsedUrl, options, (proxyRes) => {
console.log('Received response from the reverse proxy. Status:', proxyRes.statusCode);
if (proxyRes.statusCode === 429 || proxyRes.statusCode === 418 || proxyRes.statusCode === 502 || proxyRes.statusCode === 400) {
handleReverseProxyError(res, url, data);
} else {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
}
});
req.on('error', (error) => {
console.error('Error sending request to OpenAI:', error);
handleReverseProxyError(res, url, data);
});
getNextApiKey();
req.write(data);
req.end();
}
function checkModel(apiKey, model, url, data, res) {
if (apiKey === undefined) {
res.statusCode = 500;
res.end(JSON.stringify({ error: 'No API key found' }));
return;
}
const apiKeyInfo = apiKeys.find((info) => info.key === apiKey);
if (!apiKeyInfo) {
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Invalid API key' }));
} else if (apiKeyInfo.models.includes(model)) {
attemptCount = 0;
console.log(`Forwarding to ${apiKeyInfo.url} with API key: ${apiKey}`);
forwardToOpenAI(apiKeyInfo, url, data, res);
} else {
console.log('Model not supported by this API key');
modelNotSupported(apiKey, model, url, data, res);
}
}
function handleReverseProxyError(res, url, data) {
console.log('Error from the reverse proxy. Changing API key and retrying.');
const newApiKeyInfo = getNextApiKey();
forwardToOpenAI(newApiKeyInfo, url, data, res);
console.log('Forwarding to', newApiKeyInfo.url, 'with API key:', newApiKeyInfo.key);
}
async function modelNotSupported(apiKey, model, url, data, res) {
if (attemptCount >= apiKeys.length) {
// All API keys have been tried and none support the model
res.statusCode = 500;
res.end(JSON.stringify({ error: 'No API key available for this model' }));
return;
}
attemptCount++; // Increment the count of attempts
const newApiKeyInfo = getNextApiKey();
checkModel(newApiKeyInfo.key, model, url, data, res);
}
http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
console.log('Received POST request:', req.url);
if (req.method !== 'POST') {
res.statusCode = 405; // Method Not Allowed
res.end();
return;
}
let data = '';
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
try {
const payload = JSON.parse(data);
const model = payload.model;
const apiKeyInfo = apiKeys[currentApiKeyIndex];
const apiKey = apiKeyInfo.key;
checkModel(apiKey, model, req.url, data, res);
} catch (error) {
console.error('Error processing request:', error);
res.statusCode = 400; // Bad Request
res.end(JSON.stringify({ error: 'Invalid JSON payload' }));
}
});
}).listen(3456, 'localhost', () => {
console.log('Server running at http://localhost:3000/');
});