OllamaInterface/OllamaClient.py

Thu, 08 Aug 2024 18:33:49 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 08 Aug 2024 18:33:49 +0200
changeset 7
eb1dec15b2f0
parent 5
6e8af43d537d
child 8
3118d16e526e
permissions
-rw-r--r--

Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.

3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de>
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the 'ollama' client.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import contextlib
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import datetime
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import enum
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import json
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
15 from PyQt6.QtCore import (
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
16 QCoreApplication,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
17 QObject,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
18 QThread,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
19 QTimer,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
20 QUrl,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
21 pyqtSignal,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
22 pyqtSlot,
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
23 )
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
24 from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 from eric7.EricNetwork.EricNetworkProxyFactory import proxyAuthenticationRequired
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 class OllamaClientState(enum.Enum):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Class defining the various client states.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
33
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Waiting = 0
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 Requesting = 1
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 Receiving = 2
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Finished = 3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 class OllamaClient(QObject):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 Class implementing the 'ollama' client.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
44 @signal replyReceived(content:str, role:str, done:bool) emitted after a response
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
45 from the 'ollama' server was received
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @signal modelsList(modelNames:list[str]) emitted after the list of model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 names was obtained from the 'ollama' server
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @signal detailedModelsList(models:list[dict]) emitted after the list of
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 models was obtained from the 'ollama' server giving some model details
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @signal runningModelsList(models:list[dict]) emitted after the list of
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 running models was obtained from the 'ollama' server giving some model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 execution details
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 @signal pullStatus(msg:str, id:str, total:int, completed:int) emitted to indicate
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 the status of a pull request as reported by the 'ollama' server
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
55 @signal serverVersion(version:str) emitted after the server version was obtained
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
56 from the 'ollama' server
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @signal finished() emitted to indicate the completion of a request
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 @signal errorOccurred(error:str) emitted to indicate a network error occurred
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 while processing the request
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
60 @signal serverStateChanged(ok:bool) emitted to indicate a change of the server
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
61 responsiveness
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
64 replyReceived = pyqtSignal(str, str, bool)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 modelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 detailedModelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 runningModelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 pullStatus = pyqtSignal(str, str, int, int)
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
69 serverVersion = pyqtSignal(str)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 finished = pyqtSignal()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 errorOccurred = pyqtSignal(str)
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
72 serverStateChanged = pyqtSignal(bool)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 def __init__(self, plugin, parent=None):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 Constructor
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @param plugin reference to the plugin object
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 @type PluginOllamaInterface
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 @param parent reference to the parent object (defaults to None)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @type QObject (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 super().__init__(parent=parent)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 self.__plugin = plugin
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 self.__replies = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__networkManager = QNetworkAccessManager(self)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__networkManager.proxyAuthenticationRequired.connect(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 proxyAuthenticationRequired
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
93 self.__serverResponding = False
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
94 self.__heartbeatTimer = QTimer(self)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
95 self.__heartbeatTimer.timeout.connect(self.__periodicHeartbeat)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
96
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 self.__state = OllamaClientState.Waiting
7
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
98 self.__localServer = False
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
100 self.__serverResponding = False # start with a faulty state
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
101
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
102 self.__plugin.preferencesChanged.connect(self.__setHeartbeatTimer)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
103 self.__setHeartbeatTimer()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
104
7
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
105 def setMode(self, local):
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
106 """
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
107 Public method to set the client mode to local.
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
108
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
109 @param local flag indicating to connect to a locally started ollama server
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
110 @type bool
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
111 """
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
112 self.__localServer = local
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
113 self.__serverResponding = False
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
114
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
115 def chat(self, model, messages, streaming=True):
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 Public method to request a chat completion from the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 @param model name of the model to be used
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 @param messages list of message objects
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @type list of dict
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
123 @param streaming flag indicating to receive a streaming response
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
124 @type bool
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 "model": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 "messages": messages,
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
129 "stream": streaming,
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 "chat", data=ollamaRequest, processResponse=self.__processChatResponse
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 def __processChatResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 Private method to process the chat response of the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @param response dictionary containing the chat response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 message = response["message"]
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
144 done = response["done"]
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 if message:
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
146 self.replyReceived.emit(message["content"], message["role"], done)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 def generate(self, model, prompt, suffix=None):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 Public method to request to generate a completion from the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 @param model name of the model to be used
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 @param prompt prompt to generate a response for
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 @param suffix text after the model response (defaults to None)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 @type str (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 "model": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 "prompt": prompt,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 if suffix is not None:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 ollamaRequest["suffix"] = suffix
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 "generate",
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 data=ollamaRequest,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 processResponse=self.__processGenerateResponse,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 def __processGenerateResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 Private method to process the generate response of the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 @param response dictionary containing the generate response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 with contextlib.suppress(KeyError):
5
6e8af43d537d Implemented the 'chat' functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4
diff changeset
179 self.replyReceived.emit(response["response"], "", response["done"])
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 def pull(self, model):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 Public method to ask the 'ollama' server to pull the given model.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 @param model name of the model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 "name": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 "pull", data=ollamaRequest, processResponse=self.__processPullResponse
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 def __processPullResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 Private method to process a pull response of the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 @param response dictionary containing the pull response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 status = response["status"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 idStr = response.get("digest", "")[:20]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 total = response.get("total", 0)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 completed = response.get("completed", 0)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 self.pullStatus.emit(status, idStr, total, completed)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 def remove(self, model):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 Public method to ask the 'ollama' server to delete the given model.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 @param model name of the model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 "name": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 self.__sendRequest("delete", data=ollamaRequest)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 def list(self):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 Public method to request a list of models available locally from the 'ollama'
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 self.__sendRequest("tags", processResponse=self.__processModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 def __processModelsList(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 Private method to process the tags response of the 'ollama' server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 @param response dictionary containing the tags response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 if name:
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
242 models.append(name.replace(":latest", ""))
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 self.modelsList.emit(models)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 def listDetails(self):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 Public method to request a list of models available locally from the 'ollama'
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 server with some model details.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 self.__sendRequest("tags", processResponse=self.__processDetailedModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 def __processDetailedModelsList(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 Private method to process the tags response of the 'ollama' server extracting
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 some model details.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 @param response dictionary containing the tags response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 if name:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 models.append(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 "name": name,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 "id": model["digest"][:20], # first 20 characters only
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 "size": model["size"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 "modified": datetime.datetime.fromisoformat(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 model["modified_at"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 ),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 self.detailedModelsList.emit(models)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 def listRunning(self):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 """
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
280 Public method to request a list of running models from the 'ollama' server.
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 self.__sendRequest("ps", processResponse=self.__processRunningModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 def __processRunningModelsList(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 Private method to process the ps response of the 'ollama' server extracting
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 some model execution details.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 @param response dictionary containing the ps response
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 if name:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 if model["size_vram"] == 0:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 processor = self.tr("100% CPU")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 elif model["size_vram"] == model["size"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 processor = self.tr("100% GPU")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 elif model["size_vram"] > model["size_"] or model["size"] == 0:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 processor = self.tr("unknown")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 else:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 sizeCpu = model["size"] - model["size_vram"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 cpuPercent = round(sizeCpu / model["size_vram"] * 100)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 processor = self.tr("{0}% / {1}% CPU / GPU").format(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 cpuPercent, 100 - cpuPercent
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 models.append(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 "name": name,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 "id": model["digest"][:20], # first 20 characters only
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 "size": model["size"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 "size_vram": model["size_vram"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 "processor": processor,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 "expires": datetime.datetime.fromisoformat(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 model["expires_at"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 ),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 self.runningModelsList.emit(models)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
324 def version(self):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
325 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
326 Public method to request the version from the 'ollama' server.
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
327 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
328 self.__sendRequest("version", processResponse=self.__processVersion)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
329
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
330 def __processVersion(self, response):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
331 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
332 Private method to process the version response of the 'ollama' server.
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
333
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
334 @param response dictionary containing the version response
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
335 @type dict
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
336 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
337 with contextlib.suppress(KeyError):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
338 self.serverVersion.emit(response["version"])
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
339
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 def state(self):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 Public method to get the current client state.
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
343
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 @return current client state
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 @rtype OllamaClientState
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 return self.__state
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 def __sendRequest(self, endpoint, data=None, processResponse=None):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 Private method to send a request to the 'ollama' server and handle its
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 responses.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 @param endpoint 'ollama' API endpoint to be contacted
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 @param data dictionary containing the data to send to the server
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 (defaults to None)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 @type dict (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 @param processResponse function handling the received data (defaults to None)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 @type function (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 self.__state = OllamaClientState.Requesting
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 ollamaUrl = QUrl(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 "{0}://{1}:{2}/api/{3}".format(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 self.__plugin.getPreferences("OllamaScheme"),
7
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
367 (
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
368 "127.0.0.1"
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
369 if self.__localServer
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
370 else self.__plugin.getPreferences("OllamaHost")
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
371 ),
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
372 (
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
373 self.__plugin.getPreferences("OllamaLocalPort")
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
374 if self.__localServer
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
375 else self.__plugin.getPreferences("OllamaPort")
eb1dec15b2f0 Implemented a menu, dialog and actions to start a local 'ollama' server with the capability to monitor its log output.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5
diff changeset
376 ),
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 endpoint,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 request = QNetworkRequest(ollamaUrl)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 if data is not None:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 request.setHeader(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/json"
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 jsonData = json.dumps(data).encode("utf-8")
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
386 reply = self.__networkManager.post(request, jsonData)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 else:
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
388 reply = self.__networkManager.get(request)
3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 reply.finished.connect(lambda: self.__replyFinished(reply))
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 reply.errorOccurred.connect(lambda error: self.__errorOccurred(error, reply))
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 reply.readyRead.connect(lambda: self.__processData(reply, processResponse))
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 self.__replies.append(reply)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 def __replyFinished(self, reply):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 Private method to handle the finished signal of the reply.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 @param reply reference to the finished network reply object
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 self.__state = OllamaClientState.Finished
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 if reply in self.__replies:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 self.__replies.remove(reply)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 reply.deleteLater()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 def __errorOccurred(self, errorCode, reply):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 Private method to handle a network error of the given reply.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 @param errorCode error code reported by the reply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 @type QNetworkReply.NetworkError
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 @param reply reference to the network reply object
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 if errorCode != QNetworkReply.NetworkError.NoError:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 self.errorOccurred.emit(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 self.tr("<p>A network error occurred.</p><p>Error: {0}</p>").format(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 reply.errorString()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 def __processData(self, reply, processResponse):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 Private method to receive data from the 'ollama' server and process it with a
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 given processing function or method.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 @param reply reference to the network reply object
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 @param processResponse processing function
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 @type function
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 self.__state = OllamaClientState.Receiving
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 buffer = bytes(reply.readAll())
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 if buffer:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 with contextlib.suppress(json.JSONDecodeError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 data = json.loads(buffer)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 if data and processResponse:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 processResponse(data)
4
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
443
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
444 def heartbeat(self):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
445 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
446 Public method to check, if the 'ollama' server has started and is responsive.
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
447
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
448 @return flag indicating a responsive 'ollama' server
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
449 @rtype bool
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
450 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
451 ollamaUrl = QUrl(
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
452 "{0}://{1}:{2}/".format(
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
453 self.__plugin.getPreferences("OllamaScheme"),
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
454 self.__plugin.getPreferences("OllamaHost"),
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
455 self.__plugin.getPreferences("OllamaPort"),
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
456 )
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
457 )
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
458 request = QNetworkRequest(ollamaUrl)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
459 reply = self.__networkManager.head(request)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
460 while not reply.isFinished():
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
461 QCoreApplication.processEvents()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
462 QThread.msleep(100)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
463
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
464 reply.deleteLater()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
465
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
466 return reply.error() == QNetworkReply.NetworkError.NoError
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
467
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
468 @pyqtSlot()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
469 def __setHeartbeatTimer(self):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
470 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
471 Private slot to configure the heartbeat timer.
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
472 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
473 interval = self.__plugin.getPreferences("OllamaHeartbeatInterval")
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
474 if interval:
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
475 self.__heartbeatTimer.setInterval(interval * 1000) # interval in ms
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
476 self.__heartbeatTimer.start()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
477 else:
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
478 self.__heartbeatTimer.stop()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
479
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
480 @pyqtSlot()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
481 def __periodicHeartbeat(self):
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
482 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
483 Private slot to do a periodic check of the 'ollama' server responsiveness.
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
484 """
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
485 responding = self.heartbeat()
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
486 if responding != self.__serverResponding:
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
487 self.serverStateChanged.emit(responding)
7dd1b9cd3150 Implemented most of the Chat History widgets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3
diff changeset
488 self.__serverResponding = responding

eric ide

mercurial