--- a/OllamaInterface/OllamaChatWidget.py Fri Aug 30 12:04:28 2024 +0200 +++ b/OllamaInterface/OllamaChatWidget.py Fri Aug 30 15:22:47 2024 +0200 @@ -8,7 +8,7 @@ """ from PyQt6.QtCore import Qt, QTimer, pyqtSlot -from PyQt6.QtWidgets import QVBoxLayout, QWidget +from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QWidget from .OllamaChatMessageBox import OllamaChatMessageBox from .Ui_OllamaChatWidget import Ui_OllamaChatWidget @@ -42,11 +42,14 @@ ) self.__messagesLayout = QVBoxLayout() - self.__messagesLayout.setContentsMargins(4, 4, 4, 4) + if parent is None: + self.__messagesLayout.setContentsMargins(4, 4, 4, 4) + else: + self.__messagesLayout.setContentsMargins(0, 0, 0, 0) self.__messagesLayout.setAlignment(Qt.AlignmentFlag.AlignTop) self.chatMessagesWidget.setLayout(self.__messagesLayout) - def addMessage(self, role, message): + def addMessage(self, role, message, scrollToBottom=True): """ Public method to add a new message. @@ -54,11 +57,15 @@ @type str @param message message text @type str + @param scrollToBottom flag indicating to scroll the view to the bottom + (defaults to True) + @type bool (optional) """ msgWidget = OllamaChatMessageBox(role=role, message=message) self.__messagesLayout.addWidget(msgWidget) - QTimer.singleShot(0, self.__scrollChatToBottom) + if scrollToBottom: + QTimer.singleShot(100, self.__scrollChatToBottom) def appendMessage(self, message): """ @@ -102,3 +109,73 @@ self.__messagesLayout.count() - 1 ).widget() return msgBox.getMessage() + + def clear(self): + """ + Public method to clear the list of messages. + """ + while not self.__messagesLayout.isEmpty(): + itm = self.__messagesLayout.takeAt(0) + itm.widget().deleteLater() + + scrollbar = self.chatMessagesScrollArea.verticalScrollBar() + scrollbar.setValue(0) + + +class OllamaChatDialog(QDialog): + """ + Class implementing a dialog showing the chat with the 'ollama' server. + """ + + def __init__(self, hid, title, model, parent=None): + """ + Constructor + + @param hid ID of the chat history + @type str + @param title title of the chat + @type str + @param model model name used for the chat + @type str + @param parent reference to the parent widget (defaults to None) + @type QWidget (optional) + """ + super().__init__(parent) + + self.__layout = QVBoxLayout(self) + self.__chatWidget = OllamaChatWidget( + hid=hid, title=title, model=model, parent=self + ) + self.__layout.addWidget(self.__chatWidget) + self.__buttonBox = QDialogButtonBox( + QDialogButtonBox.StandardButton.Close, Qt.Orientation.Horizontal, self + ) + self.__buttonBox.rejected.connect(self.reject) + self.__layout.addWidget(self.__buttonBox) + + self.setModal(False) + self.setSizeGripEnabled(True) + self.resize(600, 750) + + def getHistoryId(self): + """ + Public method to get the history ID of this chat. + + @return DESCRIPTION + @rtype TYPE + """ + return self.__chatWidget.getHistoryId() + + def setHistory(self, messages): + """ + Public method to add a list of messages to the view. + + @param messages list of chat messages + @type list[dict[str, str]] + """ + self.__chatWidget.clear() + + for message in messages: + self.__chatWidget.addMessage( + role=message["role"], message=message["content"], scrollToBottom=False + )