Update server.js
Browse files
server.js
CHANGED
|
@@ -37,15 +37,41 @@ function authenticateProxyKeyAndModel(req, res, next) {
|
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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) => {
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
+
app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
|
| 41 |
+
// Process the request body before sending it to the proxy
|
| 42 |
+
if (req.body && req.body.messages) {
|
| 43 |
+
req.body.messages = req.body.messages.map(message => {
|
| 44 |
+
if (message.content) {
|
| 45 |
+
// Remove all newlines and trim the content
|
| 46 |
+
message.content = message.content.replace(/\n+/g, ' ').trim();
|
| 47 |
+
}
|
| 48 |
+
return message;
|
| 49 |
+
});
|
| 50 |
+
}
|
| 51 |
+
next();
|
| 52 |
+
}, proxy(targetUrl, {
|
| 53 |
proxyReqPathResolver: (req) => '/api/v1/chat/completions',
|
| 54 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
|
|
|
| 55 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 56 |
+
|
| 57 |
+
// Stringify the modified body
|
| 58 |
+
if (srcReq.body) {
|
| 59 |
+
const bodyData = JSON.stringify(srcReq.body);
|
| 60 |
+
proxyReqOpts.headers['Content-Length'] = Buffer.byteLength(bodyData);
|
| 61 |
+
proxyReqOpts.body = bodyData;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
return proxyReqOpts;
|
| 65 |
},
|
| 66 |
}));
|
| 67 |
+
//app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
|
| 68 |
+
// proxyReqPathResolver: (req) => '/api/v1/chat/completions',
|
| 69 |
+
// proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
| 70 |
+
// Modify the request headers if necessary
|
| 71 |
+
// proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 72 |
+
// return proxyReqOpts;
|
| 73 |
+
// },
|
| 74 |
+
//}));
|
| 75 |
|
| 76 |
|
| 77 |
app.get("/", (req, res) => {
|