2023-04-30 20:28:07 -04:00
|
|
|
#ifndef CHAT_H
|
|
|
|
#define CHAT_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QtQml>
|
2023-05-04 15:31:41 -04:00
|
|
|
#include <QDataStream>
|
2023-04-30 20:28:07 -04:00
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
#include "chatllm.h"
|
2023-04-30 20:28:07 -04:00
|
|
|
#include "chatmodel.h"
|
2023-05-24 14:49:43 -04:00
|
|
|
#include "database.h"
|
2023-05-11 16:46:25 -04:00
|
|
|
#include "server.h"
|
2023-04-30 20:28:07 -04:00
|
|
|
|
|
|
|
class Chat : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QString id READ id NOTIFY idChanged)
|
2023-05-01 20:27:07 -04:00
|
|
|
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
2023-04-30 20:28:07 -04:00
|
|
|
Q_PROPERTY(ChatModel *chatModel READ chatModel NOTIFY chatModelChanged)
|
2023-05-01 09:10:05 -04: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)
|
|
|
|
Q_PROPERTY(bool responseInProgress READ responseInProgress NOTIFY responseInProgressChanged)
|
|
|
|
Q_PROPERTY(bool isRecalc READ isRecalc NOTIFY recalcChanged)
|
2023-05-04 15:31:41 -04:00
|
|
|
Q_PROPERTY(QList<QString> modelList READ modelList NOTIFY modelListChanged)
|
2023-05-11 16:46:25 -04:00
|
|
|
Q_PROPERTY(bool isServer READ isServer NOTIFY isServerChanged)
|
2023-05-20 20:04:36 -04:00
|
|
|
Q_PROPERTY(QString responseState READ responseState NOTIFY responseStateChanged)
|
2023-05-23 14:51:14 -04:00
|
|
|
Q_PROPERTY(QList<QString> collectionList READ collectionList NOTIFY collectionListChanged)
|
2023-04-30 20:28:07 -04:00
|
|
|
QML_ELEMENT
|
|
|
|
QML_UNCREATABLE("Only creatable from c++!")
|
|
|
|
|
|
|
|
public:
|
2023-05-20 20:04:36 -04:00
|
|
|
enum ResponseState {
|
|
|
|
ResponseStopped,
|
|
|
|
LocalDocsRetrieval,
|
|
|
|
LocalDocsProcessing,
|
|
|
|
PromptProcessing,
|
|
|
|
ResponseGeneration
|
|
|
|
};
|
|
|
|
Q_ENUM(ResponseState)
|
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
explicit Chat(QObject *parent = nullptr);
|
2023-05-11 16:46:25 -04:00
|
|
|
explicit Chat(bool isServer, QObject *parent = nullptr);
|
2023-05-12 14:06:03 -04:00
|
|
|
virtual ~Chat();
|
|
|
|
void connectLLM();
|
2023-04-30 20:28:07 -04:00
|
|
|
|
|
|
|
QString id() const { return m_id; }
|
2023-05-01 20:27:07 -04:00
|
|
|
QString name() const { return m_userName.isEmpty() ? m_name : m_userName; }
|
|
|
|
void setName(const QString &name)
|
|
|
|
{
|
|
|
|
m_userName = name;
|
|
|
|
emit nameChanged();
|
|
|
|
}
|
2023-04-30 20:28:07 -04:00
|
|
|
ChatModel *chatModel() { return m_chatModel; }
|
|
|
|
|
2023-05-01 09:10:05 -04:00
|
|
|
Q_INVOKABLE void reset();
|
|
|
|
Q_INVOKABLE bool isModelLoaded() const;
|
2023-05-04 15:31:41 -04:00
|
|
|
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, float repeat_penalty, int32_t repeat_penalty_tokens);
|
2023-05-01 09:10:05 -04:00
|
|
|
Q_INVOKABLE void regenerateResponse();
|
|
|
|
Q_INVOKABLE void stopGenerating();
|
2023-05-01 12:30:54 -04:00
|
|
|
Q_INVOKABLE void newPromptResponsePair(const QString &prompt);
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
QString response() const;
|
|
|
|
bool responseInProgress() const { return m_responseInProgress; }
|
2023-05-20 20:04:36 -04:00
|
|
|
QString responseState() const;
|
2023-05-01 09:10:05 -04:00
|
|
|
QString modelName() const;
|
|
|
|
void setModelName(const QString &modelName);
|
|
|
|
bool isRecalc() const;
|
2023-04-30 21:05:54 -04:00
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
void loadDefaultModel();
|
|
|
|
void loadModel(const QString &modelName);
|
|
|
|
void unloadModel();
|
|
|
|
void reloadModel();
|
2023-05-13 19:05:35 -04:00
|
|
|
void unloadAndDeleteLater();
|
2023-05-04 15:31:41 -04:00
|
|
|
|
|
|
|
qint64 creationDate() const { return m_creationDate; }
|
2023-05-08 05:52:57 -04:00
|
|
|
bool serialize(QDataStream &stream, int version) const;
|
|
|
|
bool deserialize(QDataStream &stream, int version);
|
2023-05-04 15:31:41 -04:00
|
|
|
|
|
|
|
QList<QString> modelList() const;
|
2023-05-11 16:46:25 -04:00
|
|
|
bool isServer() const { return m_isServer; }
|
|
|
|
|
2023-05-23 14:51:14 -04:00
|
|
|
QList<QString> collectionList() const;
|
|
|
|
|
|
|
|
Q_INVOKABLE bool hasCollection(const QString &collection) const;
|
|
|
|
Q_INVOKABLE void addCollection(const QString &collection);
|
|
|
|
Q_INVOKABLE void removeCollection(const QString &collection);
|
|
|
|
|
2023-05-11 16:46:25 -04:00
|
|
|
public Q_SLOTS:
|
|
|
|
void serverNewPromptResponsePair(const QString &prompt);
|
2023-05-01 20:27:07 -04:00
|
|
|
|
2023-04-30 20:28:07 -04:00
|
|
|
Q_SIGNALS:
|
|
|
|
void idChanged();
|
|
|
|
void nameChanged();
|
|
|
|
void chatModelChanged();
|
2023-05-01 09:10:05 -04:00
|
|
|
void isModelLoadedChanged();
|
|
|
|
void responseChanged();
|
|
|
|
void responseInProgressChanged();
|
2023-05-20 20:04:36 -04:00
|
|
|
void responseStateChanged();
|
2023-05-04 15:31:41 -04:00
|
|
|
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, float repeat_penalty, int32_t repeat_penalty_tokens,
|
|
|
|
int32_t n_threads);
|
2023-05-01 09:10:05 -04:00
|
|
|
void regenerateResponseRequested();
|
|
|
|
void resetResponseRequested();
|
|
|
|
void resetContextRequested();
|
|
|
|
void modelNameChangeRequested(const QString &modelName);
|
|
|
|
void modelNameChanged();
|
|
|
|
void recalcChanged();
|
2023-05-04 15:31:41 -04:00
|
|
|
void loadDefaultModelRequested();
|
|
|
|
void loadModelRequested(const QString &modelName);
|
2023-05-02 11:19:17 -04:00
|
|
|
void generateNameRequested();
|
2023-05-04 15:31:41 -04:00
|
|
|
void modelListChanged();
|
2023-05-08 20:51:03 -04:00
|
|
|
void modelLoadingError(const QString &error);
|
2023-05-11 16:46:25 -04:00
|
|
|
void isServerChanged();
|
2023-05-23 14:51:14 -04:00
|
|
|
void collectionListChanged();
|
2023-05-01 09:10:05 -04:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
2023-05-24 14:49:43 -04:00
|
|
|
void handleLocalDocsRetrieved(const QString &uid, const QList<ResultInfo> &results);
|
2023-05-04 15:31:41 -04:00
|
|
|
void handleResponseChanged();
|
2023-05-13 19:05:35 -04:00
|
|
|
void handleModelLoadedChanged();
|
2023-05-20 20:04:36 -04:00
|
|
|
void promptProcessing();
|
2023-05-01 09:10:05 -04:00
|
|
|
void responseStopped();
|
2023-05-02 11:19:17 -04:00
|
|
|
void generatedNameChanged();
|
2023-05-02 20:31:17 -04:00
|
|
|
void handleRecalculating();
|
2023-05-04 15:31:41 -04:00
|
|
|
void handleModelNameChanged();
|
2023-04-30 20:28:07 -04:00
|
|
|
|
|
|
|
private:
|
2023-05-18 18:59:10 -04:00
|
|
|
struct Prompt {
|
|
|
|
QString prompt;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-04-30 20:28:07 -04:00
|
|
|
QString m_id;
|
|
|
|
QString m_name;
|
2023-05-01 20:27:07 -04:00
|
|
|
QString m_userName;
|
2023-05-03 12:45:14 -04:00
|
|
|
QString m_savedModelName;
|
2023-05-23 14:51:14 -04:00
|
|
|
QList<QString> m_collections;
|
2023-05-24 14:49:43 -04:00
|
|
|
QList<ResultInfo> m_results;
|
2023-04-30 20:28:07 -04:00
|
|
|
ChatModel *m_chatModel;
|
2023-05-01 09:10:05 -04:00
|
|
|
bool m_responseInProgress;
|
2023-05-20 20:04:36 -04:00
|
|
|
ResponseState m_responseState;
|
2023-05-04 15:31:41 -04:00
|
|
|
qint64 m_creationDate;
|
|
|
|
ChatLLM *m_llmodel;
|
2023-05-11 16:46:25 -04:00
|
|
|
bool m_isServer;
|
2023-05-13 19:05:35 -04:00
|
|
|
bool m_shouldDeleteLater;
|
2023-05-18 18:59:10 -04:00
|
|
|
Prompt m_queuedPrompt;
|
2023-04-30 20:28:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CHAT_H
|