Skip to content

Commit c124e16

Browse files
authored
Merge branch 'mindsdb:main' into patch-1
2 parents e429e99 + bac1981 commit c124e16

File tree

8 files changed

+111
-27
lines changed

8 files changed

+111
-27
lines changed

minds/__about__.py

Lines changed: 1 addition & 1 deletion
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__ = '[email protected]'
66
__author__ = 'MindsDB Inc'

minds/datasources/datasources.py

Lines changed: 7 additions & 9 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from typing import List, Union, Iterable
22
from urllib.parse import urlparse, urlunparse
3-
from datetime import datetime
43

54
from openai import OpenAI
6-
5+
import minds.utils as utils
76
import minds.exceptions as exc
8-
97
from minds.datasources import Datasource, DatabaseConfig
108

119
DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\'s question: {{question}}'
@@ -25,7 +23,7 @@ def __init__(
2523
self.api = client.api
2624
self.client = client
2725
self.project = 'mindsdb'
28-
26+
2927
self.name = name
3028
self.model_name = model_name
3129
self.provider = provider
@@ -73,6 +71,9 @@ def update(
7371
:param parameters, dict: alter other parameters of the mind, optional
7472
"""
7573
data = {}
74+
75+
if name is not None:
76+
utils.validate_mind_name(name)
7677

7778
if datasources is not None:
7879
ds_names = []
@@ -226,7 +227,7 @@ def get(self, name: str) -> Mind:
226227
:param name: name of the mind
227228
:return: a mind object
228229
"""
229-
230+
230231
item = self.api.get(f'/projects/{self.project}/minds/{name}').json()
231232
return Mind(self.client, **item)
232233

@@ -253,6 +254,7 @@ def create(
253254
datasources=None,
254255
parameters=None,
255256
replace=False,
257+
update=False,
256258
) -> Mind:
257259
"""
258260
Create a new mind and return it
@@ -269,8 +271,12 @@ def create(
269271
:param datasources: list of datasources used by mind, optional
270272
:param parameters, dict: other parameters of the mind, optional
271273
:param replace: if true - to remove existing mind, default is false
274+
:param update: if true - to update mind if exists, default is false
272275
:return: created mind
273276
"""
277+
278+
if name is not None:
279+
utils.validate_mind_name(name)
274280

275281
if replace:
276282
try:
@@ -294,7 +300,12 @@ def create(
294300
if 'prompt_template' not in parameters:
295301
parameters['prompt_template'] = DEFAULT_PROMPT_TEMPLATE
296302

297-
self.api.post(
303+
if update:
304+
method = self.api.put
305+
else:
306+
method = self.api.post
307+
308+
method(
298309
f'/projects/{self.project}/minds',
299310
data={
300311
'name': name,

minds/rest_api.py

Lines changed: 10 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import re
2+
import minds.exceptions as exc
3+
4+
def validate_mind_name(mind_name):
5+
"""
6+
Validate the Mind name.
7+
8+
A valid Mind name should:
9+
- Start with a letter
10+
- Contain only letters, numbers, or underscores
11+
- Have a maximum length of 32 characters
12+
- Not contain spaces
13+
14+
Parameters:
15+
mind_name (str): The Mind name to validate.
16+
17+
Returns:
18+
bool: True if valid, False otherwise.
19+
"""
20+
# Regular expression pattern
21+
pattern = r'^[A-Za-z][A-Za-z0-9_]{0,31}$'
22+
23+
# Check if the Mind name matches the pattern
24+
if not re.match(pattern, mind_name):
25+
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.")
26+

tests/integration/test_base_flow.py

Lines changed: 31 additions & 2 deletions
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

Lines changed: 16 additions & 9 deletions
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)