2023-05-01 09:10:05 -04:00
|
|
|
#ifndef CHATLLM_H
|
|
|
|
#define CHATLLM_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include "llmodel/llmodel.h"
|
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
class Chat;
|
2023-05-01 09:10:05 -04: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 11:19:17 -04:00
|
|
|
Q_PROPERTY(QString generatedName READ generatedName NOTIFY generatedNameChanged)
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
public:
|
2023-05-04 15:31:41 -04:00
|
|
|
ChatLLM(Chat *parent);
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
bool isModelLoaded() const;
|
|
|
|
void regenerateResponse();
|
|
|
|
void resetResponse();
|
|
|
|
void resetContext();
|
|
|
|
|
|
|
|
void stopGenerating() { m_stopGenerating = true; }
|
|
|
|
|
|
|
|
QString response() const;
|
|
|
|
QString modelName() const;
|
|
|
|
|
|
|
|
void setModelName(const QString &modelName);
|
|
|
|
|
|
|
|
bool isRecalc() const { return m_isRecalc; }
|
|
|
|
|
2023-05-02 11:19:17 -04:00
|
|
|
QString generatedName() const { return QString::fromStdString(m_nameResponse); }
|
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
bool serialize(QDataStream &stream);
|
|
|
|
bool deserialize(QDataStream &stream);
|
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
public Q_SLOTS:
|
2023-05-04 15:31:41 -04:00
|
|
|
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, float repeat_penalty, int32_t repeat_penalty_tokens,
|
|
|
|
int32_t n_threads);
|
|
|
|
bool loadDefaultModel();
|
|
|
|
bool loadModel(const QString &modelName);
|
2023-05-01 09:10:05 -04:00
|
|
|
void modelNameChangeRequested(const QString &modelName);
|
2023-05-04 15:31:41 -04:00
|
|
|
void unloadModel();
|
|
|
|
void reloadModel(const QString &modelName);
|
2023-05-02 11:19:17 -04:00
|
|
|
void generateName();
|
2023-05-04 15:31:41 -04:00
|
|
|
void handleChatIdChanged();
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void isModelLoadedChanged();
|
|
|
|
void responseChanged();
|
|
|
|
void responseStarted();
|
|
|
|
void responseStopped();
|
|
|
|
void modelNameChanged();
|
|
|
|
void recalcChanged();
|
|
|
|
void sendStartup();
|
|
|
|
void sendModelLoaded();
|
|
|
|
void sendResetContext();
|
2023-05-02 11:19:17 -04:00
|
|
|
void generatedNameChanged();
|
2023-05-04 15:31:41 -04:00
|
|
|
void stateChanged();
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void resetContextPrivate();
|
|
|
|
bool handlePrompt(int32_t token);
|
|
|
|
bool handleResponse(int32_t token, const std::string &response);
|
|
|
|
bool handleRecalculate(bool isRecalc);
|
2023-05-02 11:19:17 -04:00
|
|
|
bool handleNamePrompt(int32_t token);
|
|
|
|
bool handleNameResponse(int32_t token, const std::string &response);
|
|
|
|
bool handleNameRecalculate(bool isRecalc);
|
2023-05-04 15:31:41 -04:00
|
|
|
void saveState();
|
|
|
|
void restoreState();
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
LLModel::PromptContext m_ctx;
|
|
|
|
LLModel *m_llmodel;
|
|
|
|
std::string m_response;
|
2023-05-02 11:19:17 -04:00
|
|
|
std::string m_nameResponse;
|
2023-05-01 09:10:05 -04:00
|
|
|
quint32 m_promptResponseTokens;
|
|
|
|
quint32 m_responseLogits;
|
|
|
|
QString m_modelName;
|
2023-05-04 15:31:41 -04:00
|
|
|
Chat *m_chat;
|
|
|
|
QByteArray m_state;
|
2023-05-01 09:10:05 -04:00
|
|
|
QThread m_llmThread;
|
|
|
|
std::atomic<bool> m_stopGenerating;
|
|
|
|
bool m_isRecalc;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CHATLLM_H
|