OllamaInterface/OllamaWidget.py

changeset 7
eb1dec15b2f0
parent 6
d8064fb63eac
child 8
3118d16e526e
equal deleted inserted replaced
6:d8064fb63eac 7:eb1dec15b2f0
7 """ 7 """
8 8
9 import json 9 import json
10 import os 10 import os
11 11
12 from PyQt6.QtCore import Qt, QTimer, pyqtSlot 12 from PyQt6.QtCore import QProcessEnvironment, Qt, QTimer, pyqtSlot
13 from PyQt6.QtWidgets import ( 13 from PyQt6.QtWidgets import (
14 QDialog, 14 QDialog,
15 QInputDialog, 15 QInputDialog,
16 QLineEdit, 16 QLineEdit,
17 QMenu, 17 QMenu,
81 81
82 self.mainSplitter.setSizes([200, 2000]) 82 self.mainSplitter.setSizes([200, 2000])
83 83
84 self.newChatButton.setEnabled(False) 84 self.newChatButton.setEnabled(False)
85 self.__handleServerStateChanged(False) 85 self.__handleServerStateChanged(False)
86
87 self.__localServerDialog = None
88 self.__localServerProcess = None
86 89
87 self.__connectClient() 90 self.__connectClient()
88 91
89 self.__initOllamaMenu() 92 self.__initOllamaMenu()
90 93
617 self.__chatHistoryMenu.addSeparator() 620 self.__chatHistoryMenu.addSeparator()
618 self.__chatHistoryMenu.addAction(self.tr("Import"), self.__menuImportHistories) 621 self.__chatHistoryMenu.addAction(self.tr("Import"), self.__menuImportHistories)
619 self.__chatHistoryMenu.addAction(self.tr("Export"), self.__menuExportHistories) 622 self.__chatHistoryMenu.addAction(self.tr("Export"), self.__menuExportHistories)
620 623
621 ################################################################### 624 ###################################################################
625 ## Menu with Local Server related actions
626 ###################################################################
627
628 self.__localServerMenu = QMenu(self.tr("Local Server"))
629 self.__localServerStartMonitorAct = self.__localServerMenu.addAction(
630 self.tr("Start with Monitoring"), self.__startLocalServerMonitoring
631 )
632 self.__localServerMenu.addSeparator()
633 self.__startLocalServerAct = self.__localServerMenu.addAction(
634 self.tr("Start"), self.__startLocalServer
635 )
636 self.__stopLocalServerAct = self.__localServerMenu.addAction(
637 self.tr("Stop"), self.__stopLocalServer
638 )
639
640 ###################################################################
622 ## Main menu 641 ## Main menu
623 ################################################################### 642 ###################################################################
624 643
625 self.__ollamaMenu = QMenu() 644 self.__ollamaMenu = QMenu()
626 self.__ollamaMenu.addMenu(self.__chatHistoryMenu) 645 self.__ollamaMenu.addMenu(self.__chatHistoryMenu)
627 self.__ollamaMenu.addSeparator() 646 self.__ollamaMenu.addSeparator()
647 self.__ollamaMenu.addMenu(self.__localServerMenu)
648 self.__ollamaMenu.addSeparator()
628 self.__ollamaMenu.addAction(self.tr("Configure..."), self.__ollamaConfigure) 649 self.__ollamaMenu.addAction(self.tr("Configure..."), self.__ollamaConfigure)
629 650
630 self.__ollamaMenu.aboutToShow.connect(self.__aboutToShowOllamaMenu) 651 self.__ollamaMenu.aboutToShow.connect(self.__aboutToShowOllamaMenu)
631 652
632 self.ollamaMenuButton.setMenu(self.__ollamaMenu) 653 self.ollamaMenuButton.setMenu(self.__ollamaMenu)
635 def __aboutToShowOllamaMenu(self): 656 def __aboutToShowOllamaMenu(self):
636 """ 657 """
637 Private slot to set the action enabled status. 658 Private slot to set the action enabled status.
638 """ 659 """
639 self.__clearHistoriesAct.setEnabled(self.__chatHistoryLayout.count() > 1) 660 self.__clearHistoriesAct.setEnabled(self.__chatHistoryLayout.count() > 1)
661
662 self.__localServerStartMonitorAct.setEnabled(
663 self.__localServerProcess is None and self.__localServerDialog is None
664 )
665 self.__startLocalServerAct.setEnabled(
666 self.__localServerProcess is None and self.__localServerDialog is None
667 )
668 self.__stopLocalServerAct.setEnabled(
669 self.__localServerProcess is not None and self.__localServerDialog is None
670 )
640 671
641 @pyqtSlot() 672 @pyqtSlot()
642 def __ollamaConfigure(self): 673 def __ollamaConfigure(self):
643 """ 674 """
644 Private slot to show the ollama configuration page. 675 Private slot to show the ollama configuration page.
715 for _, hid in selectedChats: 746 for _, hid in selectedChats:
716 historyWidget = self.__findHistoryWidget(hid) 747 historyWidget = self.__findHistoryWidget(hid)
717 if historyWidget is not None: 748 if historyWidget is not None:
718 entries[hid] = historyWidget.saveToJson() 749 entries[hid] = historyWidget.saveToJson()
719 self.__saveChatHistoryFile(fileName, entries) 750 self.__saveChatHistoryFile(fileName, entries)
751
752 def prepareServerRuntimeEnvironment(self):
753 """
754 Public method to prepare a QProcessEnvironment object.
755
756 @return prepared environment object to be used with QProcess
757 @rtype QProcessEnvironment
758 """
759 env = QProcessEnvironment.systemEnvironment()
760 env.insert(
761 "OLLAMA_HOST",
762 "127.0.0.1:{0}".format(self.__plugin.getPreferences("OllamaLocalPort")),
763 )
764
765 return env
766
767 @pyqtSlot()
768 def __startLocalServerMonitoring(self):
769 """
770 Private slot to open a dialog for running a local 'ollama' server instance
771 and monitor its output.
772 """
773 # TODO: not implemented yet
774 from .RunOllamaServerDialog import RunOllamaServerDialog
775
776 self.__localServerDialog = RunOllamaServerDialog(
777 self.__client, self.__plugin, self
778 )
779 self.__localServerDialog.serverStarted.connect(self.__serverStarted)
780 self.__localServerDialog.serverStopped.connect(self.__serverStopped)
781 self.__localServerDialog.finished.connect(self.__serverDialogClosed)
782 self.__localServerDialog.show()
783 self.__localServerDialog.startServer()
784
785 @pyqtSlot()
786 def __startLocalServer(self):
787 """
788 Private slot to start a local 'ollama' server instance in the background.
789 """
790 # TODO: not implemented yet
791 pass
792
793 @pyqtSlot()
794 def __stopLocalServer(self):
795 """
796 Private slot to stop a running local 'ollama' server instance.
797 """
798 # TODO: not implemented yet
799 pass
800
801 @pyqtSlot()
802 def __serverStarted(self):
803 """
804 Private slot to handle the start of a local server.
805 """
806 self.__client.setMode(True)
807 self.on_reloadModelsButton_clicked()
808
809 @pyqtSlot()
810 def __serverStopped(self):
811 """
812 Private slot to handle the stopping of a local server.
813 """
814 self.__client.setMode(False)
815 self.on_reloadModelsButton_clicked()
816
817 @pyqtSlot()
818 def __serverDialogClosed(self):
819 """
820 Private slot handling the closing of the local server dialog.
821 """
822 self.__localServerDialog.deleteLater()
823 self.__localServerDialog = None

eric ide

mercurial