Skip to content

Commit 9918cbd

Browse files
authored
Merge branch 'main' into main
2 parents 77388ad + dcbe1e5 commit 9918cbd

File tree

11 files changed

+171
-55
lines changed

11 files changed

+171
-55
lines changed

.github/workflows/cla.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "MindsDB CLA Assistant"
2+
on:
3+
issue_comment:
4+
types: [created]
5+
pull_request_target:
6+
types: [opened,closed,synchronize]
7+
jobs:
8+
CLAssistant:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: "CLA Assistant"
12+
if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target'
13+
uses: contributor-assistant/[email protected]
14+
env:
15+
GITHUB_TOKEN: ${{ secrets.CLA_TOKEN }}
16+
PERSONAL_ACCESS_TOKEN : ${{ secrets.CLA_TOKEN }}
17+
with:
18+
path-to-signatures: 'assets/contributions-agreement/cla.json'
19+
# Add path to the CLA here
20+
path-to-document: 'https://github.com/mindsdb/mindsdb/blob/main/assets/contributions-agreement/individual-contributor.md'
21+
branch: 'cla'
22+
allowlist: bot*, ZoranPandovski, torrmal, Stpmax, mindsdbadmin, ea-rus, tmichaeldb, dusvyat, hamishfagg, MinuraPunchihewa, martyna-mindsdb, lucas-koontz

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ client.datasources.drop('my_datasource')
149149
```
150150
>Note: The SDK currently does not support automatically removing a data source if it is no longer connected to any mind.
151151
152-
# Other SDK's
153152

154-
Go SDK : https://github.com/Abiji-2020/minds-go-sdk
155153

154+
155+
### Other SDKs
156+
#### [Command-Line](https://github.com/Better-Boy/minds-cli-sdk)
157+
#### [Go SDK](https://github.com/Abiji-2020/minds-go-sdk)

assets/contributions-agreement/cla.json

Whitespace-only changes.

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: 13 additions & 11 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)
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]:
@@ -76,11 +74,15 @@ def get(self, name: str) -> Datasource:
7674
raise exc.ObjectNotSupported(f'Wrong type of datasource: {name}')
7775
return Datasource(**data)
7876

79-
def drop(self, name: str):
77+
def drop(self, name: str, force=False):
8078
"""
8179
Drop datasource by name
8280
8381
:param name: name of datasource
82+
:param force: if True - remove from all minds, default: False
8483
"""
84+
data = None
85+
if force:
86+
data = {'cascade': True}
8587

86-
self.api.delete(f'/datasources/{name}')
88+
self.api.delete(f'/datasources/{name}', data=data)

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: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
from typing import List, Union, Iterable
2-
from urllib.parse import urlparse, urlunparse
3-
from datetime import datetime
4-
2+
import utils
53
from openai import OpenAI
6-
4+
import minds.utils as utils
75
import minds.exceptions as exc
8-
96
from minds.datasources import Datasource, DatabaseConfig
107

118
DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\'s question: {{question}}'
129

13-
1410
class Mind:
1511
def __init__(
1612
self, client, name,
@@ -25,7 +21,7 @@ def __init__(
2521
self.api = client.api
2622
self.client = client
2723
self.project = 'mindsdb'
28-
24+
2925
self.name = name
3026
self.model_name = model_name
3127
self.provider = provider
@@ -35,7 +31,11 @@ def __init__(
3531
self.parameters = parameters
3632
self.created_at = created_at
3733
self.updated_at = updated_at
38-
34+
base_url = utils.get_openai_base_url(self.api.base_url)
35+
self.openai_client = OpenAI(
36+
api_key=self.api.api_key,
37+
base_url=base_url
38+
)
3939
self.datasources = datasources
4040

4141
def __repr__(self):
@@ -74,6 +74,9 @@ def update(
7474
:param parameters, dict: alter other parameters of the mind, optional
7575
"""
7676
data = {}
77+
78+
if name is not None:
79+
utils.validate_mind_name(name)
7780

7881
if datasources is not None:
7982
ds_names = []
@@ -156,23 +159,7 @@ def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[
156159
157160
:return: string if stream mode is off or iterator of ChoiceDelta objects (by openai)
158161
"""
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(
162+
response = self.openai_client.chat.completions.create(
176163
model=self.name,
177164
messages=[
178165
{'role': 'user', 'content': message}
@@ -216,7 +203,7 @@ def get(self, name: str) -> Mind:
216203
:param name: name of the mind
217204
:return: a mind object
218205
"""
219-
206+
220207
item = self.api.get(f'/projects/{self.project}/minds/{name}').json()
221208
return Mind(self.client, **item)
222209

@@ -243,6 +230,7 @@ def create(
243230
datasources=None,
244231
parameters=None,
245232
replace=False,
233+
update=False,
246234
) -> Mind:
247235
"""
248236
Create a new mind and return it
@@ -259,8 +247,12 @@ def create(
259247
:param datasources: list of datasources used by mind, optional
260248
:param parameters, dict: other parameters of the mind, optional
261249
:param replace: if true - to remove existing mind, default is false
250+
:param update: if true - to update mind if exists, default is false
262251
:return: created mind
263252
"""
253+
254+
if name is not None:
255+
utils.validate_mind_name(name)
264256

265257
if replace:
266258
try:
@@ -284,7 +276,12 @@ def create(
284276
if 'prompt_template' not in parameters:
285277
parameters['prompt_template'] = DEFAULT_PROMPT_TEMPLATE
286278

287-
self.api.post(
279+
if update:
280+
method = self.api.put
281+
else:
282+
method = self.api.post
283+
284+
method(
288285
f'/projects/{self.project}/minds',
289286
data={
290287
'name': name,

minds/rest_api.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ def get(self, url):
3737
_raise_for_status(resp)
3838
return resp
3939

40-
def delete(self, url):
41-
resp = requests.delete(self.base_url + url, headers=self._headers())
40+
def delete(self, url, data=None):
41+
resp = requests.delete(
42+
self.base_url + url,
43+
headers=self._headers(),
44+
json=data
45+
)
4246

4347
_raise_for_status(resp)
4448
return resp
@@ -53,6 +57,16 @@ def post(self, url, data):
5357
_raise_for_status(resp)
5458
return resp
5559

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+
5670
def patch(self, url, data):
5771
resp = requests.patch(
5872
self.base_url + url,

minds/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import re
2+
import minds.exceptions as exc
3+
from urllib.parse import urlparse, urlunparse
4+
5+
def get_openai_base_url(base_url: str) -> str:
6+
parsed = urlparse(base_url)
7+
8+
netloc = parsed.netloc
9+
if netloc == 'mdb.ai':
10+
llm_host = 'llm.mdb.ai'
11+
else:
12+
llm_host = 'ai.' + netloc
13+
14+
parsed = parsed._replace(path='', netloc=llm_host)
15+
16+
return urlunparse(parsed)
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

Lines changed: 32 additions & 3 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():
@@ -31,13 +31,14 @@ def test_datasources():
3131

3232
# remove previous object
3333
try:
34-
client.datasources.drop(example_ds.name)
34+
client.datasources.drop(example_ds.name, force=True)
3535
except ObjectNotFound:
3636
...
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)

0 commit comments

Comments
 (0)