5 |
5 |
6 """ |
6 """ |
7 Module implementing the main ollama interface widget. |
7 Module implementing the main ollama interface widget. |
8 """ |
8 """ |
9 |
9 |
|
10 import contextlib |
10 import json |
11 import json |
11 import os |
12 import os |
12 |
13 |
13 from PyQt6.QtCore import QProcess, QProcessEnvironment, Qt, QTimer, QUrl, pyqtSlot |
14 from PyQt6.QtCore import QProcess, QProcessEnvironment, Qt, QTimer, QUrl, pyqtSlot |
14 from PyQt6.QtGui import QDesktopServices |
15 from PyQt6.QtGui import QDesktopServices |
25 from eric7.EricGui import EricPixmapCache |
26 from eric7.EricGui import EricPixmapCache |
26 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
27 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
27 from eric7.EricWidgets.EricApplication import ericApp |
28 from eric7.EricWidgets.EricApplication import ericApp |
28 from eric7.EricWidgets.EricListSelectionDialog import EricListSelectionDialog |
29 from eric7.EricWidgets.EricListSelectionDialog import EricListSelectionDialog |
29 |
30 |
30 from .OllamaChatWidget import OllamaChatWidget |
31 from .OllamaChatWidget import OllamaChatDialog, OllamaChatWidget |
31 from .OllamaClient import OllamaClient |
32 from .OllamaClient import OllamaClient |
32 from .OllamaHistoryWidget import OllamaHistoryWidget |
33 from .OllamaHistoryWidget import OllamaHistoryWidget |
33 from .Ui_OllamaWidget import Ui_OllamaWidget |
34 from .Ui_OllamaWidget import Ui_OllamaWidget |
34 |
35 |
35 |
36 |
88 |
89 |
89 self.__localServerDialog = None |
90 self.__localServerDialog = None |
90 self.__localServerProcess = None |
91 self.__localServerProcess = None |
91 |
92 |
92 self.__availableModels = [] |
93 self.__availableModels = [] |
|
94 self.__chatHistoryDialogs = {} |
93 |
95 |
94 self.__initOllamaMenu() |
96 self.__initOllamaMenu() |
95 |
97 |
96 self.newChatButton.setEnabled(False) |
98 self.newChatButton.setEnabled(False) |
97 self.__handleServerStateChanged(False, "") |
99 self.__handleServerStateChanged(False, "") |
247 ) |
249 ) |
248 |
250 |
249 history.deleteChatHistory.connect(self.__deleteHistory) |
251 history.deleteChatHistory.connect(self.__deleteHistory) |
250 history.dataChanged.connect(self.__saveHistory) |
252 history.dataChanged.connect(self.__saveHistory) |
251 history.newChatWithHistory.connect(self.__newChatWithHistory) |
253 history.newChatWithHistory.connect(self.__newChatWithHistory) |
|
254 history.viewChatHistory.connect(self.__viewChatHistory) |
252 |
255 |
253 self.__saveHistory() |
256 self.__saveHistory() |
254 |
257 |
255 QTimer.singleShot(0, self.__scrollHistoryToBottom) |
258 QTimer.singleShot(0, self.__scrollHistoryToBottom) |
256 |
259 |
512 # simply switch to the already existing chatWidget |
515 # simply switch to the already existing chatWidget |
513 self.chatStackWidget.setCurrentWidget(chatWidget) |
516 self.chatStackWidget.setCurrentWidget(chatWidget) |
514 |
517 |
515 self.__updateMessageEditState() |
518 self.__updateMessageEditState() |
516 self.messageEdit.setFocus(Qt.FocusReason.OtherFocusReason) |
519 self.messageEdit.setFocus(Qt.FocusReason.OtherFocusReason) |
|
520 |
|
521 @pyqtSlot(str) |
|
522 def __viewChatHistory(self, hid): |
|
523 """ |
|
524 Private slot to show the chat history in a separate window. |
|
525 |
|
526 @param hid ID of the history to be shown |
|
527 @type str |
|
528 """ |
|
529 historyWidget = self.__findHistoryWidget(hid) |
|
530 if historyWidget is None: |
|
531 # Oops, just ignore it |
|
532 return |
|
533 |
|
534 try: |
|
535 dlg = self.__chatHistoryDialogs[hid] |
|
536 except KeyError: |
|
537 dlg = OllamaChatDialog( |
|
538 hid=hid, |
|
539 title=historyWidget.getTitle(), |
|
540 model=historyWidget.getModel(), |
|
541 parent=self, |
|
542 ) |
|
543 dlg.rejected.connect(lambda: self.__chatHistoryDialogClosed(dlg)) |
|
544 self.__chatHistoryDialogs[hid] = dlg |
|
545 |
|
546 dlg.setHistory(historyWidget.getMessages()) |
|
547 dlg.show() |
|
548 dlg.raise_() |
|
549 |
|
550 def __chatHistoryDialogClosed(self, dialog): |
|
551 """ |
|
552 Private method to handle the closing of a chat history dialog. |
|
553 |
|
554 @param dialog reference to the closed dialog |
|
555 @type OllamaChatDialog |
|
556 """ |
|
557 hid = dialog.getHistoryId() |
|
558 with contextlib.suppress(KeyError): |
|
559 del self.__chatHistoryDialogs[hid] |
517 |
560 |
518 def __removeChatWidget(self, hid): |
561 def __removeChatWidget(self, hid): |
519 """ |
562 """ |
520 Private method to remove a chat widget given its chat history ID. |
563 Private method to remove a chat widget given its chat history ID. |
521 |
564 |