Minds
Minds are AI systems with built-in expertise designed to help AI agents accomplish tasks. These plug-and-play systems need little setup. Database Mind, the first in our catalog, is designed to simplify data access and answer questions directly from databases, in natural language. It eliminates the complexity of reasoning and execution loops for data retrieval and can be customized for complex use cases.
Currently available data sources include ClickHouse, MariaDB, MySQL, PostgreSQL, Snowflake, and Google BigQuery.
![Database-Mind flow diagram](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fmind_workflow.60bc4a49.png&w=3840&q=75)
Create a Mind
To create a Mind, install the latest version of MindsDB SDK by running pip install mindsdb_sdk
and create an account at MindsDB Cloud to generate the MindsDB API key.
Create a Database Mind using the create_mind
function from the mindsdb_sdk
package and defining all required arguments.
from mindsdb_sdk.utils.mind import create_mind
mind = create_mind(
name='house_sales_db_mind',
description='House Sales',
base_url='https://llm.mdb.ai/',
api_key='MINDSDB_API_KEY',
model='gpt-4',
data_source_type='postgres',
data_source_connection_args={
'user': 'demo_user',
'password': 'demo_password',
'host': 'samples.mindsdb.com',
'port': '5432',
'database': 'demo',
'schema': 'demo_data'
}
)
The following arguments are required to create a Mind:
name
is the unique name of the Mind.description
is the description of the Mind.base_url
is the URL where requests are sent.api_key
is the MindsDB API key generated at MindsDB Cloud.model
is the large language model to be used by the Mind. Currently, it supports the GPT-4 model.data_source_type
is the database engine of the data source to be used by the Mind. Find all supported data sources here.data_source_connection_args
stores the connection arguments to connect to the data source.
In the code, we’re connecting to our sample database, see full details here.
Users have two options to access Database Minds:
The following sections explain each method in detail.
Use via Completions API
Once the Mind is created, it can be accessed like a model. Therefore, when using the OpenAI SDK, you need to specify the name of the Mind in the model parameter.
Firstly, install OpenAI SDK by running pip install openai
and create an account at MindsDB Cloud to generate the MindsDB API key.
Use this code to connect to OpenAI SDK providing the MindsDB API key.
from openai import OpenAI
client = OpenAI(
api_key='MINDSDB_API_KEY',
base_url='https://llm.mdb.ai/'
)
Utilizing the Database Mind, as created in this section, send a request to the completions endpoint.
completion = client.chat.completions.create(
model=<the mind that you created>,
messages=[
{'role': 'user', 'content': 'How many three-bedroom houses were sold in 2008?'}
],
stream=False
)
print(completion.choices[0].message.content)
Here is the output:
There were 8 three-bedroom houses sold in 2008.
Example App using Completions API
Get started easily with Database Minds by cloning this example app. All you need to do is provide your MindsDB API key generated at MindsDB Cloud. Then, ask questions about the sample data or modify the code to connect your data source. For detailed setup instructions, refer to this guide.
Use via Assistants API
Once the Mind is created, it can be accessed like a model. Therefore, when using the OpenAI SDK, you need to specify the name of the Mind in the assistant_id parameter.
Firstly, install OpenAI SDK by running pip install openai
and create an account at MindsDB Cloud to generate the MindsDB API key.
Use this code to connect to OpenAI SDK providing the MindsDB API key.
from openai import OpenAI
client = OpenAI(
api_key='MINDSDB_API_KEY',
base_url='https://llm.mdb.ai/'
)
Utilizing the Database Mind, as created in this section, create threads, messages, and runs.
Create a thread, add a message to it, and run this thread using the Database Mind created earlier.
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="How many 2-bedroom houses are on the market?"
)
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=<the mind that you created>
)
Get the response:
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages.data[1].role + ': ' + messages.data[1].content[0].text.value)
print(messages.data[0].role + ': ' + messages.data[0].content[0].text.value)
else:
print(run.status)
At the end, delete a thread:
client.beta.threads.delete(thread.id)
Here is the output:
user: How many 2-bedroom houses are on the market?
assistant: There are 99 two-bedroom houses that were once on the market according to the data in the database.
Example App using Assistants API
Get started easily with Assistant Mind by cloning this example app. All you need to do is provide your MindsDB API key generated at MindsDB Cloud. Then, ask questions about the sample data or modify the code to connect your data source. For detailed setup instructions, refer to this guide.