Ayeantics commited on
Commit
c4cb864
·
verified ·
1 Parent(s): e5b45d3

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +11 -31
server.js CHANGED
@@ -13,6 +13,9 @@ const openaiKey = process.env.OPENAI_KEY;
13
  const port = 7860;
14
  const baseUrl = getExternalUrl(process.env.SPACE_ID);
15
 
 
 
 
16
  const allowedModels = [
17
  'gpt-3.5-turbo-0125',
18
  'gpt-3.5-turbo',
@@ -23,9 +26,6 @@ const allowedModels = [
23
  'gpt-3.5-turbo-16k-0613'
24
  ];
25
 
26
- let requestDetails = [];
27
- let apiKeyDetails = {}; // Track API key usage and first use timestamp
28
-
29
  io.on('connection', (socket) => {
30
  console.log('A user connected to the websocket for live logs.');
31
  });
@@ -56,52 +56,32 @@ function authenticateApiKey(req, res, next) {
56
  return res.status(401).send('Unauthorized: API key is invalid');
57
  }
58
 
59
- const displayKey = apiKeyMatch.trim().substring(0, 7); // Only use the first four characters for display
60
 
61
  if (!apiKeyDetails[apiKeyMatch]) {
62
  apiKeyDetails[apiKeyMatch] = {
63
  count: 0,
64
- firstUse: moment() // Set the first use to now if it's not already set
65
  };
66
  }
67
 
68
- // Check if more than a week has passed since the first use
69
  if (moment().diff(apiKeyDetails[apiKeyMatch].firstUse, 'weeks') >= 1) {
70
  return res.status(429).send(`API Key ${displayKey}... is blocked after one week of use.`);
71
  }
72
 
73
- // Increment the request count
74
  apiKeyDetails[apiKeyMatch].count++;
75
-
76
  const logMessage = `API Key ${displayKey}... used ${apiKeyDetails[apiKeyMatch].count} times so far.`;
77
  logAndEmit(logMessage, true);
78
 
79
- req.apiKey = apiKeyMatch; // Store the API key for the request
80
  next();
81
  }
82
 
83
- app.use('/api', authenticateApiKey, (req, res, next) => {
84
- const urlParts = req.url.split('/');
85
- const modelId = urlParts.length > 1 ? urlParts[1] : null;
86
-
87
- // Check if the model ID is part of the allowed models.
88
- if (!modelId || !allowedModels.includes(modelId)) {
89
- return res.status(403).send("Access Denied: This model is not allowed.");
90
  }
91
-
92
- const timestamp = moment().tz("Asia/Tbilisi").format("DD-MMM-YYYY HH:mm");
93
- const displayKey = req.apiKey.substring(0, 10);
94
- const requestData = {
95
- requestNumber: apiKeyDetails[req.apiKey].count,
96
- apiKey: displayKey,
97
- timestamp: timestamp,
98
- text: req.method + ' ' + req.url
99
- };
100
- requestDetails.push(requestData);
101
-
102
- const detailedLog = `Request ${requestData.requestNumber} on ${requestData.timestamp} from API Key ${displayKey}... with method "${req.method}" and URL line "${req.url}"`;
103
- logAndEmit(detailedLog, true);
104
-
105
  next();
106
  }, proxy(targetUrl, {
107
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
@@ -131,5 +111,5 @@ function getExternalUrl(spaceId) {
131
 
132
  server.listen(port, () => {
133
  const message = `Reverse proxy server and WebSocket running on port ${port}`;
134
- logAndEmit(message, true); // true to include total requests in the log
135
  });
 
13
  const port = 7860;
14
  const baseUrl = getExternalUrl(process.env.SPACE_ID);
15
 
16
+ let requestDetails = [];
17
+ let apiKeyDetails = {};
18
+
19
  const allowedModels = [
20
  'gpt-3.5-turbo-0125',
21
  'gpt-3.5-turbo',
 
26
  'gpt-3.5-turbo-16k-0613'
27
  ];
28
 
 
 
 
29
  io.on('connection', (socket) => {
30
  console.log('A user connected to the websocket for live logs.');
31
  });
 
56
  return res.status(401).send('Unauthorized: API key is invalid');
57
  }
58
 
59
+ const displayKey = apiKeyMatch.trim().substring(0, 7);
60
 
61
  if (!apiKeyDetails[apiKeyMatch]) {
62
  apiKeyDetails[apiKeyMatch] = {
63
  count: 0,
64
+ firstUse: moment()
65
  };
66
  }
67
 
 
68
  if (moment().diff(apiKeyDetails[apiKeyMatch].firstUse, 'weeks') >= 1) {
69
  return res.status(429).send(`API Key ${displayKey}... is blocked after one week of use.`);
70
  }
71
 
 
72
  apiKeyDetails[apiKeyMatch].count++;
 
73
  const logMessage = `API Key ${displayKey}... used ${apiKeyDetails[apiKeyMatch].count} times so far.`;
74
  logAndEmit(logMessage, true);
75
 
76
+ req.apiKey = apiKeyMatch;
77
  next();
78
  }
79
 
80
+ app.use('/api/models/:model', authenticateApiKey, (req, res, next) => {
81
+ const modelRequested = req.params.model;
82
+ if (!allowedModels.includes(modelRequested)) {
83
+ return res.status(403).send('The requested model is not permitted.');
 
 
 
84
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  next();
86
  }, proxy(targetUrl, {
87
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
 
111
 
112
  server.listen(port, () => {
113
  const message = `Reverse proxy server and WebSocket running on port ${port}`;
114
+ logAndEmit(message, true);
115
  });