Update server.js
Browse files
server.js
CHANGED
|
@@ -38,12 +38,14 @@ function authenticateProxyKeyAndModel(req, res, next) {
|
|
| 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
|
| 46 |
-
message.content = message.content
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
return message;
|
| 49 |
});
|
|
@@ -54,7 +56,6 @@ app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
|
|
| 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);
|
|
@@ -64,6 +65,7 @@ app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
|
|
| 64 |
return proxyReqOpts;
|
| 65 |
},
|
| 66 |
}));
|
|
|
|
| 67 |
//app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
|
| 68 |
// proxyReqPathResolver: (req) => '/api/v1/chat/completions',
|
| 69 |
// proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
|
|
|
|
| 41 |
if (req.body && req.body.messages) {
|
| 42 |
req.body.messages = req.body.messages.map(message => {
|
| 43 |
if (message.content) {
|
| 44 |
+
// Remove all newlines at the start and end, and replace multiple newlines with a single space
|
| 45 |
+
message.content = message.content
|
| 46 |
+
.replace(/^\n+|\n+$/g, '') // Remove leading and trailing newlines
|
| 47 |
+
.replace(/\n+/g, ' ') // Replace multiple newlines with a single space
|
| 48 |
+
.trim(); // Remove any leading or trailing spaces
|
| 49 |
}
|
| 50 |
return message;
|
| 51 |
});
|
|
|
|
| 56 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
| 57 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 58 |
|
|
|
|
| 59 |
if (srcReq.body) {
|
| 60 |
const bodyData = JSON.stringify(srcReq.body);
|
| 61 |
proxyReqOpts.headers['Content-Length'] = Buffer.byteLength(bodyData);
|
|
|
|
| 65 |
return proxyReqOpts;
|
| 66 |
},
|
| 67 |
}));
|
| 68 |
+
|
| 69 |
//app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
|
| 70 |
// proxyReqPathResolver: (req) => '/api/v1/chat/completions',
|
| 71 |
// proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|