2023-06-22 19:44:49 +00:00
|
|
|
#ifndef MODELLIST_H
|
|
|
|
#define MODELLIST_H
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QtQml>
|
|
|
|
|
|
|
|
struct ModelInfo {
|
|
|
|
Q_GADGET
|
2023-07-01 15:34:21 +00:00
|
|
|
Q_PROPERTY(QString id READ id WRITE setId)
|
|
|
|
Q_PROPERTY(QString name READ name WRITE setName)
|
|
|
|
Q_PROPERTY(QString filename READ filename WRITE setFilename)
|
2023-06-22 19:44:49 +00:00
|
|
|
Q_PROPERTY(QString dirpath MEMBER dirpath)
|
|
|
|
Q_PROPERTY(QString filesize MEMBER filesize)
|
|
|
|
Q_PROPERTY(QByteArray md5sum MEMBER md5sum)
|
|
|
|
Q_PROPERTY(bool calcHash MEMBER calcHash)
|
|
|
|
Q_PROPERTY(bool installed MEMBER installed)
|
|
|
|
Q_PROPERTY(bool isDefault MEMBER isDefault)
|
|
|
|
Q_PROPERTY(bool disableGUI MEMBER disableGUI)
|
|
|
|
Q_PROPERTY(bool isChatGPT MEMBER isChatGPT)
|
|
|
|
Q_PROPERTY(QString description MEMBER description)
|
|
|
|
Q_PROPERTY(QString requiresVersion MEMBER requiresVersion)
|
|
|
|
Q_PROPERTY(QString deprecatedVersion MEMBER deprecatedVersion)
|
|
|
|
Q_PROPERTY(QString url MEMBER url)
|
|
|
|
Q_PROPERTY(qint64 bytesReceived MEMBER bytesReceived)
|
|
|
|
Q_PROPERTY(qint64 bytesTotal MEMBER bytesTotal)
|
|
|
|
Q_PROPERTY(qint64 timestamp MEMBER timestamp)
|
|
|
|
Q_PROPERTY(QString speed MEMBER speed)
|
|
|
|
Q_PROPERTY(bool isDownloading MEMBER isDownloading)
|
|
|
|
Q_PROPERTY(bool isIncomplete MEMBER isIncomplete)
|
|
|
|
Q_PROPERTY(QString downloadError MEMBER downloadError)
|
|
|
|
Q_PROPERTY(QString order MEMBER order)
|
|
|
|
Q_PROPERTY(int ramrequired MEMBER ramrequired)
|
|
|
|
Q_PROPERTY(QString parameters MEMBER parameters)
|
|
|
|
Q_PROPERTY(QString quant MEMBER quant)
|
|
|
|
Q_PROPERTY(QString type MEMBER type)
|
2023-07-01 15:34:21 +00:00
|
|
|
Q_PROPERTY(bool isClone MEMBER isClone)
|
|
|
|
Q_PROPERTY(double temperature READ temperature WRITE setTemperature)
|
|
|
|
Q_PROPERTY(double topP READ topP WRITE setTopP)
|
|
|
|
Q_PROPERTY(int topK READ topK WRITE setTopK)
|
|
|
|
Q_PROPERTY(int maxLength READ maxLength WRITE setMaxLength)
|
|
|
|
Q_PROPERTY(int promptBatchSize READ promptBatchSize WRITE setPromptBatchSize)
|
|
|
|
Q_PROPERTY(double repeatPenalty READ repeatPenalty WRITE setRepeatPenalty)
|
|
|
|
Q_PROPERTY(int repeatPenaltyTokens READ repeatPenaltyTokens WRITE setRepeatPenaltyTokens)
|
|
|
|
Q_PROPERTY(QString promptTemplate READ promptTemplate WRITE setPromptTemplate)
|
|
|
|
Q_PROPERTY(QString systemPrompt READ systemPrompt WRITE setSystemPrompt)
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
public:
|
2023-07-01 15:34:21 +00:00
|
|
|
QString id() const;
|
|
|
|
void setId(const QString &id);
|
|
|
|
|
|
|
|
QString name() const;
|
|
|
|
void setName(const QString &name);
|
|
|
|
|
|
|
|
QString filename() const;
|
|
|
|
void setFilename(const QString &name);
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
QString dirpath;
|
|
|
|
QString filesize;
|
|
|
|
QByteArray md5sum;
|
|
|
|
bool calcHash = false;
|
|
|
|
bool installed = false;
|
|
|
|
bool isDefault = false;
|
|
|
|
bool isChatGPT = false;
|
|
|
|
bool disableGUI = false;
|
|
|
|
QString description;
|
|
|
|
QString requiresVersion;
|
|
|
|
QString deprecatedVersion;
|
|
|
|
QString url;
|
|
|
|
qint64 bytesReceived = 0;
|
|
|
|
qint64 bytesTotal = 0;
|
|
|
|
qint64 timestamp = 0;
|
|
|
|
QString speed;
|
|
|
|
bool isDownloading = false;
|
|
|
|
bool isIncomplete = false;
|
|
|
|
QString downloadError;
|
|
|
|
QString order;
|
|
|
|
int ramrequired = 0;
|
|
|
|
QString parameters;
|
|
|
|
QString quant;
|
|
|
|
QString type;
|
2023-07-01 15:34:21 +00:00
|
|
|
bool isClone = false;
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
bool operator==(const ModelInfo &other) const {
|
2023-07-01 15:34:21 +00:00
|
|
|
return m_id == other.m_id;
|
2023-06-22 19:44:49 +00:00
|
|
|
}
|
2023-07-01 15:34:21 +00:00
|
|
|
|
|
|
|
double temperature() const;
|
|
|
|
void setTemperature(double t);
|
|
|
|
double topP() const;
|
|
|
|
void setTopP(double p);
|
|
|
|
int topK() const;
|
|
|
|
void setTopK(int k);
|
|
|
|
int maxLength() const;
|
|
|
|
void setMaxLength(int l);
|
|
|
|
int promptBatchSize() const;
|
|
|
|
void setPromptBatchSize(int s);
|
|
|
|
double repeatPenalty() const;
|
|
|
|
void setRepeatPenalty(double p);
|
|
|
|
int repeatPenaltyTokens() const;
|
|
|
|
void setRepeatPenaltyTokens(int t);
|
|
|
|
QString promptTemplate() const;
|
|
|
|
void setPromptTemplate(const QString &t);
|
|
|
|
QString systemPrompt() const;
|
|
|
|
void setSystemPrompt(const QString &p);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString m_id;
|
|
|
|
QString m_name;
|
|
|
|
QString m_filename;
|
|
|
|
double m_temperature = 0.7;
|
|
|
|
double m_topP = 0.1;
|
|
|
|
int m_topK = 40;
|
|
|
|
int m_maxLength = 4096;
|
|
|
|
int m_promptBatchSize = 128;
|
|
|
|
double m_repeatPenalty = 1.18;
|
|
|
|
int m_repeatPenaltyTokens = 64;
|
|
|
|
QString m_promptTemplate = "### Human:\n%1\n### Assistant:\n";
|
|
|
|
QString m_systemPrompt = "### System:\nYou are an AI assistant who gives quality response to whatever humans ask of you.\n";
|
|
|
|
friend class MySettings;
|
2023-06-22 19:44:49 +00:00
|
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(ModelInfo)
|
|
|
|
|
|
|
|
class InstalledModels : public QSortFilterProxyModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2023-06-26 13:35:29 +00:00
|
|
|
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
2023-06-22 19:44:49 +00:00
|
|
|
public:
|
2023-06-26 13:35:29 +00:00
|
|
|
explicit InstalledModels(QObject *parent);
|
|
|
|
int count() const;
|
2023-07-01 15:34:21 +00:00
|
|
|
QString firstId() const;
|
2023-06-26 13:35:29 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void countChanged();
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DownloadableModels : public QSortFilterProxyModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
|
|
|
Q_PROPERTY(bool expanded READ isExpanded WRITE setExpanded NOTIFY expandedChanged)
|
|
|
|
public:
|
|
|
|
explicit DownloadableModels(QObject *parent);
|
|
|
|
int count() const;
|
|
|
|
|
|
|
|
bool isExpanded() const;
|
|
|
|
void setExpanded(bool expanded);
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void countChanged();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void expandedChanged(bool expanded);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_expanded;
|
|
|
|
int m_limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModelList : public QAbstractListModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
|
|
|
Q_PROPERTY(InstalledModels* installedModels READ installedModels NOTIFY installedModelsChanged)
|
|
|
|
Q_PROPERTY(DownloadableModels* downloadableModels READ downloadableModels NOTIFY downloadableModelsChanged)
|
|
|
|
Q_PROPERTY(QList<QString> userDefaultModelList READ userDefaultModelList NOTIFY userDefaultModelListChanged)
|
|
|
|
|
|
|
|
public:
|
|
|
|
static ModelList *globalInstance();
|
|
|
|
|
|
|
|
enum Roles {
|
2023-07-01 15:34:21 +00:00
|
|
|
IdRole = Qt::UserRole + 1,
|
|
|
|
NameRole,
|
2023-06-22 19:44:49 +00:00
|
|
|
FilenameRole,
|
|
|
|
DirpathRole,
|
|
|
|
FilesizeRole,
|
|
|
|
Md5sumRole,
|
|
|
|
CalcHashRole,
|
|
|
|
InstalledRole,
|
|
|
|
DefaultRole,
|
|
|
|
ChatGPTRole,
|
|
|
|
DisableGUIRole,
|
|
|
|
DescriptionRole,
|
|
|
|
RequiresVersionRole,
|
|
|
|
DeprecatedVersionRole,
|
|
|
|
UrlRole,
|
|
|
|
BytesReceivedRole,
|
|
|
|
BytesTotalRole,
|
|
|
|
TimestampRole,
|
|
|
|
SpeedRole,
|
|
|
|
DownloadingRole,
|
|
|
|
IncompleteRole,
|
|
|
|
DownloadErrorRole,
|
|
|
|
OrderRole,
|
|
|
|
RamrequiredRole,
|
|
|
|
ParametersRole,
|
|
|
|
QuantRole,
|
2023-07-01 15:34:21 +00:00
|
|
|
TypeRole,
|
|
|
|
IsCloneRole,
|
|
|
|
TemperatureRole,
|
|
|
|
TopPRole,
|
|
|
|
TopKRole,
|
|
|
|
MaxLengthRole,
|
|
|
|
PromptBatchSizeRole,
|
|
|
|
RepeatPenaltyRole,
|
|
|
|
RepeatPenaltyTokensRole,
|
|
|
|
PromptTemplateRole,
|
|
|
|
SystemPromptRole,
|
2023-06-22 19:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
QHash<int, QByteArray> roleNames() const override
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
2023-07-01 15:34:21 +00:00
|
|
|
roles[IdRole] = "id";
|
2023-06-22 19:44:49 +00:00
|
|
|
roles[NameRole] = "name";
|
|
|
|
roles[FilenameRole] = "filename";
|
|
|
|
roles[DirpathRole] = "dirpath";
|
|
|
|
roles[FilesizeRole] = "filesize";
|
|
|
|
roles[Md5sumRole] = "md5sum";
|
|
|
|
roles[CalcHashRole] = "calcHash";
|
|
|
|
roles[InstalledRole] = "installed";
|
|
|
|
roles[DefaultRole] = "isDefault";
|
|
|
|
roles[ChatGPTRole] = "isChatGPT";
|
|
|
|
roles[DisableGUIRole] = "disableGUI";
|
|
|
|
roles[DescriptionRole] = "description";
|
|
|
|
roles[RequiresVersionRole] = "requiresVersion";
|
|
|
|
roles[DeprecatedVersionRole] = "deprecatedVersion";
|
|
|
|
roles[UrlRole] = "url";
|
|
|
|
roles[BytesReceivedRole] = "bytesReceived";
|
|
|
|
roles[BytesTotalRole] = "bytesTotal";
|
|
|
|
roles[TimestampRole] = "timestamp";
|
|
|
|
roles[SpeedRole] = "speed";
|
|
|
|
roles[DownloadingRole] = "isDownloading";
|
|
|
|
roles[IncompleteRole] = "isIncomplete";
|
|
|
|
roles[DownloadErrorRole] = "downloadError";
|
|
|
|
roles[OrderRole] = "order";
|
|
|
|
roles[RamrequiredRole] = "ramrequired";
|
|
|
|
roles[ParametersRole] = "parameters";
|
|
|
|
roles[QuantRole] = "quant";
|
|
|
|
roles[TypeRole] = "type";
|
2023-07-01 15:34:21 +00:00
|
|
|
roles[IsCloneRole] = "isClone";
|
|
|
|
roles[TemperatureRole] = "temperature";
|
|
|
|
roles[TopPRole] = "topP";
|
|
|
|
roles[TopKRole] = "topK";
|
|
|
|
roles[MaxLengthRole] = "maxLength";
|
|
|
|
roles[PromptBatchSizeRole] = "promptBatchSize";
|
|
|
|
roles[RepeatPenaltyRole] = "repeatPenalty";
|
|
|
|
roles[RepeatPenaltyTokensRole] = "repeatPenaltyTokens";
|
|
|
|
roles[PromptTemplateRole] = "promptTemplate";
|
|
|
|
roles[SystemPromptRole] = "systemPrompt";
|
2023-06-22 19:44:49 +00:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
2023-07-01 15:34:21 +00:00
|
|
|
QVariant data(const QString &id, int role) const;
|
2023-07-06 00:12:37 +00:00
|
|
|
QVariant dataByFilename(const QString &filename, int role) const;
|
2023-07-01 15:34:21 +00:00
|
|
|
void updateData(const QString &id, int role, const QVariant &value);
|
2023-07-06 00:12:37 +00:00
|
|
|
void updateDataByFilename(const QString &filename, int role, const QVariant &value);
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
int count() const { return m_models.size(); }
|
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
bool contains(const QString &id) const;
|
|
|
|
bool containsByFilename(const QString &filename) const;
|
|
|
|
Q_INVOKABLE ModelInfo modelInfo(const QString &id) const;
|
|
|
|
Q_INVOKABLE ModelInfo modelInfoByFilename(const QString &filename) const;
|
|
|
|
Q_INVOKABLE bool isUniqueName(const QString &name) const;
|
|
|
|
Q_INVOKABLE QString clone(const ModelInfo &model);
|
|
|
|
Q_INVOKABLE void remove(const ModelInfo &model);
|
2023-06-22 19:44:49 +00:00
|
|
|
ModelInfo defaultModelInfo() const;
|
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
void addModel(const QString &id);
|
2023-07-10 20:14:57 +00:00
|
|
|
void changeId(const QString &oldId, const QString &newId);
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
const QList<ModelInfo> exportModelList() const;
|
|
|
|
const QList<QString> userDefaultModelList() const;
|
|
|
|
|
|
|
|
InstalledModels *installedModels() const { return m_installedModels; }
|
|
|
|
DownloadableModels *downloadableModels() const { return m_downloadableModels; }
|
|
|
|
|
|
|
|
static inline QString toFileSize(quint64 sz) {
|
|
|
|
if (sz < 1024) {
|
|
|
|
return QString("%1 bytes").arg(sz);
|
|
|
|
} else if (sz < 1024 * 1024) {
|
|
|
|
return QString("%1 KB").arg(qreal(sz) / 1024, 0, 'g', 3);
|
|
|
|
} else if (sz < 1024 * 1024 * 1024) {
|
|
|
|
return QString("%1 MB").arg(qreal(sz) / (1024 * 1024), 0, 'g', 3);
|
|
|
|
} else {
|
|
|
|
return QString("%1 GB").arg(qreal(sz) / (1024 * 1024 * 1024), 0, 'g', 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString incompleteDownloadPath(const QString &modelFile);
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void countChanged();
|
|
|
|
void installedModelsChanged();
|
|
|
|
void downloadableModelsChanged();
|
|
|
|
void userDefaultModelListChanged();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
2023-07-01 15:34:21 +00:00
|
|
|
void updateModelsFromJson();
|
2023-07-10 20:14:57 +00:00
|
|
|
void updateModelsFromJsonAsync();
|
2023-07-01 15:34:21 +00:00
|
|
|
void updateModelsFromSettings();
|
2023-06-22 19:44:49 +00:00
|
|
|
void updateModelsFromDirectory();
|
2023-07-01 15:34:21 +00:00
|
|
|
void updateDataForSettings();
|
2023-07-10 20:14:57 +00:00
|
|
|
void handleModelsJsonDownloadFinished();
|
|
|
|
void handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QString modelDirPath(const QString &modelName, bool isChatGPT);
|
|
|
|
int indexForModel(ModelInfo *model);
|
|
|
|
QVariant dataInternal(const ModelInfo *info, int role) const;
|
|
|
|
static bool lessThan(const ModelInfo* a, const ModelInfo* b);
|
2023-07-10 20:14:57 +00:00
|
|
|
void parseModelsJsonFile(const QByteArray &jsonData, bool save);
|
2023-07-01 15:34:21 +00:00
|
|
|
QString uniqueModelName(const ModelInfo &model) const;
|
2023-06-22 19:44:49 +00:00
|
|
|
|
|
|
|
private:
|
2023-06-26 00:22:38 +00:00
|
|
|
mutable QMutex m_mutex;
|
2023-06-28 15:13:33 +00:00
|
|
|
QNetworkAccessManager m_networkManager;
|
2023-06-22 19:44:49 +00:00
|
|
|
InstalledModels *m_installedModels;
|
|
|
|
DownloadableModels *m_downloadableModels;
|
|
|
|
QList<ModelInfo*> m_models;
|
|
|
|
QHash<QString, ModelInfo*> m_modelMap;
|
|
|
|
QFileSystemWatcher *m_watcher;
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ModelList();
|
|
|
|
~ModelList() {}
|
|
|
|
friend class MyModelList;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MODELLIST_H
|