File indexing completed on 2025-01-05 05:18:57

0001 // SPDX-FileCopyrightText: 2023 Loren Burkholder <computersemiexpert@outlook.com>
0002 // SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 #include "KLLMReply.h"
0007 #include "kllmcore_debug.h"
0008 
0009 #include <QNetworkReply>
0010 
0011 using namespace Qt::StringLiterals;
0012 using namespace KLLMCore;
0013 
0014 KLLMReply::KLLMReply(QNetworkReply *netReply, QObject *parent)
0015     : QObject{parent}
0016     , m_reply{netReply}
0017 {
0018     connect(m_reply, &QNetworkReply::finished, m_reply, [this] {
0019         // Normally, we could assume that the tokens will never be empty once the request finishes, but it could be possible
0020         // that the request failed and we have no tokens to parse.
0021         if (!m_tokens.empty()) {
0022             m_context.setOllamaContext(m_tokens.constLast()["context"_L1].toArray());
0023         }
0024 
0025         qCDebug(KLLMCORE_LOG) << "Ollama response finished";
0026         m_finished = true;
0027         Q_EMIT finished();
0028     });
0029     connect(m_reply, &QNetworkReply::errorOccurred, m_reply, [](QNetworkReply::NetworkError e) {
0030         qCDebug(KLLMCORE_LOG) << "Ollama HTTP error:" << e;
0031     });
0032     connect(m_reply, &QNetworkReply::downloadProgress, m_reply, [this](qint64 received, qint64 /*total*/) {
0033         m_incompleteTokens += m_reply->read(received - m_receivedSize);
0034         m_receivedSize = received;
0035 
0036         auto completeTokens = m_incompleteTokens.split('\n');
0037         if (completeTokens.size() <= 1) {
0038             return;
0039         }
0040         m_incompleteTokens = completeTokens.last();
0041         completeTokens.removeLast();
0042 
0043         m_tokens.reserve(completeTokens.count());
0044         for (const auto &tok : completeTokens) {
0045             m_tokens.append(QJsonDocument::fromJson(tok));
0046         }
0047 
0048         Q_EMIT contentAdded();
0049     });
0050 }
0051 
0052 QString KLLMReply::readResponse() const
0053 {
0054     QString ret;
0055     for (const auto &tok : m_tokens)
0056         ret += tok["response"_L1].toString();
0057     return ret;
0058 }
0059 
0060 const KLLMContext &KLLMReply::context() const
0061 {
0062     return m_context;
0063 }
0064 
0065 bool KLLMReply::isFinished() const
0066 {
0067     return m_finished;
0068 }
0069 #include "moc_KLLMReply.cpp"