OllamaInterface/OllamaClient.py

Sun, 04 Aug 2024 16:57:01 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 04 Aug 2024 16:57:01 +0200
changeset 3
ca28466a186d
child 4
7dd1b9cd3150
permissions
-rw-r--r--

Implemented the ollama client object (not tested yet).

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
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from PyQt6.QtCore import pyqtSignal, QObject, QUrl
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from eric7.EricNetwork.EricNetworkProxyFactory import proxyAuthenticationRequired
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 class OllamaClientState(enum.Enum):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Class defining the various client states.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Waiting = 0
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Requesting = 1
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Receiving = 2
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Finished = 3
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
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 OllamaClient(QObject):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Class implementing the 'ollama' client.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @signal replyReceived(content:str, role:str) emitted after a response from the
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 'ollama' server was received
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @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
38 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
39 @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
40 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
41 @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
42 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
43 execution details
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @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
45 the status of a pull request as reported by the 'ollama' server
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @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
47 @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
48 while processing the request
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 replyReceived = pyqtSignal(str, str)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 modelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 detailedModelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 runningModelsList = pyqtSignal(list)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 pullStatus = pyqtSignal(str, str, int, int)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 finished = pyqtSignal()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 errorOccurred = pyqtSignal(str)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 def __init__(self, plugin, parent=None):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 Constructor
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 @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
64 @type PluginOllamaInterface
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 @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
66 @type QObject (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 super().__init__(parent=parent)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 self.__plugin = plugin
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__replies = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.__networkManager = QNetworkAccessManager(self)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.__networkManager.proxyAuthenticationRequired.connect(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 proxyAuthenticationRequired
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 )
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 self.__state = OllamaClientState.Waiting
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def chat(self, model, messages):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 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
83
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @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
85 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 @param messages list of message objects
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 @type list of dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 "model": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 "messages": messages,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 "chat", data=ollamaRequest, processResponse=self.__processChatResponse
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 def __processChatResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 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
101
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 @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
103 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 message = response["message"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 if message:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.replyReceived.emit(message["content"], message["role"])
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 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
111 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 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
113
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 @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
115 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 @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
117 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 @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
119 @type str (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 "model": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 "prompt": prompt,
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 if suffix is not None:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 ollamaRequest["suffix"] = suffix
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 "generate",
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 data=ollamaRequest,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 processResponse=self.__processGenerateResponse,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 )
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 def __processGenerateResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 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
137
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @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
139 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.replyReceived.emit(response["response"], "")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 def pull(self, model):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 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
147
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 @param model name of the model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 "name": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.__sendRequest(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 "pull", data=ollamaRequest, processResponse=self.__processPullResponse
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 )
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 def __processPullResponse(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 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
162
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 @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
164 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 status = response["status"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 idStr = response.get("digest", "")[:20]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 total = response.get("total", 0)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 completed = response.get("completed", 0)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 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
172
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 def remove(self, model):
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 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
176
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 @param model name of the model
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 ollamaRequest = {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 "name": model,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 self.__sendRequest("delete", data=ollamaRequest)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 def list(self):
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 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
189 server.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 self.__sendRequest("tags", processResponse=self.__processModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 def __processModelsList(self, response):
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 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
197
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 @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
199 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 if name:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 models.append(name)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 self.modelsList.emit(models)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 def listDetails(self):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 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
212 server with some model details.
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 @return list of available models
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 @rtype list of dict
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 self.__sendRequest("tags", processResponse=self.__processDetailedModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 def __processDetailedModelsList(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 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
223 some model details.
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 @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
226 @type dict
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 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 if name:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 models.append(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 "name": name,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 "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
237 "size": model["size"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 "modified": datetime.datetime.fromisoformat(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 model["modified_at"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 ),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 self.detailedModelsList.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 listRunning(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 running models from the 'ollama' server
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 # TODO: not implemented yet
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 self.__sendRequest("ps", processResponse=self.__processRunningModelsList)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 def __processRunningModelsList(self, response):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 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
255 some model execution details.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 @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
258 @type dict
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 models = []
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 with contextlib.suppress(KeyError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 for model in response["models"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 name = model["name"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 if name:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 if model["size_vram"] == 0:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 processor = self.tr("100% CPU")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 elif model["size_vram"] == model["size"]:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 processor = self.tr("100% GPU")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 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
270 processor = self.tr("unknown")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 else:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 sizeCpu = model["size"] - model["size_vram"]
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 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
274 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
275 cpuPercent, 100 - cpuPercent
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 models.append(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 {
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 "name": name,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 "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
281 "size": model["size"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 "size_vram": model["size_vram"],
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 "processor": processor,
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 "expires": datetime.datetime.fromisoformat(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 model["expires_at"]
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 }
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 self.runningModelsList.emit(models)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 def state(self):
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 Public method to get the current client state.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 @return current client state
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 @rtype OllamaClientState
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 return self.__state
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 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
301 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 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
303 responses.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 @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
306 @type str
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 @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
308 (defaults to None)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 @type dict (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 @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
311 @type function (optional)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 self.__state = OllamaClientState.Requesting
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 ollamaUrl = QUrl(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 "{0}://{1}:{2}/api/{3}".format(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 self.__plugin.getPreferences("OllamaScheme"),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 self.__plugin.getPreferences("OllamaHost"),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 self.__plugin.getPreferences("OllamaPort"),
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 endpoint,
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 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 request = QNetworkRequest(ollamaUrl)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 if data is not None:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 request.setHeader(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/json"
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 jsonData = json.dumps(data).encode("utf-8")
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 reply = self.__networkManager.post(request=request, data=jsonData)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 else:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 reply = self.__networkManager.get(request=request)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 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
334 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
335 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
336 self.__replies.append(reply)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 def __replyFinished(self, reply):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 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
341
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 @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
343 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 self.__state = OllamaClientState.Finished
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 if reply in self.__replies:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 self.__replies.remove(reply)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 reply.deleteLater()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 def __errorOccurred(self, errorCode, reply):
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 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
355
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 @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
357 @type QNetworkReply.NetworkError
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 @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
359 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 if errorCode != QNetworkReply.NetworkError.NoError:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 self.errorOccurred.emit(
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 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
364 reply.errorString()
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 )
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 def __processData(self, reply, processResponse):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 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
371 given processing function or method.
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 @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
374 @type QNetworkReply
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 @param processResponse processing function
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 @type function
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 """
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 self.__state = OllamaClientState.Receiving
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 buffer = bytes(reply.readAll())
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 if buffer:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 with contextlib.suppress(json.JSONDecodeError):
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 data = json.loads(buffer)
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 if data and processResponse:
ca28466a186d Implemented the ollama client object (not tested yet).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 processResponse(data)

eric ide

mercurial