-
Notifications
You must be signed in to change notification settings - Fork 8
/
openai_message_model.cpp
151 lines (133 loc) · 4.14 KB
/
openai_message_model.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
#include "openai_message_model.h"
OpenAIMessageModel::OpenAIMessageModel(QObject *parent)
: QAbstractListModel(parent)
{
connect(this, &OpenAIMessageModel::messagesChanged, this, &OpenAIMessageModel::jsonStringChanged);
}
OpenAIMessageModel::~OpenAIMessageModel()
{
}
int OpenAIMessageModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_messages.count();
}
QVariant OpenAIMessageModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
OpenAIMessage *message = m_messages.at(index.row());
switch (role) {
case MessageRole:
return QVariant::fromValue(message);
case RoleRole:
return QVariant::fromValue(message->role());
default:
return QVariant();
}
}
QHash<int, QByteArray> OpenAIMessageModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[MessageRole] = "message";
roles[RoleRole] = "role";
return roles;
}
void OpenAIMessageModel::addMessage(OpenAIMessage *message)
{
beginInsertRows(QModelIndex(), m_messages.count(), m_messages.count());
m_messages.append(message);
endInsertRows();
emit messagesChanged();
}
void OpenAIMessageModel::addMessage(OpenAIMessage::Role role, const QString &content)
{
OpenAIMessage *message = new OpenAIMessage(content, role);
addMessage(message);
}
void OpenAIMessageModel::removeMessage(OpenAIMessage *message)
{
int pos = m_messages.indexOf(message);
if (pos != -1) {
beginRemoveRows(QModelIndex(), pos, pos);
m_messages.removeAt(pos);
endRemoveRows();
emit messagesChanged();
message->deleteLater();
}
}
void OpenAIMessageModel::clearMessages()
{
beginResetModel();
for (OpenAIMessage *message : m_messages) {
message->deleteLater();
}
m_messages.clear();
endResetModel();
emit messagesChanged();
}
QList<OpenAIMessage *> OpenAIMessageModel::messagesFromJson(const QString &jsonString) const
{
QList<OpenAIMessage *> messages;
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8());
if (jsonDocument.isNull()) {
qWarning() << "Failed to read JSON document";
return messages;
}
// read "messages" from json object
QJsonObject jsonObject = jsonDocument.object();
if (!jsonObject.contains("messages")) {
qWarning() << "JSON document does not contain 'messages' key";
return messages;
}
QJsonArray jsonArray = jsonObject["messages"].toArray();
for (const QJsonValue &jsonValue : jsonArray) {
QJsonObject messageObject = jsonValue.toObject();
if (!messageObject.contains("role") || !messageObject.contains("content")) {
qWarning() << "JSON document does not contain 'role' or 'content' key";
continue;
}
OpenAIMessage *message = new OpenAIMessage(
messageObject["content"].toString(),
OpenAIMessage::roleFromString(messageObject["role"].toString())
);
messages.append(message);
}
return messages;
}
QString OpenAIMessageModel::jsonFromMessages(const QList<OpenAIMessage*> &messages) const
{
QJsonArray jsonArray;
for (OpenAIMessage *message : messages) {
QJsonObject messageObject;
messageObject["role"] = OpenAIMessage::roleToString(message->role());
messageObject["content"] = message->content();
jsonArray.append(messageObject);
}
QJsonObject jsonObject;
jsonObject["messages"] = jsonArray;
QJsonDocument jsonDocument(jsonObject);
return jsonDocument.toJson(QJsonDocument::Indented);
}
QList<OpenAIMessage *> OpenAIMessageModel::messages() const
{
return m_messages;
}
void OpenAIMessageModel::setMessages(const QList<OpenAIMessage *> &newMessages)
{
if (m_messages == newMessages)
return;
beginResetModel();
m_messages = newMessages;
endResetModel();
emit messagesChanged();
}
QString OpenAIMessageModel::jsonString() const
{
return jsonFromMessages(m_messages);
}
void OpenAIMessageModel::setJsonString(const QString &newJsonString)
{
setMessages(messagesFromJson(newJsonString));
}