Spaces:
Paused
Paused
File size: 3,092 Bytes
b916984 ab5f95e b916984 47dc05b b916984 9b90c04 c201395 9b90c04 95324b3 9b90c04 95324b3 9b90c04 95324b3 9b90c04 95324b3 9b90c04 95324b3 9b90c04 95324b3 9b90c04 f334d87 95324b3 2ce0bd8 f8f1dbb 352b062 2ce0bd8 ff13605 352b062 ff13605 2ce0bd8 5c49bef b916984 111921a 2ce0bd8 352b062 9b4e460 7d16ae5 352b062 7d16ae5 2ce0bd8 b916984 894eb66 5c49bef 95324b3 98993ab 7b568a3 b916984 95324b3 b916984 a9d61d0 b916984 a9d61d0 4644cc6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
const bodyParser = require('body-parser');
const targetUrl = 'https://openrouter.ai';
const openaiKey = process.env.OPENAI_KEY;
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
const port = 7860;
const baseUrl = getExternalUrl(process.env.SPACE_ID);
app.use(bodyParser.json({ limit: '50mb' }));
// Middleware to log request details
app.use((req, res, next) => {
console.log(`Incoming Request - Method: ${req.method}, URL: ${req.url}`);
console.log('Headers:', req.headers);
console.log('Body:', req.body);
next();
});
// Middleware to authenticate requests with the proxy key and check the model
function authenticateProxyKeyAndModel(req, res, next) {
const providedKey = req.headers['auro']; // Assuming the key is sent in the 'x-proxy-key' header
const requestedModel = req.body.model;
// List of allowed models
const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b'];
if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) {
// If the provided key matches the expected key and the requested model is allowed, allow the request to proceed
next();
} else {
// If the key is missing or incorrect, or the model is not allowed, reject the request with an error response
res.status(401).json({ error: 'Unauthorized or invalid model' });
}
}
app.use('/api', authenticateProxyKeyAndModel, (req, res, next) => {
if (req.body && req.body.messages) {
req.body.messages = req.body.messages.map(message => {
if (message.role !== 'system' && message.content) {
// Remove newlines and extra spaces from the start and end of the content
// for non-system messages only
message.content = message.content.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}
return message;
});
// Log the processed messages to verify the changes
console.log('Processed messages:', JSON.stringify(req.body.messages, null, 2));
}
next();
}, proxy(targetUrl, {
proxyReqPathResolver: (req) => '/api/v1/chat/completions',
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
if (srcReq.body) {
// Use the modified body
const modifiedBody = JSON.stringify(srcReq.body);
proxyReqOpts.headers['Content-Length'] = Buffer.byteLength(modifiedBody);
proxyReqOpts.body = modifiedBody;
// Log the body being sent to the API
console.log('Body being sent to API:', modifiedBody);
}
return proxyReqOpts;
},
}));
app.get("/", (req, res) => {
// res.send(This is your OpenAI Reverse Proxy URL: ${baseUrl});
});
function getExternalUrl(spaceId) {
try {
const [username, spacename] = spaceId.split("/");
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
} catch (e) {
return "";
}
}
app.listen(port, () => {
console.log(`Reverse proxy server running on ${baseUrl}`);
}); |