The
ML_RAG
routine runs retrieval-augmented generation which aims to
generate more accurate responses for your queries.
As of MySQL 9.2.1, for context retrieval, the
ML_RAG
routine uses the name of the embedding model used to embed the
input query to find relevant vector store tables that contain
vector embeddings from the same embedding model.
In earlier versions of MySQL, for context retrieval, the
ML_RAG
routine uses the language specified for generating the output
and the name of the embedding model used to embed the input
query to find relevant vector store tables that contain
information in the same language and vector embeddings from the
same embedding model.
This topic contains the following sections:
-
Complete the steps to set up a vector store.
The examples in this topic use the vector store table
demo_embeddings
created in the section Ingesting Files Using the URI with Asynchronous Load. For Running Batch Queries, add the natural-language queries to a column in a new or existing table.
To enter a natural-language query, retrieve the context, and generate results using RAG, perform the following steps:
-
To specify the table for retrieving the vector embeddings to use as context, set the
@options
variable:Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("DBName.VectorStoreTableName"), "model_options", JSON_OBJECT("language", "Language"));
Replace the following:
DBName
: the name of the database that contains the vector store table.VectorStoreTableName
: the name of the vector store table.-
Language
: the two-letterISO 639-1
code for the language you want to use. Default language isen
, which is English. To view the list of supported languages, see Languages.The
language
parameter is supported as of MySQL 9.0.1-u1.
For example:
Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("demo_db.demo_embeddings"), "model_options", JSON_OBJECT("language", "en"));
To learn more about the available routine options, see ML_RAG Syntax.
-
To define your natural-language query, set the
@query
variable:Press CTRL+C to copySET @query="AddYourQuery";
Replace
AddYourQuery
with your natural-language query.For example:
Press CTRL+C to copySET @query="What is AutoML?";
-
To retrieve the augmented prompt, use the
ML_RAG
routine:Press CTRL+C to copyCALL sys.ML_RAG(@query,@output,@options);
-
Print the output:
Press CTRL+C to copySELECT JSON_PRETTY(@output);
Text-based content that is generated by the LLM in response to your query is printed as output. The output generated by RAG is comprised of two parts:
The text section contains the text-based content generated by the LLM as a response for your query.
The citations section shows the segments and documents it referred to as context.
The output looks similar to the following:
Press CTRL+C to copy| { "text": " AutoML is a machine learning technique that uses algorithms to automatically generate and optimize models for specific tasks, without the need for manual intervention. It combines the power of machine learning with the ease of use of traditional programming tools, allowing users to quickly and easily create accurate models for their data.", "citations": [ { "segment": "| { \"text\": \" AutoML is a machine learning technique that uses algorithms to automatically generate and optimize models for specific tasks, without the need for manual intervention. It combines the power of machine learning with the ease of use of traditional programming tools, allowing users to quickly and easily create accurate models for their data.\", \"citations\": [ {\n \"segment\": \"What is MySQL?\", \"distance\": 0.7121, \"document_name\": \"\" },\n {", "distance": 0.0725, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" }, { "segment": "| { \"text\": \" AutoML is a machine learning technique that automates the process of selecting, training, and evaluating machine learning models. It involves using algorithms and techniques to automatically identify the best model for a given dataset and optimize its hyperparameters without requiring manual intervention from data analysts or ML practitioners. AutoML can be used in various stages of the machine learning pipeline, including data preprocessing, feature engineering, model", "distance": 0.0743, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" }, { "segment": "| { \"text\": \" AutoML is a subfield of machine learning that focuses on automating the process of building and training machine learning models. It involves using algorithms and techniques to automatically select features, tune hyperparameters, and evaluate model performance, without requiring human intervention. AutoML can be used for a variety of tasks, including classification, regression, clustering, and anomaly detection.\", \"citations\": [ {", "distance": 0.0762, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" } ], "vector_store": [ "`demo_db`.`demo_embeddings`" ], "retrieval_info": { "method": "n_citations", "threshold": 0.0762 } } |
To continue running more queries in the same session, repeat steps 3 to 5.
To enter a natural-language query and retrieve the context without generating a response for the query, perform the following steps:
-
To specify the table for retrieving the vector embeddings and to skip generation of content, set the
@options
variable:Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("DBName.VectorStoreTableName"), "skip_generate", true);
Replace the following:
DBName
: the name of the database that contains the vector store table.VectorStoreTableName
: the name of the vector store table.
For example:
Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("demo_db.demo_embeddings"), "skip_generate", true);
-
To define your natural-language query, set the
@query
variable:Press CTRL+C to copySET @query="AddYourQuery";
Replace
AddYourQuery
with your natural-language query.For example:
Press CTRL+C to copySET @query="What is AutoML?";
-
To retrieve the augmented prompt, use the
ML_RAG
routine:Press CTRL+C to copyCALL sys.ML_RAG(@query,@output,@options);
-
Print the output:
Press CTRL+C to copySELECT JSON_PRETTY(@output);
Semantically similar text segments used as content for the query and the name of the documents they were found in are printed as output.
The output looks similar to the following:
Press CTRL+C to copy| { "citations": [ { "segment": "| { \"text\": \" AutoML is a machine learning technique that uses algorithms to automatically generate and optimize models for specific tasks, without the need for manual intervention. It combines the power of machine learning with the ease of use of traditional programming tools, allowing users to quickly and easily create accurate models for their data.\", \"citations\": [ {\n \"segment\": \"What is MySQL?\", \"distance\": 0.7121, \"document_name\": \"\" },\n {", "distance": 0.0725, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" }, { "segment": "| { \"text\": \" AutoML is a machine learning technique that automates the process of selecting, training, and evaluating machine learning models. It involves using algorithms and techniques to automatically identify the best model for a given dataset and optimize its hyperparameters without requiring manual intervention from data analysts or ML practitioners. AutoML can be used in various stages of the machine learning pipeline, including data preprocessing, feature engineering, model", "distance": 0.0743, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" }, { "segment": "| { \"text\": \" AutoML is a subfield of machine learning that focuses on automating the process of building and training machine learning models. It involves using algorithms and techniques to automatically select features, tune hyperparameters, and evaluate model performance, without requiring human intervention. AutoML can be used for a variety of tasks, including classification, regression, clustering, and anomaly detection.\", \"citations\": [ {", "distance": 0.0762, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf" } ], "vector_store": [ "`demo_db`.`demo_embeddings`" ], "retrieval_info": { "method": "n_citations", "threshold": 0.0762 } } |
To continue running more queries in the same session, repeat steps 3 to 5.
To run multiple RAG queries in parallel, use the
ML_RAG_TABLE
routine. This method is faster than running the
ML_RAG
routine multiple times.
In versions older than MySQL 9.2.1, to alter an existing table
or create a new table, MySQL requires you to set the
sql-require-primary-key
system variable to 0
.
The
ML_RAG_TABLE
routine is available as of MySQL 9.0.1-u1.
To run the steps in this section, create a new table
input_table
in demo_db
:
Press CTRL+C to copyUSE demo_db; CREATE TABLE input_table (id INT AUTO_INCREMENT, Input TEXT, primary key (id)); INSERT INTO input_table (Input) VALUES('What is HeatWave Lakehouse?'); INSERT INTO input_table (Input) VALUES('What is HeatWave AutoML?'); INSERT INTO input_table (Input) VALUES('What is HeatWave GenAI?');
To run batch queries using
ML_RAG_TABLE
,
perform the following steps:
-
To specify the table for retrieving the vector embeddings to use as context, set the
@options
variable:Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("DBName.VectorStoreTableName"), "model_options", JSON_OBJECT("language", "Language"));
Replace the following:
DBName
: the name of the database that contains the vector store table.VectorStoreTableName
: the name of the vector store table.Language
: the two-letterISO 639-1
code for the language you want to use. Default language isen
, which is English. To view the list of supported languages, see Languages.
For example:
Press CTRL+C to copySET @options = JSON_OBJECT("vector_store", JSON_ARRAY("demo_db.demo_embeddings"), "model_options", JSON_OBJECT("language", "en"));
To learn more about the available routine options, see ML_RAG_TABLE Syntax.
-
In the
ML_RAG_TABLE
routine, specify the table columns containing the input queries and for storing the generated outputs:Press CTRL+C to copyCALL sys.ML_RAG_TABLE("InputDBName.InputTableName.InputColumn", "OutputDBName.OutputTableName.OutputColumn", @options);
Replace the following:
InputDBName
: the name of the database that contains the table column where your input queries are stored.InputTableName
: the name of the table that contains the column where your input queries are stored.InputColumn
: the name of the column that contains input queries.OutputDBName
: the name of the database that contains the table where you want to store the generated outputs. This can be the same as the input database.OutputTableName
: the name of the table where you want to create a new column to store the generated outputs. This can be the same as the input table. If the specified table doesn't exist, a new table is created.OutputColumn
: the name for the new column where you want to store the output generated for the input queries.
For example:
Press CTRL+C to copyCALL sys.ML_RAG_TABLE("demo_db.input_table.Input", "demo_db.output_table.Output", @options);
-
View the contents of the output table:
Press CTRL+C to copySELECT * FROM output_table; +----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | id | Output | +----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 1 | {"text": " HeatWave Lakehouse is a feature of the HeatWave platform that enables query processing on data resident in Object Storage. The source data is read from Object Storage, transformed to the memory optimized HeatWave format, stored in the HeatWave persistence storage layer in Object Storage, and then loaded to HeatWave cluster memory for in-memory query processing. It allows you to create tables which point to external data sources and provides rapid and lakehouse as the primary and secondary engines respectively.", "error": null, "citations": [{"segment": "See: Chapter 4, HeatWave GenAI.\n1.5 HeatWave Lakehouse The Lakehouse feature of HeatWave enables query processing on data resident in Object Storage. The source data is read from Object Storage, transformed to the memory optimized HeatWave format, stored in the HeatWave persistence storage layer in Object Storage, and then loaded to HeatWave cluster memory.\n• Provides in-memory query processing on data resident in Object Storage.\n• Data is not loaded into the MySQL InnoDB storage layer.", "distance": 0.1032, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "The Lakehouse Engine enables you to create tables which point to external data sources.\nFor HeatWave Lakehouse, lakehouse is the primary engine, and rapid is the secondary engine.\n5.1.3 Data Storage", "distance": 0.106, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "The Lakehouse feature of HeatWave enables query processing on data resident in Object Storage. The source data is read from Object Storage, transformed to the memory optimized HeatWave format, stored in the HeatWave persistence storage layer in Object Storage, and then loaded to HeatWave cluster memory.\n• Provides in-memory query processing on data resident in Object Storage.\n• Data is not loaded into the MySQL InnoDB storage layer.", "distance": 0.1063, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}], "vector_store": ["`demo_db`.`demo_embeddings`"], "retrieval_info": {"method": "n_citations", "threshold": 0.1063}} | | 2 | {"text": " HeatWave AutoML is a feature of MySQL HeatWave that makes it easy to use machine learning, whether you are a novice user or an experienced ML practitioner. It analyzes the characteristics of the data and creates an optimized machine learning model that can be used to generate predictions and explanations. The data and models never leave MySQL HeatWave, saving time and effort while keeping the data and models secure. HeatWave AutoML is optimized for HeatWave shapes and scaling, and all HeatWave AutoML makes it easy to use machine learning, whether you are a novice user or an experienced ML practitioner. You provide the data, and HeatWave AutoML analyzes the characteristics of the data and creates an optimized machine learning model that you can use to generate predictions and explanations. An ML model makes predictions by identifying patterns in your data and applying those patterns to unseen data. HeatWave AutoML explanations help you understand how predictions are made,", "error": null, "citations": [{"segment": "| HeatWave AutoML is a feature of MySQL HeatWave that makes it easy to use machine learning, whether you are a novice user or an experienced ML practitioner. It analyzes the characteristics of the data and creates an optimized machine learning model that can be used to generate predictions and explanations. The data and models never leave MySQL HeatWave, saving time and effort while keeping the data and models secure. HeatWave AutoML is optimized for HeatWave shapes and scaling, and all", "distance": 0.0561, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "HeatWave AutoML makes it easy to use machine learning, whether you are a novice user or an experienced ML practitioner. You provide the data, and HeatWave AutoML analyzes the characteristics of the data and creates an optimized machine learning model that you can use to generate predictions and explanations. An ML model makes predictions by identifying patterns in your data and applying those patterns to unseen data. HeatWave AutoML explanations help you understand how predictions are made,", "distance": 0.0598, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "2. HeatWave AutoML analyzes the training data, trains an optimized machine learning model, and stores the model in a model catalog on the MySQL DB System. See Section 3.14.1, “The Model Catalog”.\n115", "distance": 0.0669, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}], "vector_store": ["`demo_db`.`demo_embeddings`"], "retrieval_info": {"method": "n_citations", "threshold": 0.0669}} | | 3 | {"text": " HeatWave GenAI is a feature of HeatWave that allows users to communicate with unstructured data in HeatWave using natural-language queries. It uses a familiar SQL interface which makes it easy to use for content generation, summarization, and retrieval-augmented generation (RAG).", "error": null, "citations": [{"segment": "4.1 HeatWave GenAI Overview HeatWave GenAI is a feature of HeatWave that lets you communicate with unstructured data in HeatWave using natural-language queries. It uses a familiar SQL interface which makes it is easy to use for content generation, summarization, and retrieval-augmented generation (RAG).", "distance": 0.0521, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "HeatWave Chat also provides a graphical interface integrated with the Visual Studio Code plugin for MySQL Shell.\nBenefits\nHeatWave GenAI lets you integrate generative AI into the applications, providing an integrated end-to-end pipeline including vector store generation, vector search with RAG, and an inbuilt chatbot.\nSome key benefits of using HeatWave GenAI are described below:", "distance": 0.0781, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}, {"segment": "HeatWave GenAI retrieves content from the vector store and provides it as context to the LLM along with the query. This process of generating an augmented prompt is called retrieval-augmented generation (RAG), and it helps HeatWave GenAI produce more contextually relevant, personalised, and accurate results.\n• HeatWave Chat", "distance": 0.0811, "document_name": "https://demo_namespace.objectstorage.demo_region.oci.customer-oci.com/p/demo_url/n/demo_namespace/b/demo_bucket/o/heatwave-en.pdf"}], "vector_store": ["`demo_db`.`demo_embeddings`"], "retrieval_info": {"method": "n_citations", "threshold": 0.0811}} | +----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
As of MySQL 9.3.0, the output table generated using the
ML_RAG_TABLE
routine contains an additional details for error reporting. In case the routine fails to generate output for specific rows, details of the errors encountered and default values used are added for the rows in the output column.