2023-05-01 13:10:05 +00:00
|
|
|
|
#ifndef CHATLLM_H
|
|
|
|
|
#define CHATLLM_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <QThread>
|
2023-05-13 23:05:35 +00:00
|
|
|
|
#include <QFileInfo>
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
2023-06-01 18:13:12 +00:00
|
|
|
|
#include "localdocs.h"
|
2023-05-10 15:46:40 +00:00
|
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
|
enum LLModelType {
|
|
|
|
|
MPT_,
|
|
|
|
|
GPTJ_,
|
2023-05-15 00:12:15 +00:00
|
|
|
|
LLAMA_,
|
|
|
|
|
CHATGPT_,
|
2023-06-06 21:09:00 +00:00
|
|
|
|
REPLIT_
|
2023-05-13 23:05:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct LLModelInfo {
|
|
|
|
|
LLModel *model = nullptr;
|
|
|
|
|
QFileInfo fileInfo;
|
|
|
|
|
// NOTE: This does not store the model type or name on purpose as this is left for ChatLLM which
|
|
|
|
|
// must be able to serialize the information even if it is in the unloaded state
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-19 18:34:53 +00:00
|
|
|
|
class TokenTimer : public QObject {
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
explicit TokenTimer(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_elapsed(0) {}
|
|
|
|
|
|
|
|
|
|
static int rollingAverage(int oldAvg, int newNumber, int n)
|
|
|
|
|
{
|
|
|
|
|
// i.e. to calculate the new average after then nth number,
|
|
|
|
|
// you multiply the old average by n−1, add the new number, and divide the total by n.
|
|
|
|
|
return qRound(((float(oldAvg) * (n - 1)) + newNumber) / float(n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void start() { m_tokens = 0; m_elapsed = 0; m_time.invalidate(); }
|
|
|
|
|
void stop() { handleTimeout(); }
|
|
|
|
|
void inc() {
|
|
|
|
|
if (!m_time.isValid())
|
|
|
|
|
m_time.start();
|
|
|
|
|
++m_tokens;
|
|
|
|
|
if (m_time.elapsed() > 999)
|
|
|
|
|
handleTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
|
void report(const QString &speed);
|
|
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
|
void handleTimeout()
|
|
|
|
|
{
|
|
|
|
|
m_elapsed += m_time.restart();
|
|
|
|
|
emit report(QString("%1 tokens/sec").arg(m_tokens / float(m_elapsed / 1000.0f), 0, 'g', 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QElapsedTimer m_time;
|
|
|
|
|
qint64 m_elapsed;
|
|
|
|
|
quint32 m_tokens;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-04 19:31:41 +00:00
|
|
|
|
class Chat;
|
2023-05-01 13:10:05 +00:00
|
|
|
|
class ChatLLM : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PROPERTY(bool isModelLoaded READ isModelLoaded NOTIFY isModelLoadedChanged)
|
|
|
|
|
Q_PROPERTY(QString response READ response NOTIFY responseChanged)
|
|
|
|
|
Q_PROPERTY(QString modelName READ modelName WRITE setModelName NOTIFY modelNameChanged)
|
|
|
|
|
Q_PROPERTY(bool isRecalc READ isRecalc NOTIFY recalcChanged)
|
2023-05-02 15:19:17 +00:00
|
|
|
|
Q_PROPERTY(QString generatedName READ generatedName NOTIFY generatedNameChanged)
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2023-05-13 23:33:19 +00:00
|
|
|
|
ChatLLM(Chat *parent, bool isServer = false);
|
2023-05-12 18:06:03 +00:00
|
|
|
|
virtual ~ChatLLM();
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
|
|
|
|
bool isModelLoaded() const;
|
|
|
|
|
void regenerateResponse();
|
|
|
|
|
void resetResponse();
|
|
|
|
|
void resetContext();
|
|
|
|
|
|
|
|
|
|
void stopGenerating() { m_stopGenerating = true; }
|
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
|
bool shouldBeLoaded() const { return m_shouldBeLoaded; }
|
|
|
|
|
void setShouldBeLoaded(bool b);
|
|
|
|
|
|
2023-05-01 13:10:05 +00:00
|
|
|
|
QString response() const;
|
|
|
|
|
QString modelName() const;
|
|
|
|
|
|
|
|
|
|
void setModelName(const QString &modelName);
|
|
|
|
|
|
|
|
|
|
bool isRecalc() const { return m_isRecalc; }
|
|
|
|
|
|
2023-05-02 15:19:17 +00:00
|
|
|
|
QString generatedName() const { return QString::fromStdString(m_nameResponse); }
|
|
|
|
|
|
2023-05-08 09:52:57 +00:00
|
|
|
|
bool serialize(QDataStream &stream, int version);
|
|
|
|
|
bool deserialize(QDataStream &stream, int version);
|
2023-05-04 19:31:41 +00:00
|
|
|
|
|
2023-05-01 13:10:05 +00:00
|
|
|
|
public Q_SLOTS:
|
2023-06-19 23:51:28 +00:00
|
|
|
|
bool prompt(const QList<QString> &collectionList, const QString &prompt, const QString &prompt_template,
|
|
|
|
|
int32_t n_predict, int32_t top_k, float top_p, float temp, int32_t n_batch, float repeat_penalty,
|
|
|
|
|
int32_t repeat_penalty_tokens, int32_t n_threads);
|
2023-05-04 19:31:41 +00:00
|
|
|
|
bool loadDefaultModel();
|
|
|
|
|
bool loadModel(const QString &modelName);
|
2023-05-01 13:10:05 +00:00
|
|
|
|
void modelNameChangeRequested(const QString &modelName);
|
2023-05-13 23:05:35 +00:00
|
|
|
|
void forceUnloadModel();
|
2023-05-04 19:31:41 +00:00
|
|
|
|
void unloadModel();
|
2023-05-13 23:05:35 +00:00
|
|
|
|
void reloadModel();
|
2023-05-02 15:19:17 +00:00
|
|
|
|
void generateName();
|
2023-06-19 23:51:28 +00:00
|
|
|
|
void handleChatIdChanged(const QString &id);
|
|
|
|
|
void handleDefaultModelChanged(const QString &defaultModel);
|
2023-05-13 23:05:35 +00:00
|
|
|
|
void handleShouldBeLoadedChanged();
|
2023-06-19 18:34:53 +00:00
|
|
|
|
void handleThreadStarted();
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
|
void isModelLoadedChanged();
|
2023-05-09 00:51:03 +00:00
|
|
|
|
void modelLoadingError(const QString &error);
|
2023-05-01 13:10:05 +00:00
|
|
|
|
void responseChanged();
|
2023-05-21 00:04:36 +00:00
|
|
|
|
void promptProcessing();
|
2023-05-01 13:10:05 +00:00
|
|
|
|
void responseStopped();
|
|
|
|
|
void modelNameChanged();
|
|
|
|
|
void recalcChanged();
|
|
|
|
|
void sendStartup();
|
|
|
|
|
void sendModelLoaded();
|
2023-05-02 15:19:17 +00:00
|
|
|
|
void generatedNameChanged();
|
2023-05-04 19:31:41 +00:00
|
|
|
|
void stateChanged();
|
2023-05-11 20:46:25 +00:00
|
|
|
|
void threadStarted();
|
2023-05-13 23:05:35 +00:00
|
|
|
|
void shouldBeLoadedChanged();
|
2023-06-01 18:13:12 +00:00
|
|
|
|
void requestRetrieveFromDB(const QList<QString> &collections, const QString &text, int retrievalSize, QList<ResultInfo> *results);
|
2023-06-19 18:34:53 +00:00
|
|
|
|
void reportSpeed(const QString &speed);
|
2023-06-19 22:23:54 +00:00
|
|
|
|
void databaseResultsChanged(const QList<ResultInfo>&);
|
2023-05-11 20:46:25 +00:00
|
|
|
|
|
|
|
|
|
protected:
|
2023-05-01 13:10:05 +00:00
|
|
|
|
bool handlePrompt(int32_t token);
|
|
|
|
|
bool handleResponse(int32_t token, const std::string &response);
|
|
|
|
|
bool handleRecalculate(bool isRecalc);
|
2023-05-02 15:19:17 +00:00
|
|
|
|
bool handleNamePrompt(int32_t token);
|
|
|
|
|
bool handleNameResponse(int32_t token, const std::string &response);
|
|
|
|
|
bool handleNameRecalculate(bool isRecalc);
|
2023-05-04 19:31:41 +00:00
|
|
|
|
void saveState();
|
|
|
|
|
void restoreState();
|
2023-05-01 13:10:05 +00:00
|
|
|
|
|
2023-05-13 23:33:19 +00:00
|
|
|
|
protected:
|
2023-06-19 23:51:28 +00:00
|
|
|
|
// The following are all accessed by multiple threads and are thus guarded with thread protection
|
|
|
|
|
// mechanisms
|
2023-05-13 23:33:19 +00:00
|
|
|
|
LLModel::PromptContext m_ctx;
|
|
|
|
|
quint32 m_promptTokens;
|
|
|
|
|
quint32 m_promptResponseTokens;
|
2023-06-19 23:51:28 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// The following are all accessed by multiple threads and are thus guarded with thread protection
|
|
|
|
|
// mechanisms
|
2023-05-01 13:10:05 +00:00
|
|
|
|
std::string m_response;
|
2023-05-02 15:19:17 +00:00
|
|
|
|
std::string m_nameResponse;
|
2023-06-19 23:51:28 +00:00
|
|
|
|
LLModelInfo m_modelInfo;
|
|
|
|
|
LLModelType m_modelType;
|
2023-05-01 13:10:05 +00:00
|
|
|
|
QString m_modelName;
|
2023-06-19 23:51:28 +00:00
|
|
|
|
|
|
|
|
|
// The following are only accessed by this thread
|
|
|
|
|
QString m_defaultModel;
|
2023-06-19 18:34:53 +00:00
|
|
|
|
TokenTimer *m_timer;
|
2023-05-04 19:31:41 +00:00
|
|
|
|
QByteArray m_state;
|
2023-05-01 13:10:05 +00:00
|
|
|
|
QThread m_llmThread;
|
|
|
|
|
std::atomic<bool> m_stopGenerating;
|
2023-05-13 23:05:35 +00:00
|
|
|
|
std::atomic<bool> m_shouldBeLoaded;
|
2023-06-19 22:24:11 +00:00
|
|
|
|
std::atomic<bool> m_isRecalc;
|
2023-05-13 23:33:19 +00:00
|
|
|
|
bool m_isServer;
|
2023-05-01 13:10:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // CHATLLM_H
|