mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2024-10-01 01:06:10 -04:00
01f67c74ea
Signed-off-by: Adam Treat <treat.adam@gmail.com>
51 lines
1.7 KiB
C++
51 lines
1.7 KiB
C++
#include "localdocssearch.h"
|
|
#include "database.h"
|
|
#include "localdocs.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QDebug>
|
|
#include <QGuiApplication>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QThread>
|
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
QString LocalDocsSearch::run(const QJsonObject ¶meters, qint64 timeout)
|
|
{
|
|
QList<QString> collections;
|
|
QJsonArray collectionsArray = parameters["collections"].toArray();
|
|
for (int i = 0; i < collectionsArray.size(); ++i)
|
|
collections.append(collectionsArray[i].toString());
|
|
const QString text = parameters["text"].toString();
|
|
const int count = parameters["count"].toInt();
|
|
QThread workerThread;
|
|
LocalDocsWorker worker;
|
|
worker.moveToThread(&workerThread);
|
|
connect(&worker, &LocalDocsWorker::finished, &workerThread, &QThread::quit, Qt::DirectConnection);
|
|
connect(&workerThread, &QThread::started, [&worker, collections, text, count]() {
|
|
worker.request(collections, text, count);
|
|
});
|
|
workerThread.start();
|
|
workerThread.wait(timeout);
|
|
workerThread.quit();
|
|
workerThread.wait();
|
|
return worker.response();
|
|
}
|
|
|
|
LocalDocsWorker::LocalDocsWorker()
|
|
: QObject(nullptr)
|
|
{
|
|
// The following are blocking operations and will block the calling thread
|
|
connect(this, &LocalDocsWorker::requestRetrieveFromDB, LocalDocs::globalInstance()->database(),
|
|
&Database::retrieveFromDB, Qt::BlockingQueuedConnection);
|
|
}
|
|
|
|
void LocalDocsWorker::request(const QList<QString> &collections, const QString &text, int count)
|
|
{
|
|
QString jsonResult;
|
|
emit requestRetrieveFromDB(collections, text, count, jsonResult); // blocks
|
|
m_response = jsonResult;
|
|
emit finished();
|
|
}
|