починил нейроконструктор, исправил распознавание сообщений из чата
This commit is contained in:
+134
-3
@@ -1,4 +1,5 @@
|
||||
#include "commandprocessor.h"
|
||||
#include "neuraltemplatemanager.h"
|
||||
#include <QRegularExpression>
|
||||
#include <QRandomGenerator>
|
||||
#include <QFile>
|
||||
@@ -6,6 +7,12 @@
|
||||
#include <QEventLoop>
|
||||
#include <QTimer>
|
||||
|
||||
struct Replacement {
|
||||
int start;
|
||||
int length;
|
||||
QString text;
|
||||
};
|
||||
|
||||
CommandProcessor::CommandProcessor(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
@@ -48,14 +55,13 @@ QString CommandProcessor::generateResponse(QString userIndex, const QString &com
|
||||
Command cmd = findCommand(command);
|
||||
if (cmd.command.isEmpty()) {
|
||||
qDebug() << "Команда не найдена:" << command;
|
||||
return QString("Команда '%1' не найдена").arg(command);
|
||||
return "";
|
||||
}
|
||||
|
||||
qDebug() << "Найдена команда:" << cmd.command << "ответ:" << cmd.response;
|
||||
|
||||
QString fullCommand = command + (message.isEmpty() ? "" : " " + message);
|
||||
QString result = processCommand(username, fullCommand, cmd.response);
|
||||
|
||||
qDebug() << "Итоговый результат:" << result;
|
||||
return result;
|
||||
}
|
||||
@@ -111,6 +117,9 @@ QString CommandProcessor::processCommand(const QString &sender, const QString &f
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
response = parseNeuralTemplates(response, sender, parameters);
|
||||
|
||||
response = parseRandomNumbers(response); // Затем обрабатываем случайные числа ВНУТРИ них
|
||||
response = parseSounds(response);
|
||||
response = parseTextFiles(response);
|
||||
@@ -372,7 +381,7 @@ QString CommandProcessor::parseAI(const QString &response, const QString &questi
|
||||
eventLoop.quit();
|
||||
});
|
||||
|
||||
m_context.neuralManager->sendMessage(question, NeuralNetworkManager::DeepSeek);
|
||||
m_context.neuralManager->sendMessage(question);
|
||||
|
||||
QTimer::singleShot(60000, &eventLoop, &QEventLoop::quit);
|
||||
eventLoop.exec();
|
||||
@@ -474,3 +483,125 @@ QString CommandProcessor::getPeriodEnding(int n, int r)
|
||||
return endings[r][2];
|
||||
}
|
||||
}
|
||||
|
||||
QString CommandProcessor::parseNeuralTemplates(const QString &response, const QString &sender, const QString ¶meters)
|
||||
{
|
||||
QString result = response;
|
||||
qDebug() << "parseNeuralTemplates: входная строка:" << response;
|
||||
|
||||
// Исправленное регулярное выражение
|
||||
QRegularExpression regex("<\\|([^<]+)<\\|");
|
||||
QRegularExpressionMatchIterator matches = regex.globalMatch(response);
|
||||
|
||||
QList<Replacement> replacements;
|
||||
int matchCount = 0;
|
||||
|
||||
while (matches.hasNext()) {
|
||||
matchCount++;
|
||||
QRegularExpressionMatch match = matches.next();
|
||||
QString templateName = match.captured(1).trimmed();
|
||||
qDebug() << "Найден шаблон:" << templateName << "на позиции" << match.capturedStart();
|
||||
|
||||
if (m_context.neuralTemplateManager) {
|
||||
QString templateText = m_context.neuralTemplateManager->getTemplateText(templateName);
|
||||
qDebug() << "Текст шаблона:" << templateText;
|
||||
|
||||
if (!templateText.isEmpty()) {
|
||||
// Заменяем плейсхолдеры в шаблоне
|
||||
QString processedTemplate = templateText;
|
||||
processedTemplate.replace("[TO]", parameters);
|
||||
processedTemplate.replace("[USERNAME]", sender);
|
||||
|
||||
qDebug() << "Шаблон после замены плейсхолдеров:" << processedTemplate;
|
||||
|
||||
// Получаем ответ от нейросети
|
||||
QString aiResponse;
|
||||
if (m_context.neuralManager) {
|
||||
// Создаем event loop для асинхронного ожидания
|
||||
QEventLoop eventLoop;
|
||||
bool responseReceived = false;
|
||||
bool errorOccurred = false;
|
||||
QString errorMessage;
|
||||
|
||||
// Подключаем сигналы от нейросети
|
||||
auto conn1 = connect(m_context.neuralManager, &NeuralNetworkManager::responseReceived,
|
||||
[&](const QString &responseText) {
|
||||
aiResponse = responseText;
|
||||
responseReceived = true;
|
||||
eventLoop.quit();
|
||||
});
|
||||
|
||||
auto conn2 = connect(m_context.neuralManager, &NeuralNetworkManager::errorOccurred,
|
||||
[&](const QString &error) {
|
||||
errorMessage = error;
|
||||
errorOccurred = true;
|
||||
eventLoop.quit();
|
||||
});
|
||||
|
||||
// Отправляем запрос к нейросети
|
||||
qDebug() << "Отправляем запрос к нейросети:" << processedTemplate;
|
||||
m_context.neuralManager->sendMessage(processedTemplate);
|
||||
|
||||
// Таймаут 30 секунд
|
||||
QTimer::singleShot(30000, &eventLoop, &QEventLoop::quit);
|
||||
eventLoop.exec();
|
||||
|
||||
disconnect(conn1);
|
||||
disconnect(conn2);
|
||||
|
||||
if (errorOccurred) {
|
||||
aiResponse = QString("Ошибка нейросети: %1").arg(errorMessage);
|
||||
qDebug() << "Ошибка нейросети:" << errorMessage;
|
||||
} else if (!responseReceived) {
|
||||
aiResponse = "Таймаут при ожидании ответа от нейросети";
|
||||
qDebug() << "Таймаут нейросети";
|
||||
} else {
|
||||
qDebug() << "Получен ответ от нейросети:" << aiResponse;
|
||||
}
|
||||
} else {
|
||||
aiResponse = "Нейросеть недоступна";
|
||||
qDebug() << "NeuralManager не доступен";
|
||||
}
|
||||
|
||||
// Заменяем шаблон на ответ от нейросети
|
||||
Replacement repl;
|
||||
repl.start = match.capturedStart();
|
||||
repl.length = match.capturedLength();
|
||||
repl.text = aiResponse;
|
||||
replacements.append(repl);
|
||||
} else {
|
||||
qDebug() << "Шаблон не найден или пустой:" << templateName;
|
||||
// Если шаблон не найден, оставляем как есть или заменяем на заглушку
|
||||
Replacement repl;
|
||||
repl.start = match.capturedStart();
|
||||
repl.length = match.capturedLength();
|
||||
repl.text = "[Шаблон не найден]";
|
||||
replacements.append(repl);
|
||||
}
|
||||
} else {
|
||||
qDebug() << "NeuralTemplateManager не доступен";
|
||||
// Если менеджер не доступен, заменяем на заглушку
|
||||
Replacement repl;
|
||||
repl.start = match.capturedStart();
|
||||
repl.length = match.capturedLength();
|
||||
repl.text = "[Нейросеть недоступна]";
|
||||
replacements.append(repl);
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "Всего найдено шаблонов:" << matchCount;
|
||||
|
||||
// Выполняем замены с конца к началу
|
||||
std::sort(replacements.begin(), replacements.end(),
|
||||
[](const Replacement &a, const Replacement &b) {
|
||||
return a.start > b.start;
|
||||
});
|
||||
|
||||
for (const auto &replacement : replacements) {
|
||||
result.replace(replacement.start, replacement.length, replacement.text);
|
||||
}
|
||||
|
||||
qDebug() << "parseNeuralTemplates: результат:" << result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user