PluginAiOllama.py

changeset 4
7dd1b9cd3150
parent 3
ca28466a186d
child 5
6e8af43d537d
equal deleted inserted replaced
3:ca28466a186d 4:7dd1b9cd3150
7 Module implementing the ollama Interface plug-in. 7 Module implementing the ollama Interface plug-in.
8 """ 8 """
9 9
10 import os 10 import os
11 11
12 from PyQt6.QtCore import QObject, Qt, QTranslator 12 from PyQt6.QtCore import QObject, Qt, QTranslator, pyqtSignal
13 from PyQt6.QtGui import QKeySequence
13 14
14 from eric7 import Preferences 15 from eric7 import Preferences
16 from eric7.EricGui import EricPixmapCache
17 from eric7.EricGui.EricAction import EricAction
15 from eric7.EricWidgets.EricApplication import ericApp 18 from eric7.EricWidgets.EricApplication import ericApp
19
20 try:
21 from eric7.UI.UserInterface import UserInterfaceSide
22
23 _Side = UserInterfaceSide.Right
24 except ImportError:
25 # backward compatibility for eric < 24.2
26 from eric7.UI.UserInterface import UserInterface
27
28 _Side = UserInterface.RightSide
16 29
17 # Start-Of-Header 30 # Start-Of-Header
18 __header__ = { 31 __header__ = {
19 "name": "ollama Interface", 32 "name": "ollama Interface",
20 "author": "Detlev Offenbach <detlev@die-offenbachs.de>", 33 "author": "Detlev Offenbach <detlev@die-offenbachs.de>",
79 92
80 def clearPrivateData(): 93 def clearPrivateData():
81 """ 94 """
82 Function to clear the private data of the plug-in. 95 Function to clear the private data of the plug-in.
83 """ 96 """
84 # TODO: not implemented yet 97 if ollamaInterfacePluginObject is not None:
85 pass 98 widget = ollamaInterfacePluginObject.getWidget()
99 if widget is not None:
100 widget.clearHistory()
86 101
87 102
88 class PluginOllamaInterface(QObject): 103 class PluginOllamaInterface(QObject):
89 """ 104 """
90 Class implementing the ollama Interface plug-in. 105 Class implementing the ollama Interface plug-in.
106
107 @signal preferencesChanged() emitted to signal a change of preferences. This
108 signal is simply relayed from the main UI.
91 """ 109 """
92 110
93 PreferencesKey = "Ollama" 111 PreferencesKey = "Ollama"
112
113 preferencesChanged = pyqtSignal()
94 114
95 def __init__(self, ui): 115 def __init__(self, ui):
96 """ 116 """
97 Constructor 117 Constructor
98 118
105 125
106 self.__defaults = { 126 self.__defaults = {
107 "OllamaScheme": "http", 127 "OllamaScheme": "http",
108 "OllamaHost": "localhost", 128 "OllamaHost": "localhost",
109 "OllamaPort": 11434, 129 "OllamaPort": 11434,
130 "OllamaHeartbeatInterval": 5, # 5 seconds heartbeat time; 0 = disabled
110 } 131 }
111 132
112 self.__translator = None 133 self.__translator = None
113 self.__loadTranslator() 134 self.__loadTranslator()
114 135
123 Public method to activate this plug-in. 144 Public method to activate this plug-in.
124 145
125 @return tuple of None and activation status 146 @return tuple of None and activation status
126 @rtype bool 147 @rtype bool
127 """ 148 """
128 global error 149 from OllamaInterface.OllamaWidget import OllamaWidget
150
151 global error, ollamaInterfacePluginObject
129 error = "" # clear previous error 152 error = "" # clear previous error
130 # TODO: not implemented yet 153 ollamaInterfacePluginObject = self
154
155 usesDarkPalette = ericApp().usesDarkPalette()
156 iconSuffix = "dark" if usesDarkPalette else "light"
157
158 self.__widget = OllamaWidget(self, fromEric=True)
159 iconName = (
160 "sbOllama96"
161 if self.__ui.getLayoutType() == "Sidebars"
162 else "ollama22-{0}".format(iconSuffix)
163 )
164 self.__ui.addSideWidget(
165 _Side,
166 self.__widget,
167 EricPixmapCache.getIcon(os.path.join("OllamaInterface", "icons", iconName)),
168 self.tr("ollama AI Interface"),
169 )
170
171 self.__activateAct = EricAction(
172 self.tr("ollama AI Interface"),
173 self.tr("ollama AI Interface"),
174 QKeySequence(self.tr("Ctrl+Alt+Shift+O")),
175 0,
176 self,
177 "ollama_interface_activate",
178 )
179 self.__activateAct.setStatusTip(
180 self.tr("Switch the input focus to the ollama AI window.")
181 )
182 self.__activateAct.setWhatsThis(
183 self.tr(
184 """<b>Activate ollama AI Interface</b>"""
185 """<p>This switches the input focus to the ollama AI window.</p>"""
186 )
187 )
188 self.__activateAct.triggered.connect(self.__activateWidget)
189
190 self.__ui.addEricActions([self.__activateAct], "ui")
191 menu = self.__ui.getMenu("subwindow")
192 menu.addAction(self.__activateAct)
193
194 self.__ui.preferencesChanged.connect(self.preferencesChanged)
131 195
132 return None, True 196 return None, True
133 197
134 def deactivate(self): 198 def deactivate(self):
135 """ 199 """
136 Public method to deactivate this plug-in. 200 Public method to deactivate this plug-in.
137 """ 201 """
138 # TODO: not implemented yet 202 self.__ui.preferencesChanged.disconnect(self.preferencesChanged)
139 pass 203
204 menu = self.__ui.getMenu("subwindow")
205 menu.removeAction(self.__activateAct)
206 self.__ui.removeEricActions([self.__activateAct], "ui")
207 self.__ui.removeSideWidget(self.__widget)
208
209 self.__initialize()
140 210
141 def __loadTranslator(self): 211 def __loadTranslator(self):
142 """ 212 """
143 Private method to load the translation file. 213 Private method to load the translation file.
144 """ 214 """
159 "Warning: translation file '{0}' could not be" 229 "Warning: translation file '{0}' could not be"
160 " loaded.".format(translation) 230 " loaded.".format(translation)
161 ) 231 )
162 print("Using default.") 232 print("Using default.")
163 233
234 def getWidget(self):
235 """
236 Public method to get a reference to the 'ollama' widget.
237
238 @return reference to the 'ollama' widget
239 @rtype OllamaWidget
240 """
241 return self.__widget
242
164 def __activateWidget(self): 243 def __activateWidget(self):
165 """ 244 """
166 Private slot to handle the activation of the pipx interface. 245 Private slot to handle the activation of the pipx interface.
167 """ 246 """
168 uiLayoutType = self.__ui.getLayoutType() 247 uiLayoutType = self.__ui.getLayoutType()
186 @param key the key of the value to get 265 @param key the key of the value to get
187 @type str 266 @type str
188 @return the requested setting value 267 @return the requested setting value
189 @rtype Any 268 @rtype Any
190 """ 269 """
191 if key in ("OllamaPort",): 270 if key in ("OllamaPort", "OllamaHeartbeatInterval"):
192 return int( 271 return int(
193 Preferences.Prefs.settings.value( 272 Preferences.Prefs.settings.value(
194 self.PreferencesKey + "/" + key, self.__defaults[key] 273 self.PreferencesKey + "/" + key, self.__defaults[key]
195 ) 274 )
196 ) 275 )
232 ) 311 )
233 sidebar.show() 312 sidebar.show()
234 sidebar.setCurrentWidget(widget) 313 sidebar.setCurrentWidget(widget)
235 314
236 315
237 def installDependencies(pipInstall):
238 """
239 Function to install dependencies of this plug-in.
240
241 @param pipInstall function to be called with a list of package names.
242 @type function
243 """
244 # TODO: not implemented yet
245 pass
246
247
248 # 316 #
249 # eflag: noqa = M801, U200 317 # eflag: noqa = M801, U200

eric ide

mercurial