MySQL AI User Guide
The following sections in this topic describe how to generate new text-based content using the GenAI feature of MySQL AI:
Review the GenAI requirements and privileges.
For Running Batch Queries, add the natural-language queries to a column in a new or existing table.
To generate text-based content using GenAI, perform the following steps:
To define your natural-language query, set the
@query
variable:
mysql> SET @query="QueryInNaturalLanguage
";
Replace QueryInNaturalLanguage
with a natural-language query of your choice. For example:
mysql> SET @query="Write an article on Artificial intelligence in 200 words.";
To generate text-based content, pass the query to the LLM
using the
ML_GENERATE
routine with the task
parameter set to
generation
:
mysql> SELECT sys.ML_GENERATE(@query,
JSON_OBJECT("task", "generation", "model_id", "LLM
", "language", "Language
"));
Replace the following:
LLM
: LLM to use, which must
be the same as the one you loaded in the previous
step.
Language
: the two-letter
ISO 639-1
code for the language you
want to use. Default language is
en
, which is English. To view the
list of supported languages, see
Languages.
For example:
mysql> SELECT sys.ML_GENERATE(@query,
JSON_OBJECT("task", "generation", "model_id", "llama3.2-3b-instruct-v1", "language", "en"));
Text-based content that is generated by the LLM in response to your query is printed as output. It looks similar to the text output shown below:
| {"text": "\n**The Rise of Artificial Intelligence: Revolutionizing the Future**\n\nArtificial intelligence (AI) has been a topic of interest for decades, and its impact is becoming increasingly evident in various aspects of our lives. AI refers to the development of computer systems that can perform tasks that typically require human intelligence, such as learning, problem-solving, and decision-making.\n\nThe latest advancements in machine learning algorithms and natural language processing have enabled AI systems to become more sophisticated and efficient. Applications of AI are expanding rapidly across industries, including healthcare, finance, transportation, and education. For instance, AI-powered chatbots are being used to provide customer support, while self-driving cars are being tested on roads worldwide.\n\nThe benefits of AI are numerous. It can automate repetitive tasks, improve accuracy, and enhance productivity. Moreover, AI has the potential to solve complex problems that were previously unsolvable by humans. However, there are also concerns about job displacement and bias in AI decision-making.\n\nAs AI continues to evolve, it is essential to address these challenges and ensure that its benefits are shared equitably among all stakeholders. With continued investment in research and development, AI has the potential to transform industries and improve lives worldwide. The future of work will be shaped by AI, and it's crucial to prepare for this", "license": "Your use of this llama model is subject to your Oracle agreements and this llama license agreement: https://downloads.mysql.com/docs/LLAMA_32_3B_INSTRUCT-license.pdf"} |
To run multiple generation
queries in
parallel, use the
ML_GENERATE_TABLE
routine. This method is faster than running the
ML_GENERATE
routine multiple times.
To run the steps in this section, you can create a new
database demo_db
and table
input_table
:
mysql>CREATE DATABASE demo_db;
mysql>USE demo_db;
mysql>CREATE TABLE input_table (id INT AUTO_INCREMENT, Input TEXT, primary key (id));
mysql>INSERT INTO input_table (Input) VALUES('Describe what is MySQL in 50 words.');
mysql>INSERT INTO input_table (Input) VALUES('Describe Artificial Intelligence in 50 words.');
mysql>INSERT INTO input_table (Input) VALUES('Describe Machine Learning in 50 words.');
To run batch queries using
ML_GENERATE_TABLE
, perform the following
steps:
In the ML_GENERATE_TABLE
routine,
specify the table columns containing the input queries and
for storing the generated text-based responses:
mysql> CALL sys.ML_GENERATE_TABLE("InputDBName.InputTableName.InputColumn
", "OutputDBName.OutputTableName.OutputColumn
",
JSON_OBJECT("task", "generation", "model_id", "LLM
", "language", "Language
"));
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.
LLM
: LLM to use, which must
be the same as the LLM you loaded in the previous
step.
Language
: the two-letter
ISO 639-1
code for the language you
want to use. Default language is
en
, which is English. To view the
list of supported languages, see
Languages.
For example:
mysql> CALL sys.ML_GENERATE_TABLE("demo_db.input_table.Input", "demo_db.output_table.Output",
JSON_OBJECT("task", "generation", "model_id", "llama3.2-3b-instruct-v1", "language", "en"));
View the contents of the output table:
mysql> SELECT * FROM output_table\G
*************************** 1. row ***************************
id: 1
Output: {"text": "\nMySQL is an open-source relational database
management system (RDBMS) that allows users to store, manage,
and retrieve data in a structured format. It supports various
features like SQL queries, indexing, transactions, and security,
making it a popular choice for web applications, enterprise
software, and mobile apps development.",
"error": null,
"license": "Your use of this llama model is subject to your
Oracle agreements and this llama license agreement:
https://downloads.mysql.com/docs/LLAMA_32_3B_INSTRUCT-license.pdf"}
*************************** 2. row ***************************
id: 2
Output: {"text": "\nArtificial Intelligence (AI) refers to the
development of computer systems that can perform tasks that
typically require human intelligence, such as learning,
problem-solving, and decision-making. AI uses algorithms and
data to mimic human thought processes, enabling machines to
analyze, reason, and interact with humans in increasingly
sophisticated ways.",
"error": null}
*************************** 3. row ***************************
id: 3
Output: {"text": "\nMachine Learning (ML) is a subset of
Artificial Intelligence that enables systems to automatically
improve performance on a task without being explicitly programmed.
It involves training algorithms on data, allowing them to learn
patterns and make predictions or decisions based on new, unseen
data, without human intervention.",
"error": null}
The output table generated using the
ML_GENERATE_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 row in the output column.
If you created a new database for testing the steps in this section, delete the database to free up space:
mysql> DROP DATABASE demo_db;
To learn more about the available routine options, see ML_GENERATE_TABLE Syntax.
Learn how to Summarize Existing Content.