KTextAddons

ollamaplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "ollamaplugin.h"
8#include "autogeneratetext_ollama_debug.h"
9#include "core/textautogeneratechatmodel.h"
10#include "core/textautogeneratemanager.h"
11#include "ollamamanager.h"
12#include "ollamasettings.h"
13
14OllamaPlugin::OllamaPlugin(QObject *parent)
15 : TextAutogenerateText::TextAutogenerateTextPlugin{parent}
16{
17 if (!loadSettings()) {
18 qCWarning(AUTOGENERATETEXT_OLLAMA_LOG) << "Impossible to load settings";
19 return;
20 }
21
22 connect(OllamaManager::self(), &OllamaManager::modelsLoadDone, this, [this](const OllamaManager::ModelsInfo &modelinfo) {
23 if (modelinfo.hasError) {
24 setReady(false);
25 Q_EMIT errorOccurred(modelinfo.errorOccured);
26 } else {
27 setReady(true);
28 }
29 });
30 OllamaManager::self()->loadModels();
31}
32
33OllamaPlugin::~OllamaPlugin() = default;
34
35bool OllamaPlugin::loadSettings()
36{
37 setCurrentModel(OllamaSettings::model());
38 // TODO verify that server is ok.
39 return true;
40}
41
42void OllamaPlugin::clear()
43{
44 for (const auto &connection : std::as_const(mConnections)) {
45 disconnect(connection.second);
46 }
47 mConnections.clear();
48 // TODO clear all thread
49}
50
51void OllamaPlugin::setPrompt(const QString &text)
52{
53 // TODO
54}
55
56QString OllamaPlugin::currentModel() const
57{
58 return mCurrentModel;
59}
60
61void OllamaPlugin::setCurrentModel(const QString &newCurrentModel)
62{
63 mCurrentModel = newCurrentModel;
64}
65
66void OllamaPlugin::cancelRequest(const QByteArray &uuid)
67{
68 if (uuid.isEmpty()) {
69 clear();
70 } else {
71 for (const auto &connection : std::as_const(mConnections)) {
72 if (connection.first == uuid) {
73 disconnect(connection.second);
74 // mConnections.take(connection.);
75 }
76 }
77 // TODO
78 }
79}
80
81void OllamaPlugin::sendToLLM(const QString &message, const QByteArray &uuid)
82{
83 OllamaRequest req;
84 req.setMessage(message);
85 req.setModel(mCurrentModel);
86 /*
87 for (const auto &msg : m_messages | std::views::reverse) {
88 if (msg.sender == Sender::LLM) {
89 req.setContext(message.context);
90 break;
91 }
92 }
93 */
94 auto reply = OllamaManager::self()->getCompletion(req);
95
96 mConnections.insert(reply,
97 QPair<QByteArray, QMetaObject::Connection>(
98 uuid,
99 connect(reply, &OllamaReply::contentAdded, this, [reply, uuid]() {
100 TextAutogenerateText::TextAutogenerateManager::self()->textAutoGenerateChatModel()->replaceContent(uuid, reply->readResponse());
101 })));
102 mConnections.insert(reply,
103 QPair<QByteArray, QMetaObject::Connection>(
104 uuid,
105 connect(reply, &OllamaReply::finished, this, [reply, uuid, this] {
106 TextAutogenerateText::TextAutogenerateManager::self()->textAutoGenerateChatModel()->changeInProgress(uuid, false);
107 mConnections.remove(reply);
108 reply->deleteLater();
109#if 0
110 message.context = message.llmReply->context();
111 message.info = message.llmReply->info();
112#endif
113 // Q_EMIT finished(message); // TODO add message as argument ???
114 })));
115}
116
117#include "moc_ollamaplugin.cpp"
void finished()
Emits when the LLM has finished returning its response.
void contentAdded()
Emits when new content has been added to the response.
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:06:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.