|
| 1 | +import time |
| 2 | + |
| 3 | +from openai import OpenAI |
| 4 | + |
1 | 5 | from minds.client import Client |
2 | 6 |
|
3 | | -# --- connect --- |
4 | | -client = Client("YOUR_API_KEY") |
5 | 7 |
|
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 |
9 | 13 |
|
| 14 | +client = Client(API_KEY) |
10 | 15 |
|
11 | | -# create datasource |
12 | | -from minds.datasources import DatabaseConfig |
| 16 | +# or with custom base URL |
| 17 | +client = Client(API_KEY, base_url=BASE_URL) |
13 | 18 |
|
14 | | -postgres_config = DatabaseConfig( |
| 19 | +# create Datasource |
| 20 | +datasource = client.datasources.create( |
15 | 21 | name='my_datasource', |
16 | | - description='<DESCRIPTION-OF-YOUR-DATA>', |
| 22 | + description='House sales data', |
17 | 23 | engine='postgres', |
18 | 24 | connection_data={ |
19 | 25 | 'user': 'demo_user', |
|
22 | 28 | 'port': 5432, |
23 | 29 | 'database': 'demo', |
24 | 30 | 'schema': 'demo_data' |
25 | | - }, |
26 | | - tables=['<TABLE-1>', '<TABLE-2>'] |
| 31 | + } |
27 | 32 | ) |
28 | 33 |
|
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 | +) |
39 | 44 |
|
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) |
42 | 87 |
|
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) |
47 | 91 |
|
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) |
54 | 95 |
|
55 | 96 |
|
56 | | -# --- managing minds --- |
| 97 | +# Mind Management |
57 | 98 |
|
58 | 99 | # 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) |
60 | 111 |
|
61 | 112 | # 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 | + ], |
65 | 122 | ) |
| 123 | +wait_for_mind(mind) |
66 | 124 |
|
67 | 125 | # list |
68 | | -print(client.minds.list()) |
| 126 | +minds = client.minds.list() |
69 | 127 |
|
70 | | -# get by name |
| 128 | +# get |
71 | 129 | mind = client.minds.get('mind_name') |
72 | 130 |
|
73 | | -# removing datasource |
74 | | -mind.del_datasource(datasource) |
75 | | - |
76 | | -# remove mind |
| 131 | +# remove |
77 | 132 | client.minds.drop('mind_name') |
78 | 133 |
|
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 |
87 | 135 |
|
88 | 136 | # 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 | +) |
90 | 151 |
|
| 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 | +) |
91 | 166 |
|
92 | 167 | # list |
93 | | -print(client.datasources.list()) |
| 168 | +datasources = client.datasources.list() |
94 | 169 |
|
95 | 170 | # get |
96 | 171 | datasource = client.datasources.get('my_datasource') |
97 | 172 |
|
98 | 173 | # remove |
99 | 174 | client.datasources.drop('my_datasource') |
100 | | - |
101 | | - |
|
0 commit comments