2023-04-30 20:28:07 -04:00
|
|
|
#include "chat.h"
|
2024-06-04 14:47:11 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
#include "chatlistmodel.h"
|
2023-06-28 16:05:35 -04:00
|
|
|
#include "mysettings.h"
|
2023-05-01 09:10:05 -04:00
|
|
|
#include "network.h"
|
2023-06-04 14:55:05 -04:00
|
|
|
#include "server.h"
|
2024-06-04 14:47:11 -04:00
|
|
|
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QLatin1String>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <Qt>
|
|
|
|
#include <QtGlobal>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <QtLogging>
|
2024-06-04 14:47:11 -04:00
|
|
|
|
|
|
|
#include <utility>
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
Chat::Chat(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_id(Network::globalInstance()->generateUniqueId())
|
|
|
|
, m_name(tr("New Chat"))
|
|
|
|
, m_chatModel(new ChatModel(this))
|
2023-05-20 20:04:36 -04:00
|
|
|
, m_responseState(Chat::ResponseStopped)
|
2023-05-04 15:31:41 -04:00
|
|
|
, m_creationDate(QDateTime::currentSecsSinceEpoch())
|
|
|
|
, m_llmodel(new ChatLLM(this))
|
2023-10-24 12:13:32 -04:00
|
|
|
, m_collectionModel(new LocalDocsCollectionsModel(this))
|
2023-05-11 16:46:25 -04:00
|
|
|
{
|
|
|
|
connectLLM();
|
|
|
|
}
|
|
|
|
|
|
|
|
Chat::Chat(bool isServer, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_id(Network::globalInstance()->generateUniqueId())
|
|
|
|
, m_name(tr("Server Chat"))
|
|
|
|
, m_chatModel(new ChatModel(this))
|
2023-05-20 20:04:36 -04:00
|
|
|
, m_responseState(Chat::ResponseStopped)
|
2023-05-11 16:46:25 -04:00
|
|
|
, m_creationDate(QDateTime::currentSecsSinceEpoch())
|
|
|
|
, m_llmodel(new Server(this))
|
|
|
|
, m_isServer(true)
|
2023-10-24 12:13:32 -04:00
|
|
|
, m_collectionModel(new LocalDocsCollectionsModel(this))
|
2023-05-12 14:06:03 -04:00
|
|
|
{
|
|
|
|
connectLLM();
|
|
|
|
}
|
|
|
|
|
|
|
|
Chat::~Chat()
|
|
|
|
{
|
|
|
|
delete m_llmodel;
|
|
|
|
m_llmodel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::connectLLM()
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2023-05-04 15:31:41 -04:00
|
|
|
// Should be in different threads
|
2024-02-07 09:37:59 -05:00
|
|
|
connect(m_llmodel, &ChatLLM::modelLoadingPercentageChanged, this, &Chat::handleModelLoadingPercentageChanged, Qt::QueuedConnection);
|
2023-05-04 15:31:41 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::responseChanged, this, &Chat::handleResponseChanged, Qt::QueuedConnection);
|
2023-05-20 20:04:36 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::promptProcessing, this, &Chat::promptProcessing, Qt::QueuedConnection);
|
2023-05-01 09:10:05 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::responseStopped, this, &Chat::responseStopped, Qt::QueuedConnection);
|
2023-06-04 14:55:05 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::modelLoadingError, this, &Chat::handleModelLoadingError, Qt::QueuedConnection);
|
2024-03-06 17:14:54 -05:00
|
|
|
connect(m_llmodel, &ChatLLM::modelLoadingWarning, this, &Chat::modelLoadingWarning, Qt::QueuedConnection);
|
2023-05-02 20:31:17 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::recalcChanged, this, &Chat::handleRecalculating, Qt::QueuedConnection);
|
2023-05-02 11:19:17 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::generatedNameChanged, this, &Chat::generatedNameChanged, Qt::QueuedConnection);
|
2023-06-19 14:34:53 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::reportSpeed, this, &Chat::handleTokenSpeedChanged, Qt::QueuedConnection);
|
2024-06-26 15:26:27 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::loadedModelInfoChanged, this, &Chat::loadedModelInfoChanged, Qt::QueuedConnection);
|
2023-06-19 18:23:54 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::databaseResultsChanged, this, &Chat::handleDatabaseResultsChanged, Qt::QueuedConnection);
|
2023-06-22 15:44:49 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::modelInfoChanged, this, &Chat::handleModelInfoChanged, Qt::QueuedConnection);
|
2024-05-15 14:07:03 -04:00
|
|
|
connect(m_llmodel, &ChatLLM::trySwitchContextOfLoadedModelCompleted, this, &Chat::handleTrySwitchContextOfLoadedModelCompleted, Qt::QueuedConnection);
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
connect(this, &Chat::promptRequested, m_llmodel, &ChatLLM::prompt, Qt::QueuedConnection);
|
2023-06-22 15:44:49 -04:00
|
|
|
connect(this, &Chat::modelChangeRequested, m_llmodel, &ChatLLM::modelChangeRequested, Qt::QueuedConnection);
|
2023-05-04 15:31:41 -04:00
|
|
|
connect(this, &Chat::loadDefaultModelRequested, m_llmodel, &ChatLLM::loadDefaultModel, Qt::QueuedConnection);
|
|
|
|
connect(this, &Chat::loadModelRequested, m_llmodel, &ChatLLM::loadModel, Qt::QueuedConnection);
|
2023-05-02 11:19:17 -04:00
|
|
|
connect(this, &Chat::generateNameRequested, m_llmodel, &ChatLLM::generateName, Qt::QueuedConnection);
|
2023-05-30 18:17:59 -04:00
|
|
|
connect(this, &Chat::regenerateResponseRequested, m_llmodel, &ChatLLM::regenerateResponse, Qt::QueuedConnection);
|
2023-06-20 16:14:30 -04:00
|
|
|
connect(this, &Chat::resetResponseRequested, m_llmodel, &ChatLLM::resetResponse, Qt::QueuedConnection);
|
|
|
|
connect(this, &Chat::resetContextRequested, m_llmodel, &ChatLLM::resetContext, Qt::QueuedConnection);
|
2023-07-01 11:34:21 -04:00
|
|
|
connect(this, &Chat::processSystemPromptRequested, m_llmodel, &ChatLLM::processSystemPrompt, Qt::QueuedConnection);
|
2023-06-26 09:35:29 -04:00
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
connect(this, &Chat::collectionListChanged, m_collectionModel, &LocalDocsCollectionsModel::setCollections);
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::reset()
|
|
|
|
{
|
2023-05-01 12:24:51 -04:00
|
|
|
stopGenerating();
|
2023-05-04 15:31:41 -04:00
|
|
|
// Erase our current on disk representation as we're completely resetting the chat along with id
|
2023-06-22 15:44:49 -04:00
|
|
|
ChatListModel::globalInstance()->removeChatFile(this);
|
2023-06-20 16:14:30 -04:00
|
|
|
emit resetContextRequested();
|
2023-05-01 09:10:05 -04:00
|
|
|
m_id = Network::globalInstance()->generateUniqueId();
|
2023-06-19 19:51:28 -04:00
|
|
|
emit idChanged(m_id);
|
2023-05-04 15:31:41 -04:00
|
|
|
// NOTE: We deliberately do no reset the name or creation date to indictate that this was originally
|
|
|
|
// an older chat that was reset for another purpose. Resetting this data will lead to the chat
|
|
|
|
// name label changing back to 'New Chat' and showing up in the chat model list as a 'New Chat'
|
|
|
|
// further down in the list. This might surprise the user. In the future, we me might get rid of
|
|
|
|
// the "reset context" button in the UI. Right now, by changing the model in the combobox dropdown
|
|
|
|
// we effectively do a reset context. We *have* to do this right now when switching between different
|
|
|
|
// types of models. The only way to get rid of that would be a very long recalculate where we rebuild
|
|
|
|
// the context if we switch between different types of models. Probably the right way to fix this
|
|
|
|
// is to allow switching models but throwing up a dialog warning users if we switch between types
|
|
|
|
// of models that a long recalculation will ensue.
|
2023-05-01 09:10:05 -04:00
|
|
|
m_chatModel->clear();
|
|
|
|
}
|
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
void Chat::processSystemPrompt()
|
|
|
|
{
|
|
|
|
emit processSystemPromptRequested();
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:23:27 -04:00
|
|
|
void Chat::resetResponseState()
|
|
|
|
{
|
|
|
|
if (m_responseInProgress && m_responseState == Chat::LocalDocsRetrieval)
|
|
|
|
return;
|
|
|
|
|
2023-06-19 14:34:53 -04:00
|
|
|
m_tokenSpeed = QString();
|
|
|
|
emit tokenSpeedChanged();
|
2023-06-01 17:23:27 -04:00
|
|
|
m_responseInProgress = true;
|
2024-01-29 13:57:42 -05:00
|
|
|
m_responseState = m_collections.empty() ? Chat::PromptProcessing : Chat::LocalDocsRetrieval;
|
2023-06-01 17:23:27 -04:00
|
|
|
emit responseInProgressChanged();
|
|
|
|
emit responseStateChanged();
|
|
|
|
}
|
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
void Chat::prompt(const QString &prompt)
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2023-06-01 17:23:27 -04:00
|
|
|
resetResponseState();
|
2024-01-29 13:57:42 -05:00
|
|
|
emit promptRequested(m_collections, prompt);
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::regenerateResponse()
|
|
|
|
{
|
2023-06-01 17:23:27 -04:00
|
|
|
const int index = m_chatModel->count() - 1;
|
2024-06-24 18:49:23 -04:00
|
|
|
m_chatModel->updateSources(index, QList<ResultInfo>());
|
2023-05-30 18:17:59 -04:00
|
|
|
emit regenerateResponseRequested();
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::stopGenerating()
|
|
|
|
{
|
|
|
|
m_llmodel->stopGenerating();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Chat::response() const
|
|
|
|
{
|
2023-06-20 16:14:30 -04:00
|
|
|
return m_response;
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
2023-10-29 18:34:42 -04:00
|
|
|
Chat::ResponseState Chat::responseState() const
|
|
|
|
{
|
|
|
|
return m_responseState;
|
2023-05-20 20:04:36 -04:00
|
|
|
}
|
|
|
|
|
2023-06-20 16:14:30 -04:00
|
|
|
void Chat::handleResponseChanged(const QString &response)
|
2023-05-04 15:31:41 -04:00
|
|
|
{
|
2023-05-20 20:04:36 -04:00
|
|
|
if (m_responseState != Chat::ResponseGeneration) {
|
|
|
|
m_responseState = Chat::ResponseGeneration;
|
|
|
|
emit responseStateChanged();
|
|
|
|
}
|
|
|
|
|
2023-06-20 16:14:30 -04:00
|
|
|
m_response = response;
|
2023-05-04 15:31:41 -04:00
|
|
|
const int index = m_chatModel->count() - 1;
|
2023-06-20 16:14:30 -04:00
|
|
|
m_chatModel->updateValue(index, this->response());
|
2023-05-04 15:31:41 -04:00
|
|
|
emit responseChanged();
|
|
|
|
}
|
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
void Chat::handleModelLoadingPercentageChanged(float loadingPercentage)
|
2023-05-13 19:05:35 -04:00
|
|
|
{
|
|
|
|
if (m_shouldDeleteLater)
|
|
|
|
deleteLater();
|
2023-06-20 16:14:30 -04:00
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
if (loadingPercentage == m_modelLoadingPercentage)
|
2023-06-20 16:14:30 -04:00
|
|
|
return;
|
|
|
|
|
2024-05-15 14:07:03 -04:00
|
|
|
bool wasLoading = isCurrentlyLoading();
|
|
|
|
bool wasLoaded = isModelLoaded();
|
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
m_modelLoadingPercentage = loadingPercentage;
|
|
|
|
emit modelLoadingPercentageChanged();
|
2024-05-15 14:07:03 -04:00
|
|
|
|
|
|
|
if (isCurrentlyLoading() != wasLoading)
|
|
|
|
emit isCurrentlyLoadingChanged();
|
|
|
|
|
|
|
|
if (isModelLoaded() != wasLoaded)
|
2024-02-07 09:37:59 -05:00
|
|
|
emit isModelLoadedChanged();
|
2023-05-13 19:05:35 -04:00
|
|
|
}
|
|
|
|
|
2023-05-20 20:04:36 -04:00
|
|
|
void Chat::promptProcessing()
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2023-06-01 16:12:21 -04:00
|
|
|
m_responseState = !databaseResults().isEmpty() ? Chat::LocalDocsProcessing : Chat::PromptProcessing;
|
2023-05-20 20:04:36 -04:00
|
|
|
emit responseStateChanged();
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
void Chat::responseStopped(qint64 promptResponseMs)
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2023-06-19 14:34:53 -04:00
|
|
|
m_tokenSpeed = QString();
|
|
|
|
emit tokenSpeedChanged();
|
2024-04-15 11:34:57 -04:00
|
|
|
emit responseChanged();
|
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
m_responseInProgress = false;
|
2023-05-20 20:04:36 -04:00
|
|
|
m_responseState = Chat::ResponseStopped;
|
2023-05-01 09:10:05 -04:00
|
|
|
emit responseInProgressChanged();
|
2023-05-20 20:04:36 -04:00
|
|
|
emit responseStateChanged();
|
2023-06-20 16:14:30 -04:00
|
|
|
if (m_generatedName.isEmpty())
|
2023-05-02 11:19:17 -04:00
|
|
|
emit generateNameRequested();
|
2024-04-25 13:16:52 -04:00
|
|
|
|
|
|
|
Network::globalInstance()->trackChatEvent("response_complete", {
|
|
|
|
{"first", m_firstResponse},
|
|
|
|
{"message_count", chatModel()->count()},
|
|
|
|
{"$duration", promptResponseMs / 1000.},
|
|
|
|
});
|
|
|
|
m_firstResponse = false;
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
ModelInfo Chat::modelInfo() const
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
return m_modelInfo;
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
void Chat::setModelInfo(const ModelInfo &modelInfo)
|
2023-05-01 09:10:05 -04:00
|
|
|
{
|
2024-02-07 09:37:59 -05:00
|
|
|
if (m_modelInfo == modelInfo && isModelLoaded())
|
2023-06-20 16:14:30 -04:00
|
|
|
return;
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
m_modelInfo = modelInfo;
|
|
|
|
emit modelInfoChanged();
|
|
|
|
emit modelChangeRequested(modelInfo);
|
2023-05-01 09:10:05 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 12:30:54 -04:00
|
|
|
void Chat::newPromptResponsePair(const QString &prompt)
|
|
|
|
{
|
2023-06-01 17:23:27 -04:00
|
|
|
resetResponseState();
|
2023-05-11 16:46:25 -04:00
|
|
|
m_chatModel->updateCurrentResponse(m_chatModel->count() - 1, false);
|
2023-05-01 12:30:54 -04:00
|
|
|
m_chatModel->appendPrompt(tr("Prompt: "), prompt);
|
|
|
|
m_chatModel->appendResponse(tr("Response: "), prompt);
|
2023-06-20 16:14:30 -04:00
|
|
|
emit resetResponseRequested();
|
2023-05-01 12:30:54 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:46:25 -04:00
|
|
|
void Chat::serverNewPromptResponsePair(const QString &prompt)
|
|
|
|
{
|
2023-06-01 17:23:27 -04:00
|
|
|
resetResponseState();
|
|
|
|
m_chatModel->updateCurrentResponse(m_chatModel->count() - 1, false);
|
2023-05-11 16:46:25 -04:00
|
|
|
m_chatModel->appendPrompt(tr("Prompt: "), prompt);
|
|
|
|
m_chatModel->appendResponse(tr("Response: "), prompt);
|
|
|
|
}
|
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
bool Chat::isRecalc() const
|
|
|
|
{
|
|
|
|
return m_llmodel->isRecalc();
|
|
|
|
}
|
2023-05-01 20:27:07 -04:00
|
|
|
|
2023-05-13 19:05:35 -04:00
|
|
|
void Chat::unloadAndDeleteLater()
|
|
|
|
{
|
|
|
|
if (!isModelLoaded()) {
|
|
|
|
deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shouldDeleteLater = true;
|
|
|
|
unloadModel();
|
|
|
|
}
|
|
|
|
|
2024-03-06 12:59:34 -05:00
|
|
|
void Chat::markForDeletion()
|
|
|
|
{
|
|
|
|
m_llmodel->setMarkedForDeletion(true);
|
|
|
|
}
|
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
void Chat::unloadModel()
|
2023-05-01 20:27:07 -04:00
|
|
|
{
|
|
|
|
stopGenerating();
|
2023-05-13 19:05:35 -04:00
|
|
|
m_llmodel->setShouldBeLoaded(false);
|
2023-05-01 20:27:07 -04:00
|
|
|
}
|
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
void Chat::reloadModel()
|
2023-05-01 20:27:07 -04:00
|
|
|
{
|
2023-05-13 19:05:35 -04:00
|
|
|
m_llmodel->setShouldBeLoaded(true);
|
2023-05-01 20:27:07 -04:00
|
|
|
}
|
2023-05-02 11:19:17 -04:00
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
void Chat::forceUnloadModel()
|
2023-06-26 09:35:29 -04:00
|
|
|
{
|
2024-02-07 09:37:59 -05:00
|
|
|
stopGenerating();
|
|
|
|
m_llmodel->setForceUnloadModel(true);
|
|
|
|
m_llmodel->setShouldBeLoaded(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::forceReloadModel()
|
|
|
|
{
|
|
|
|
m_llmodel->setForceUnloadModel(true);
|
|
|
|
m_llmodel->setShouldBeLoaded(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::trySwitchContextOfLoadedModel()
|
|
|
|
{
|
2024-05-15 14:07:03 -04:00
|
|
|
m_trySwitchContextInProgress = 1;
|
|
|
|
emit trySwitchContextInProgressChanged();
|
|
|
|
m_llmodel->requestTrySwitchContext();
|
2023-06-26 09:35:29 -04:00
|
|
|
}
|
|
|
|
|
2023-06-20 16:14:30 -04:00
|
|
|
void Chat::generatedNameChanged(const QString &name)
|
2023-05-02 11:19:17 -04:00
|
|
|
{
|
|
|
|
// Only use the first three words maximum and remove newlines and extra spaces
|
2023-06-20 16:14:30 -04:00
|
|
|
m_generatedName = name.simplified();
|
|
|
|
QStringList words = m_generatedName.split(' ', Qt::SkipEmptyParts);
|
2024-06-24 18:49:23 -04:00
|
|
|
int wordCount = qMin(7, words.size());
|
2023-05-02 11:19:17 -04:00
|
|
|
m_name = words.mid(0, wordCount).join(' ');
|
|
|
|
emit nameChanged();
|
|
|
|
}
|
2023-05-02 20:31:17 -04:00
|
|
|
|
|
|
|
void Chat::handleRecalculating()
|
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
Network::globalInstance()->trackChatEvent("recalc_context", { {"length", m_chatModel->count()} });
|
2023-05-04 15:31:41 -04:00
|
|
|
emit recalcChanged();
|
|
|
|
}
|
|
|
|
|
2023-06-04 14:55:05 -04:00
|
|
|
void Chat::handleModelLoadingError(const QString &error)
|
|
|
|
{
|
2024-05-15 14:07:03 -04:00
|
|
|
if (!error.isEmpty()) {
|
|
|
|
auto stream = qWarning().noquote() << "ERROR:" << error << "id";
|
|
|
|
stream.quote() << id();
|
|
|
|
}
|
2023-06-04 14:55:05 -04:00
|
|
|
m_modelLoadingError = error;
|
|
|
|
emit modelLoadingErrorChanged();
|
|
|
|
}
|
|
|
|
|
2023-06-19 14:34:53 -04:00
|
|
|
void Chat::handleTokenSpeedChanged(const QString &tokenSpeed)
|
|
|
|
{
|
|
|
|
m_tokenSpeed = tokenSpeed;
|
|
|
|
emit tokenSpeedChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-26 15:26:27 -04:00
|
|
|
QString Chat::deviceBackend() const
|
2023-09-14 08:25:37 -04:00
|
|
|
{
|
2024-06-26 15:26:27 -04:00
|
|
|
return m_llmodel->deviceBackend();
|
2023-09-14 08:25:37 -04:00
|
|
|
}
|
|
|
|
|
2024-06-26 15:26:27 -04:00
|
|
|
QString Chat::device() const
|
2023-09-29 14:25:37 -04:00
|
|
|
{
|
2024-06-26 15:26:27 -04:00
|
|
|
return m_llmodel->device();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Chat::fallbackReason() const
|
|
|
|
{
|
|
|
|
return m_llmodel->fallbackReason();
|
2023-09-29 14:25:37 -04:00
|
|
|
}
|
|
|
|
|
2023-06-19 18:23:54 -04:00
|
|
|
void Chat::handleDatabaseResultsChanged(const QList<ResultInfo> &results)
|
|
|
|
{
|
|
|
|
m_databaseResults = results;
|
2024-06-24 18:49:23 -04:00
|
|
|
const int index = m_chatModel->count() - 1;
|
|
|
|
m_chatModel->updateSources(index, m_databaseResults);
|
2023-06-19 18:23:54 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
void Chat::handleModelInfoChanged(const ModelInfo &modelInfo)
|
|
|
|
{
|
|
|
|
if (m_modelInfo == modelInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_modelInfo = modelInfo;
|
|
|
|
emit modelInfoChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void Chat::handleTrySwitchContextOfLoadedModelCompleted(int value)
|
|
|
|
{
|
2024-05-15 14:07:03 -04:00
|
|
|
m_trySwitchContextInProgress = value;
|
|
|
|
emit trySwitchContextInProgressChanged();
|
|
|
|
}
|
|
|
|
|
2023-05-08 05:52:57 -04:00
|
|
|
bool Chat::serialize(QDataStream &stream, int version) const
|
2023-05-04 15:31:41 -04:00
|
|
|
{
|
|
|
|
stream << m_creationDate;
|
|
|
|
stream << m_id;
|
|
|
|
stream << m_name;
|
|
|
|
stream << m_userName;
|
2023-07-01 11:34:21 -04:00
|
|
|
if (version > 4)
|
|
|
|
stream << m_modelInfo.id();
|
|
|
|
else
|
|
|
|
stream << m_modelInfo.filename();
|
2023-05-24 14:49:43 -04:00
|
|
|
if (version > 2)
|
|
|
|
stream << m_collections;
|
2023-10-11 10:23:33 -04:00
|
|
|
|
|
|
|
const bool serializeKV = MySettings::globalInstance()->saveChatsContext();
|
|
|
|
if (version > 5)
|
|
|
|
stream << serializeKV;
|
|
|
|
if (!m_llmodel->serialize(stream, version, serializeKV))
|
2023-05-04 15:31:41 -04:00
|
|
|
return false;
|
2023-05-08 05:52:57 -04:00
|
|
|
if (!m_chatModel->serialize(stream, version))
|
2023-05-04 15:31:41 -04:00
|
|
|
return false;
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
2023-05-08 05:52:57 -04:00
|
|
|
bool Chat::deserialize(QDataStream &stream, int version)
|
2023-05-04 15:31:41 -04:00
|
|
|
{
|
|
|
|
stream >> m_creationDate;
|
|
|
|
stream >> m_id;
|
2023-06-19 19:51:28 -04:00
|
|
|
emit idChanged(m_id);
|
2023-05-04 15:31:41 -04:00
|
|
|
stream >> m_name;
|
|
|
|
stream >> m_userName;
|
2023-10-28 16:41:03 -04:00
|
|
|
m_generatedName = QLatin1String("nonempty");
|
2023-05-04 15:31:41 -04:00
|
|
|
emit nameChanged();
|
2023-06-22 15:44:49 -04:00
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
QString modelId;
|
|
|
|
stream >> modelId;
|
|
|
|
if (version > 4) {
|
2023-10-10 16:43:02 -04:00
|
|
|
if (ModelList::globalInstance()->contains(modelId))
|
|
|
|
m_modelInfo = ModelList::globalInstance()->modelInfo(modelId);
|
2023-07-01 11:34:21 -04:00
|
|
|
} else {
|
2023-10-10 16:43:02 -04:00
|
|
|
if (ModelList::globalInstance()->containsByFilename(modelId))
|
|
|
|
m_modelInfo = ModelList::globalInstance()->modelInfoByFilename(modelId);
|
2023-07-01 11:34:21 -04:00
|
|
|
}
|
2023-10-10 16:43:02 -04:00
|
|
|
if (!m_modelInfo.id().isEmpty())
|
|
|
|
emit modelInfoChanged();
|
|
|
|
|
|
|
|
bool discardKV = m_modelInfo.id().isEmpty();
|
2023-06-22 15:44:49 -04:00
|
|
|
|
2023-05-08 17:23:02 -04:00
|
|
|
// Prior to version 2 gptj models had a bug that fixed the kv_cache to F32 instead of F16 so
|
|
|
|
// unfortunately, we cannot deserialize these
|
2023-07-01 11:34:21 -04:00
|
|
|
if (version < 2 && m_modelInfo.filename().contains("gpt4all-j"))
|
2023-10-10 16:43:02 -04:00
|
|
|
discardKV = true;
|
|
|
|
|
2023-05-24 14:49:43 -04:00
|
|
|
if (version > 2) {
|
|
|
|
stream >> m_collections;
|
2023-06-19 19:51:28 -04:00
|
|
|
emit collectionListChanged(m_collections);
|
2023-05-24 14:49:43 -04:00
|
|
|
}
|
2023-10-11 10:23:33 -04:00
|
|
|
|
|
|
|
bool deserializeKV = true;
|
|
|
|
if (version > 5)
|
|
|
|
stream >> deserializeKV;
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
m_llmodel->setModelInfo(m_modelInfo);
|
2023-10-10 16:43:02 -04:00
|
|
|
if (!m_llmodel->deserialize(stream, version, deserializeKV, discardKV))
|
2023-05-04 15:31:41 -04:00
|
|
|
return false;
|
2023-05-08 05:52:57 -04:00
|
|
|
if (!m_chatModel->deserialize(stream, version))
|
2023-05-04 15:31:41 -04:00
|
|
|
return false;
|
2023-10-10 16:43:02 -04:00
|
|
|
|
2023-12-12 11:45:03 -05:00
|
|
|
m_llmodel->setStateFromText(m_chatModel->text());
|
2023-10-10 16:43:02 -04:00
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
emit chatModelChanged();
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
2023-05-23 14:51:14 -04:00
|
|
|
QList<QString> Chat::collectionList() const
|
|
|
|
{
|
|
|
|
return m_collections;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Chat::hasCollection(const QString &collection) const
|
|
|
|
{
|
|
|
|
return m_collections.contains(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::addCollection(const QString &collection)
|
|
|
|
{
|
|
|
|
if (hasCollection(collection))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_collections.append(collection);
|
2023-06-19 19:51:28 -04:00
|
|
|
emit collectionListChanged(m_collections);
|
2023-05-23 14:51:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chat::removeCollection(const QString &collection)
|
|
|
|
{
|
|
|
|
if (!hasCollection(collection))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_collections.removeAll(collection);
|
2023-06-19 19:51:28 -04:00
|
|
|
emit collectionListChanged(m_collections);
|
2023-05-23 14:51:14 -04:00
|
|
|
}
|