|
1 | 1 | from minds.client import Client |
| 2 | +from openai import OpenAI |
2 | 3 |
|
3 | | -# --- connect --- |
4 | | -client = Client("YOUR_API_KEY") |
5 | 4 |
|
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 |
9 | 6 |
|
| 7 | +# connect |
| 8 | +API_KEY = "YOUR_API_KEY" |
| 9 | +BASE_URL = 'https://custom_cloud.mdb.ai/api/v1' # optional, if you use custom server |
10 | 10 |
|
11 | | -# create datasource |
12 | | -from minds.datasources import DatabaseConfig |
| 11 | +client = Client(API_KEY) |
13 | 12 |
|
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( |
15 | 18 | name='my_datasource', |
16 | | - description='<DESCRIPTION-OF-YOUR-DATA>', |
| 19 | + description='House sales data', |
17 | 20 | engine='postgres', |
18 | 21 | connection_data={ |
19 | 22 | 'user': 'demo_user', |
|
22 | 25 | 'port': 5432, |
23 | 26 | 'database': 'demo', |
24 | 27 | 'schema': 'demo_data' |
25 | | - }, |
26 | | - tables=['<TABLE-1>', '<TABLE-2>'] |
| 28 | + } |
27 | 29 | ) |
28 | 30 |
|
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 | +) |
39 | 41 |
|
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) |
42 | 67 |
|
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) |
47 | 71 |
|
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) |
54 | 75 |
|
55 | 76 |
|
56 | | -# --- managing minds --- |
| 77 | +# Mind Management |
57 | 78 |
|
58 | 79 | # 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 | +) |
60 | 90 |
|
61 | 91 | # 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 | + ], |
65 | 101 | ) |
66 | 102 |
|
67 | 103 | # list |
68 | | -print(client.minds.list()) |
| 104 | +minds = client.minds.list() |
69 | 105 |
|
70 | | -# get by name |
| 106 | +# get |
71 | 107 | mind = client.minds.get('mind_name') |
72 | 108 |
|
73 | | -# removing datasource |
74 | | -mind.del_datasource(datasource) |
75 | | - |
76 | | -# remove mind |
| 109 | +# remove |
77 | 110 | client.minds.drop('mind_name') |
78 | 111 |
|
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 |
87 | 113 |
|
88 | 114 | # 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 | +) |
90 | 129 |
|
| 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 | +) |
91 | 144 |
|
92 | 145 | # list |
93 | | -print(client.datasources.list()) |
| 146 | +datasources = client.datasources.list() |
94 | 147 |
|
95 | 148 | # get |
96 | 149 | datasource = client.datasources.get('my_datasource') |
97 | 150 |
|
98 | 151 | # remove |
99 | 152 | client.datasources.drop('my_datasource') |
100 | | - |
101 | | - |
|
0 commit comments