gpt4all/llm.h

128 lines
3.9 KiB
C
Raw Normal View History

2023-04-09 03:28:39 +00:00
#ifndef LLM_H
#define LLM_H
#include <QObject>
#include <QThread>
#include "gptj.h"
#include "llamamodel.h"
2023-04-09 03:28:39 +00:00
class LLMObject : public QObject
2023-04-09 03:28:39 +00:00
{
Q_OBJECT
Q_PROPERTY(QList<QString> modelList READ modelList NOTIFY modelListChanged)
2023-04-09 03:28:39 +00:00
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)
2023-04-18 13:46:03 +00:00
Q_PROPERTY(int32_t threadCount READ threadCount WRITE setThreadCount NOTIFY threadCountChanged)
2023-04-09 03:28:39 +00:00
public:
LLMObject();
2023-04-09 03:28:39 +00:00
bool isModelLoaded() const;
2023-04-17 18:11:41 +00:00
void regenerateResponse();
2023-04-09 03:28:39 +00:00
void resetResponse();
void resetContext();
2023-04-09 03:28:39 +00:00
void stopGenerating() { m_stopGenerating = true; }
2023-04-18 13:46:03 +00:00
void setThreadCount(int32_t n_threads);
int32_t threadCount();
2023-04-09 03:28:39 +00:00
QString response() const;
QString modelName() const;
2023-04-09 03:28:39 +00:00
QList<QString> modelList() const;
void setModelName(const QString &modelName);
2023-04-09 03:28:39 +00:00
public Q_SLOTS:
bool prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
float temp, int32_t n_batch);
bool loadModel();
void modelNameChangeRequested(const QString &modelName);
2023-04-09 03:28:39 +00:00
Q_SIGNALS:
void isModelLoadedChanged();
void responseChanged();
void responseStarted();
void responseStopped();
void modelNameChanged();
void modelListChanged();
2023-04-18 13:46:03 +00:00
void threadCountChanged();
2023-04-09 03:28:39 +00:00
private:
bool loadModelPrivate(const QString &modelName);
2023-04-09 03:28:39 +00:00
bool handleResponse(const std::string &response);
private:
LLModel *m_llmodel;
2023-04-09 03:28:39 +00:00
std::string m_response;
quint32 m_responseTokens;
quint32 m_responseLogits;
QString m_modelName;
2023-04-09 03:28:39 +00:00
QThread m_llmThread;
std::atomic<bool> m_stopGenerating;
};
class LLM : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<QString> modelList READ modelList NOTIFY modelListChanged)
2023-04-09 03:28:39 +00:00
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)
2023-04-09 03:28:39 +00:00
Q_PROPERTY(bool responseInProgress READ responseInProgress NOTIFY responseInProgressChanged)
2023-04-18 13:46:03 +00:00
Q_PROPERTY(int32_t threadCount READ threadCount WRITE setThreadCount NOTIFY threadCountChanged)
2023-04-09 03:28:39 +00:00
public:
static LLM *globalInstance();
Q_INVOKABLE bool isModelLoaded() const;
Q_INVOKABLE void prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
float temp, int32_t n_batch);
2023-04-17 18:11:41 +00:00
Q_INVOKABLE void regenerateResponse();
2023-04-09 03:28:39 +00:00
Q_INVOKABLE void resetResponse();
2023-04-17 18:11:41 +00:00
Q_INVOKABLE void resetContext();
2023-04-09 03:28:39 +00:00
Q_INVOKABLE void stopGenerating();
2023-04-18 13:46:03 +00:00
Q_INVOKABLE void setThreadCount(int32_t n_threads);
Q_INVOKABLE int32_t threadCount();
2023-04-09 03:28:39 +00:00
QString response() const;
bool responseInProgress() const { return m_responseInProgress; }
QList<QString> modelList() const;
QString modelName() const;
void setModelName(const QString &modelName);
2023-04-11 03:34:34 +00:00
Q_INVOKABLE bool checkForUpdates() const;
2023-04-09 03:28:39 +00:00
Q_SIGNALS:
void isModelLoadedChanged();
void responseChanged();
void responseInProgressChanged();
void promptRequested(const QString &prompt, const QString &prompt_template, int32_t n_predict, int32_t top_k, float top_p,
float temp, int32_t n_batch);
2023-04-17 18:11:41 +00:00
void regenerateResponseRequested();
2023-04-09 03:28:39 +00:00
void resetResponseRequested();
void resetContextRequested();
void modelNameChangeRequested(const QString &modelName);
void modelNameChanged();
void modelListChanged();
2023-04-18 13:46:03 +00:00
void threadCountChanged();
void setThreadCountRequested(int32_t threadCount);
2023-04-09 03:28:39 +00:00
private Q_SLOTS:
void responseStarted();
void responseStopped();
private:
LLMObject *m_llmodel;
2023-04-09 03:28:39 +00:00
bool m_responseInProgress;
private:
explicit LLM();
~LLM() {}
friend class MyLLM;
};
#endif // LLM_H