Update server.js
Browse files
server.js
CHANGED
|
@@ -5,18 +5,24 @@ const axios = require('axios');
|
|
| 5 |
const app = express();
|
| 6 |
app.use(express.json());
|
| 7 |
|
| 8 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const limiter = rateLimit({
|
| 11 |
-
windowMs: 5 * 1000, //
|
| 12 |
-
max: 1, // лимит каждые
|
| 13 |
handler: function (req, res) {
|
| 14 |
return res.status(429).json("wait");
|
| 15 |
},
|
| 16 |
});
|
| 17 |
|
| 18 |
-
// Применение ограничителя скорости перед обработчиком маршрута /
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
const start = `${process.env.start}`;
|
| 22 |
|
|
@@ -26,7 +32,7 @@ app.post('/cr', async (req, res) => {
|
|
| 26 |
|
| 27 |
app.post('/pl', async (req, res) => {
|
| 28 |
const prompt = req.body.prompt;
|
| 29 |
-
const apiKey = req.body.api ||
|
| 30 |
|
| 31 |
if (!prompt) {
|
| 32 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
@@ -37,11 +43,7 @@ app.post('/pl', async (req, res) => {
|
|
| 37 |
messages: [{'role': 'system', 'content': start}, {'role': 'user', 'content': prompt}],
|
| 38 |
max_tokens: 2000,
|
| 39 |
temperature: 0.8,
|
| 40 |
-
|
| 41 |
-
//frequency_penalty: -0.1,
|
| 42 |
-
model: "gemini-1.5-pro-latest",
|
| 43 |
-
//model: "deepseek-chat",
|
| 44 |
-
//model: "gemini-1.5-flash-latest",
|
| 45 |
}, {
|
| 46 |
headers: {
|
| 47 |
'Authorization': `Bearer ${apiKey}`,
|
|
@@ -51,7 +53,6 @@ app.post('/pl', async (req, res) => {
|
|
| 51 |
|
| 52 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 53 |
const content = response.data.choices[0].message.content.trim();
|
| 54 |
-
//console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`);
|
| 55 |
res.json({ content });
|
| 56 |
} else {
|
| 57 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
@@ -64,7 +65,7 @@ app.post('/pl', async (req, res) => {
|
|
| 64 |
|
| 65 |
app.post('/cre', async (req, res) => {
|
| 66 |
const prompt = req.body.prompt;
|
| 67 |
-
const apiKey = req.body.api ||
|
| 68 |
|
| 69 |
if (!prompt) {
|
| 70 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
@@ -75,11 +76,7 @@ app.post('/cre', async (req, res) => {
|
|
| 75 |
messages: [{'role': 'system', 'content': start}, {'role': 'system', 'content': 'И ОБЯЗАТЕЛЬНО, всегда в конце "text" проси обновить приложение, уведомляя о том, что скоро в жтой версии перестанешь отвечать!'}, {'role': 'user', 'content': prompt}],
|
| 76 |
max_tokens: 2000,
|
| 77 |
temperature: 0.6,
|
| 78 |
-
// presence_penalty: 0.0,
|
| 79 |
-
//frequency_penalty: -0.1,
|
| 80 |
model: "gemini-1.5-pro-002",
|
| 81 |
-
// model: "deepseek-chat",
|
| 82 |
-
//model: "gemini-1.5-flash-latest",
|
| 83 |
}, {
|
| 84 |
headers: {
|
| 85 |
'Authorization': `Bearer ${apiKey}`,
|
|
@@ -89,7 +86,6 @@ app.post('/cre', async (req, res) => {
|
|
| 89 |
|
| 90 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 91 |
const content = response.data.choices[0].message.content.trim();
|
| 92 |
-
// console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`);
|
| 93 |
res.json({ content });
|
| 94 |
} else {
|
| 95 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
@@ -102,7 +98,7 @@ app.post('/cre', async (req, res) => {
|
|
| 102 |
|
| 103 |
app.post('/crebeta', async (req, res) => {
|
| 104 |
const prompt = req.body.prompt;
|
| 105 |
-
const apiKey = req.body.api ||
|
| 106 |
|
| 107 |
if (!prompt) {
|
| 108 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
@@ -113,11 +109,7 @@ app.post('/crebeta', async (req, res) => {
|
|
| 113 |
messages: [{'role': 'system', 'content': start}, {'role': 'user', 'content': prompt}],
|
| 114 |
max_tokens: 2000,
|
| 115 |
temperature: 0.24,
|
| 116 |
-
// presence_penalty: 0.0,
|
| 117 |
-
//frequency_penalty: -0.1,
|
| 118 |
model: "gemini-1.5-pro-002",
|
| 119 |
-
// model: "gemma-2-27b-it",
|
| 120 |
-
//model: "gemini-1.5-flash-latest",
|
| 121 |
}, {
|
| 122 |
headers: {
|
| 123 |
'Authorization': `Bearer ${apiKey}`,
|
|
@@ -127,7 +119,6 @@ app.post('/crebeta', async (req, res) => {
|
|
| 127 |
|
| 128 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 129 |
const content = response.data.choices[0].message.content.trim();
|
| 130 |
-
// console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`);
|
| 131 |
res.json({ content });
|
| 132 |
} else {
|
| 133 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
@@ -138,7 +129,6 @@ app.post('/crebeta', async (req, res) => {
|
|
| 138 |
}
|
| 139 |
});
|
| 140 |
|
| 141 |
-
|
| 142 |
const port = 7860;
|
| 143 |
app.listen(port, () => {
|
| 144 |
console.log(`API сервер запущен на порту ${port}`);
|
|
|
|
| 5 |
const app = express();
|
| 6 |
app.use(express.json());
|
| 7 |
|
| 8 |
+
const openai_keys = process.env.OPENAI_KEY.split(',');
|
| 9 |
+
|
| 10 |
+
function getRandomApiKey() {
|
| 11 |
+
const randomIndex = Math.floor(Math.random() * openai_keys.length);
|
| 12 |
+
return openai_keys[randomIndex];
|
| 13 |
+
}
|
| 14 |
|
| 15 |
const limiter = rateLimit({
|
| 16 |
+
windowMs: 5 * 1000, // 5 секунд
|
| 17 |
+
max: 1, // лимит каждые 5 секунд на IP
|
| 18 |
handler: function (req, res) {
|
| 19 |
return res.status(429).json("wait");
|
| 20 |
},
|
| 21 |
});
|
| 22 |
|
| 23 |
+
// Применение ограничителя скорости перед обработчиком маршрута /pl и /crebeta
|
| 24 |
+
app.use('/pl', limiter);
|
| 25 |
+
app.use('/crebeta', limiter);
|
| 26 |
|
| 27 |
const start = `${process.env.start}`;
|
| 28 |
|
|
|
|
| 32 |
|
| 33 |
app.post('/pl', async (req, res) => {
|
| 34 |
const prompt = req.body.prompt;
|
| 35 |
+
const apiKey = req.body.api || getRandomApiKey();
|
| 36 |
|
| 37 |
if (!prompt) {
|
| 38 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
|
|
| 43 |
messages: [{'role': 'system', 'content': start}, {'role': 'user', 'content': prompt}],
|
| 44 |
max_tokens: 2000,
|
| 45 |
temperature: 0.8,
|
| 46 |
+
model: "deepseek-chat",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}, {
|
| 48 |
headers: {
|
| 49 |
'Authorization': `Bearer ${apiKey}`,
|
|
|
|
| 53 |
|
| 54 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 55 |
const content = response.data.choices[0].message.content.trim();
|
|
|
|
| 56 |
res.json({ content });
|
| 57 |
} else {
|
| 58 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
|
|
| 65 |
|
| 66 |
app.post('/cre', async (req, res) => {
|
| 67 |
const prompt = req.body.prompt;
|
| 68 |
+
const apiKey = req.body.api || getRandomApiKey();
|
| 69 |
|
| 70 |
if (!prompt) {
|
| 71 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
|
|
| 76 |
messages: [{'role': 'system', 'content': start}, {'role': 'system', 'content': 'И ОБЯЗАТЕЛЬНО, всегда в конце "text" проси обновить приложение, уведомляя о том, что скоро в жтой версии перестанешь отвечать!'}, {'role': 'user', 'content': prompt}],
|
| 77 |
max_tokens: 2000,
|
| 78 |
temperature: 0.6,
|
|
|
|
|
|
|
| 79 |
model: "gemini-1.5-pro-002",
|
|
|
|
|
|
|
| 80 |
}, {
|
| 81 |
headers: {
|
| 82 |
'Authorization': `Bearer ${apiKey}`,
|
|
|
|
| 86 |
|
| 87 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 88 |
const content = response.data.choices[0].message.content.trim();
|
|
|
|
| 89 |
res.json({ content });
|
| 90 |
} else {
|
| 91 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
|
|
| 98 |
|
| 99 |
app.post('/crebeta', async (req, res) => {
|
| 100 |
const prompt = req.body.prompt;
|
| 101 |
+
const apiKey = req.body.api || getRandomApiKey();
|
| 102 |
|
| 103 |
if (!prompt) {
|
| 104 |
return res.status(400).json("wait"); // Не удалось принять данные
|
|
|
|
| 109 |
messages: [{'role': 'system', 'content': start}, {'role': 'user', 'content': prompt}],
|
| 110 |
max_tokens: 2000,
|
| 111 |
temperature: 0.24,
|
|
|
|
|
|
|
| 112 |
model: "gemini-1.5-pro-002",
|
|
|
|
|
|
|
| 113 |
}, {
|
| 114 |
headers: {
|
| 115 |
'Authorization': `Bearer ${apiKey}`,
|
|
|
|
| 119 |
|
| 120 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 121 |
const content = response.data.choices[0].message.content.trim();
|
|
|
|
| 122 |
res.json({ content });
|
| 123 |
} else {
|
| 124 |
res.status(500).json({ content: 'errora' }); // Ошибка прочтения
|
|
|
|
| 129 |
}
|
| 130 |
});
|
| 131 |
|
|
|
|
| 132 |
const port = 7860;
|
| 133 |
app.listen(port, () => {
|
| 134 |
console.log(`API сервер запущен на порту ${port}`);
|