|
| 1 | +# Minds SDK |
| 2 | + |
| 3 | +> **⚠️ API Version Notice** |
| 4 | +> |
| 5 | +> This documentation reflects the **current SDK version**, compatible with the **latest Minds Cloud API**. |
| 6 | +> |
| 7 | +> For users of the **legacy Minds Demo environment** at https://demo.mdb.ai, please refer to the [legacy documentation](README-v1.md) for compatibility. |
| 8 | +> |
| 9 | +> **Current Version**: v2.x+ (New API for Minds Cloud environments) |
| 10 | +> **Legacy Version**: v1.x ([Legacy Documentation](README-v1.md)) |
| 11 | +
|
| 12 | +### Installation |
| 13 | + |
| 14 | +To install the SDK, use pip: |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install minds-sdk |
| 18 | +``` |
| 19 | + |
| 20 | +### Getting Started |
| 21 | + |
| 22 | +#### 1. Initialize the Client |
| 23 | + |
| 24 | +To get started, you'll need to initialize the Client with your API key. If you're using a different server, you can also specify a custom base URL. |
| 25 | + |
| 26 | +```python |
| 27 | +from minds.client import Client |
| 28 | + |
| 29 | +# Default connection to Minds Cloud |
| 30 | +API_KEY = "YOUR_API_KEY" |
| 31 | +client = Client(API_KEY) |
| 32 | + |
| 33 | +# Or with custom base URL |
| 34 | +BASE_URL = 'https://custom_cloud.mdb.ai/api/v1' |
| 35 | +client = Client(API_KEY, base_url=BASE_URL) |
| 36 | +``` |
| 37 | + |
| 38 | +#### 2. Creating a Datasource |
| 39 | + |
| 40 | +Create a datasource to connect to your database: |
| 41 | + |
| 42 | +```python |
| 43 | +# Create datasource |
| 44 | +datasource = client.datasources.create( |
| 45 | + name='my_datasource', |
| 46 | + description='House sales data', |
| 47 | + engine='postgres', |
| 48 | + connection_data={ |
| 49 | + 'user': 'demo_user', |
| 50 | + 'password': 'demo_password', |
| 51 | + 'host': 'samples.mindsdb.com', |
| 52 | + 'port': 5432, |
| 53 | + 'database': 'demo', |
| 54 | + 'schema': 'demo_data' |
| 55 | + } |
| 56 | +) |
| 57 | +``` |
| 58 | + |
| 59 | +#### 3. Creating a Mind |
| 60 | + |
| 61 | +Create a mind and associate it with your datasource: |
| 62 | + |
| 63 | +```python |
| 64 | +# Create mind with datasource |
| 65 | +mind = client.minds.create( |
| 66 | + name='mind_name', |
| 67 | + datasources=[ |
| 68 | + { |
| 69 | + 'name': datasource.name, |
| 70 | + 'tables': ['house_sales'] |
| 71 | + } |
| 72 | + ] |
| 73 | +) |
| 74 | + |
| 75 | +# Or add datasource to existing mind |
| 76 | +mind = client.minds.create(name='mind_name') |
| 77 | +mind.add_datasource(datasource.name, tables=['house_sales']) |
| 78 | +``` |
| 79 | + |
| 80 | +#### 4. Wait for Mind to be Ready |
| 81 | + |
| 82 | +Before using your mind, wait for it to complete processing: |
| 83 | + |
| 84 | +```python |
| 85 | +import time |
| 86 | + |
| 87 | +def wait_for_mind(mind): |
| 88 | + status = mind.status |
| 89 | + while status != 'COMPLETED': |
| 90 | + print(f'Mind status: {status}') |
| 91 | + time.sleep(3) |
| 92 | + mind = client.minds.get(mind.name) |
| 93 | + status = mind.status |
| 94 | + |
| 95 | + if status == 'FAILED': |
| 96 | + raise Exception('Mind creation failed') |
| 97 | + |
| 98 | + print('Mind creation successful!') |
| 99 | + |
| 100 | +wait_for_mind(mind) |
| 101 | +``` |
| 102 | + |
| 103 | +### Using Your Mind |
| 104 | + |
| 105 | +#### Chat with OpenAI-Compatible API |
| 106 | + |
| 107 | +You can use the OpenAI client to interact with your minds: |
| 108 | + |
| 109 | +```python |
| 110 | +from openai import OpenAI |
| 111 | + |
| 112 | +# Initialize OpenAI client |
| 113 | +openai_client = OpenAI(api_key=API_KEY, base_url=BASE_URL) |
| 114 | + |
| 115 | +# Chat without streaming |
| 116 | +completion = openai_client.chat.completions.create( |
| 117 | + model=mind.name, |
| 118 | + messages=[ |
| 119 | + {'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'} |
| 120 | + ], |
| 121 | + stream=False |
| 122 | +) |
| 123 | +print(completion.choices[0].message.content) |
| 124 | + |
| 125 | +# Chat with streaming |
| 126 | +completion_stream = openai_client.chat.completions.create( |
| 127 | + model=mind.name, |
| 128 | + messages=[ |
| 129 | + {'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'} |
| 130 | + ], |
| 131 | + stream=True |
| 132 | +) |
| 133 | +for chunk in completion_stream: |
| 134 | + print(chunk.choices[0].delta.content) |
| 135 | +``` |
| 136 | + |
| 137 | +#### Direct Mind Completion |
| 138 | + |
| 139 | +You can also interact directly with the mind: |
| 140 | + |
| 141 | +```python |
| 142 | +# Without streaming |
| 143 | +response = mind.completion('How many three-bedroom houses were sold in 2008?') |
| 144 | +print(response) |
| 145 | + |
| 146 | +# With streaming |
| 147 | +for chunk in mind.completion('How many three-bedroom houses were sold in 2008?', stream=True): |
| 148 | + print(chunk) |
| 149 | +``` |
| 150 | + |
| 151 | +### Managing Minds |
| 152 | + |
| 153 | +#### Create or Replace |
| 154 | + |
| 155 | +Create a new mind or replace an existing one: |
| 156 | + |
| 157 | +```python |
| 158 | +mind = client.minds.create( |
| 159 | + name='mind_name', |
| 160 | + datasources=[ |
| 161 | + { |
| 162 | + 'name': datasource.name, |
| 163 | + 'tables': ['home_rentals'] |
| 164 | + } |
| 165 | + ], |
| 166 | + replace=True |
| 167 | +) |
| 168 | +wait_for_mind(mind) |
| 169 | +``` |
| 170 | + |
| 171 | +#### Update Mind |
| 172 | + |
| 173 | +Update an existing mind: |
| 174 | + |
| 175 | +```python |
| 176 | +mind = client.minds.update( |
| 177 | + name='mind_name', # required |
| 178 | + new_name='new_mind_name', # optional |
| 179 | + datasources=[ # optional |
| 180 | + { |
| 181 | + 'name': datasource.name, |
| 182 | + 'tables': ['home_rentals'] |
| 183 | + } |
| 184 | + ], |
| 185 | +) |
| 186 | +wait_for_mind(mind) |
| 187 | +``` |
| 188 | + |
| 189 | +#### List Minds |
| 190 | + |
| 191 | +Get all your minds: |
| 192 | + |
| 193 | +```python |
| 194 | +minds = client.minds.list() |
| 195 | +print(minds) |
| 196 | +``` |
| 197 | + |
| 198 | +#### Get Mind by Name |
| 199 | + |
| 200 | +Retrieve a specific mind: |
| 201 | + |
| 202 | +```python |
| 203 | +mind = client.minds.get('mind_name') |
| 204 | +``` |
| 205 | + |
| 206 | +#### Remove Mind |
| 207 | + |
| 208 | +Delete a mind: |
| 209 | + |
| 210 | +```python |
| 211 | +client.minds.drop('mind_name') |
| 212 | +``` |
| 213 | + |
| 214 | +### Managing Datasources |
| 215 | + |
| 216 | +#### Create or Replace |
| 217 | + |
| 218 | +Create a new datasource or replace an existing one: |
| 219 | + |
| 220 | +```python |
| 221 | +datasource = client.datasources.create( |
| 222 | + name='my_datasource', |
| 223 | + description='House sales data', |
| 224 | + engine='postgres', |
| 225 | + connection_data={ |
| 226 | + 'user': 'demo_user', |
| 227 | + 'password': 'demo_password', |
| 228 | + 'host': 'samples.mindsdb.com', |
| 229 | + 'port': 5432, |
| 230 | + 'database': 'demo', |
| 231 | + 'schema': 'demo_data' |
| 232 | + }, |
| 233 | + replace=True |
| 234 | +) |
| 235 | +``` |
| 236 | + |
| 237 | +#### Update Datasource |
| 238 | + |
| 239 | +Update an existing datasource: |
| 240 | + |
| 241 | +```python |
| 242 | +datasource = client.datasources.update( |
| 243 | + name='my_datasource', |
| 244 | + new_name='updated_datasource', |
| 245 | + description='Updated House sales data', |
| 246 | + connection_data={ |
| 247 | + 'user': 'demo_user', |
| 248 | + 'password': 'demo_password', |
| 249 | + 'host': 'samples.mindsdb.com', |
| 250 | + 'port': 5432, |
| 251 | + 'database': 'demo', |
| 252 | + 'schema': 'demo_data' |
| 253 | + } |
| 254 | +) |
| 255 | +``` |
| 256 | + |
| 257 | +#### List Datasources |
| 258 | + |
| 259 | +Get all your datasources: |
| 260 | + |
| 261 | +```python |
| 262 | +datasources = client.datasources.list() |
| 263 | +print(datasources) |
| 264 | +``` |
| 265 | + |
| 266 | +#### Get Datasource by Name |
| 267 | + |
| 268 | +Retrieve a specific datasource: |
| 269 | + |
| 270 | +```python |
| 271 | +datasource = client.datasources.get('my_datasource') |
| 272 | +``` |
| 273 | + |
| 274 | +#### Remove Datasource |
| 275 | + |
| 276 | +Delete a datasource: |
| 277 | + |
| 278 | +```python |
| 279 | +client.datasources.drop('my_datasource') |
| 280 | +``` |
0 commit comments