This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
llm.cpp
194 lines (161 loc) · 4.78 KB
/
llm.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "llm.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QResource>
#include <fstream>
class MyLLM: public LLM { };
Q_GLOBAL_STATIC(MyLLM, llmInstance)
LLM *LLM::globalInstance()
{
return llmInstance();
}
static LLModel::PromptContext s_ctx;
LLMObject::LLMObject()
: QObject{nullptr}
, m_llmodel(new GPTJ)
{
moveToThread(&m_llmThread);
connect(&m_llmThread, &QThread::started, this, &LLMObject::loadModel);
m_llmThread.setObjectName("llm thread");
m_llmThread.start();
}
bool LLMObject::loadModel()
{
if (isModelLoaded())
return true;
QDir dir(QCoreApplication::applicationDirPath());
dir.setNameFilters(QStringList() << "ggml-*.bin");
QStringList fileNames = dir.entryList();
if (fileNames.isEmpty()) {
qDebug() << "ERROR: Could not find any applicable models in directory"
<< QCoreApplication::applicationDirPath();
}
QString modelName = fileNames.first();
QString filePath = QCoreApplication::applicationDirPath() + QDir::separator() + modelName;
QFileInfo info(filePath);
if (info.exists()) {
auto fin = std::ifstream(filePath.toStdString(), std::ios::binary);
m_llmodel->loadModel(modelName.toStdString(), fin);
emit isModelLoadedChanged();
}
if (m_llmodel) {
m_modelName = info.baseName().remove(0, 5); // remove the ggml- prefix
emit modelNameChanged();
}
return m_llmodel;
}
bool LLMObject::isModelLoaded() const
{
return m_llmodel->isModelLoaded();
}
void LLMObject::resetResponse()
{
m_response = std::string();
emit responseChanged();
}
void LLMObject::resetContext()
{
s_ctx = LLModel::PromptContext();
}
QString LLMObject::response() const
{
return QString::fromStdString(m_response);
}
QString LLMObject::modelName() const
{
return m_modelName;
}
bool LLMObject::handleResponse(const std::string &response)
{
#if 0
printf("%s", response.c_str());
fflush(stdout);
#endif
if (!response.empty()) {
m_response.append(response);
emit responseChanged();
}
return !m_stopGenerating;
}
bool LLMObject::prompt(const QString &prompt)
{
if (!isModelLoaded())
return false;
m_stopGenerating = false;
auto func = std::bind(&LLMObject::handleResponse, this, std::placeholders::_1);
emit responseStarted();
m_llmodel->prompt(prompt.toStdString(), func, s_ctx, 4096 /*number of chars to predict*/);
emit responseStopped();
return true;
}
LLM::LLM()
: QObject{nullptr}
, m_llmodel(new LLMObject)
, m_responseInProgress(false)
{
connect(m_llmodel, &LLMObject::isModelLoadedChanged, this, &LLM::isModelLoadedChanged, Qt::QueuedConnection);
connect(m_llmodel, &LLMObject::responseChanged, this, &LLM::responseChanged, Qt::QueuedConnection);
connect(m_llmodel, &LLMObject::responseStarted, this, &LLM::responseStarted, Qt::QueuedConnection);
connect(m_llmodel, &LLMObject::responseStopped, this, &LLM::responseStopped, Qt::QueuedConnection);
connect(m_llmodel, &LLMObject::modelNameChanged, this, &LLM::modelNameChanged, Qt::QueuedConnection);
connect(this, &LLM::promptRequested, m_llmodel, &LLMObject::prompt, Qt::QueuedConnection);
connect(this, &LLM::resetResponseRequested, m_llmodel, &LLMObject::resetResponse, Qt::BlockingQueuedConnection);
connect(this, &LLM::resetContextRequested, m_llmodel, &LLMObject::resetContext, Qt::BlockingQueuedConnection);
}
bool LLM::isModelLoaded() const
{
return m_llmodel->isModelLoaded();
}
void LLM::prompt(const QString &prompt)
{
emit promptRequested(prompt);
}
void LLM::resetResponse()
{
emit resetResponseRequested(); // blocking queued connection
}
void LLM::resetContext()
{
emit resetContextRequested(); // blocking queued connection
}
void LLM::stopGenerating()
{
m_llmodel->stopGenerating();
}
QString LLM::response() const
{
return m_llmodel->response();
}
void LLM::responseStarted()
{
m_responseInProgress = true;
emit responseInProgressChanged();
}
void LLM::responseStopped()
{
m_responseInProgress = false;
emit responseInProgressChanged();
}
QString LLM::modelName() const
{
return m_llmodel->modelName();
}
bool LLM::checkForUpdates() const
{
#if defined(Q_OS_LINUX)
QString tool("maintenancetool");
#elif defined(Q_OS_WINDOWS)
QString tool("maintenancetool.exe");
#elif defined(Q_OS_DARWIN)
QString tool("../../../maintenancetool.app/Contents/MacOS/maintenancetool");
#endif
QString fileName = QCoreApplication::applicationDirPath()
+ QDir::separator() + ".." + QDir::separator() + tool;
if (!QFileInfo::exists(fileName)) {
qDebug() << "Couldn't find tool at" << fileName << "so cannot check for updates!";
return false;
}
return QProcess::startDetached(fileName);
}