|
require('dotenv').config(); |
|
const express = require('express'); |
|
const rateLimit = require('express-rate-limit'); |
|
const { HfInference } = require('@huggingface/inference'); |
|
|
|
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: 60 * 1000, |
|
max: 15, |
|
handler: function (req, res) { |
|
return res.status(429).json({ content: "wait" }); |
|
}, |
|
}); |
|
|
|
|
|
app.use('/pl', limiter); |
|
app.use('/plbeta', limiter); |
|
|
|
const start = `${process.env.start}`; |
|
const startconnect = `${process.env.startconnect}`; |
|
|
|
app.post('/update', async (req, res) => { |
|
res.json({ content: `{"error":"", "title":"Требуется обновление", "text":"Текущая версия приложения устарела. Установите новую из нашего телеграм канала: @yufi_ru", "okb":"Обновить", "oklink":"https://t.me/yufi_ru", "cancelable":"false"}` }); |
|
}); |
|
|
|
async function sendRequest(prompt, prs) { |
|
const hf_api_key = getRandomApiKey(); |
|
const client = new HfInference(hf_api_key); |
|
|
|
try { |
|
const chatCompletion = await client.chatCompletion({ |
|
model: "Qwen/Qwen2.5-Coder-32B-Instruct", |
|
messages: [ |
|
{ |
|
role: "system", |
|
content: prs |
|
}, |
|
{ |
|
role: "user", |
|
content: prompt |
|
} |
|
], |
|
max_tokens: 1200, |
|
}); |
|
|
|
if (chatCompletion && chatCompletion.choices && chatCompletion.choices.length > 0 && chatCompletion.choices[0].message) { |
|
return chatCompletion.choices[0].message.content.trim(); |
|
} else { |
|
throw new Error("Ошибка прочтения ответа"); |
|
} |
|
} catch (error) { |
|
console.error("Ошибка при обращении к Hugging Face:", error); |
|
throw new Error("Ошибка при генерации"); |
|
} |
|
} |
|
|
|
app.post('/pl', async (req, res) => { |
|
const prompt = req.body.prompt; |
|
let prs; |
|
|
|
if (req.body.mode === "1") { |
|
prs = start; |
|
} else { |
|
prs = startconnect; |
|
} |
|
|
|
if (!prompt) { |
|
return res.status(400).json({ content: "wait" }); |
|
} |
|
|
|
try { |
|
const content = await sendRequest(prompt, prs); |
|
res.json({ content }); |
|
} catch (error) { |
|
res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (${error.message})", "okb":"Ок", "oklink":"", "cancelable":"true"}` }); |
|
} |
|
}); |
|
|
|
app.post('/plbeta', async (req, res) => { |
|
const prompt = req.body.prompt; |
|
let prs; |
|
|
|
if (req.body.mode === "1") { |
|
prs = start; |
|
} else { |
|
prs = startconnect; |
|
} |
|
|
|
if (!prompt) { |
|
return res.status(400).json({ content: "wait" }); |
|
} |
|
|
|
try { |
|
const content = await sendRequest(prompt, prs); |
|
console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`); |
|
res.json({ content }); |
|
} catch (error) { |
|
res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (${error.message})", "okb":"Ок", "oklink":"", "cancelable":"true"}` }); |
|
} |
|
}); |
|
|
|
const port = 7860; |
|
app.listen(port, () => { |
|
console.log(`API сервер запущен на порту ${port}`); |
|
}); |
|
|