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