2023-04-09 03:28:39 +00:00
|
|
|
#include "llm.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2023-04-11 03:34:34 +00:00
|
|
|
#include <QProcess>
|
2023-04-09 03:28:39 +00:00
|
|
|
#include <QResource>
|
2023-04-10 20:33:14 +00:00
|
|
|
#include <fstream>
|
2023-04-09 03:28:39 +00:00
|
|
|
|
|
|
|
class MyLLM: public LLM { };
|
|
|
|
Q_GLOBAL_STATIC(MyLLM, llmInstance)
|
|
|
|
LLM *LLM::globalInstance()
|
|
|
|
{
|
|
|
|
return llmInstance();
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
static LLModel::PromptContext s_ctx;
|
2023-04-10 21:13:22 +00:00
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
LLMObject::LLMObject()
|
2023-04-09 03:28:39 +00:00
|
|
|
: QObject{nullptr}
|
2023-04-14 02:15:40 +00:00
|
|
|
, m_llmodel(new GPTJ)
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
moveToThread(&m_llmThread);
|
2023-04-14 02:15:40 +00:00
|
|
|
connect(&m_llmThread, &QThread::started, this, &LLMObject::loadModel);
|
2023-04-09 03:28:39 +00:00
|
|
|
m_llmThread.setObjectName("llm thread");
|
|
|
|
m_llmThread.start();
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
bool LLMObject::loadModel()
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
if (isModelLoaded())
|
|
|
|
return true;
|
|
|
|
|
2023-04-11 12:29:55 +00:00
|
|
|
QDir dir(QCoreApplication::applicationDirPath());
|
|
|
|
dir.setNameFilters(QStringList() << "ggml-*.bin");
|
|
|
|
QStringList fileNames = dir.entryList();
|
|
|
|
if (fileNames.isEmpty()) {
|
|
|
|
qDebug() << "ERROR: Could not find any applicable models in directory"
|
|
|
|
<< QCoreApplication::applicationDirPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString modelName = fileNames.first();
|
|
|
|
QString filePath = QCoreApplication::applicationDirPath() + QDir::separator() + modelName;
|
|
|
|
QFileInfo info(filePath);
|
|
|
|
if (info.exists()) {
|
2023-04-09 03:28:39 +00:00
|
|
|
|
2023-04-11 12:29:55 +00:00
|
|
|
auto fin = std::ifstream(filePath.toStdString(), std::ios::binary);
|
2023-04-14 02:15:40 +00:00
|
|
|
m_llmodel->loadModel(modelName.toStdString(), fin);
|
2023-04-09 03:28:39 +00:00
|
|
|
emit isModelLoadedChanged();
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
if (m_llmodel) {
|
2023-04-11 12:29:55 +00:00
|
|
|
m_modelName = info.baseName().remove(0, 5); // remove the ggml- prefix
|
|
|
|
emit modelNameChanged();
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
return m_llmodel;
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
bool LLMObject::isModelLoaded() const
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
return m_llmodel->isModelLoaded();
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
void LLMObject::resetResponse()
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
m_response = std::string();
|
2023-04-12 15:39:43 +00:00
|
|
|
emit responseChanged();
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
void LLMObject::resetContext()
|
2023-04-10 21:13:22 +00:00
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
s_ctx = LLModel::PromptContext();
|
2023-04-10 21:13:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
QString LLMObject::response() const
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
return QString::fromStdString(m_response);
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
QString LLMObject::modelName() const
|
2023-04-11 12:29:55 +00:00
|
|
|
{
|
|
|
|
return m_modelName;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
bool LLMObject::handleResponse(const std::string &response)
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
printf("%s", response.c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2023-04-12 15:39:43 +00:00
|
|
|
if (!response.empty()) {
|
|
|
|
m_response.append(response);
|
|
|
|
emit responseChanged();
|
|
|
|
}
|
2023-04-09 03:28:39 +00:00
|
|
|
return !m_stopGenerating;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
bool LLMObject::prompt(const QString &prompt)
|
2023-04-09 03:28:39 +00:00
|
|
|
{
|
|
|
|
if (!isModelLoaded())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_stopGenerating = false;
|
2023-04-14 02:15:40 +00:00
|
|
|
auto func = std::bind(&LLMObject::handleResponse, this, std::placeholders::_1);
|
2023-04-09 03:28:39 +00:00
|
|
|
emit responseStarted();
|
2023-04-14 02:15:40 +00:00
|
|
|
m_llmodel->prompt(prompt.toStdString(), func, s_ctx, 4096 /*number of chars to predict*/);
|
2023-04-09 03:28:39 +00:00
|
|
|
emit responseStopped();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLM::LLM()
|
|
|
|
: QObject{nullptr}
|
2023-04-14 02:15:40 +00:00
|
|
|
, m_llmodel(new LLMObject)
|
2023-04-09 03:28:39 +00:00
|
|
|
, m_responseInProgress(false)
|
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
connect(m_llmodel, &LLMObject::isModelLoadedChanged, this, &LLM::isModelLoadedChanged, Qt::QueuedConnection);
|
|
|
|
connect(m_llmodel, &LLMObject::responseChanged, this, &LLM::responseChanged, Qt::QueuedConnection);
|
|
|
|
connect(m_llmodel, &LLMObject::responseStarted, this, &LLM::responseStarted, Qt::QueuedConnection);
|
|
|
|
connect(m_llmodel, &LLMObject::responseStopped, this, &LLM::responseStopped, Qt::QueuedConnection);
|
|
|
|
connect(m_llmodel, &LLMObject::modelNameChanged, this, &LLM::modelNameChanged, Qt::QueuedConnection);
|
2023-04-09 03:28:39 +00:00
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
connect(this, &LLM::promptRequested, m_llmodel, &LLMObject::prompt, Qt::QueuedConnection);
|
|
|
|
connect(this, &LLM::resetResponseRequested, m_llmodel, &LLMObject::resetResponse, Qt::BlockingQueuedConnection);
|
|
|
|
connect(this, &LLM::resetContextRequested, m_llmodel, &LLMObject::resetContext, Qt::BlockingQueuedConnection);
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LLM::isModelLoaded() const
|
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
return m_llmodel->isModelLoaded();
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLM::prompt(const QString &prompt)
|
|
|
|
{
|
|
|
|
emit promptRequested(prompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLM::resetResponse()
|
|
|
|
{
|
|
|
|
emit resetResponseRequested(); // blocking queued connection
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:13:22 +00:00
|
|
|
void LLM::resetContext()
|
|
|
|
{
|
|
|
|
emit resetContextRequested(); // blocking queued connection
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:28:39 +00:00
|
|
|
void LLM::stopGenerating()
|
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
m_llmodel->stopGenerating();
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LLM::response() const
|
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
return m_llmodel->response();
|
2023-04-09 03:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLM::responseStarted()
|
|
|
|
{
|
|
|
|
m_responseInProgress = true;
|
|
|
|
emit responseInProgressChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLM::responseStopped()
|
|
|
|
{
|
|
|
|
m_responseInProgress = false;
|
|
|
|
emit responseInProgressChanged();
|
|
|
|
}
|
2023-04-11 03:34:34 +00:00
|
|
|
|
2023-04-11 12:29:55 +00:00
|
|
|
QString LLM::modelName() const
|
|
|
|
{
|
2023-04-14 02:15:40 +00:00
|
|
|
return m_llmodel->modelName();
|
2023-04-11 12:29:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 03:34:34 +00:00
|
|
|
bool LLM::checkForUpdates() const
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_LINUX)
|
2023-04-11 16:16:04 +00:00
|
|
|
QString tool("maintenancetool");
|
2023-04-11 03:34:34 +00:00
|
|
|
#elif defined(Q_OS_WINDOWS)
|
2023-04-11 16:16:04 +00:00
|
|
|
QString tool("maintenancetool.exe");
|
2023-04-11 03:34:34 +00:00
|
|
|
#elif defined(Q_OS_DARWIN)
|
2023-04-12 21:57:02 +00:00
|
|
|
QString tool("../../../maintenancetool.app/Contents/MacOS/maintenancetool");
|
2023-04-11 03:34:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
QString fileName = QCoreApplication::applicationDirPath()
|
|
|
|
+ QDir::separator() + ".." + QDir::separator() + tool;
|
|
|
|
if (!QFileInfo::exists(fileName)) {
|
|
|
|
qDebug() << "Couldn't find tool at" << fileName << "so cannot check for updates!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QProcess::startDetached(fileName);
|
|
|
|
}
|
|
|
|
|