gpt4all/gpt4all-chat/llm.cpp

108 lines
2.7 KiB
C++
Raw Permalink Normal View History

2023-04-08 23:28:39 -04:00
#include "llm.h"
#include "../gpt4all-backend/llmodel.h"
2023-06-26 16:34:35 -04:00
#include "../gpt4all-backend/sysinfo.h"
2023-04-08 23:28:39 -04:00
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QGlobalStatic>
#include <QNetworkInformation>
2023-04-10 23:34:34 -04: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-08 23:28:39 -04:00
class MyLLM: public LLM { };
Q_GLOBAL_STATIC(MyLLM, llmInstance)
LLM *LLM::globalInstance()
{
return llmInstance();
}
LLM::LLM()
2023-04-08 23:28:39 -04: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-10 23:34:34 -04: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 07:41:23 -04:00
2023-04-10 23:34:34 -04:00
#if defined(Q_OS_LINUX)
QString tool = u"maintenancetool"_s;
2023-04-10 23:34:34 -04:00
#elif defined(Q_OS_WINDOWS)
QString tool = u"maintenancetool.exe"_s;
2023-04-10 23:34:34 -04:00
#elif defined(Q_OS_DARWIN)
QString tool = u"../../../maintenancetool.app/Contents/MacOS/maintenancetool"_s;
2023-04-10 23:34:34 -04:00
#endif
QString fileName = QCoreApplication::applicationDirPath()
2023-04-29 21:02:54 -04:00
+ "/../" + tool;
2023-04-10 23:34:34 -04: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-10 23:34:34 -04: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 15:44:49 -04: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;
}