diff --git a/gpt4all-chat/chat.cpp b/gpt4all-chat/chat.cpp index 2e95f62e..9f69500b 100644 --- a/gpt4all-chat/chat.cpp +++ b/gpt4all-chat/chat.cpp @@ -205,12 +205,13 @@ void Chat::responseStopped() { const QString chatResponse = response(); QList references; + QList referencesContext; int validReferenceNumber = 1; for (const ResultInfo &info : m_results) { if (info.file.isEmpty()) continue; if (validReferenceNumber == 1) - references.append((!chatResponse.endsWith("\n") ? "\n" : QString()) + QStringLiteral("---")); + references.append((!chatResponse.endsWith("\n") ? "\n" : QString()) + QStringLiteral("\n---")); QString reference; { QTextStream stream(&reference); @@ -230,12 +231,14 @@ void Chat::responseStopped() stream << "-" << info.to; stream << ". "; } + stream << "[Context](context://" << validReferenceNumber - 1 << ")"; } references.append(reference); + referencesContext.append(info.text); } const int index = m_chatModel->count() - 1; - m_chatModel->updateReferences(index, references.join("\n")); + m_chatModel->updateReferences(index, references.join("\n"), referencesContext); emit responseChanged(); m_results.clear(); diff --git a/gpt4all-chat/chatmodel.h b/gpt4all-chat/chatmodel.h index a5a53326..195a4887 100644 --- a/gpt4all-chat/chatmodel.h +++ b/gpt4all-chat/chatmodel.h @@ -18,6 +18,7 @@ struct ChatItem Q_PROPERTY(bool thumbsUpState MEMBER thumbsUpState) Q_PROPERTY(bool thumbsDownState MEMBER thumbsDownState) Q_PROPERTY(QString references MEMBER references) + Q_PROPERTY(QList referencesContext MEMBER referencesContext) public: int id = 0; @@ -26,6 +27,7 @@ public: QString prompt; QString newResponse; QString references; + QList referencesContext; bool currentResponse = false; bool stopped = false; bool thumbsUpState = false; @@ -51,7 +53,8 @@ public: StoppedRole, ThumbsUpStateRole, ThumbsDownStateRole, - ReferencesRole + ReferencesRole, + ReferencesContextRole }; int rowCount(const QModelIndex &parent = QModelIndex()) const override @@ -87,6 +90,8 @@ public: return item.thumbsDownState; case ReferencesRole: return item.references; + case ReferencesContextRole: + return item.referencesContext; } return QVariant(); @@ -105,6 +110,7 @@ public: roles[ThumbsUpStateRole] = "thumbsUpState"; roles[ThumbsDownStateRole] = "thumbsDownState"; roles[ReferencesRole] = "references"; + roles[ReferencesContextRole] = "referencesContext"; return roles; } @@ -181,7 +187,7 @@ public: } } - Q_INVOKABLE void updateReferences(int index, const QString &references) + Q_INVOKABLE void updateReferences(int index, const QString &references, const QList &referencesContext) { if (index < 0 || index >= m_chatItems.size()) return; @@ -190,6 +196,10 @@ public: item.references = references; emit dataChanged(createIndex(index, 0), createIndex(index, 0), {ReferencesRole}); } + if (item.referencesContext != referencesContext) { + item.referencesContext = referencesContext; + emit dataChanged(createIndex(index, 0), createIndex(index, 0), {ReferencesContextRole}); + } } Q_INVOKABLE void updateThumbsUpState(int index, bool b) @@ -240,8 +250,10 @@ public: stream << c.stopped; stream << c.thumbsUpState; stream << c.thumbsDownState; - if (version > 2) + if (version > 2) { stream << c.references; + stream << c.referencesContext; + } } return stream.status() == QDataStream::Ok; } @@ -261,8 +273,10 @@ public: stream >> c.stopped; stream >> c.thumbsUpState; stream >> c.thumbsDownState; - if (version > 2) + if (version > 2) { stream >> c.references; + stream >> c.referencesContext; + } beginInsertRows(QModelIndex(), m_chatItems.size(), m_chatItems.size()); m_chatItems.append(c); endInsertRows(); diff --git a/gpt4all-chat/main.qml b/gpt4all-chat/main.qml index f01b540a..c693fe57 100644 --- a/gpt4all-chat/main.qml +++ b/gpt4all-chat/main.qml @@ -555,6 +555,14 @@ Window { } } + PopupDialog { + id: referenceContextDialog + anchors.centerIn: parent + shouldTimeOut: false + shouldShowBusy: false + modal: true + } + Rectangle { id: conversation color: theme.backgroundLight @@ -600,6 +608,7 @@ Window { width: listView.width color: theme.textColor wrapMode: Text.WordWrap + textFormat: TextEdit.MarkdownText focus: false readOnly: true font.pixelSize: theme.fontSizeLarge @@ -621,6 +630,17 @@ Window { leftPadding: 100 rightPadding: 100 + onLinkActivated: function (link) { + if (!link.startsWith("context://")) + return; + + console.log("link " + link); + var integer = parseInt(link.split("://")[1]); + console.log("context is" + referencesContext[integer - 1]); + referenceContextDialog.text = referencesContext[integer - 1]; + referenceContextDialog.open(); + } + Item { anchors.left: parent.left anchors.leftMargin: 90 diff --git a/gpt4all-chat/qml/PopupDialog.qml b/gpt4all-chat/qml/PopupDialog.qml index dfd80d54..5ed0ea22 100644 --- a/gpt4all-chat/qml/PopupDialog.qml +++ b/gpt4all-chat/qml/PopupDialog.qml @@ -27,8 +27,12 @@ Dialog { Text { id: textField - anchors.verticalCenter: busyIndicator.verticalCenter - horizontalAlignment: Text.AlignJustify + width: Math.min(1024, implicitWidth) + height: Math.min(600, implicitHeight) + anchors.verticalCenter: shouldShowBusy ? busyIndicator.verticalCenter : parent.verticalCenter + horizontalAlignment: Text.AlignLeft + verticalAlignment: Text.AlignVCenter + wrapMode: Text.WordWrap color: theme.textColor Accessible.role: Accessible.HelpBalloon Accessible.name: text