Skip to content

Commit 1d6b9ff

Browse files
Merge pull request #81 from mindsdb/feature/api_versioning
Updated SDK Compatibility with Migrated Minds API
2 parents 3225ac8 + 250a2e7 commit 1d6b9ff

File tree

11 files changed

+807
-846
lines changed

11 files changed

+807
-846
lines changed

examples/base_usage.py

Lines changed: 129 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
import time
2+
3+
from openai import OpenAI
4+
15
from minds.client import Client
26

3-
# --- connect ---
4-
client = Client("YOUR_API_KEY")
57

6-
# or use not default server
7-
base_url = 'https://custom_cloud.mdb.ai/'
8-
client = Client("YOUR_API_KEY", base_url)
8+
# Basic Setup and Workflow
9+
10+
# connect
11+
API_KEY = "YOUR_API_KEY"
12+
BASE_URL = 'https://custom_cloud.mdb.ai/api/v1' # optional, if you use custom server
913

14+
client = Client(API_KEY)
1015

11-
# create datasource
12-
from minds.datasources import DatabaseConfig
16+
# or with custom base URL
17+
client = Client(API_KEY, base_url=BASE_URL)
1318

14-
postgres_config = DatabaseConfig(
19+
# create Datasource
20+
datasource = client.datasources.create(
1521
name='my_datasource',
16-
description='<DESCRIPTION-OF-YOUR-DATA>',
22+
description='House sales data',
1723
engine='postgres',
1824
connection_data={
1925
'user': 'demo_user',
@@ -22,80 +28,147 @@
2228
'port': 5432,
2329
'database': 'demo',
2430
'schema': 'demo_data'
25-
},
26-
tables=['<TABLE-1>', '<TABLE-2>']
31+
}
2732
)
2833

29-
# using sample config
30-
from minds.datasources.examples import example_ds
31-
32-
# create mind
33-
# with datasource at the same time
34-
mind = client.minds.create(name='mind_name', datasources=[postgres_config] )
35-
36-
# or separately
37-
datasource = client.datasources.create(postgres_config)
38-
mind = client.minds.create(name='mind_name', datasources=[datasource] )
34+
# create Mind
35+
mind = client.minds.create(
36+
name='mind_name',
37+
datasources=[
38+
{
39+
'name': datasource.name,
40+
'tables': ['house_sales']
41+
}
42+
]
43+
)
3944

40-
# with prompt template
41-
mind = client.minds.create(name='mind_name', prompt_template='You are codding assistant')
45+
# or add to existing Mind
46+
mind = client.minds.create(name='mind_name')
47+
mind.add_datasource(datasource.name, tables=['house_sales'])
48+
49+
# wait until Mind is ready
50+
def wait_for_mind(mind):
51+
status = mind.status
52+
while status != 'COMPLETED':
53+
print(f'Mind status: {status}')
54+
time.sleep(3)
55+
mind = client.minds.get(mind.name)
56+
status = mind.status
57+
58+
if status == 'FAILED':
59+
raise Exception('Mind creation failed')
60+
61+
print('Mind creation successful!')
62+
63+
wait_for_mind(mind)
64+
65+
# chat with the Mind using the OpenAI-compatible Completions API (without streaming)
66+
openai_client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
67+
68+
completion = openai_client.chat.completions.create(
69+
model=mind.name,
70+
messages=[
71+
{'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'}
72+
],
73+
stream=False
74+
)
75+
print(completion.choices[0].message.content)
76+
77+
# with streaming
78+
completion_stream = openai_client.chat.completions.create(
79+
model=mind.name,
80+
messages=[
81+
{'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'}
82+
],
83+
stream=True
84+
)
85+
for chunk in completion_stream:
86+
print(chunk.choices[0].delta.content)
4287

43-
# restrict tables for datasource in context of the mind:
44-
from minds.datasources.datasources import DatabaseTables
45-
datasource = DatabaseTables(name='my_datasource', tables=['table1', 'table1'])
46-
mind = client.minds.create(name='mind_name', datasources=[datasource])
88+
# or chat with the Mind directly (without streaming)
89+
response = mind.completion('How many three-bedroom houses were sold in 2008?')
90+
print(response)
4791

48-
# or add to existed mind
49-
mind = client.minds.create(name='mind_name')
50-
# by config
51-
mind.add_datasource(postgres_config)
52-
# or by datasource
53-
mind.add_datasource(datasource)
92+
# with streaming
93+
for chunk in mind.completion('How many three-bedroom houses were sold in 2008?', stream=True):
94+
print(chunk)
5495

5596

56-
# --- managing minds ---
97+
# Mind Management
5798

5899
# create or replace
59-
mind = client.minds.create(name='mind_name', replace=True, datasources=[postgres_config] )
100+
mind = client.minds.create(
101+
name='mind_name',
102+
datasources=[
103+
{
104+
'name': datasource.name,
105+
'tables': ['home_rentals']
106+
}
107+
],
108+
replace=True
109+
)
110+
wait_for_mind(mind)
60111

61112
# update
62-
mind.update(
63-
name='mind_name', # is required
64-
datasources=[postgres_config] # it will replace current datasource list
113+
mind = client.minds.update(
114+
name='mind_name', # required
115+
new_name='new_mind_name', # optional
116+
datasources=[ # optional
117+
{
118+
'name': datasource.name,
119+
'tables': ['home_rentals']
120+
}
121+
],
65122
)
123+
wait_for_mind(mind)
66124

67125
# list
68-
print(client.minds.list())
126+
minds = client.minds.list()
69127

70-
# get by name
128+
# get
71129
mind = client.minds.get('mind_name')
72130

73-
# removing datasource
74-
mind.del_datasource(datasource)
75-
76-
# remove mind
131+
# remove
77132
client.minds.drop('mind_name')
78133

79-
# call completion
80-
print(mind.completion('2+3'))
81-
82-
# stream completion
83-
for chunk in mind.completion('2+3', stream=True):
84-
print(chunk.content)
85-
86-
# --- managing datasources ---
134+
# Datasource Management
87135

88136
# create or replace
89-
datasource = client.datasources.create(postgres_config, replace=True)
137+
datasource = client.datasources.create(
138+
name='my_datasource',
139+
description='House sales data',
140+
engine='postgres',
141+
connection_data={
142+
'user': 'demo_user',
143+
'password': 'demo_password',
144+
'host': 'samples.mindsdb.com',
145+
'port': 5432,
146+
'database': 'demo',
147+
'schema': 'demo_data'
148+
},
149+
replace=True
150+
)
90151

152+
# update
153+
datasource = client.datasources.update(
154+
name='my_datasource',
155+
new_name='updated_datasource',
156+
description='Updated House sales data',
157+
connection_data={
158+
'user': 'demo_user',
159+
'password': 'demo_password',
160+
'host': 'samples.mindsdb.com',
161+
'port': 5432,
162+
'database': 'demo',
163+
'schema': 'demo_data'
164+
}
165+
)
91166

92167
# list
93-
print(client.datasources.list())
168+
datasources = client.datasources.list()
94169

95170
# get
96171
datasource = client.datasources.get('my_datasource')
97172

98173
# remove
99174
client.datasources.drop('my_datasource')
100-
101-

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.3.3"
3+
__version__ = "2.0.0"
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/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
class Client:
1010

11-
def __init__(self, api_key, base_url=None):
11+
def __init__(self, api_key, base_url=None, version='v1'):
1212

13-
self.api = RestAPI(api_key, base_url)
13+
self.api = RestAPI(api_key, base_url, version)
1414

1515
self.datasources = Datasources(self)
16-
self.knowledge_bases = KnowledgeBases(self)
16+
# self.knowledge_bases = KnowledgeBases(self)
1717

1818
self.minds = Minds(self)

0 commit comments

Comments
 (0)