1
1
from typing import List , Union , Iterable
2
- from urllib .parse import urlparse , urlunparse
3
- from datetime import datetime
4
-
5
2
from openai import OpenAI
6
-
3
+ import minds . utils as utils
7
4
import minds .exceptions as exc
8
-
9
5
from minds .datasources import Datasource , DatabaseConfig
10
6
11
7
DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\' s question: {{question}}'
12
8
13
-
14
9
class Mind :
15
10
def __init__ (
16
11
self , client , name ,
@@ -25,7 +20,7 @@ def __init__(
25
20
self .api = client .api
26
21
self .client = client
27
22
self .project = 'mindsdb'
28
-
23
+
29
24
self .name = name
30
25
self .model_name = model_name
31
26
self .provider = provider
@@ -35,7 +30,11 @@ def __init__(
35
30
self .parameters = parameters
36
31
self .created_at = created_at
37
32
self .updated_at = updated_at
38
-
33
+ base_url = utils .get_openai_base_url (self .api .base_url )
34
+ self .openai_client = OpenAI (
35
+ api_key = self .api .api_key ,
36
+ base_url = base_url
37
+ )
39
38
self .datasources = datasources
40
39
41
40
def __repr__ (self ):
@@ -74,6 +73,9 @@ def update(
74
73
:param parameters, dict: alter other parameters of the mind, optional
75
74
"""
76
75
data = {}
76
+
77
+ if name is not None :
78
+ utils .validate_mind_name (name )
77
79
78
80
if datasources is not None :
79
81
ds_names = []
@@ -156,23 +158,7 @@ def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[
156
158
157
159
:return: string if stream mode is off or iterator of ChoiceDelta objects (by openai)
158
160
"""
159
- parsed = urlparse (self .api .base_url )
160
-
161
- netloc = parsed .netloc
162
- if netloc == 'mdb.ai' :
163
- llm_host = 'llm.mdb.ai'
164
- else :
165
- llm_host = 'ai.' + netloc
166
-
167
- parsed = parsed ._replace (path = '' , netloc = llm_host )
168
-
169
- base_url = urlunparse (parsed )
170
- openai_client = OpenAI (
171
- api_key = self .api .api_key ,
172
- base_url = base_url
173
- )
174
-
175
- response = openai_client .chat .completions .create (
161
+ response = self .openai_client .chat .completions .create (
176
162
model = self .name ,
177
163
messages = [
178
164
{'role' : 'user' , 'content' : message }
@@ -216,7 +202,7 @@ def get(self, name: str) -> Mind:
216
202
:param name: name of the mind
217
203
:return: a mind object
218
204
"""
219
-
205
+
220
206
item = self .api .get (f'/projects/{ self .project } /minds/{ name } ' ).json ()
221
207
return Mind (self .client , ** item )
222
208
@@ -243,6 +229,7 @@ def create(
243
229
datasources = None ,
244
230
parameters = None ,
245
231
replace = False ,
232
+ update = False ,
246
233
) -> Mind :
247
234
"""
248
235
Create a new mind and return it
@@ -259,8 +246,12 @@ def create(
259
246
:param datasources: list of datasources used by mind, optional
260
247
:param parameters, dict: other parameters of the mind, optional
261
248
:param replace: if true - to remove existing mind, default is false
249
+ :param update: if true - to update mind if exists, default is false
262
250
:return: created mind
263
251
"""
252
+
253
+ if name is not None :
254
+ utils .validate_mind_name (name )
264
255
265
256
if replace :
266
257
try :
@@ -284,8 +275,15 @@ def create(
284
275
if 'prompt_template' not in parameters :
285
276
parameters ['prompt_template' ] = DEFAULT_PROMPT_TEMPLATE
286
277
287
- self .api .post (
288
- f'/projects/{ self .project } /minds' ,
278
+ if update :
279
+ method = self .api .put
280
+ url = f'/projects/{ self .project } /minds/{ name } '
281
+ else :
282
+ method = self .api .post
283
+ url = f'/projects/{ self .project } /minds'
284
+
285
+ method (
286
+ url ,
289
287
data = {
290
288
'name' : name ,
291
289
'model_name' : model_name ,
0 commit comments