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

4
chat.h
View File

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

View File

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

View File

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

View File

@ -35,8 +35,8 @@ public:
QString generatedName() const { return QString::fromStdString(m_nameResponse); }
bool serialize(QDataStream &stream);
bool deserialize(QDataStream &stream);
bool serialize(QDataStream &stream, int version);
bool deserialize(QDataStream &stream, int version);
public Q_SLOTS:
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(); }
bool serialize(QDataStream &stream) const
bool serialize(QDataStream &stream, int version) const
{
stream << count();
for (auto c : m_chatItems) {
@ -227,7 +227,7 @@ public:
return stream.status() == QDataStream::Ok;
}
bool deserialize(QDataStream &stream)
bool deserialize(QDataStream &stream, int version)
{
int size;
stream >> size;