KTextAddons

ollamareply.h
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#pragma once
8
9#include <QJsonDocument>
10#include <TextAutogenerateText/TextAutogenerateTextContext>
11
12class QNetworkReply;
13/**
14 * @brief The OllamaReplyInfo class represents information about a reply from an LLM.
15 *
16 * When an LLM generates a completion, the server generally will return some information about the completion, including the
17 * duration of the completion, the number of tokens received, and the duration of the prompt evaluation. This struct encapsulates such information.
18 * If any one of these fields is not available, it will be set to its default value.
19 */
21 //! The total time from when the request was received by the server to when the reply was returned.
22 std::chrono::nanoseconds totalDuration;
23
24 //! The time spent loading the model.
25 std::chrono::nanoseconds loadDuration;
26
27 //! The number of tokens in the prompt.
29
30 //! The time spent evaluating the prompt.
31 std::chrono::nanoseconds promptEvalDuration;
32
33 //! The number of tokens in the reply.
35
36 //! The time spent generating the reply.
37 std::chrono::nanoseconds duration;
38};
39
40/**
41 * @brief The OllamaReply class represents a reply from an LLM.
42 *
43 * If you want to stream a reply as it is written in real time, connect to contentAdded() and use readResponse() to retrieve
44 * the new content. If you prefer to wait for the entire reply before displaying anything, connect to finished(), which will
45 * only be emitted once the reply is complete.
46 */
47class OllamaReply : public QObject
48{
50
51public:
52 /**
53 * @brief Get the current response content.
54 *
55 * This function returns what it has recieved of the response so far. Therefore, until finished() is emitted, this
56 * function may return different values. However, once finished() is emitted, the content is guaranteed to remain
57 * constant.
58 *
59 * @return The content that has been returned so far.
60 */
61 [[nodiscard]] QString readResponse() const;
62
63 /**
64 * @brief Get the context token for this response.
65 *
66 * Messages sent by most LLMs have a context identifier that allows you to chain messages into a conversation. To create
67 * such a conversation, you need to take this context object and set it on the next KLLMRequest in the conversation.
68 * KLLMInterface::getCompletion() will use that context object to continue the message thread.
69 *
70 * @return A context object that refers to this response.
71 */
72 const TextAutogenerateText::TextAutogenerateTextContext &context() const;
73
74 /**
75 * @brief Get extra information about the reply.
76 *
77 * This function returns a KLLMReplyInfo object containing information about this reply. If the reply has not finished, the KLLMReplyInfo object will have
78 * all members set to their default values.
79 *
80 * @return Extra information about the reply.
81 */
82 const OllamaReplyInfo &info() const;
83
84 /**
85 * @brief Check whether the reply has finished.
86 *
87 * If you need to know if the response has finished changing or if the context has been received yet, call this function.
88 *
89 * @return Whether the reply has finished.
90 */
91 [[nodiscard]] bool isFinished() const;
92
93 explicit OllamaReply(QNetworkReply *netReply, QObject *parent = nullptr);
94
96 /**
97 * @brief Emits when new content has been added to the response.
98 *
99 * If you are not streaming the response live, this signal is not of importance to you. However, if you are streaming
100 * content, when this signal is emitted, you should call readResponse() to update the response that your application
101 * shows.
102 */
104
105 /**
106 * @brief Emits when the LLM has finished returning its response.
107 *
108 * After this signal has emitted, the content is guaranteed to not change. At this point, you should call readResponse()
109 * to get the content and then either take ownership of the KLLMReply or delete it, as automatic reply deletion is not
110 * implemented yet.
111 */
112 void finished();
113
114private:
115 QNetworkReply *const mReply;
116 QByteArray mIncompleteTokens;
117
118 QList<QJsonDocument> mTokens;
119
120 TextAutogenerateText::TextAutogenerateTextContext mContext;
121 OllamaReplyInfo mInfo;
122
123 int mReceivedSize = 0;
124 bool mFinished = false;
125};
const TextAutogenerateText::TextAutogenerateTextContext & context() const
Get the context token for this response.
void finished()
Emits when the LLM has finished returning its 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.
void contentAdded()
Emits when new content has been added to the response.
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
The OllamaReplyInfo class represents information about a reply from an LLM.
Definition ollamareply.h:20
std::chrono::nanoseconds duration
The time spent generating the reply.
Definition ollamareply.h:37
std::chrono::nanoseconds totalDuration
The total time from when the request was received by the server to when the reply was returned.
Definition ollamareply.h:22
int tokenCount
The number of tokens in the reply.
Definition ollamareply.h:34
std::chrono::nanoseconds promptEvalDuration
The time spent evaluating the prompt.
Definition ollamareply.h:31
int promptEvalTokenCount
The number of tokens in the prompt.
Definition ollamareply.h:28
std::chrono::nanoseconds loadDuration
The time spent loading the model.
Definition ollamareply.h:25
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.