gpt4all/main.qml

264 lines
8.6 KiB
QML
Raw Normal View History

2023-04-09 03:28:39 +00:00
import QtQuick
import QtQuick.Controls
2023-04-10 19:03:00 +00:00
import QtQuick.Controls.Basic
2023-04-09 03:28:39 +00:00
import llm
Window {
id: window
width: 1280
height: 720
visible: true
title: qsTr("GPT4All Chat")
// Rectangle {
// id: conversationList
// width: 300
// anchors.left: parent.left
// anchors.top: parent.top
// anchors.bottom: parent.bottom
// color: "#202123"
2023-04-09 03:28:39 +00:00
// Button {
// id: newChat
// text: qsTr("New chat")
// anchors.top: parent.top
// anchors.left: parent.left
// anchors.right: parent.right
// anchors.margins: 15
// padding: 15
// background: Rectangle {
// opacity: .5
// border.color: "#7d7d8e"
// border.width: 1
// radius: 10
// color: "#343541"
// }
// }
// }
2023-04-09 03:28:39 +00:00
Rectangle {
id: conversation
color: "#343541"
// anchors.left: conversationList.right
anchors.left: parent.left
2023-04-09 03:28:39 +00:00
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.top: parent.top
ScrollView {
id: scrollView
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: textInput.top
anchors.bottomMargin: 30
ScrollBar.vertical.policy: ScrollBar.AlwaysOn
ListModel {
id: chatModel
}
Rectangle {
anchors.fill: parent
color: "#444654"
ListView {
id: listView
anchors.fill: parent
header: TextField {
id: modelName
width: parent.width
color: "#d1d5db"
padding: 20
font.pixelSize: 24
2023-04-09 11:38:25 +00:00
text: "Model: GPT4ALL-J-6B-4bit"
2023-04-09 03:28:39 +00:00
background: Rectangle {
color: "#444654"
}
focus: false
horizontalAlignment: TextInput.AlignHCenter
}
model: chatModel
delegate: TextArea {
text: currentResponse ? LLM.response : value
width: listView.width
2023-04-09 03:28:39 +00:00
color: "#d1d5db"
wrapMode: Text.WordWrap
focus: false
padding: 20
font.pixelSize: 24
cursorVisible: currentResponse ? LLM.responseInProgress : false
cursorPosition: text.length
background: Rectangle {
color: name === qsTr("Response: ") ? "#444654" : "#343541"
}
leftPadding: 100
Rectangle {
anchors.left: parent.left
anchors.top: parent.top
anchors.leftMargin: 20
anchors.topMargin: 20
width: 30
height: 30
radius: 5
color: name === qsTr("Response: ") ? "#10a37f" : "#ec86bf"
Text {
anchors.centerIn: parent
text: name === qsTr("Response: ") ? "R" : "P"
color: "white"
}
}
}
property bool shouldAutoScroll: true
property bool isAutoScrolling: false
Connections {
target: LLM
function onResponseChanged() {
if (listView.shouldAutoScroll) {
listView.isAutoScrolling = true
listView.positionViewAtEnd()
listView.isAutoScrolling = false
}
}
}
onContentYChanged: {
if (!isAutoScrolling)
shouldAutoScroll = atYEnd
}
Component.onCompleted: {
shouldAutoScroll = true
positionViewAtEnd()
}
footer: Item {
id: bottomPadding
width: parent.width
height: 60
}
}
}
}
Button {
Image {
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 15
source: LLM.responseInProgress ? "qrc:/gpt4all-chat/icons/stop_generating.svg" : "qrc:/gpt4all-chat/icons/regenerate.svg"
}
2023-04-09 11:38:25 +00:00
leftPadding: 50
text: LLM.responseInProgress ? qsTr("Stop generating") : qsTr("Regenerate response")
2023-04-09 03:28:39 +00:00
onClicked: {
if (LLM.responseInProgress)
LLM.stopGenerating()
else {
LLM.resetResponse()
if (chatModel.count) {
var listElement = chatModel.get(chatModel.count - 1)
if (listElement.name === qsTr("Response: ")) {
listElement.currentResponse = true
listElement.value = LLM.response
LLM.prompt(listElement.prompt)
}
}
}
}
anchors.bottom: textInput.top
anchors.horizontalCenter: textInput.horizontalCenter
anchors.bottomMargin: 40
padding: 15
background: Rectangle {
opacity: .5
border.color: "#7d7d8e"
border.width: 1
radius: 10
color: "#343541"
}
}
TextField {
id: textInput
anchors.left: parent.left
anchors.right: resetContextButton.left
2023-04-09 03:28:39 +00:00
anchors.bottom: parent.bottom
anchors.margins: 30
color: "#dadadc"
padding: 20
font.pixelSize: 24
placeholderText: qsTr("Send a message...")
placeholderTextColor: "#7d7d8e"
background: Rectangle {
color: "#40414f"
radius: 10
}
onAccepted: {
LLM.stopGenerating()
if (chatModel.count) {
var listElement = chatModel.get(chatModel.count - 1)
listElement.currentResponse = false
listElement.value = LLM.response
}
var prompt = textInput.text + "\n"
2023-04-09 03:28:39 +00:00
chatModel.append({"name": qsTr("Prompt: "), "currentResponse": false, "value": textInput.text})
chatModel.append({"name": qsTr("Response: "), "currentResponse": true, "value": "", "prompt": prompt})
2023-04-09 03:28:39 +00:00
2023-04-09 17:39:28 +00:00
// var contextPrompt;
// for (var i = 0; i < chatModel.count; ++i)
// contextPrompt += chatModel.get(i).value + "\n";
// prompt = contextPrompt + textInput.text + "\n"
2023-04-09 03:28:39 +00:00
LLM.resetResponse()
LLM.prompt(prompt)
2023-04-09 03:28:39 +00:00
textInput.text = ""
}
Button {
anchors.right: textInput.right
anchors.verticalCenter: textInput.verticalCenter
anchors.rightMargin: 15
width: 30
height: 30
background: Image {
anchors.centerIn: parent
source: "qrc:/gpt4all-chat/icons/send_message.svg"
}
onClicked: {
2023-04-10 19:03:00 +00:00
textInput.accepted()
2023-04-09 03:28:39 +00:00
}
}
}
Button {
id: resetContextButton
anchors.right: parent.right
anchors.rightMargin: 30
anchors.verticalCenter: textInput.verticalCenter
width: 40
height: 40
background: Image {
anchors.fill: parent
source: "qrc:/gpt4all-chat/icons/regenerate.svg"
}
onClicked: {
LLM.stopGenerating()
LLM.resetContext()
chatModel.clear()
}
}
2023-04-09 03:28:39 +00:00
}
}