modellist: fix a few issues with loading remote models (#2875)

Signed-off-by: Jared Van Bortel <jared@nomic.ai>
This commit is contained in:
Jared Van Bortel 2024-08-30 12:44:10 -04:00 committed by GitHub
parent 813ccaf5d1
commit 55946ffc93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 3608 additions and 3521 deletions

View File

@ -23,8 +23,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- Set the window icon on Linux ([#2880](https://github.com/nomic-ai/gpt4all/pull/2880)) - Set the window icon on Linux ([#2880](https://github.com/nomic-ai/gpt4all/pull/2880))
- Corrections to the Romanian translation (by [@SINAPSA-IC](https://github.com/SINAPSA-IC) in [#2890](https://github.com/nomic-ai/gpt4all/pull/2890)) - Corrections to the Romanian translation (by [@SINAPSA-IC](https://github.com/SINAPSA-IC) in [#2890](https://github.com/nomic-ai/gpt4all/pull/2890))
- Fix singular/plural forms of LocalDocs "x Sources" (by [@cosmic-snow](https://github.com/cosmic-snow) in [#2885](https://github.com/nomic-ai/gpt4all/pull/2885)) - Fix singular/plural forms of LocalDocs "x Sources" (by [@cosmic-snow](https://github.com/cosmic-snow) in [#2885](https://github.com/nomic-ai/gpt4all/pull/2885))
- Fixed typo in several files. (by [@3Simplex](https://github.com/3Simplex) in [#2916](https://github.com/nomic-ai/gpt4all/pull/2916)) - Fix a typo in Model Settings (by [@3Simplex](https://github.com/3Simplex) in [#2916](https://github.com/nomic-ai/gpt4all/pull/2916))
- Fix the antenna icon tooltip when using the local server ([#2922](https://github.com/nomic-ai/gpt4all/pull/2922)) - Fix the antenna icon tooltip when using the local server ([#2922](https://github.com/nomic-ai/gpt4all/pull/2922))
- Fix a few issues with locating files and handling errors when loading remote models on startup ([#2875](https://github.com/nomic-ai/gpt4all/pull/2875))
## [3.2.1] - 2024-08-13 ## [3.2.1] - 2024-08-13

View File

@ -1208,51 +1208,55 @@ bool ModelList::modelExists(const QString &modelFilename) const
return false; return false;
} }
void ModelList::updateModelsFromDirectory() void ModelList::updateOldRemoteModels(const QString &path)
{ {
const QString exePath = QCoreApplication::applicationDirPath() + QDir::separator(); QDirIterator it(path, QDir::Files, QDirIterator::Subdirectories);
const QString localPath = MySettings::globalInstance()->modelPath();
auto updateOldRemoteModels = [&](const QString& path) {
QDirIterator it(path, QDirIterator::Subdirectories);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); QFileInfo info = it.nextFileInfo();
if (!it.fileInfo().isDir()) {
QString filename = it.fileName(); QString filename = it.fileName();
if (filename.startsWith("chatgpt-") && filename.endsWith(".txt")) { if (!filename.startsWith("chatgpt-") || !filename.endsWith(".txt"))
continue;
QString apikey; QString apikey;
QString modelname(filename); QString modelname(filename);
modelname.chop(4); // strip ".txt" extension modelname.chop(4); // strip ".txt" extension
modelname.remove(0, 8); // strip "chatgpt-" prefix modelname.remove(0, 8); // strip "chatgpt-" prefix
QFile file(path + filename); QFile file(info.filePath());
if (file.open(QIODevice::ReadWrite)) { if (!file.open(QIODevice::ReadOnly)) {
qWarning().noquote() << tr("cannot open \"%1\": %2").arg(file.fileName(), file.errorString());
continue;
}
{
QTextStream in(&file); QTextStream in(&file);
apikey = in.readAll(); apikey = in.readAll();
file.close(); file.close();
} }
QJsonObject obj; QFile newfile(u"%1/gpt4all-%2.rmodel"_s.arg(info.dir().path(), modelname));
obj.insert("apiKey", apikey); if (!newfile.open(QIODevice::ReadWrite)) {
obj.insert("modelName", modelname); qWarning().noquote() << tr("cannot create \"%1\": %2").arg(newfile.fileName(), file.errorString());
QJsonDocument doc(obj); continue;
auto newfilename = u"gpt4all-%1.rmodel"_s.arg(modelname);
QFile newfile(path + newfilename);
if (newfile.open(QIODevice::ReadWrite)) {
QTextStream out(&newfile);
out << doc.toJson();
newfile.close();
} }
QJsonObject obj {
{ "apiKey", apikey },
{ "modelName", modelname },
};
QTextStream out(&newfile);
out << QJsonDocument(obj).toJson();
newfile.close();
file.remove(); file.remove();
} }
} }
}
};
auto processDirectory = [&](const QString& path) { void ModelList::processModelDirectory(const QString &path)
{
QDirIterator it(path, QDir::Files, QDirIterator::Subdirectories); QDirIterator it(path, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); QFileInfo info = it.nextFileInfo();
QString filename = it.fileName(); QString filename = it.fileName();
if (filename.startsWith("incomplete") || FILENAME_BLACKLIST.contains(filename)) if (filename.startsWith("incomplete") || FILENAME_BLACKLIST.contains(filename))
@ -1260,22 +1264,6 @@ void ModelList::updateModelsFromDirectory()
if (!filename.endsWith(".gguf") && !filename.endsWith(".rmodel")) if (!filename.endsWith(".gguf") && !filename.endsWith(".rmodel"))
continue; continue;
QVector<QString> modelsById;
{
QMutexLocker locker(&m_mutex);
for (ModelInfo *info : m_models)
if (info->filename() == filename)
modelsById.append(info->id());
}
if (modelsById.isEmpty()) {
if (!contains(filename))
addModel(filename);
modelsById.append(filename);
}
QFileInfo info = it.fileInfo();
bool isOnline(filename.endsWith(".rmodel")); bool isOnline(filename.endsWith(".rmodel"));
bool isCompatibleApi(filename.endsWith("-capi.rmodel")); bool isCompatibleApi(filename.endsWith("-capi.rmodel"));
@ -1284,10 +1272,11 @@ void ModelList::updateModelsFromDirectory()
if (isCompatibleApi) { if (isCompatibleApi) {
QJsonObject obj; QJsonObject obj;
{ {
QFile file(path + filename); QFile file(info.filePath());
bool success = file.open(QIODeviceBase::ReadOnly); if (!file.open(QIODeviceBase::ReadOnly)) {
(void)success; qWarning().noquote() << tr("cannot open \"%1\": %2").arg(file.fileName(), file.errorString());
Q_ASSERT(success); continue;
}
QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
obj = doc.object(); obj = doc.object();
} }
@ -1305,6 +1294,20 @@ void ModelList::updateModelsFromDirectory()
} }
} }
QVector<QString> modelsById;
{
QMutexLocker locker(&m_mutex);
for (ModelInfo *info : m_models)
if (info->filename() == filename)
modelsById.append(info->id());
}
if (modelsById.isEmpty()) {
if (!contains(filename))
addModel(filename);
modelsById.append(filename);
}
for (const QString &id : modelsById) { for (const QString &id : modelsById) {
QVector<QPair<int, QVariant>> data { QVector<QPair<int, QVariant>> data {
{ InstalledRole, true }, { InstalledRole, true },
@ -1326,14 +1329,18 @@ void ModelList::updateModelsFromDirectory()
updateData(id, data); updateData(id, data);
} }
} }
}; }
void ModelList::updateModelsFromDirectory()
{
const QString exePath = QCoreApplication::applicationDirPath() + QDir::separator();
const QString localPath = MySettings::globalInstance()->modelPath();
updateOldRemoteModels(exePath); updateOldRemoteModels(exePath);
processDirectory(exePath); processModelDirectory(exePath);
if (localPath != exePath) { if (localPath != exePath) {
updateOldRemoteModels(localPath); updateOldRemoteModels(localPath);
processDirectory(localPath); processModelDirectory(localPath);
} }
} }

View File

@ -502,6 +502,8 @@ private:
void parseModelsJsonFile(const QByteArray &jsonData, bool save); void parseModelsJsonFile(const QByteArray &jsonData, bool save);
void parseDiscoveryJsonFile(const QByteArray &jsonData); void parseDiscoveryJsonFile(const QByteArray &jsonData);
QString uniqueModelName(const ModelInfo &model) const; QString uniqueModelName(const ModelInfo &model) const;
void updateOldRemoteModels(const QString &path);
void processModelDirectory(const QString &path);
private: private:
mutable QMutex m_mutex; mutable QMutex m_mutex;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff