OllamaInterface/OllamaClient.py

changeset 15
c55270946c9a
parent 13
3fd49d7004b2
child 17
43b1396fe72f
equal deleted inserted replaced
14:08932ee12a69 15:c55270946c9a
92 92
93 self.__state = OllamaClientState.Waiting 93 self.__state = OllamaClientState.Waiting
94 self.__localServer = False 94 self.__localServer = False
95 95
96 self.__plugin.preferencesChanged.connect(self.__setHeartbeatTimer) 96 self.__plugin.preferencesChanged.connect(self.__setHeartbeatTimer)
97 self.__setHeartbeatTimer() 97 QTimer.singleShot(0, self.__setHeartbeatTimer)
98 98
99 def setMode(self, local): 99 def setMode(self, local):
100 """ 100 """
101 Public method to set the client mode to local. 101 Public method to set the client mode to local.
102 102
103 @param local flag indicating to connect to a locally started ollama server 103 @param local flag indicating to connect to a locally started ollama server
104 @type bool 104 @type bool
105 """ 105 """
106 self.__localServer = local 106 self.__localServer = local
107 self.__serverResponding = None 107 self.__serverResponding = None
108 if not self.__plugin.getPreferences("OllamaHeartbeatInterval"):
109 # schedule one heartbeat check giving local server some time to start
110 QTimer.singleShot(
111 10 * 1000 if self.__localServer else 0,
112 self.__periodicHeartbeat,
113 )
108 114
109 def chat(self, model, messages, streaming=True): 115 def chat(self, model, messages, streaming=True):
110 """ 116 """
111 Public method to request a chat completion from the 'ollama' server. 117 Public method to request a chat completion from the 'ollama' server.
112 118
440 @param errorCode error code reported by the reply 446 @param errorCode error code reported by the reply
441 @type QNetworkReply.NetworkError 447 @type QNetworkReply.NetworkError
442 @param reply reference to the network reply object 448 @param reply reference to the network reply object
443 @type QNetworkReply 449 @type QNetworkReply
444 """ 450 """
445 if errorCode not in ( 451 if errorCode == QNetworkReply.NetworkError.ConnectionRefusedError:
452 self.serverStateChanged.emit(False, self.__serverNotRespondingMessage())
453 elif errorCode not in (
446 QNetworkReply.NetworkError.NoError, 454 QNetworkReply.NetworkError.NoError,
447 QNetworkReply.NetworkError.OperationCanceledError, 455 QNetworkReply.NetworkError.OperationCanceledError,
448 ): 456 ):
449 self.errorOccurred.emit( 457 self.errorOccurred.emit(
450 self.tr("<p>A network error occurred.</p><p>Error: {0}</p>").format( 458 self.tr("<p>A network error occurred.</p><p>Error: {0}</p>").format(
553 if interval: 561 if interval:
554 self.__heartbeatTimer.setInterval(interval * 1000) # interval in ms 562 self.__heartbeatTimer.setInterval(interval * 1000) # interval in ms
555 self.__heartbeatTimer.start() 563 self.__heartbeatTimer.start()
556 else: 564 else:
557 self.__heartbeatTimer.stop() 565 self.__heartbeatTimer.stop()
566 self.serverStateChanged.emit(True, "")
558 567
559 @pyqtSlot() 568 @pyqtSlot()
560 def __periodicHeartbeat(self): 569 def __periodicHeartbeat(self):
561 """ 570 """
562 Private slot to do a periodic check of the 'ollama' server responsiveness. 571 Private slot to do a periodic check of the 'ollama' server responsiveness.
564 responding = self.heartbeat() 573 responding = self.heartbeat()
565 if responding != self.__serverResponding: 574 if responding != self.__serverResponding:
566 msg = ( 575 msg = (
567 "" 576 ""
568 if responding 577 if responding
569 else ( 578 else self.__serverNotRespondingMessage()
570 self.tr(
571 "<p>Error: The local server at <b>{0}</b> is not responding."
572 "</p>"
573 )
574 if self.__localServer
575 else self.tr(
576 "<p>Error: The configured server at <b>{0}</b> is not"
577 " responding.</p>"
578 )
579 ).format(self.__getHeartbeatUrl())
580 ) 579 )
581 self.serverStateChanged.emit(responding, msg) 580 self.serverStateChanged.emit(responding, msg)
582 self.__serverResponding = responding 581 self.__serverResponding = responding
582
583 def __serverNotRespondingMessage(self):
584 """
585 Private method to assemble and return a message for a non-responsive server.
586
587 @return error message
588 @rtype str
589 """
590 return (
591 self.tr(
592 "<p>Error: The local server at <b>{0}</b> is not responding."
593 "</p>"
594 )
595 if self.__localServer
596 else self.tr(
597 "<p>Error: The configured server at <b>{0}</b> is not"
598 " responding.</p>"
599 )
600 ).format(self.__getHeartbeatUrl())

eric ide

mercurial