|
const express = require('express'); |
|
const rateLimit = require('express-rate-limit'); |
|
const axios = require('axios'); |
|
|
|
const app = express(); |
|
app.use(express.json()); |
|
|
|
|
|
app.set('trust proxy', 1); |
|
|
|
const openai_keys = process.env.OPENAI_KEY.split(','); |
|
|
|
function getRandomApiKey() { |
|
const randomIndex = Math.floor(Math.random() * openai_keys.length); |
|
return openai_keys[randomIndex]; |
|
} |
|
|
|
const limiter = rateLimit({ |
|
windowMs: 5 * 1000, |
|
max: 1, |
|
handler: function (req, res) { |
|
return res.status(429).json("wait"); |
|
}, |
|
}); |
|
|
|
|
|
app.use('/pl', limiter); |
|
app.use('/crebeta', limiter); |
|
|
|
const start = `${process.env.start}`; |
|
const startconnect = `${process.env.startconnect}`; |
|
|
|
app.post('/cr', async (req, res) => { |
|
res.json({ content: `{"whate":"🪨", "howe":"ОБНОВИТЕСЬ", "text":"Текущая версия приложения устарела. Установите новую из нашего телеграм канала: @yufi_ru", "succ":"победа", "what":"Версию", "how":"Обновите", "howl":"@yufi_ru"}` }); |
|
}); |
|
|
|
app.post('/pl', async (req, res) => { |
|
const prompt = req.body.prompt; |
|
|
|
|
|
if (!prompt) { |
|
return res.status(400).json("wait"); |
|
} |
|
|
|
try { |
|
const response = await axios.post('https://text.pollinations.ai/openai', { |
|
messages: [{'role': 'system', 'content': start}, {'role': 'user', 'content': prompt}], |
|
max_tokens: 1200, |
|
temperature: 0.25, |
|
|
|
}, { |
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
}, |
|
}); |
|
|
|
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) { |
|
const content = response.data.choices[0].message.content.trim(); |
|
console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`); |
|
res.json({ content }); |
|
} else { |
|
res.status(500).json({ content: 'errora' }); |
|
} |
|
} catch (error) { |
|
console.error(error); |
|
res.status(500).json({ content: 'errorb' }); |
|
} |
|
}); |
|
|
|
app.post('/cre', async (req, res) => { |
|
const prompt = req.body.prompt; |
|
|
|
|
|
if (!prompt) { |
|
return res.status(400).json("wait"); |
|
} |
|
|
|
try { |
|
const response = await axios.post('https://text.pollinations.ai/openai', { |
|
messages: [{'role': 'system', 'content': start}, {'role': 'system', 'content': 'И ОБЯЗАТЕЛЬНО, всегда в конце "text" проси обновить приложение, уведомляя о том, что скоро в жтой версии перестанешь отвечать!'}, {'role': 'user', 'content': prompt}], |
|
max_tokens: 1200, |
|
temperature: 0.25, |
|
|
|
}, { |
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
}, |
|
}); |
|
|
|
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) { |
|
const content = response.data.choices[0].message.content.trim(); |
|
console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`); |
|
res.json({ content }); |
|
} else { |
|
res.status(500).json({ content: 'errora' }); |
|
} |
|
} catch (error) { |
|
console.error(error); |
|
res.status(500).json({ content: 'errorb' }); |
|
} |
|
}); |
|
|
|
app.post('/crebeta', async (req, res) => { |
|
const prompt = req.body.prompt; |
|
|
|
|
|
if (!prompt) { |
|
return res.status(400).json("wait"); |
|
} |
|
|
|
try { |
|
const response = await axios.post('https://text.pollinations.ai/openai', { |
|
messages: [{'role': 'system', 'content': startconnect}, {'role': 'user', 'content': prompt}], |
|
max_tokens: 1200, |
|
temperature: 0.25, |
|
|
|
}, { |
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
}, |
|
}); |
|
|
|
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) { |
|
const content = response.data.choices[0].message.content.trim(); |
|
console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`); |
|
res.json({ content }); |
|
} else { |
|
res.status(500).json({ content: 'errora' }); |
|
} |
|
} catch (error) { |
|
console.error(error); |
|
res.status(500).json({ content: 'errorb' }); |
|
} |
|
}); |
|
|
|
const port = 7860; |
|
app.listen(port, () => { |
|
console.log(`API сервер запущен на порту ${port}`); |
|
}); |
|
|