2023-10-24 12:13:32 -04:00
|
|
|
#ifndef EMBLLM_H
|
|
|
|
#define EMBLLM_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
2024-01-22 12:36:01 -05:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
|
|
|
|
2024-01-22 12:36:01 -05:00
|
|
|
struct EmbeddingChunk {
|
|
|
|
int folder_id;
|
|
|
|
int chunk_id;
|
|
|
|
QString chunk;
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(EmbeddingChunk)
|
|
|
|
|
|
|
|
struct EmbeddingResult {
|
|
|
|
int folder_id;
|
|
|
|
int chunk_id;
|
|
|
|
std::vector<float> embedding;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmbeddingLLMWorker : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
EmbeddingLLMWorker();
|
|
|
|
virtual ~EmbeddingLLMWorker();
|
|
|
|
|
|
|
|
void wait();
|
|
|
|
|
|
|
|
std::vector<float> lastResponse() const { return m_lastResponse; }
|
|
|
|
|
|
|
|
bool loadModel();
|
|
|
|
bool hasModel() const;
|
|
|
|
bool isNomic() const;
|
|
|
|
|
|
|
|
std::vector<float> generateSyncEmbedding(const QString &text);
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void requestSyncEmbedding(const QString &text);
|
|
|
|
void requestAsyncEmbedding(const QVector<EmbeddingChunk> &chunks);
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void embeddingsGenerated(const QVector<EmbeddingResult> &embeddings);
|
|
|
|
void errorGenerated(int folder_id, const QString &error);
|
|
|
|
void finished();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void handleFinished();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString m_nomicAPIKey;
|
|
|
|
QNetworkAccessManager *m_networkManager;
|
|
|
|
std::vector<float> m_lastResponse;
|
|
|
|
LLModel *m_model = nullptr;
|
|
|
|
QThread m_workerThread;
|
|
|
|
};
|
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
class EmbeddingLLM : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
EmbeddingLLM();
|
|
|
|
virtual ~EmbeddingLLM();
|
|
|
|
|
2024-01-22 12:36:01 -05:00
|
|
|
bool loadModel();
|
2023-10-24 12:13:32 -04:00
|
|
|
bool hasModel() const;
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
2024-01-22 12:36:01 -05:00
|
|
|
std::vector<float> generateEmbeddings(const QString &text); // synchronous
|
|
|
|
void generateAsyncEmbeddings(const QVector<EmbeddingChunk> &chunks);
|
2023-10-24 12:13:32 -04:00
|
|
|
|
2024-01-22 12:36:01 -05:00
|
|
|
Q_SIGNALS:
|
|
|
|
void requestSyncEmbedding(const QString &text);
|
|
|
|
void requestAsyncEmbedding(const QVector<EmbeddingChunk> &chunks);
|
|
|
|
void embeddingsGenerated(const QVector<EmbeddingResult> &embeddings);
|
|
|
|
void errorGenerated(int folder_id, const QString &error);
|
2023-10-24 12:13:32 -04:00
|
|
|
|
|
|
|
private:
|
2024-01-22 12:36:01 -05:00
|
|
|
EmbeddingLLMWorker *m_embeddingWorker;
|
2023-10-24 12:13:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // EMBLLM_H
|