Back to Mindsdb

Tutorial to Get Started with MindsDB

docs/quickstart-tutorial.mdx

26.1.05.2 KB
Original Source

Before we start, install MindsDB locally via Docker or Docker Desktop.

Get started with MindsDB in a few simple steps:

<Steps> <Step title="Connect"> Connect one or more data sources. Explore all available [data sources here](/integrations/data-overview). </Step> <Step title="Unify"> Unify your data with [knowledge bases](/mindsdb_sql/knowledge_bases/overview). </Step> <Step title="Respond"> Respond to questions over your data with [AI agents](/mindsdb_sql/agents/agent). </Step> </Steps>

Step 1. Connect

MindsDB enables connecting data from various data sources and operating on data without moving it from its source. Learn more here.

  • Connecting Structured Data

Use the CREATE DATABASE statement to connect a data source to MindsDB.

sql
CREATE DATABASE mysql_demo_db
WITH ENGINE = 'mysql',
PARAMETERS = {
    "user": "user",
    "password": "MindsDBUser123!",
    "host": "samples.mindsdb.com",
    "port": "3306",
    "database": "public"
};

This is the input data used in the following steps:

sql
SELECT *
FROM mysql_demo_db.home_rentals
LIMIT 3;

The sample contains contains information about properties for rent.

  • Connecting Unstructured Data

Extract data from webpages using the web crawler or upload files to MindsDB.

In this example, we fetch data from MindsDB Documentation webpage using the web crawler.

sql
CREATE DATABASE my_web 
WITH ENGINE = 'web';

SELECT url, text_content
FROM my_web.crawler
WHERE url = 'https://docs.mindsdb.com/'

Now we save this data into a view which is saved in the default mindsdb project.

sql
CREATE VIEW mindsdb_docs (
    SELECT url, text_content
    FROM my_web.crawler
    WHERE url = 'https://docs.mindsdb.com/'
);

SELECT *
FROM mindsdb.mindsdb_docs;

Step 2. Unify

MindsDB enables unifying data from structured and unstructured data sources into a single, queryable interface. This unified view allows seamless querying and model-building across all data without consolidation into one system. Learn more here.

Create a knowledge base to store all your data in a single location. Learn more about knowledge bases here.

sql
CREATE KNOWLEDGE_BASE my_kb
USING
    embedding_model = {
        "provider": "openai",
        "model_name" : "text-embedding-3-large",
        "api_key": "your-openai-api-key"
    },
    reranking_model = {
        "provider": "openai",
        "model_name": "gpt-4o",
        "api_key": "your-openai-api-key"
    },
    content_columns = ['content'];

Insert data from Step 1 into the knowledge base.

sql
INSERT INTO my_kb
    SELECT
            'number_of_rooms: ' || number_of_rooms || ', ' ||
            'number_of_bathrooms' || number_of_bathrooms || ', ' ||
            'sqft' || sqft || ', ' ||
            'location' || location || ', ' ||
            'days_on_market' || days_on_market || ', ' ||
            'neighborhood' || neighborhood || ', ' ||
            'rental_price' || rental_price
                AS content
    FROM mysql_demo_db.home_rentals;

INSERT INTO my_kb
    SELECT text_content AS content
    FROM mindsdb.mindsdb_docs;

Query the knowledge base to search your data.

sql
SELECT *
FROM my_kb
WHERE content = 'what is MindsDB';

SELECT *
FROM my_kb
WHERE content = 'rental price lower than 2000';
<Tip> In order to keep the knowledge base up-to-date with your data, use [jobs](/mindsdb_sql/sql/create/jobs) to automate data inserts every time your data is modified.
sql
CREATE JOB update_kb (

  INSERT INTO my_kb
      SELECT
              'number_of_rooms: ' || number_of_rooms || ', ' ||
              'number_of_bathrooms' || number_of_bathrooms || ', ' ||
              'sqft' || sqft || ', ' ||
              'location' || location || ', ' ||
              'days_on_market' || days_on_market || ', ' ||
              'neighborhood' || neighborhood || ', ' ||
              'rental_price' || rental_price
                  AS content
      FROM mysql_demo_db.home_rentals
      WHERE created_at > LATEST
)
EVERY 1 day;
</Tip>

Step 3. Respond

MindsDB enables generating insightful and accurate responses from unified data using natural language. Learn more here.

Create an agent that can answer questions over your unified data from Step 2.

sql
CREATE AGENT my_agent
USING
    model = {
        "provider": "openai",
        "model_name" : "gpt-4o",
        "api_key": "your-openai-api-key"
    },
    data = {
         "knowledge_bases": ["mindsdb.my_kb"],
         "tables": ["mysql_demo_db.home_rentals"]
    },
    prompt_template = 'mindsdb.my_kb stores data about mindsdb and home rentals,
                      mysql_demo_db.home_rentals stores data about home rentals';

Now you can ask questions over your data.

sql
SELECT *
FROM my_agent
WHERE question = 'what is MindsDB?';

Visit the Respond tab in the MindsDB Editor to chat with an agent.