Update server.js
Browse files
server.js
CHANGED
|
@@ -40,12 +40,16 @@ function authenticateProxyKeyAndModel(req, res, next) {
|
|
| 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 newlines from the start and end of the content
|
| 45 |
-
|
|
|
|
| 46 |
}
|
| 47 |
return message;
|
| 48 |
});
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
next();
|
| 51 |
}, proxy(targetUrl, {
|
|
@@ -54,9 +58,13 @@ app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
|
|
| 54 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 55 |
|
| 56 |
if (srcReq.body) {
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
proxyReqOpts.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
return proxyReqOpts;
|
|
|
|
| 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.role !== 'system' && message.content) {
|
| 44 |
+
// Remove newlines and extra spaces from the start and end of the content
|
| 45 |
+
// for non-system messages only
|
| 46 |
+
message.content = message.content.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
|
| 47 |
}
|
| 48 |
return message;
|
| 49 |
});
|
| 50 |
+
|
| 51 |
+
// Log the processed messages to verify the changes
|
| 52 |
+
console.log('Processed messages:', JSON.stringify(req.body.messages, null, 2));
|
| 53 |
}
|
| 54 |
next();
|
| 55 |
}, proxy(targetUrl, {
|
|
|
|
| 58 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 59 |
|
| 60 |
if (srcReq.body) {
|
| 61 |
+
// Use the modified body
|
| 62 |
+
const modifiedBody = JSON.stringify(srcReq.body);
|
| 63 |
+
proxyReqOpts.headers['Content-Length'] = Buffer.byteLength(modifiedBody);
|
| 64 |
+
proxyReqOpts.body = modifiedBody;
|
| 65 |
+
|
| 66 |
+
// Log the body being sent to the API
|
| 67 |
+
console.log('Body being sent to API:', modifiedBody);
|
| 68 |
}
|
| 69 |
|
| 70 |
return proxyReqOpts;
|