-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
175 lines (148 loc) · 5.15 KB
/
server.js
File metadata and controls
175 lines (148 loc) · 5.15 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
const http = require('http');
const https = require('https');
const config = require('./config.json');
const apiKeys = config.apiKeys;
const PORT = 3456;
let currentApiKeyIndex = 0;
let attemptCount = 0;
updateLatencies().catch((err) => console.error('Failed to update latencies', err));
setInterval(updateLatencies, 60000);
async function measureLatency(apiKeyInfo) {
return new Promise((resolve) => {
try {
const url = new URL(apiKeyInfo.url);
const transport = url.protocol === 'https:' ? https : http;
const start = Date.now();
const req = transport.request(url, { method: 'HEAD' }, (res) => {
res.on('end', () => resolve(Date.now() - start));
res.resume();
});
req.on('error', () => resolve(Number.MAX_SAFE_INTEGER));
req.setTimeout(5000, () => {
req.destroy();
resolve(Number.MAX_SAFE_INTEGER);
});
req.end();
} catch {
resolve(Number.MAX_SAFE_INTEGER);
}
});
}
async function updateLatencies() {
const latencies = await Promise.all(apiKeys.map((k) => measureLatency(k)));
latencies.forEach((latency, i) => {
apiKeys[i].latency = latency;
});
apiKeys.sort((a, b) => a.latency - b.latency);
currentApiKeyIndex = 0;
console.log('Updated proxy latencies:', apiKeys.map(k => `${k.url}:${k.latency}`).join(', '));
}
function getNextApiKey() {
const numKeys = apiKeys.length;
currentApiKeyIndex = (currentApiKeyIndex + 1) % numKeys;
return apiKeys[currentApiKeyIndex];
}
function getBestApiKeyForModel(model) {
for (let i = 0; i < apiKeys.length; i++) {
const info = apiKeys[i];
if (info.models.includes(model) || info.models.includes('others')) {
currentApiKeyIndex = i;
return info;
}
}
return undefined;
}
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) || apiKeyInfo.models.includes('others')) {
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 = getBestApiKeyForModel(model);
const apiKey = apiKeyInfo ? apiKeyInfo.key : undefined;
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(PORT, '0.0.0.0', () => {
console.log(`Server running at http://0.0.0.0:${PORT}/`);
});