Add AI text correction button to journal edit modal

This commit is contained in:
dev
2026-09-10 00:06:38 +03:00
parent 5ae476551d
commit 89152295ea
4 changed files with 165 additions and 36 deletions
+37
View File
@@ -621,6 +621,32 @@ const gunzipAsync = require('util').promisify(require('zlib').gunzip);
const zlib = require('zlib');
const tar = require('tar');
const os = require('os');
const AI_URL = process.env.AI_URL || 'http://text-corrector:8080';
async function aiCorrectText(text) {
try {
const res = await fetch(`${AI_URL}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'qwen2.5-1.5b-instruct',
messages: [
{ role: 'system', content: 'Ты — редактор текстов для педагогического журнала. Исправь грамматические, орфографические и пунктуационные ошибки. Сохрани смысл и стиль. Верни ТОЛЬКО исправленный текст, без пояснений.' },
{ role: 'user', content: text }
],
temperature: 0.1,
max_tokens: 512
})
});
if (!res.ok) throw new Error(`AI service error: ${res.status}`);
const data = await res.json();
return data.choices?.[0]?.message?.content?.trim() || text;
} catch (e) {
console.error('AI correct error:', e);
throw e;
}
}
const uploadBackup = multer({
storage: multer.diskStorage({
destination: (_, __, cb) => {
@@ -2248,6 +2274,17 @@ app.delete('/api/entries/:id/permanent', requireAuth, async (req, res) => {
res.json({ ok: true });
});
app.post('/api/ai/correct', requireAuth, async (req, res) => {
const text = reqStr(req.body?.text, 5000);
if (!text) return res.status(400).json({ error: 'Текст не указан' });
try {
const corrected = await aiCorrectText(text);
res.json({ suggestion: corrected });
} catch (e) {
res.status(502).json({ error: 'Сервис ИИ недоступен: ' + e.message });
}
});
app.get('/api/trash', requireAuth, async (req, res) => {
const { limit, offset } = req.query;
const bw = branchScope(req.user);