2023-10-24 16:13:32 +00:00
|
|
|
#ifndef EMBLLM_H
|
|
|
|
#define EMBLLM_H
|
|
|
|
|
2024-06-04 18:47:11 +00:00
|
|
|
#include <QByteArray>
|
2024-06-24 22:49:23 +00:00
|
|
|
#include <QMutex>
|
2023-10-24 16:13:32 +00:00
|
|
|
#include <QObject>
|
2024-06-04 18:47:11 +00:00
|
|
|
#include <QString>
|
2024-03-13 22:09:24 +00:00
|
|
|
#include <QStringList>
|
2023-10-24 16:13:32 +00:00
|
|
|
#include <QThread>
|
2024-06-04 18:47:11 +00:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QVector>
|
2024-01-22 17:36:01 +00:00
|
|
|
|
2024-06-04 18:47:11 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class LLModel;
|
|
|
|
class QNetworkAccessManager;
|
2023-10-24 16:13:32 +00:00
|
|
|
|
2024-01-22 17:36:01 +00:00
|
|
|
struct EmbeddingChunk {
|
2024-06-24 22:49:23 +00:00
|
|
|
QString model; // TODO(jared): use to select model
|
2024-01-22 17:36:01 +00:00
|
|
|
int folder_id;
|
|
|
|
int chunk_id;
|
|
|
|
QString chunk;
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(EmbeddingChunk)
|
|
|
|
|
|
|
|
struct EmbeddingResult {
|
2024-06-24 22:49:23 +00:00
|
|
|
QString model;
|
2024-01-22 17:36:01 +00:00
|
|
|
int folder_id;
|
|
|
|
int chunk_id;
|
|
|
|
std::vector<float> embedding;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmbeddingLLMWorker : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
EmbeddingLLMWorker();
|
2024-06-24 22:49:23 +00:00
|
|
|
~EmbeddingLLMWorker() override;
|
2024-01-22 17:36:01 +00:00
|
|
|
|
|
|
|
void wait();
|
|
|
|
|
|
|
|
std::vector<float> lastResponse() const { return m_lastResponse; }
|
|
|
|
|
|
|
|
bool loadModel();
|
2024-06-24 22:49:23 +00:00
|
|
|
bool isNomic() const { return !m_nomicAPIKey.isEmpty(); }
|
|
|
|
bool hasModel() const { return isNomic() || m_model; }
|
2024-01-22 17:36:01 +00:00
|
|
|
|
2024-06-24 22:49:23 +00:00
|
|
|
std::vector<float> generateQueryEmbedding(const QString &text);
|
2024-01-22 17:36:01 +00:00
|
|
|
|
|
|
|
public Q_SLOTS:
|
2024-06-24 22:49:23 +00:00
|
|
|
void atlasQueryEmbeddingRequested(const QString &text);
|
|
|
|
void docEmbeddingsRequested(const QVector<EmbeddingChunk> &chunks);
|
2024-01-22 17:36:01 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
2024-06-24 22:49:23 +00:00
|
|
|
void requestAtlasQueryEmbedding(const QString &text);
|
2024-01-22 17:36:01 +00:00
|
|
|
void embeddingsGenerated(const QVector<EmbeddingResult> &embeddings);
|
2024-06-24 22:49:23 +00:00
|
|
|
void errorGenerated(const QVector<EmbeddingChunk> &chunks, const QString &error);
|
2024-01-22 17:36:01 +00:00
|
|
|
void finished();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void handleFinished();
|
|
|
|
|
|
|
|
private:
|
2024-06-24 22:49:23 +00:00
|
|
|
void sendAtlasRequest(const QStringList &texts, const QString &taskType, const QVariant &userData = {});
|
2024-03-13 22:09:24 +00:00
|
|
|
|
2024-01-22 17:36:01 +00:00
|
|
|
QString m_nomicAPIKey;
|
|
|
|
QNetworkAccessManager *m_networkManager;
|
|
|
|
std::vector<float> m_lastResponse;
|
|
|
|
LLModel *m_model = nullptr;
|
2024-05-15 14:01:53 +00:00
|
|
|
std::atomic<bool> m_stopGenerating;
|
2024-01-22 17:36:01 +00:00
|
|
|
QThread m_workerThread;
|
2024-06-24 22:49:23 +00:00
|
|
|
QMutex m_mutex; // guards m_model and m_nomicAPIKey
|
2024-01-22 17:36:01 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 16:13:32 +00:00
|
|
|
class EmbeddingLLM : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
EmbeddingLLM();
|
2024-06-24 22:49:23 +00:00
|
|
|
~EmbeddingLLM() override;
|
2023-10-24 16:13:32 +00:00
|
|
|
|
2024-06-24 22:49:23 +00:00
|
|
|
static QString model();
|
2024-01-22 17:36:01 +00:00
|
|
|
bool loadModel();
|
2023-10-24 16:13:32 +00:00
|
|
|
bool hasModel() const;
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
2024-06-24 22:49:23 +00:00
|
|
|
std::vector<float> generateQueryEmbedding(const QString &text); // synchronous
|
|
|
|
void generateDocEmbeddingsAsync(const QVector<EmbeddingChunk> &chunks);
|
2023-10-24 16:13:32 +00:00
|
|
|
|
2024-01-22 17:36:01 +00:00
|
|
|
Q_SIGNALS:
|
2024-06-24 22:49:23 +00:00
|
|
|
void requestDocEmbeddings(const QVector<EmbeddingChunk> &chunks);
|
2024-01-22 17:36:01 +00:00
|
|
|
void embeddingsGenerated(const QVector<EmbeddingResult> &embeddings);
|
2024-06-24 22:49:23 +00:00
|
|
|
void errorGenerated(const QVector<EmbeddingChunk> &chunks, const QString &error);
|
2023-10-24 16:13:32 +00:00
|
|
|
|
|
|
|
private:
|
2024-01-22 17:36:01 +00:00
|
|
|
EmbeddingLLMWorker *m_embeddingWorker;
|
2023-10-24 16:13:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // EMBLLM_H
|