Skip to content

Commit 6a850fe

Browse files
Merge pull request #31 from Better-Boy/mind-name-validation
added validation for mind name
2 parents 68b979a + 5667fcc commit 6a850fe

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

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: 9 additions & 5 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
@@ -74,6 +72,9 @@ def update(
7472
:param parameters, dict: alter other parameters of the mind, optional
7573
"""
7674
data = {}
75+
76+
if name is not None:
77+
utils.validate_mind_name(name)
7778

7879
if datasources is not None:
7980
ds_names = []
@@ -216,7 +217,7 @@ def get(self, name: str) -> Mind:
216217
:param name: name of the mind
217218
:return: a mind object
218219
"""
219-
220+
220221
item = self.api.get(f'/projects/{self.project}/minds/{name}').json()
221222
return Mind(self.client, **item)
222223

@@ -263,6 +264,9 @@ def create(
263264
:param update: if true - to update mind if exists, default is false
264265
:return: created mind
265266
"""
267+
268+
if name is not None:
269+
utils.validate_mind_name(name)
266270

267271
if replace:
268272
try:

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: 23 additions & 1 deletion
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():
@@ -58,6 +58,7 @@ def test_minds():
5858
ds_name = 'test_datasource_'
5959
ds_name2 = 'test_datasource2_'
6060
mind_name = 'int_test_mind_'
61+
invalid_mind_name = 'mind-123'
6162
mind_name2 = 'int_test_mind2_'
6263
prompt1 = 'answer in german'
6364
prompt2 = 'answer in spanish'
@@ -80,6 +81,13 @@ def test_minds():
8081
ds2_cfg.tables = ['home_rentals']
8182

8283
# create
84+
with pytest.raises(MindNameInvalid):
85+
mind = client.minds.create(
86+
invalid_mind_name,
87+
datasources=[ds],
88+
provider='openai'
89+
)
90+
8391
mind = client.minds.create(
8492
mind_name,
8593
datasources=[ds],
@@ -102,6 +110,9 @@ def test_minds():
102110
mind = client.minds.get(mind_name)
103111
assert len(mind.datasources) == 2
104112
assert mind.prompt_template == prompt1
113+
114+
with pytest.raises(MindNameInvalid):
115+
client.minds.get(invalid_mind_name)
105116

106117
# list
107118
mind_list = client.minds.list()
@@ -113,6 +124,14 @@ def test_minds():
113124
datasources=[ds.name],
114125
prompt_template=prompt2
115126
)
127+
128+
with pytest.raises(MindNameInvalid):
129+
mind.update(
130+
name=invalid_mind_name,
131+
datasources=[ds.name],
132+
prompt_template=prompt2
133+
)
134+
116135
with pytest.raises(ObjectNotFound):
117136
# this name not exists
118137
client.minds.get(mind_name)
@@ -160,3 +179,6 @@ def test_minds():
160179
client.minds.drop(mind_name2)
161180
client.datasources.drop(ds.name)
162181
client.datasources.drop(ds2_cfg.name)
182+
183+
with pytest.raises(MindNameInvalid):
184+
client.minds.drop(invalid_mind_name)

0 commit comments

Comments
 (0)