gpt4all/gpt4all-chat/llm.cpp

108 lines
2.7 KiB
C++
Raw Normal View History

2023-04-09 03:28:39 +00:00
#include "llm.h"
#include "../gpt4all-backend/llmodel.h"
2023-06-26 20:34:35 +00:00
#include "../gpt4all-backend/sysinfo.h"
2023-04-09 03:28:39 +00:00
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QGlobalStatic>
#include <QNetworkInformation>
2023-04-11 03:34:34 +00:00
#include <QProcess>
#include <QSettings>
#include <QUrl>
#include <QtLogging>
#ifdef GPT4ALL_OFFLINE_INSTALLER
# include <QDesktopServices>
#else
# include "network.h"
#endif
using namespace Qt::Literals::StringLiterals;
2023-04-09 03:28:39 +00:00
class MyLLM: public LLM { };
Q_GLOBAL_STATIC(MyLLM, llmInstance)
LLM *LLM::globalInstance()
{
return llmInstance();
}
LLM::LLM()
2023-04-09 03:28:39 +00:00
: QObject{nullptr}
, m_compatHardware(LLModel::Implementation::hasSupportedCPU())
{
QNetworkInformation::loadDefaultBackend();
auto * netinfo = QNetworkInformation::instance();
if (netinfo) {
connect(netinfo, &QNetworkInformation::reachabilityChanged,
this, &LLM::isNetworkOnlineChanged);
}
}
bool LLM::hasSettingsAccess() const
{
QSettings settings;
settings.sync();
return settings.status() == QSettings::NoError;
}
2023-04-11 03:34:34 +00:00
bool LLM::checkForUpdates() const
{
#ifdef GPT4ALL_OFFLINE_INSTALLER
# pragma message(__FILE__ ": WARNING: offline installer build will not check for updates!")
return QDesktopServices::openUrl(QUrl("https://gpt4all.io/"));
#else
Network::globalInstance()->trackEvent("check_for_updates");
2023-04-27 11:41:23 +00:00
2023-04-11 03:34:34 +00:00
#if defined(Q_OS_LINUX)
QString tool = u"maintenancetool"_s;
2023-04-11 03:34:34 +00:00
#elif defined(Q_OS_WINDOWS)
QString tool = u"maintenancetool.exe"_s;
2023-04-11 03:34:34 +00:00
#elif defined(Q_OS_DARWIN)
QString tool = u"../../../maintenancetool.app/Contents/MacOS/maintenancetool"_s;
2023-04-11 03:34:34 +00:00
#endif
QString fileName = QCoreApplication::applicationDirPath()
2023-04-30 01:02:54 +00:00
+ "/../" + tool;
2023-04-11 03:34:34 +00:00
if (!QFileInfo::exists(fileName)) {
qDebug() << "Couldn't find tool at" << fileName << "so cannot check for updates!";
return false;
}
return QProcess::startDetached(fileName);
#endif
2023-04-11 03:34:34 +00:00
}
bool LLM::directoryExists(const QString &path)
{
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 QUrl url(path);
const QString localFilePath = url.isLocalFile() ? url.toLocalFile() : path;
const QFileInfo info(localFilePath);
return info.exists() && info.isFile();
}
2023-06-22 19:44:49 +00:00
qint64 LLM::systemTotalRAMInGB() const
{
return getSystemTotalRAMInGB();
}
QString LLM::systemTotalRAMInGBString() const
{
return QString::fromStdString(getSystemTotalRAMInGBString());
}
bool LLM::isNetworkOnline() const
{
auto * netinfo = QNetworkInformation::instance();
return !netinfo || netinfo->reachability() == QNetworkInformation::Reachability::Online;
}