Convert the old format properly.

This commit is contained in:
Adam Treat 2023-05-08 05:52:57 -04:00
parent 7b66cb7119
commit 3c30310539
6 changed files with 27 additions and 23 deletions

View File

@ -179,21 +179,21 @@ void Chat::handleModelNameChanged()
emit modelNameChanged(); emit modelNameChanged();
} }
bool Chat::serialize(QDataStream &stream) const bool Chat::serialize(QDataStream &stream, int version) const
{ {
stream << m_creationDate; stream << m_creationDate;
stream << m_id; stream << m_id;
stream << m_name; stream << m_name;
stream << m_userName; stream << m_userName;
stream << m_savedModelName; stream << m_savedModelName;
if (!m_llmodel->serialize(stream)) if (!m_llmodel->serialize(stream, version))
return false; return false;
if (!m_chatModel->serialize(stream)) if (!m_chatModel->serialize(stream, version))
return false; return false;
return stream.status() == QDataStream::Ok; return stream.status() == QDataStream::Ok;
} }
bool Chat::deserialize(QDataStream &stream) bool Chat::deserialize(QDataStream &stream, int version)
{ {
stream >> m_creationDate; stream >> m_creationDate;
stream >> m_id; stream >> m_id;
@ -202,9 +202,9 @@ bool Chat::deserialize(QDataStream &stream)
stream >> m_userName; stream >> m_userName;
emit nameChanged(); emit nameChanged();
stream >> m_savedModelName; stream >> m_savedModelName;
if (!m_llmodel->deserialize(stream)) if (!m_llmodel->deserialize(stream, version))
return false; return false;
if (!m_chatModel->deserialize(stream)) if (!m_chatModel->deserialize(stream, version))
return false; return false;
emit chatModelChanged(); emit chatModelChanged();
return stream.status() == QDataStream::Ok; return stream.status() == QDataStream::Ok;

4
chat.h
View File

@ -55,8 +55,8 @@ public:
void reloadModel(); void reloadModel();
qint64 creationDate() const { return m_creationDate; } qint64 creationDate() const { return m_creationDate; }
bool serialize(QDataStream &stream) const; bool serialize(QDataStream &stream, int version) const;
bool deserialize(QDataStream &stream); bool deserialize(QDataStream &stream, int version);
QList<QString> modelList() const; QList<QString> modelList() const;

View File

@ -5,7 +5,7 @@
#include <QDataStream> #include <QDataStream>
#define CHAT_FORMAT_MAGIC 0xF5D553CC #define CHAT_FORMAT_MAGIC 0xF5D553CC
#define CHAT_FORMAT_VERSION 100 #define CHAT_FORMAT_VERSION 1
ChatListModel::ChatListModel(QObject *parent) ChatListModel::ChatListModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
@ -70,7 +70,7 @@ void ChatListModel::saveChats() const
out.setVersion(QDataStream::Qt_6_2); out.setVersion(QDataStream::Qt_6_2);
qDebug() << "serializing chat" << fileName; qDebug() << "serializing chat" << fileName;
if (!chat->serialize(out)) { if (!chat->serialize(out, CHAT_FORMAT_VERSION)) {
qWarning() << "ERROR: Couldn't serialize chat to file:" << file.fileName(); qWarning() << "ERROR: Couldn't serialize chat to file:" << file.fileName();
file.remove(); file.remove();
} }
@ -169,6 +169,7 @@ void ChatsRestoreThread::run()
} }
QDataStream in(&file); QDataStream in(&file);
qint32 version = 0;
if (!f.oldFile) { if (!f.oldFile) {
// Read and check the header // Read and check the header
quint32 magic; quint32 magic;
@ -179,14 +180,13 @@ void ChatsRestoreThread::run()
} }
// Read the version // Read the version
qint32 version;
in >> version; in >> version;
if (version < 100) { if (version < 1) {
qWarning() << "ERROR: Chat file has non supported version:" << file.fileName(); qWarning() << "ERROR: Chat file has non supported version:" << file.fileName();
continue; continue;
} }
if (version <= 100) if (version <= 1)
in.setVersion(QDataStream::Qt_6_2); in.setVersion(QDataStream::Qt_6_2);
} }
@ -194,7 +194,7 @@ void ChatsRestoreThread::run()
Chat *chat = new Chat; Chat *chat = new Chat;
chat->moveToThread(qApp->thread()); chat->moveToThread(qApp->thread());
if (!chat->deserialize(in)) { if (!chat->deserialize(in, version)) {
qWarning() << "ERROR: Couldn't deserialize chat from file:" << file.fileName(); qWarning() << "ERROR: Couldn't deserialize chat from file:" << file.fileName();
file.remove(); file.remove();
} else { } else {

View File

@ -368,7 +368,7 @@ bool ChatLLM::handleNameRecalculate(bool isRecalc)
return true; return true;
} }
bool ChatLLM::serialize(QDataStream &stream) bool ChatLLM::serialize(QDataStream &stream, int version)
{ {
stream << response(); stream << response();
stream << generatedName(); stream << generatedName();
@ -388,7 +388,7 @@ bool ChatLLM::serialize(QDataStream &stream)
return stream.status() == QDataStream::Ok; return stream.status() == QDataStream::Ok;
} }
bool ChatLLM::deserialize(QDataStream &stream) bool ChatLLM::deserialize(QDataStream &stream, int version)
{ {
QString response; QString response;
stream >> response; stream >> response;
@ -407,9 +407,13 @@ bool ChatLLM::deserialize(QDataStream &stream)
stream >> tokensSize; stream >> tokensSize;
m_ctx.tokens.resize(tokensSize); m_ctx.tokens.resize(tokensSize);
stream.readRawData(reinterpret_cast<char*>(m_ctx.tokens.data()), tokensSize * sizeof(int)); stream.readRawData(reinterpret_cast<char*>(m_ctx.tokens.data()), tokensSize * sizeof(int));
QByteArray compressed; if (version > 0) {
stream >> compressed; QByteArray compressed;
m_state = qUncompress(compressed); stream >> compressed;
m_state = qUncompress(compressed);
} else {
stream >> m_state;
}
#if defined(DEBUG) #if defined(DEBUG)
qDebug() << "chatllm deserialize" << m_chat->id(); qDebug() << "chatllm deserialize" << m_chat->id();
#endif #endif

View File

@ -35,8 +35,8 @@ public:
QString generatedName() const { return QString::fromStdString(m_nameResponse); } QString generatedName() const { return QString::fromStdString(m_nameResponse); }
bool serialize(QDataStream &stream); bool serialize(QDataStream &stream, int version);
bool deserialize(QDataStream &stream); bool deserialize(QDataStream &stream, int version);
public Q_SLOTS: public Q_SLOTS:
bool prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, bool prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict,

View File

@ -210,7 +210,7 @@ public:
int count() const { return m_chatItems.size(); } int count() const { return m_chatItems.size(); }
bool serialize(QDataStream &stream) const bool serialize(QDataStream &stream, int version) const
{ {
stream << count(); stream << count();
for (auto c : m_chatItems) { for (auto c : m_chatItems) {
@ -227,7 +227,7 @@ public:
return stream.status() == QDataStream::Ok; return stream.status() == QDataStream::Ok;
} }
bool deserialize(QDataStream &stream) bool deserialize(QDataStream &stream, int version)
{ {
int size; int size;
stream >> size; stream >> size;