Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const axios = require('axios');
|
| 3 |
+
const bodyParser = require('body-parser');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const port = 3000;
|
| 7 |
+
|
| 8 |
+
app.use(bodyParser.json());
|
| 9 |
+
|
| 10 |
+
app.post('/v1/chat/completions', async (req, res) => {
|
| 11 |
+
const requestBody = req.body;
|
| 12 |
+
|
| 13 |
+
// Check if the model is gpt-3.5-turbo and change it to gpt
|
| 14 |
+
if (requestBody.model === 'llama-3.1-8b-instant') {
|
| 15 |
+
requestBody.model = 'test';
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
// Forward the modified request to the OpenAI API
|
| 20 |
+
const response = await axios.post('https://api.groq.com/openai/v1/chat/completions', requestBody, {
|
| 21 |
+
headers: {
|
| 22 |
+
'Authorization': req.headers['authorization'],
|
| 23 |
+
'Content-Type': 'application/json'
|
| 24 |
+
}
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
// Send the response back to the client
|
| 28 |
+
res.json(response.data);
|
| 29 |
+
} catch (error) {
|
| 30 |
+
// Handle any errors
|
| 31 |
+
res.status(error.response ? error.response.status : 500).json({
|
| 32 |
+
error: error.message,
|
| 33 |
+
details: error.response ? error.response.data : null
|
| 34 |
+
});
|
| 35 |
+
}
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
app.listen(port, () => {
|
| 39 |
+
console.log(`API proxy server running on port ${port}`);
|
| 40 |
+
});
|