|
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; |
|
const port = 7860; |
|
const baseUrl = getExternalUrl(process.env.SPACE_ID); |
|
|
|
app.use(bodyParser.json({ limit: '50mb' })); |
|
|
|
|
|
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(); |
|
}); |
|
|
|
|
|
|
|
function authenticateProxyKeyAndModel(req, res, next) { |
|
const providedKey = req.headers['auro']; |
|
const requestedModel = req.body.model; |
|
|
|
|
|
const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b' ,'undi95/toppy-m-7b']; |
|
|
|
if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) { |
|
|
|
next(); |
|
} else { |
|
|
|
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) { |
|
|
|
|
|
message.content = message.content.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); |
|
} |
|
return message; |
|
}); |
|
|
|
|
|
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) { |
|
|
|
const modifiedBody = JSON.stringify(srcReq.body); |
|
proxyReqOpts.headers['Content-Length'] = Buffer.byteLength(modifiedBody); |
|
proxyReqOpts.body = modifiedBody; |
|
|
|
|
|
console.log('Body being sent to API:', modifiedBody); |
|
} |
|
|
|
return proxyReqOpts; |
|
}, |
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get("/", (req, res) => { |
|
|
|
}); |
|
|
|
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}`); |
|
}); |