OllamaInterface/OllamaClient.py

changeset 5
6e8af43d537d
parent 4
7dd1b9cd3150
child 7
eb1dec15b2f0
equal deleted inserted replaced
4:7dd1b9cd3150 5:6e8af43d537d
39 39
40 class OllamaClient(QObject): 40 class OllamaClient(QObject):
41 """ 41 """
42 Class implementing the 'ollama' client. 42 Class implementing the 'ollama' client.
43 43
44 @signal replyReceived(content:str, role:str) emitted after a response from the 44 @signal replyReceived(content:str, role:str, done:bool) emitted after a response
45 'ollama' server was received 45 from the 'ollama' server was received
46 @signal modelsList(modelNames:list[str]) emitted after the list of model 46 @signal modelsList(modelNames:list[str]) emitted after the list of model
47 names was obtained from the 'ollama' server 47 names was obtained from the 'ollama' server
48 @signal detailedModelsList(models:list[dict]) emitted after the list of 48 @signal detailedModelsList(models:list[dict]) emitted after the list of
49 models was obtained from the 'ollama' server giving some model details 49 models was obtained from the 'ollama' server giving some model details
50 @signal runningModelsList(models:list[dict]) emitted after the list of 50 @signal runningModelsList(models:list[dict]) emitted after the list of
59 while processing the request 59 while processing the request
60 @signal serverStateChanged(ok:bool) emitted to indicate a change of the server 60 @signal serverStateChanged(ok:bool) emitted to indicate a change of the server
61 responsiveness 61 responsiveness
62 """ 62 """
63 63
64 replyReceived = pyqtSignal(str, str) 64 replyReceived = pyqtSignal(str, str, bool)
65 modelsList = pyqtSignal(list) 65 modelsList = pyqtSignal(list)
66 detailedModelsList = pyqtSignal(list) 66 detailedModelsList = pyqtSignal(list)
67 runningModelsList = pyqtSignal(list) 67 runningModelsList = pyqtSignal(list)
68 pullStatus = pyqtSignal(str, str, int, int) 68 pullStatus = pyqtSignal(str, str, int, int)
69 serverVersion = pyqtSignal(str) 69 serverVersion = pyqtSignal(str)
99 self.__serverResponding = False # start with a faulty state 99 self.__serverResponding = False # start with a faulty state
100 100
101 self.__plugin.preferencesChanged.connect(self.__setHeartbeatTimer) 101 self.__plugin.preferencesChanged.connect(self.__setHeartbeatTimer)
102 self.__setHeartbeatTimer() 102 self.__setHeartbeatTimer()
103 103
104 def chat(self, model, messages): 104 def chat(self, model, messages, streaming=True):
105 """ 105 """
106 Public method to request a chat completion from the 'ollama' server. 106 Public method to request a chat completion from the 'ollama' server.
107 107
108 @param model name of the model to be used 108 @param model name of the model to be used
109 @type str 109 @type str
110 @param messages list of message objects 110 @param messages list of message objects
111 @type list of dict 111 @type list of dict
112 """ 112 @param streaming flag indicating to receive a streaming response
113 # TODO: not implemented yet 113 @type bool
114 """
114 ollamaRequest = { 115 ollamaRequest = {
115 "model": model, 116 "model": model,
116 "messages": messages, 117 "messages": messages,
118 "stream": streaming,
117 } 119 }
118 self.__sendRequest( 120 self.__sendRequest(
119 "chat", data=ollamaRequest, processResponse=self.__processChatResponse 121 "chat", data=ollamaRequest, processResponse=self.__processChatResponse
120 ) 122 )
121 123
126 @param response dictionary containing the chat response 128 @param response dictionary containing the chat response
127 @type dict 129 @type dict
128 """ 130 """
129 with contextlib.suppress(KeyError): 131 with contextlib.suppress(KeyError):
130 message = response["message"] 132 message = response["message"]
133 done = response["done"]
131 if message: 134 if message:
132 self.replyReceived.emit(message["content"], message["role"]) 135 self.replyReceived.emit(message["content"], message["role"], done)
133 136
134 def generate(self, model, prompt, suffix=None): 137 def generate(self, model, prompt, suffix=None):
135 """ 138 """
136 Public method to request to generate a completion from the 'ollama' server. 139 Public method to request to generate a completion from the 'ollama' server.
137 140
140 @param prompt prompt to generate a response for 143 @param prompt prompt to generate a response for
141 @type str 144 @type str
142 @param suffix text after the model response (defaults to None) 145 @param suffix text after the model response (defaults to None)
143 @type str (optional) 146 @type str (optional)
144 """ 147 """
145 # TODO: not implemented yet
146 ollamaRequest = { 148 ollamaRequest = {
147 "model": model, 149 "model": model,
148 "prompt": prompt, 150 "prompt": prompt,
149 } 151 }
150 if suffix is not None: 152 if suffix is not None:
161 163
162 @param response dictionary containing the generate response 164 @param response dictionary containing the generate response
163 @type dict 165 @type dict
164 """ 166 """
165 with contextlib.suppress(KeyError): 167 with contextlib.suppress(KeyError):
166 self.replyReceived.emit(response["response"], "") 168 self.replyReceived.emit(response["response"], "", response["done"])
167 169
168 def pull(self, model): 170 def pull(self, model):
169 """ 171 """
170 Public method to ask the 'ollama' server to pull the given model. 172 Public method to ask the 'ollama' server to pull the given model.
171 173

eric ide

mercurial