Skip to content

Commit 44ea99a

Browse files
updated the base usage examples
1 parent c589a56 commit 44ea99a

File tree

1 file changed

+107
-56
lines changed

1 file changed

+107
-56
lines changed

examples/base_usage.py

Lines changed: 107 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
from minds.client import Client
2+
from openai import OpenAI
23

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

6-
# or use not default server
7-
base_url = 'https://custom_cloud.mdb.ai/'
8-
client = Client("YOUR_API_KEY", base_url)
5+
# Basic Setup and Workflow
96

7+
# connect
8+
API_KEY = "YOUR_API_KEY"
9+
BASE_URL = 'https://custom_cloud.mdb.ai/api/v1' # optional, if you use custom server
1010

11-
# create datasource
12-
from minds.datasources import DatabaseConfig
11+
client = Client(API_KEY)
1312

14-
postgres_config = DatabaseConfig(
13+
# or with custom base URL
14+
client = Client(API_KEY, base_url=BASE_URL)
15+
16+
# create Datasource
17+
datasource = client.datasources.create(
1518
name='my_datasource',
16-
description='<DESCRIPTION-OF-YOUR-DATA>',
19+
description='House sales data',
1720
engine='postgres',
1821
connection_data={
1922
'user': 'demo_user',
@@ -22,80 +25,128 @@
2225
'port': 5432,
2326
'database': 'demo',
2427
'schema': 'demo_data'
25-
},
26-
tables=['<TABLE-1>', '<TABLE-2>']
28+
}
2729
)
2830

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] )
31+
# create Mind
32+
mind = client.minds.create(
33+
name='mind_name',
34+
datasources=[
35+
{
36+
'name': datasource.name,
37+
'tables': ['house_sales']
38+
}
39+
]
40+
)
3941

40-
# with prompt template
41-
mind = client.minds.create(name='mind_name', prompt_template='You are codding assistant')
42+
# or add to existing Mind
43+
mind = client.minds.create(name='mind_name')
44+
mind.add_datasource(datasource.name, tables=['house_sales'])
45+
46+
# chat with the Mind using the OpenAI-compatible Completions API (without streaming)
47+
openai_client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
48+
completion = openai_client.chat.completions.create(
49+
model=mind.name,
50+
messages=[
51+
{'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'}
52+
],
53+
stream=False
54+
)
55+
print(completion.choices[0].message.content)
56+
57+
# with streaming
58+
completion_stream = openai_client.chat.completions.create(
59+
model=mind.name,
60+
messages=[
61+
{'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'}
62+
],
63+
stream=True
64+
)
65+
for chunk in completion_stream:
66+
print(chunk.choices[0].delta)
4267

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])
68+
# or chat with the Mind directly (without streaming)
69+
response = mind.completion('How many three-bedroom houses were sold in 2008?')
70+
print(response)
4771

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)
72+
# with streaming
73+
for chunk in mind.completion('How many three-bedroom houses were sold in 2008?', stream=True):
74+
print(chunk)
5475

5576

56-
# --- managing minds ---
77+
# Mind Management
5778

5879
# create or replace
59-
mind = client.minds.create(name='mind_name', replace=True, datasources=[postgres_config] )
80+
mind = client.minds.create(
81+
name='mind_name',
82+
datasources=[
83+
{
84+
'name': datasource.name,
85+
'tables': ['house_sales']
86+
}
87+
],
88+
replace=True
89+
)
6090

6191
# update
62-
mind.update(
63-
name='mind_name', # is required
64-
datasources=[postgres_config] # it will replace current datasource list
92+
mind = client.minds.update(
93+
name='mind_name', # required
94+
new_name='new_mind_name', # optional
95+
datasources=[ # optional
96+
{
97+
'name': datasource.name,
98+
'tables': ['home_rentals']
99+
}
100+
],
65101
)
66102

67103
# list
68-
print(client.minds.list())
104+
minds = client.minds.list()
69105

70-
# get by name
106+
# get
71107
mind = client.minds.get('mind_name')
72108

73-
# removing datasource
74-
mind.del_datasource(datasource)
75-
76-
# remove mind
109+
# remove
77110
client.minds.drop('mind_name')
78111

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 ---
112+
# Datasource Management
87113

88114
# create or replace
89-
datasource = client.datasources.create(postgres_config, replace=True)
115+
datasource = client.datasources.create(
116+
name='my_datasource',
117+
description='House sales data',
118+
engine='postgres',
119+
connection_data={
120+
'user': 'demo_user',
121+
'password': 'demo_password',
122+
'host': 'samples.mindsdb.com',
123+
'port': 5432,
124+
'database': 'demo',
125+
'schema': 'demo_data'
126+
},
127+
replace=True
128+
)
90129

130+
# update
131+
datasource = client.datasources.update(
132+
name='my_datasource',
133+
new_name='updated_datasource',
134+
description='Updated House sales data',
135+
connection_data={
136+
'user': 'demo_user',
137+
'password': 'demo_password',
138+
'host': 'samples.mindsdb.com',
139+
'port': 5432,
140+
'database': 'demo',
141+
'schema': 'demo_data'
142+
}
143+
)
91144

92145
# list
93-
print(client.datasources.list())
146+
datasources = client.datasources.list()
94147

95148
# get
96149
datasource = client.datasources.get('my_datasource')
97150

98151
# remove
99152
client.datasources.drop('my_datasource')
100-
101-

0 commit comments

Comments
 (0)