2023-04-08 23:28:39 -04:00
|
|
|
#ifndef LLM_H
|
|
|
|
#define LLM_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
2023-04-25 19:16:45 -04:00
|
|
|
#include "llmodel/gptj.h"
|
|
|
|
#include "llmodel/llamamodel.h"
|
2023-04-08 23:28:39 -04:00
|
|
|
|
2023-04-13 22:15:40 -04:00
|
|
|
class LLMObject : public QObject
|
2023-04-08 23:28:39 -04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2023-04-18 11:42:16 -04:00
|
|
|
Q_PROPERTY(QList<QString> modelList READ modelList NOTIFY modelListChanged)
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_PROPERTY(bool isModelLoaded READ isModelLoaded NOTIFY isModelLoadedChanged)
|
|
|
|
Q_PROPERTY(QString response READ response NOTIFY responseChanged)
|
2023-04-18 11:42:16 -04:00
|
|
|
Q_PROPERTY(QString modelName READ modelName WRITE setModelName NOTIFY modelNameChanged)
|
2023-04-18 09:46:03 -04:00
|
|
|
Q_PROPERTY(int32_t threadCount READ threadCount WRITE setThreadCount NOTIFY threadCountChanged)
|
2023-04-25 11:20:51 -04:00
|
|
|
Q_PROPERTY(bool isRecalc READ isRecalc NOTIFY recalcChanged)
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2023-04-13 22:15:40 -04:00
|
|
|
LLMObject();
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
bool isModelLoaded() const;
|
2023-04-17 14:11:41 -04:00
|
|
|
void regenerateResponse();
|
2023-04-08 23:28:39 -04:00
|
|
|
void resetResponse();
|
2023-04-10 17:13:22 -04:00
|
|
|
void resetContext();
|
2023-04-08 23:28:39 -04:00
|
|
|
void stopGenerating() { m_stopGenerating = true; }
|
2023-04-18 09:46:03 -04:00
|
|
|
void setThreadCount(int32_t n_threads);
|
|
|
|
int32_t threadCount();
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
QString response() const;
|
2023-04-11 08:29:55 -04:00
|
|
|
QString modelName() const;
|
2023-04-08 23:28:39 -04:00
|
|
|
|
2023-04-18 11:42:16 -04:00
|
|
|
QList<QString> modelList() const;
|
|
|
|
void setModelName(const QString &modelName);
|
|
|
|
|
2023-04-25 11:20:51 -04:00
|
|
|
bool isRecalc() const { return m_isRecalc; }
|
|
|
|
|
2023-04-08 23:28:39 -04:00
|
|
|
public Q_SLOTS:
|
2023-04-16 01:51:28 -04:00
|
|
|
bool prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
|
2023-04-25 10:57:40 -04:00
|
|
|
float temp, int32_t n_batch, float repeat_penalty, int32_t repeat_penalty_tokens);
|
2023-04-18 11:42:16 -04:00
|
|
|
bool loadModel();
|
|
|
|
void modelNameChangeRequested(const QString &modelName);
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void isModelLoadedChanged();
|
|
|
|
void responseChanged();
|
|
|
|
void responseStarted();
|
|
|
|
void responseStopped();
|
2023-04-11 08:29:55 -04:00
|
|
|
void modelNameChanged();
|
2023-04-18 11:42:16 -04:00
|
|
|
void modelListChanged();
|
2023-04-18 09:46:03 -04:00
|
|
|
void threadCountChanged();
|
2023-04-25 11:20:51 -04:00
|
|
|
void recalcChanged();
|
2023-04-26 22:05:56 -04:00
|
|
|
void sendStartup();
|
|
|
|
void sendModelLoaded();
|
|
|
|
void sendResetContext();
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
private:
|
2023-04-26 22:05:56 -04:00
|
|
|
void resetContextPrivate();
|
2023-04-18 11:42:16 -04:00
|
|
|
bool loadModelPrivate(const QString &modelName);
|
2023-04-27 11:08:15 -04:00
|
|
|
bool handlePrompt(int32_t token);
|
2023-04-25 08:38:29 -04:00
|
|
|
bool handleResponse(int32_t token, const std::string &response);
|
2023-04-25 11:20:51 -04:00
|
|
|
bool handleRecalculate(bool isRecalc);
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
private:
|
2023-04-13 22:15:40 -04:00
|
|
|
LLModel *m_llmodel;
|
2023-04-08 23:28:39 -04:00
|
|
|
std::string m_response;
|
2023-04-27 11:08:15 -04:00
|
|
|
quint32 m_promptResponseTokens;
|
2023-04-15 09:19:06 -04:00
|
|
|
quint32 m_responseLogits;
|
2023-04-11 08:29:55 -04:00
|
|
|
QString m_modelName;
|
2023-04-08 23:28:39 -04:00
|
|
|
QThread m_llmThread;
|
|
|
|
std::atomic<bool> m_stopGenerating;
|
2023-04-25 11:20:51 -04:00
|
|
|
bool m_isRecalc;
|
2023-04-08 23:28:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class LLM : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2023-04-18 11:42:16 -04:00
|
|
|
Q_PROPERTY(QList<QString> modelList READ modelList NOTIFY modelListChanged)
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_PROPERTY(bool isModelLoaded READ isModelLoaded NOTIFY isModelLoadedChanged)
|
|
|
|
Q_PROPERTY(QString response READ response NOTIFY responseChanged)
|
2023-04-18 11:42:16 -04:00
|
|
|
Q_PROPERTY(QString modelName READ modelName WRITE setModelName NOTIFY modelNameChanged)
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_PROPERTY(bool responseInProgress READ responseInProgress NOTIFY responseInProgressChanged)
|
2023-04-18 09:46:03 -04:00
|
|
|
Q_PROPERTY(int32_t threadCount READ threadCount WRITE setThreadCount NOTIFY threadCountChanged)
|
2023-04-25 11:20:51 -04:00
|
|
|
Q_PROPERTY(bool isRecalc READ isRecalc NOTIFY recalcChanged)
|
|
|
|
|
2023-04-08 23:28:39 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
static LLM *globalInstance();
|
|
|
|
|
|
|
|
Q_INVOKABLE bool isModelLoaded() const;
|
2023-04-16 01:51:28 -04:00
|
|
|
Q_INVOKABLE void prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
|
2023-04-25 10:57:40 -04:00
|
|
|
float temp, int32_t n_batch, float repeat_penalty, int32_t repeat_penalty_tokens);
|
2023-04-17 14:11:41 -04:00
|
|
|
Q_INVOKABLE void regenerateResponse();
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_INVOKABLE void resetResponse();
|
2023-04-17 14:11:41 -04:00
|
|
|
Q_INVOKABLE void resetContext();
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_INVOKABLE void stopGenerating();
|
2023-04-24 15:24:55 -04:00
|
|
|
Q_INVOKABLE void syncThreadCount();
|
2023-04-18 09:46:03 -04:00
|
|
|
Q_INVOKABLE void setThreadCount(int32_t n_threads);
|
|
|
|
Q_INVOKABLE int32_t threadCount();
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
QString response() const;
|
|
|
|
bool responseInProgress() const { return m_responseInProgress; }
|
|
|
|
|
2023-04-18 11:42:16 -04:00
|
|
|
QList<QString> modelList() const;
|
|
|
|
|
2023-04-11 08:29:55 -04:00
|
|
|
QString modelName() const;
|
2023-04-18 11:42:16 -04:00
|
|
|
void setModelName(const QString &modelName);
|
2023-04-11 08:29:55 -04:00
|
|
|
|
2023-04-10 23:34:34 -04:00
|
|
|
Q_INVOKABLE bool checkForUpdates() const;
|
|
|
|
|
2023-04-25 11:20:51 -04:00
|
|
|
bool isRecalc() const;
|
|
|
|
|
2023-04-08 23:28:39 -04:00
|
|
|
Q_SIGNALS:
|
|
|
|
void isModelLoadedChanged();
|
|
|
|
void responseChanged();
|
|
|
|
void responseInProgressChanged();
|
2023-04-16 01:51:28 -04:00
|
|
|
void promptRequested(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
|
2023-04-25 10:57:40 -04:00
|
|
|
float temp, int32_t n_batch, float repeat_penalty, int32_t repeat_penalty_tokens);
|
2023-04-17 14:11:41 -04:00
|
|
|
void regenerateResponseRequested();
|
2023-04-08 23:28:39 -04:00
|
|
|
void resetResponseRequested();
|
2023-04-10 17:13:22 -04:00
|
|
|
void resetContextRequested();
|
2023-04-18 11:42:16 -04:00
|
|
|
void modelNameChangeRequested(const QString &modelName);
|
2023-04-11 08:29:55 -04:00
|
|
|
void modelNameChanged();
|
2023-04-18 11:42:16 -04:00
|
|
|
void modelListChanged();
|
2023-04-18 09:46:03 -04:00
|
|
|
void threadCountChanged();
|
|
|
|
void setThreadCountRequested(int32_t threadCount);
|
2023-04-25 11:20:51 -04:00
|
|
|
void recalcChanged();
|
2023-04-08 23:28:39 -04:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void responseStarted();
|
|
|
|
void responseStopped();
|
|
|
|
|
|
|
|
private:
|
2023-04-13 22:15:40 -04:00
|
|
|
LLMObject *m_llmodel;
|
2023-04-24 15:24:55 -04:00
|
|
|
int32_t m_desiredThreadCount;
|
2023-04-08 23:28:39 -04:00
|
|
|
bool m_responseInProgress;
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit LLM();
|
|
|
|
~LLM() {}
|
|
|
|
friend class MyLLM;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LLM_H
|