prepare for selectable backends

This commit is contained in:
2023-11-02 23:12:17 +01:00
parent fc0ca71b4d
commit 8a26f9e1e4
7 changed files with 258 additions and 57 deletions

67
exllama.cpp Normal file
View File

@ -0,0 +1,67 @@
#include "exllama.h"
#include <QJsonObject>
#include <QJsonDocument>
ExLlama::ExLlama()
{
connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &ExLlama::socketMessage);
}
bool ExLlama::ready()
{
return m_webSocket.isValid();
}
void ExLlama::socketMessage(const QString& message)
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8());
QJsonValue idVal = jsonDocument[QStringLiteral("request_id")];
if(!idVal.isDouble())
{
qDebug()<<"Got invalid response on socket";
return;
}
int id = idVal.toInt();
if(!isValidId(id))
{
qDebug()<<"Got unkown response id on socket";
return;
}
QJsonValue responseValue = jsonDocument[QStringLiteral("response")];
if(!responseValue.isString())
{
qDebug()<<"Got invalid response on socket";
return;
}
feedResponse(Response(responseValue.toString(), id, true));
}
void ExLlama::generateImpl(const Request& request)
{
QJsonObject json;
json[QStringLiteral("action")] = QStringLiteral("infer");
json[QStringLiteral("request_id")] = static_cast<double>(request.getId());
json[QStringLiteral("text")] = request.getText();
json[QStringLiteral("max_new_tokens")] = 50;
json[QStringLiteral("stream")] = false;
QJsonDocument jsonDocument(json);
QString requestText = QString::fromUtf8(jsonDocument.toJson(QJsonDocument::JsonFormat::Compact));
qDebug()<<__func__<<' '<<requestText;
m_webSocket.sendTextMessage(requestText);
}
void ExLlama::open(const QUrl& url)
{
m_webSocket.close();
m_webSocket.open(url);
}
ExLlama::~ExLlama()
{
m_webSocket.disconnect();
}