90 lines
1.8 KiB
C++
90 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <QHash>
|
|
#include <QString>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <QUrl>
|
|
#include <memory>
|
|
|
|
class AiBackend: public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
|
|
class backend_error: public std::runtime_error
|
|
{
|
|
public:
|
|
backend_error(std::string err): runtime_error(err){}
|
|
};
|
|
|
|
class Request
|
|
{
|
|
public:
|
|
|
|
typedef enum {
|
|
INFERENCE,
|
|
COUNT_TOKENS,
|
|
LEFT_TRIM,
|
|
UNKOWN
|
|
} type_t;
|
|
|
|
protected:
|
|
inline static uint32_t idCounter = 0;
|
|
QString text;
|
|
uint32_t id;
|
|
void* userPtr;
|
|
type_t type;
|
|
|
|
public:
|
|
Request() = default;
|
|
Request(const QString& text, type_t type = UNKOWN, void* userPtr = nullptr);
|
|
const QString& getText() const;
|
|
uint32_t getId() const;
|
|
type_t getType() const;
|
|
bool operator==(const Request& in);
|
|
void* getUserPtr() const;
|
|
};
|
|
|
|
class Response: public Request
|
|
{
|
|
private:
|
|
bool finished;
|
|
int64_t tokens = -1;
|
|
|
|
public:
|
|
Response() = default;
|
|
Response(QString text, uint32_t id, bool finished, type_t type = UNKOWN, int64_t tokens= -1, void* userPtr = nullptr);
|
|
bool isFinished() const;
|
|
int64_t getTokens() const;
|
|
|
|
void setUserPtr(void* ptr);
|
|
};
|
|
|
|
protected:
|
|
QHash<uint32_t, Request> m_requests;
|
|
|
|
void feedResponse(Response response);
|
|
bool isValidId(uint32_t id);
|
|
virtual bool generateImpl(const Request& request) = 0;
|
|
|
|
public:
|
|
|
|
static std::vector<QString> getAvailableBackendNames();
|
|
static std::shared_ptr<AiBackend> createBackend(const QString& name);
|
|
virtual QString backendName() = 0;
|
|
virtual bool ready() = 0;
|
|
bool generate(const Request& request);
|
|
virtual void abort(uint64_t id){(void)id;}
|
|
virtual void open(const QUrl& url){(void)url;};
|
|
Q_SIGNAL void gotResponse(Response response);
|
|
|
|
virtual ~AiBackend() = default;
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(AiBackend::Response);
|
|
Q_DECLARE_METATYPE(AiBackend::Request);
|
|
|