2024-03-13 18:23:57 -04:00
|
|
|
#ifndef CHATAPI_H
|
|
|
|
#define CHATAPI_H
|
2023-05-14 20:12:15 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
2024-02-21 15:45:32 -05:00
|
|
|
|
2024-06-06 11:59:28 -04:00
|
|
|
#include <QByteArray>
|
2023-05-14 20:12:15 -04:00
|
|
|
#include <QNetworkReply>
|
2024-03-06 14:02:18 -05:00
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QList>
|
2024-03-06 14:02:18 -05:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class QNetworkAccessManager;
|
2023-05-14 20:12:15 -04:00
|
|
|
|
2024-03-13 18:23:57 -04:00
|
|
|
class ChatAPI;
|
|
|
|
class ChatAPIWorker : public QObject {
|
2023-07-11 10:08:03 -04:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2024-03-13 18:23:57 -04:00
|
|
|
ChatAPIWorker(ChatAPI *chatAPI)
|
2023-07-11 10:08:03 -04:00
|
|
|
: QObject(nullptr)
|
|
|
|
, m_ctx(nullptr)
|
|
|
|
, m_networkManager(nullptr)
|
2024-03-13 18:23:57 -04:00
|
|
|
, m_chat(chatAPI) {}
|
|
|
|
virtual ~ChatAPIWorker() {}
|
2023-07-11 10:08:03 -04:00
|
|
|
|
|
|
|
QString currentResponse() const { return m_currentResponse; }
|
|
|
|
|
|
|
|
void request(const QString &apiKey,
|
2024-03-13 18:23:57 -04:00
|
|
|
LLModel::PromptContext *promptCtx,
|
|
|
|
const QByteArray &array);
|
2023-07-11 10:08:03 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void finished();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void handleFinished();
|
|
|
|
void handleReadyRead();
|
|
|
|
void handleErrorOccurred(QNetworkReply::NetworkError code);
|
|
|
|
|
|
|
|
private:
|
2024-03-13 18:23:57 -04:00
|
|
|
ChatAPI *m_chat;
|
2023-07-11 10:08:03 -04:00
|
|
|
LLModel::PromptContext *m_ctx;
|
|
|
|
QNetworkAccessManager *m_networkManager;
|
|
|
|
QString m_currentResponse;
|
|
|
|
};
|
|
|
|
|
2024-03-13 18:23:57 -04:00
|
|
|
class ChatAPI : public QObject, public LLModel {
|
2023-05-14 20:12:15 -04:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2024-03-13 18:23:57 -04:00
|
|
|
ChatAPI();
|
|
|
|
virtual ~ChatAPI();
|
2023-05-14 20:12:15 -04:00
|
|
|
|
2023-07-09 11:32:51 -04:00
|
|
|
bool supportsEmbedding() const override { return false; }
|
|
|
|
bool supportsCompletion() const override { return true; }
|
2024-01-31 14:17:44 -05:00
|
|
|
bool loadModel(const std::string &modelPath, int n_ctx, int ngl) override;
|
2023-05-14 20:12:15 -04:00
|
|
|
bool isModelLoaded() const override;
|
2024-01-31 14:17:44 -05:00
|
|
|
size_t requiredMem(const std::string &modelPath, int n_ctx, int ngl) override;
|
2023-05-14 20:12:15 -04:00
|
|
|
size_t stateSize() const override;
|
|
|
|
size_t saveState(uint8_t *dest) const override;
|
|
|
|
size_t restoreState(const uint8_t *src) override;
|
|
|
|
void prompt(const std::string &prompt,
|
2024-03-13 18:23:57 -04:00
|
|
|
const std::string &promptTemplate,
|
|
|
|
std::function<bool(int32_t)> promptCallback,
|
|
|
|
std::function<bool(int32_t, const std::string&)> responseCallback,
|
|
|
|
std::function<bool(bool)> recalculateCallback,
|
|
|
|
PromptContext &ctx,
|
|
|
|
bool special,
|
|
|
|
std::string *fakeReply) override;
|
2023-06-04 08:59:24 -04:00
|
|
|
|
2023-05-14 20:12:15 -04:00
|
|
|
void setThreadCount(int32_t n_threads) override;
|
2023-05-21 16:45:29 -04:00
|
|
|
int32_t threadCount() const override;
|
2023-05-14 20:12:15 -04:00
|
|
|
|
|
|
|
void setModelName(const QString &modelName) { m_modelName = modelName; }
|
|
|
|
void setAPIKey(const QString &apiKey) { m_apiKey = apiKey; }
|
2024-03-13 18:23:57 -04:00
|
|
|
void setRequestURL(const QString &requestURL) { m_requestURL = requestURL; }
|
|
|
|
QString url() const { return m_requestURL; }
|
2023-05-14 20:12:15 -04:00
|
|
|
|
2023-05-15 18:36:41 -04:00
|
|
|
QList<QString> context() const { return m_context; }
|
|
|
|
void setContext(const QList<QString> &context) { m_context = context; }
|
|
|
|
|
2024-02-21 15:45:32 -05:00
|
|
|
bool callResponse(int32_t token, const std::string &string);
|
2023-07-11 10:08:03 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void request(const QString &apiKey,
|
2024-03-13 18:23:57 -04:00
|
|
|
LLModel::PromptContext *ctx,
|
|
|
|
const QByteArray &array);
|
2023-07-11 10:08:03 -04:00
|
|
|
|
2023-06-04 08:59:24 -04:00
|
|
|
protected:
|
|
|
|
// We have to implement these as they are pure virtual in base class, but we don't actually use
|
|
|
|
// them as they are only called from the default implementation of 'prompt' which we override and
|
|
|
|
// completely replace
|
2024-02-21 15:45:32 -05:00
|
|
|
|
2024-08-01 10:46:36 -04:00
|
|
|
std::vector<Token> tokenize(PromptContext &ctx, const std::string &str, bool special) override {
|
2024-02-21 15:45:32 -05:00
|
|
|
(void)ctx;
|
|
|
|
(void)str;
|
|
|
|
(void)special;
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string tokenToString(Token id) const override {
|
|
|
|
(void)id;
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
Token sampleToken(PromptContext &ctx) const override {
|
|
|
|
(void)ctx;
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool evalTokens(PromptContext &ctx, const std::vector<int32_t> &tokens) const override {
|
|
|
|
(void)ctx;
|
|
|
|
(void)tokens;
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t contextLength() const override {
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<Token> &endTokens() const override {
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldAddBOS() const override {
|
|
|
|
throw std::logic_error("not implemented");
|
|
|
|
}
|
2023-06-04 08:59:24 -04:00
|
|
|
|
2023-05-14 20:12:15 -04:00
|
|
|
private:
|
|
|
|
std::function<bool(int32_t, const std::string&)> m_responseCallback;
|
|
|
|
QString m_modelName;
|
|
|
|
QString m_apiKey;
|
2024-03-13 18:23:57 -04:00
|
|
|
QString m_requestURL;
|
2023-05-14 20:12:15 -04:00
|
|
|
QList<QString> m_context;
|
2024-03-06 14:02:18 -05:00
|
|
|
QStringList m_queuedPrompts;
|
2023-05-14 20:12:15 -04:00
|
|
|
};
|
|
|
|
|
2024-03-13 18:23:57 -04:00
|
|
|
#endif // CHATAPI_H
|