ardasen commited on
Commit
894eb66
·
verified ·
1 Parent(s): 5c49bef

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +23 -9
server.js CHANGED
@@ -18,35 +18,49 @@ app.use((req, res, next) => {
18
  next();
19
  });
20
 
21
-
22
  // Middleware to authenticate requests with the proxy key and check the model
23
  function authenticateProxyKeyAndModel(req, res, next) {
24
- const providedKey = req.headers['auro']; // Assuming the key is sent in the 'x-proxy-key' header
25
  const requestedModel = req.body.model;
26
 
27
- // List of allowed models
28
  const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b'];
29
 
30
  if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) {
31
- // If the provided key matches the expected key and the requested model is allowed, allow the request to proceed
32
  next();
33
  } else {
34
- // If the key is missing or incorrect, or the model is not allowed, reject the request with an error response
35
  res.status(401).json({ error: 'Unauthorized or invalid model' });
36
  }
37
  }
38
 
 
 
 
 
 
39
 
40
-
41
  app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
42
  proxyReqPathResolver: (req) => '/api/v1/chat/completions',
43
  proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
44
- // Modify the request headers if necessary
45
  proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
46
  return proxyReqOpts;
47
  },
48
- }));
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  app.get("/", (req, res) => {
52
  // res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
@@ -63,4 +77,4 @@ function getExternalUrl(spaceId) {
63
 
64
  app.listen(port, () => {
65
  console.log(`Reverse proxy server running on ${baseUrl}`);
66
- });
 
18
  next();
19
  });
20
 
 
21
  // Middleware to authenticate requests with the proxy key and check the model
22
  function authenticateProxyKeyAndModel(req, res, next) {
23
+ const providedKey = req.headers['auro'];
24
  const requestedModel = req.body.model;
25
 
 
26
  const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b'];
27
 
28
  if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) {
 
29
  next();
30
  } else {
 
31
  res.status(401).json({ error: 'Unauthorized or invalid model' });
32
  }
33
  }
34
 
35
+ // Sanitize content function to only trim excessive whitespace and newlines
36
+ function sanitizeContent(content) {
37
+ // Remove excessive newlines or whitespace
38
+ return content.replace(/\s+/g, ' ').trim();
39
+ }
40
 
41
+ // Modify proxy response to sanitize content
42
  app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
43
  proxyReqPathResolver: (req) => '/api/v1/chat/completions',
44
  proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
 
45
  proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
46
  return proxyReqOpts;
47
  },
48
+ userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
49
+ const responseData = JSON.parse(proxyResData.toString('utf8'));
50
 
51
+ // Sanitize the 'content' field in the response
52
+ if (responseData && responseData.choices) {
53
+ responseData.choices = responseData.choices.map(choice => {
54
+ if (choice.message && choice.message.content) {
55
+ choice.message.content = sanitizeContent(choice.message.content);
56
+ }
57
+ return choice;
58
+ });
59
+ }
60
+
61
+ return JSON.stringify(responseData);
62
+ }
63
+ }));
64
 
65
  app.get("/", (req, res) => {
66
  // res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
 
77
 
78
  app.listen(port, () => {
79
  console.log(`Reverse proxy server running on ${baseUrl}`);
80
+ });