KTextAddons

ollamareply.cpp
1// SPDX-FileCopyrightText: 2023 Loren Burkholder <computersemiexpert@outlook.com>
2// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3// SPDX-FileCopyrightText: SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
4// Based on Alpaka code
5// SPDX-License-Identifier: GPL-2.0-or-later
6
7#include "ollamareply.h"
8#include "autogeneratetext_ollama_debug.h"
9#include <QJsonArray>
10#include <QNetworkReply>
11
12using namespace Qt::StringLiterals;
13
14OllamaReply::OllamaReply(QNetworkReply *netReply, QObject *parent)
15 : QObject{parent}
16 , mReply{netReply}
17{
18 connect(mReply, &QNetworkReply::finished, mReply, [this] {
19 // Normally, we could assume that the tokens will never be empty once the request finishes, but it could be possible
20 // that the request failed and we have no tokens to parse.
21 if (!mTokens.empty()) {
22 const auto finalResponse = mTokens.constLast();
23 mContext.setContextData(finalResponse["context"_L1].toArray());
24 mInfo.totalDuration = std::chrono::nanoseconds{finalResponse["total_duration"_L1].toVariant().toULongLong()};
25 mInfo.loadDuration = std::chrono::nanoseconds{finalResponse["load_duration"_L1].toVariant().toULongLong()};
26 mInfo.promptEvalTokenCount = finalResponse["prompt_eval_count"_L1].toVariant().toULongLong();
27 mInfo.promptEvalDuration = std::chrono::nanoseconds{finalResponse["prompt_eval_duration"_L1].toVariant().toULongLong()};
28 mInfo.tokenCount = finalResponse["eval_count"_L1].toVariant().toULongLong();
29 mInfo.duration = std::chrono::nanoseconds{finalResponse["eval_duration"_L1].toVariant().toULongLong()};
30 }
31
32 qCDebug(AUTOGENERATETEXT_OLLAMA_LOG) << "Ollama response finished";
33 mFinished = true;
34 Q_EMIT finished();
35 });
37 qCDebug(AUTOGENERATETEXT_OLLAMA_LOG) << "Ollama HTTP error:" << e;
38 });
39 connect(mReply, &QNetworkReply::downloadProgress, mReply, [this](qint64 received, qint64 /*total*/) {
40 mIncompleteTokens += mReply->read(received - mReceivedSize);
41 mReceivedSize = received;
42
43 auto completeTokens = mIncompleteTokens.split('\n');
44 if (completeTokens.size() <= 1) {
45 return;
46 }
47 mIncompleteTokens = completeTokens.last();
48 completeTokens.removeLast();
49
50 mTokens.reserve(completeTokens.count());
51 for (const auto &tok : std::as_const(completeTokens)) {
52 mTokens.append(QJsonDocument::fromJson(tok));
53 }
54
55 Q_EMIT contentAdded();
56 });
57}
58
60{
61 QString ret;
62 for (const auto &tok : mTokens) {
63 ret += tok["response"_L1].toString();
64 }
65 return ret;
66}
67
68const TextAutogenerateText::TextAutogenerateTextContext &OllamaReply::context() const
69{
70 return mContext;
71}
72
74{
75 return mInfo;
76}
77
79{
80 return mFinished;
81}
82
83#include "moc_ollamareply.cpp"
const TextAutogenerateText::TextAutogenerateTextContext & context() const
Get the context token for this response.
QString readResponse() const
Get the current response content.
bool isFinished() const
Check whether the reply has finished.
const OllamaReplyInfo & info() const
Get extra information about the reply.
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void errorOccurred(QNetworkReply::NetworkError code)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
The OllamaReplyInfo class represents information about a reply from an LLM.
Definition ollamareply.h:20
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:00:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.