2023-04-09 06:28:39 +03:00
|
|
|
#include "llm.h"
|
2023-05-08 15:23:00 +03:00
|
|
|
#include "config.h"
|
2023-04-19 04:10:06 +03:00
|
|
|
#include "download.h"
|
2023-04-27 05:05:56 +03:00
|
|
|
#include "network.h"
|
2023-04-09 06:28:39 +03:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2023-04-11 06:34:34 +03:00
|
|
|
#include <QProcess>
|
2023-04-09 06:28:39 +03:00
|
|
|
#include <QResource>
|
2023-04-27 20:52:24 +03:00
|
|
|
#include <QSettings>
|
2023-04-10 23:33:14 +03:00
|
|
|
#include <fstream>
|
2023-04-09 06:28:39 +03:00
|
|
|
|
|
|
|
class MyLLM: public LLM { };
|
|
|
|
Q_GLOBAL_STATIC(MyLLM, llmInstance)
|
|
|
|
LLM *LLM::globalInstance()
|
|
|
|
{
|
|
|
|
return llmInstance();
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:10:05 +03:00
|
|
|
LLM::LLM()
|
2023-04-09 06:28:39 +03:00
|
|
|
: QObject{nullptr}
|
2023-05-02 00:13:20 +03:00
|
|
|
, m_chatListModel(new ChatListModel(this))
|
2023-05-04 22:31:41 +03:00
|
|
|
, m_threadCount(std::min(4, (int32_t) std::thread::hardware_concurrency()))
|
2023-05-11 23:46:25 +03:00
|
|
|
, m_serverEnabled(false)
|
2023-05-10 06:17:21 +03:00
|
|
|
, m_compatHardware(true)
|
2023-04-18 18:42:16 +03:00
|
|
|
{
|
2023-06-05 16:23:17 +03:00
|
|
|
QString llmodelSearchPaths = QCoreApplication::applicationDirPath();
|
|
|
|
#if defined(Q_OS_MAC)
|
2023-06-05 16:30:50 +03:00
|
|
|
const QString binDir = QCoreApplication::applicationDirPath() + "/../../../";
|
2023-06-05 16:31:57 +03:00
|
|
|
if (directoryExists(binDir))
|
2023-06-05 16:30:50 +03:00
|
|
|
llmodelSearchPaths += ";" + binDir;
|
|
|
|
const QString frameworksDir = QCoreApplication::applicationDirPath() + "/../Frameworks/";
|
2023-06-05 16:31:57 +03:00
|
|
|
if (directoryExists(frameworksDir))
|
2023-06-05 16:30:50 +03:00
|
|
|
llmodelSearchPaths += ";" + frameworksDir;
|
2023-06-05 16:23:17 +03:00
|
|
|
#endif
|
|
|
|
LLModel::setImplementationsSearchPath(llmodelSearchPaths.toStdString());
|
|
|
|
|
2023-05-04 22:31:41 +03:00
|
|
|
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
|
|
|
this, &LLM::aboutToQuit);
|
2023-05-11 23:46:25 +03:00
|
|
|
connect(this, &LLM::serverEnabledChanged,
|
|
|
|
m_chatListModel, &ChatListModel::handleServerEnabledChanged);
|
2023-05-08 15:23:00 +03:00
|
|
|
|
2023-06-04 22:28:58 +03:00
|
|
|
#if defined(__x86_64__)
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
const bool minimal(__builtin_cpu_supports("avx"));
|
|
|
|
#else
|
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuid(cpuInfo, 1);
|
|
|
|
const bool minimal(cpuInfo[2] & (1 << 28));
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
const bool minimal = true; // Don't know how to handle non-x86_64
|
2023-05-08 15:23:00 +03:00
|
|
|
#endif
|
2023-06-04 22:28:58 +03:00
|
|
|
|
|
|
|
m_compatHardware = minimal;
|
|
|
|
emit compatHardwareChanged();
|
2023-04-18 18:42:16 +03:00
|
|
|
}
|
|
|
|
|
2023-04-11 06:34:34 +03:00
|
|
|
bool LLM::checkForUpdates() const
|
|
|
|
{
|
2023-04-27 14:41:23 +03:00
|
|
|
Network::globalInstance()->sendCheckForUpdates();
|
|
|
|
|
2023-04-11 06:34:34 +03:00
|
|
|
#if defined(Q_OS_LINUX)
|
2023-04-11 19:16:04 +03:00
|
|
|
QString tool("maintenancetool");
|
2023-04-11 06:34:34 +03:00
|
|
|
#elif defined(Q_OS_WINDOWS)
|
2023-04-11 19:16:04 +03:00
|
|
|
QString tool("maintenancetool.exe");
|
2023-04-11 06:34:34 +03:00
|
|
|
#elif defined(Q_OS_DARWIN)
|
2023-04-13 00:57:02 +03:00
|
|
|
QString tool("../../../maintenancetool.app/Contents/MacOS/maintenancetool");
|
2023-04-11 06:34:34 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
QString fileName = QCoreApplication::applicationDirPath()
|
2023-04-30 04:02:54 +03:00
|
|
|
+ "/../" + tool;
|
2023-04-11 06:34:34 +03:00
|
|
|
if (!QFileInfo::exists(fileName)) {
|
|
|
|
qDebug() << "Couldn't find tool at" << fileName << "so cannot check for updates!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QProcess::startDetached(fileName);
|
|
|
|
}
|
2023-05-01 19:41:03 +03:00
|
|
|
|
2023-06-03 05:52:55 +03:00
|
|
|
bool LLM::directoryExists(const QString &path) const
|
|
|
|
{
|
|
|
|
const QUrl url(path);
|
|
|
|
const QString localFilePath = url.isLocalFile() ? url.toLocalFile() : path;
|
|
|
|
const QFileInfo info(localFilePath);
|
|
|
|
return info.exists() && info.isDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LLM::fileExists(const QString &path) const
|
|
|
|
{
|
|
|
|
const QUrl url(path);
|
|
|
|
const QString localFilePath = url.isLocalFile() ? url.toLocalFile() : path;
|
|
|
|
const QFileInfo info(localFilePath);
|
|
|
|
return info.exists() && info.isFile();
|
|
|
|
}
|
|
|
|
|
2023-05-04 22:31:41 +03:00
|
|
|
int32_t LLM::threadCount() const
|
2023-05-01 19:41:03 +03:00
|
|
|
{
|
2023-05-04 22:31:41 +03:00
|
|
|
return m_threadCount;
|
2023-05-01 21:24:16 +03:00
|
|
|
}
|
|
|
|
|
2023-05-04 22:31:41 +03:00
|
|
|
void LLM::setThreadCount(int32_t n_threads)
|
2023-05-01 21:24:16 +03:00
|
|
|
{
|
2023-05-04 22:31:41 +03:00
|
|
|
if (n_threads <= 0)
|
|
|
|
n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
|
|
|
m_threadCount = n_threads;
|
|
|
|
emit threadCountChanged();
|
2023-05-01 19:41:03 +03:00
|
|
|
}
|
|
|
|
|
2023-05-11 23:46:25 +03:00
|
|
|
bool LLM::serverEnabled() const
|
|
|
|
{
|
|
|
|
return m_serverEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLM::setServerEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_serverEnabled == enabled)
|
|
|
|
return;
|
|
|
|
m_serverEnabled = enabled;
|
|
|
|
emit serverEnabledChanged();
|
|
|
|
}
|
|
|
|
|
2023-05-04 22:31:41 +03:00
|
|
|
void LLM::aboutToQuit()
|
2023-05-01 21:24:16 +03:00
|
|
|
{
|
2023-05-04 22:31:41 +03:00
|
|
|
m_chatListModel->saveChats();
|
2023-05-01 21:24:16 +03:00
|
|
|
}
|