This tutorial uses the RAG handler to search data.

Learn more about the RAG handler here.

Tutorial

The following examples illustrate various ways to integrate RAG with different data sources, including files, URLs, databases, and vector databases.

From a URL

The following example utilize rag_engine to create a model with the CREATE MODEL statement.

CREATE ML_ENGINE rag_engine
FROM rag
USING
    openai_api_key = 'sk-xxx';

Create a model using this engine:

CREATE MODEL mindsdb_rag_model
predict answer
USING
   engine = "rag_engine",
   llm_type = "openai",
   url='https://docs.mindsdb.com/what-is-mindsdb',
   vector_store_folder_name = 'db_connection',
   input_column = 'question'; 

Check the status of the model.

DESCRIBE mindsdb_rag_model;

Now you can use the model to answer your questions.

SELECT *
FROM mindsdb_rag_model
WHERE question = 'What ML use cases does MindsDB support?';

On execution, we get:

answersource_documentsquestion
MindsDB supports various machine learning use cases such as anomaly detection, forecasting, recommenders, classification, and regression. It also supports multimedia use cases such as video and text semantic search, text to audio, text to video, and text to image.{} What ML use cases does MindsDB support?

From Database

The following example utilize rag_engine to create a model with the CREATE MODEL statement and MySQL database as a knowlege base.

CREATE ML_ENGINE rag_engine
FROM rag
USING
    openai_api_key = 'sk-xxx';

Connect to MySQL database:

CREATE DATABASE mysql_demo_db
WITH ENGINE = 'mysql',
PARAMETERS = {
    "user": "user",
    "password": "MindsDBUser123!",
    "host": "db-demo-data.cwoyhfn6bzs0.us-east-1.rds.amazonaws.com",
    "port": "3306",
    "database": "public"
};

Create a model using this engine and include the FORM clause:

CREATE MODEL rag_handler_db
FROM mysql_demo_db 
    (SELECT * FROM demo_fda_context LIMIT 2)
PREDICT answer
USING
   engine="rag",
   llm_type="openai",
   vector_store_folder_name='test_db',
   input_column='question';

Now you can use the model to answer your questions.

SELECT *
FROM rag_handler_db
WHERE question='what product is best for treating a cold?';

On execution, we get:

answersource_documentsquestion
ShopRite Arthritis Pain Acetaminophen is not specifically designed for treating a cold. It may help to temporarily relieve minor aches and pains associated with a cold, but it is not the best product for treating a cold. It is always best to consult with a doctor or pharmacist for the most appropriate medication for treating a cold.{"column":["full_ingredients","indications_and_usage","intended_purpose_of_product","active_ingredient"],"sources_content":["ShopRite Arthritis ..."],"sources_document":["dataframe"],"sources_row":[1,1,1,1]}what product is best for treating a cold?

From File

The following example utilize rag_engine to create a model with the CREATE MODEL statement and uploaded file as a knowlege base.

CREATE ML_ENGINE rag_engine
FROM rag
USING
    openai_api_key = 'sk-xxx';

Upload a file using the GUI Upload File option. Create a model using this engine and include the FORM clause:

CREATE MODEL rag_handler_files
FROM files 
    (SELECT * FROM uploaded_file)
PREDICT answer
USING
   engine="rag",
   llm_type="openai",
   vector_store_folder_name='test_db',
   input_column='question';

Now you can use the model to answer your questions.

SELECT *
FROM rag_handler_files
WHERE question='what product is best for treating a cold?';

On execution, we get:

answersource_documentsquestion
ShopRite Arthritis Pain Acetaminophen is not specifically designed for treating a cold. It may help to temporarily relieve minor aches and pains associated with a cold, but it is not the best product for treating a cold. It is always best to consult with a doctor or pharmacist for the most appropriate medication for treating a cold.{"column":["full_ingredients","indications_and_usage","intended_purpose_of_product","active_ingredient"],"sources_content":["ShopRite Arthritis ..."],"sources_document":["dataframe"],"sources_row":[1,1,1,1]}what product is best for treating a cold?