Skip to content

Commit ae7c9d4

Browse files
authored
Merge branch 'main' into openai-client
2 parents f016c98 + bac1981 commit ae7c9d4

File tree

8 files changed

+111
-27
lines changed

8 files changed

+111
-27
lines changed

minds/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = 'minds_sdk'
22
__package_name__ = 'minds'
3-
__version__ = '1.0.7'
3+
__version__ = '1.0.8'
44
__description__ = 'An AI-Data Mind is an LLM with the built-in power to answer data questions for Agents'
55
__email__ = 'hello@mindsdb.com'
66
__author__ = 'MindsDB Inc'

minds/datasources/datasources.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class DatabaseConfig(BaseModel):
1616
class Datasource(DatabaseConfig):
1717
...
1818

19+
1920
class Datasources:
2021
def __init__(self, client):
2122
self.api = client.api
2223

23-
def create(self, ds_config: DatabaseConfig, replace=False):
24+
def create(self, ds_config: DatabaseConfig, update=False):
2425
"""
2526
Create new datasource and return it
2627
@@ -30,19 +31,16 @@ def create(self, ds_config: DatabaseConfig, replace=False):
3031
- description: str, description of the database. Used by mind to know what data can be got from it.
3132
- connection_data: dict, optional, credentials to connect to database
3233
- tables: list of str, optional, list of allowed tables
34+
:param update: if true - to update datasourse if exists, default is false
3335
:return: datasource object
3436
"""
3537

3638
name = ds_config.name
3739

38-
if replace:
39-
try:
40-
self.get(name)
41-
self.drop(name, force=True)
42-
except exc.ObjectNotFound:
43-
...
44-
45-
self.api.post('/datasources', data=ds_config.model_dump())
40+
if update:
41+
self.api.put('/datasources', data=ds_config.model_dump())
42+
else:
43+
self.api.post('/datasources', data=ds_config.model_dump())
4644
return self.get(name)
4745

4846
def list(self) -> List[Datasource]:

minds/exceptions.py

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ class Unauthorized(Exception):
1818
class UnknownError(Exception):
1919
...
2020

21+
22+
class MindNameInvalid(Exception):
23+
...

minds/minds.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from typing import List, Union, Iterable
22
import utils
33
from openai import OpenAI
4-
4+
import minds.utils as utils
55
import minds.exceptions as exc
6-
76
from minds.datasources import Datasource, DatabaseConfig
87

98
DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\'s question: {{question}}'
@@ -22,7 +21,7 @@ def __init__(
2221
self.api = client.api
2322
self.client = client
2423
self.project = 'mindsdb'
25-
24+
2625
self.name = name
2726
self.model_name = model_name
2827
self.provider = provider
@@ -75,6 +74,9 @@ def update(
7574
:param parameters, dict: alter other parameters of the mind, optional
7675
"""
7776
data = {}
77+
78+
if name is not None:
79+
utils.validate_mind_name(name)
7880

7981
if datasources is not None:
8082
ds_names = []
@@ -201,7 +203,7 @@ def get(self, name: str) -> Mind:
201203
:param name: name of the mind
202204
:return: a mind object
203205
"""
204-
206+
205207
item = self.api.get(f'/projects/{self.project}/minds/{name}').json()
206208
return Mind(self.client, **item)
207209

@@ -228,6 +230,7 @@ def create(
228230
datasources=None,
229231
parameters=None,
230232
replace=False,
233+
update=False,
231234
) -> Mind:
232235
"""
233236
Create a new mind and return it
@@ -244,8 +247,12 @@ def create(
244247
:param datasources: list of datasources used by mind, optional
245248
:param parameters, dict: other parameters of the mind, optional
246249
:param replace: if true - to remove existing mind, default is false
250+
:param update: if true - to update mind if exists, default is false
247251
:return: created mind
248252
"""
253+
254+
if name is not None:
255+
utils.validate_mind_name(name)
249256

250257
if replace:
251258
try:
@@ -269,7 +276,12 @@ def create(
269276
if 'prompt_template' not in parameters:
270277
parameters['prompt_template'] = DEFAULT_PROMPT_TEMPLATE
271278

272-
self.api.post(
279+
if update:
280+
method = self.api.put
281+
else:
282+
method = self.api.post
283+
284+
method(
273285
f'/projects/{self.project}/minds',
274286
data={
275287
'name': name,

minds/rest_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def post(self, url, data):
5757
_raise_for_status(resp)
5858
return resp
5959

60+
def put(self, url, data):
61+
resp = requests.put(
62+
self.base_url + url,
63+
headers=self._headers(),
64+
json=data,
65+
)
66+
67+
_raise_for_status(resp)
68+
return resp
69+
6070
def patch(self, url, data):
6171
resp = requests.patch(
6272
self.base_url + url,

minds/utils.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
import minds.exceptions as exc
13
from urllib.parse import urlparse, urlunparse
24

35
def get_openai_base_url(base_url: str) -> str:
@@ -12,4 +14,27 @@ def get_openai_base_url(base_url: str) -> str:
1214
parsed = parsed._replace(path='', netloc=llm_host)
1315

1416
return urlunparse(parsed)
15-
17+
18+
19+
def validate_mind_name(mind_name):
20+
"""
21+
Validate the Mind name.
22+
23+
A valid Mind name should:
24+
- Start with a letter
25+
- Contain only letters, numbers, or underscores
26+
- Have a maximum length of 32 characters
27+
- Not contain spaces
28+
29+
Parameters:
30+
mind_name (str): The Mind name to validate.
31+
32+
Returns:
33+
bool: True if valid, False otherwise.
34+
"""
35+
# Regular expression pattern
36+
pattern = r'^[A-Za-z][A-Za-z0-9_]{0,31}$'
37+
38+
# Check if the Mind name matches the pattern
39+
if not re.match(pattern, mind_name):
40+
raise exc.MindNameInvalid("Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters. Spaces are not allowed.")

tests/integration/test_base_flow.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from minds.datasources.examples import example_ds
1111

12-
from minds.exceptions import ObjectNotFound
12+
from minds.exceptions import ObjectNotFound, MindNameInvalid
1313

1414

1515
def get_client():
@@ -37,7 +37,8 @@ def test_datasources():
3737

3838
# create
3939
ds = client.datasources.create(example_ds)
40-
ds = client.datasources.create(example_ds, replace=True)
40+
assert ds.name == example_ds.name
41+
ds = client.datasources.create(example_ds, update=True)
4142
assert ds.name == example_ds.name
4243

4344
# get
@@ -57,6 +58,7 @@ def test_minds():
5758
ds_name = 'test_datasource_'
5859
ds_name2 = 'test_datasource2_'
5960
mind_name = 'int_test_mind_'
61+
invalid_mind_name = 'mind-123'
6062
mind_name2 = 'int_test_mind2_'
6163
prompt1 = 'answer in german'
6264
prompt2 = 'answer in spanish'
@@ -79,6 +81,13 @@ def test_minds():
7981
ds2_cfg.tables = ['home_rentals']
8082

8183
# create
84+
with pytest.raises(MindNameInvalid):
85+
mind = client.minds.create(
86+
invalid_mind_name,
87+
datasources=[ds],
88+
provider='openai'
89+
)
90+
8291
mind = client.minds.create(
8392
mind_name,
8493
datasources=[ds],
@@ -90,11 +99,20 @@ def test_minds():
9099
datasources=[ds.name, ds2_cfg],
91100
prompt_template=prompt1
92101
)
102+
mind = client.minds.create(
103+
mind_name,
104+
update=True,
105+
datasources=[ds.name, ds2_cfg],
106+
prompt_template=prompt1
107+
)
93108

94109
# get
95110
mind = client.minds.get(mind_name)
96111
assert len(mind.datasources) == 2
97112
assert mind.prompt_template == prompt1
113+
114+
with pytest.raises(MindNameInvalid):
115+
client.minds.get(invalid_mind_name)
98116

99117
# list
100118
mind_list = client.minds.list()
@@ -106,6 +124,14 @@ def test_minds():
106124
datasources=[ds.name],
107125
prompt_template=prompt2
108126
)
127+
128+
with pytest.raises(MindNameInvalid):
129+
mind.update(
130+
name=invalid_mind_name,
131+
datasources=[ds.name],
132+
prompt_template=prompt2
133+
)
134+
109135
with pytest.raises(ObjectNotFound):
110136
# this name not exists
111137
client.minds.get(mind_name)
@@ -153,3 +179,6 @@ def test_minds():
153179
client.minds.drop(mind_name2)
154180
client.datasources.drop(ds.name)
155181
client.datasources.drop(ds2_cfg.name)
182+
183+
with pytest.raises(MindNameInvalid):
184+
client.minds.drop(invalid_mind_name)

tests/unit/test_unit.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ def _compare_ds(self, ds1, ds2):
3636
assert ds1.tables == ds2.tables
3737

3838
@patch('requests.get')
39+
@patch('requests.put')
3940
@patch('requests.post')
4041
@patch('requests.delete')
41-
def test_create_datasources(self, mock_del, mock_post, mock_get):
42+
def test_create_datasources(self, mock_del, mock_post, mock_put, mock_get):
4243
client = get_client()
4344
response_mock(mock_get, example_ds.model_dump())
4445

@@ -53,12 +54,9 @@ def check_ds_created(ds, mock_post):
5354

5455
check_ds_created(ds, mock_post)
5556

56-
# with replace
57-
ds = client.datasources.create(example_ds, replace=True)
58-
args, _ = mock_del.call_args
59-
assert args[0].endswith(f'/api/datasources/{example_ds.name}')
60-
61-
check_ds_created(ds, mock_post)
57+
# with update
58+
ds = client.datasources.create(example_ds, update=True)
59+
check_ds_created(ds, mock_put)
6260

6361
@patch('requests.get')
6462
def test_get_datasource(self, mock_get):
@@ -115,9 +113,10 @@ def compare_mind(self, mind, mind_json):
115113
assert mind.parameters == mind_json['parameters']
116114

117115
@patch('requests.get')
116+
@patch('requests.put')
118117
@patch('requests.post')
119118
@patch('requests.delete')
120-
def test_create(self, mock_del, mock_post, mock_get):
119+
def test_create(self, mock_del, mock_post, mock_put, mock_get):
121120
client = get_client()
122121

123122
mind_name = 'test_mind'
@@ -145,7 +144,7 @@ def check_mind_created(mind, mock_post, create_params):
145144

146145
check_mind_created(mind, mock_post, create_params)
147146

148-
# with replace
147+
# -- with replace --
149148
create_params = {
150149
'name': mind_name,
151150
'prompt_template': prompt_template,
@@ -159,6 +158,14 @@ def check_mind_created(mind, mock_post, create_params):
159158

160159
check_mind_created(mind, mock_post, create_params)
161160

161+
# -- with update --
162+
mock_del.reset_mock()
163+
mind = client.minds.create(update=True, **create_params)
164+
# is not deleted
165+
assert not mock_del.called
166+
167+
check_mind_created(mind, mock_put, create_params)
168+
162169
@patch('requests.get')
163170
@patch('requests.patch')
164171
def test_update(self, mock_patch, mock_get):

0 commit comments

Comments
 (0)