-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathollamaclient.cpp
More file actions
137 lines (111 loc) · 3.63 KB
/
ollamaclient.cpp
File metadata and controls
137 lines (111 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "ollamaclient.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QNetworkReply>
OllamaClient::OllamaClient(QObject* parent) : QObject{ parent } {
networkManager = new QNetworkAccessManager(this);
}
void OllamaClient::resetConversation() {
conversationHistory.clear();
}
void OllamaClient::sendMessage(const QString &text) {
if (text.isEmpty()) {
return;
}
if (modelName.isEmpty()) {
emit textReceived("[Model not selected]");
return;
}
ChatMessage userMsg;
userMsg.role = "user";
userMsg.content = text;
conversationHistory.append(userMsg);
QJsonArray messagesArray;
for (const auto&[role, content] : conversationHistory) {
QJsonObject msgObject;
msgObject["role"] = role;
msgObject["content"] = content;
messagesArray.append(msgObject);
}
// Build request object
QJsonObject root;
root["model"] = modelName;
root["messages"] = messagesArray;
root["stream"] = true;
const QJsonDocument doc(root);
const QByteArray data = doc.toJson();
// HTTP request config
QNetworkRequest request((QUrl(apiUrl)));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
// Send request
currentReply = networkManager->post(request, data);
// Clear buffer
generatedBuffer.clear();
// Signals connections
connect(currentReply, &QNetworkReply::readyRead, this, &OllamaClient::onReadyRead);
connect(currentReply, &QNetworkReply::errorOccurred, this, [this](const QNetworkReply::NetworkError code) {
qDebug() << "Network error: " << code << currentReply->errorString();
emit textReceived("\n[Cannot connect to ollama, check whether it is running]");
currentReply->deleteLater();
currentReply = nullptr;
});
}
void OllamaClient::onReadyRead() {
while (currentReply && currentReply->canReadLine()) {
QByteArray line = currentReply->readLine();
auto doc = QJsonDocument::fromJson(line);
QJsonObject obj = doc.object();
if (obj["done"].toBool()) {
ChatMessage assistantMsg;
assistantMsg.role = "assistant";
assistantMsg.content = generatedBuffer;
conversationHistory.append(assistantMsg);
emit replyFinished();
currentReply->deleteLater();
currentReply = nullptr;
return;
}
if (obj.contains("message")) {
if (QJsonObject messageObj = obj["message"].toObject(); messageObj.contains("content")) {
if (QString token = messageObj["content"].toString(); !token.isEmpty()) {
emit textReceived(token);
generatedBuffer += token;
}
}
}
}
}
void OllamaClient::setModelName(const QString &name) {
if (const auto newName = name.trimmed(); newName != modelName) {
modelName = newName;
}
}
QString OllamaClient::getModelName() {
return modelName;
}
void OllamaClient::fetchModels() {
const QNetworkRequest request(modelsUrl);
QNetworkReply *reply = networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
const QByteArray data = reply->readAll();
const QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject root = doc.object();
QJsonArray modelsArray = root["models"].toArray();
QStringList modelNames;
for (const auto &value : modelsArray) {
QJsonObject obj = value.toObject();
modelNames.append(obj["name"].toString());
}
emit modelsReceived(modelNames);
} else if (reply->error() == QNetworkReply::ConnectionRefusedError) {
this->emit errorOccurs("Connection refused: Check whether Ollama is installed and running");
} else {
this->emit errorOccurs(reply->errorString());
qDebug() << "Error fetching models:" << reply->errorString();
qDebug() << reply->error();
}
reply->deleteLater();
});
}