HeatWave User Guide  /  ...  /  Generating New Content

4.3.1 Generating New Content

The following sections in this topic describe how to generate new text-based content using HeatWave GenAI:

Before You Begin

  • Review the Requirements.

  • Connect to your HeatWave Database System.

  • For Running Batch Queries, add the natural-language queries to a column in a new or existing table.

Generating Content

To generate text-based content using HeatWave GenAI, perform the following steps:

  1. To define your natural-language query, set the @query variable:

    SET @query="QueryInNaturalLanguage";

    Replace QueryInNaturalLanguage with a natural-language query of your choice. For example:

    SET @query="Write an article on Artificial intelligence in 200 words.";
  2. To generate text-based content, pass the query to the LLM using the ML_GENERATE routine with the task parameter set to generation:

    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.

      The language parameter is supported as of MySQL 9.0.1-u1.

    For example:

    SELECT sys.ML_GENERATE(@query, JSON_OBJECT("task", "generation", "model_id", "mistral-7b-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": " Artificial Intelligence, commonly referred to as AI, is a
      rapidly growing field that focuses on creating intelligent machines capable
      of performing tasks that typically require human intelligence. These tasks
      include things like understanding natural language, recognizing images, and
      making decisions.\n\nAI technology has come a long way in recent years, thanks
      to advances in machine learning and deep learning algorithms. These algorithms
      allow machines to learn from data and improve their performance over time.
      This has led to the development of more advanced AI systems, such as virtual
      assistants like Siri and Alexa, which can help users with tasks like setting
      reminders and answering questions.\n\nAI is also being used in a variety of other
      industries, including healthcare, finance, and transportation. In healthcare, AI
      is being used to help doctors diagnose diseases and develop treatment plans. In
      finance, AI is being used to detect fraud and make investment decisions. In
      transportation, AI is being used to develop self-driving cars and improve traffic
      flow.\n\nDespite the many benefits of AI, there are also concerns about its potential
      impact on society. Some worry that AI could lead to job displacement and a loss of
      privacy. Others worry that AI could be used for malicious purposes, such as
      cyber attacks or surveillance.\n"} |

Running Batch Queries

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.

Note

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_GENERATE_TABLE routine is supported as of MySQL 9.0.1-u1.

To run the steps in this section, create a new database demo_db and table input_table:

CREATE DATABASE demo_db;
USE demo_db;
CREATE TABLE input_table (id INT AUTO_INCREMENT, Input TEXT, primary key (id));
INSERT INTO input_table (Input) VALUES('Describe what is MySQL in 50 words.');
INSERT INTO input_table (Input) VALUES('Describe Artificial Intelligence in 50 words.');
INSERT INTO input_table (Input) VALUES('Describe Machine Learning in 50 words.');

To run batch queries using ML_GENERATE_TABLE, perform the following steps:

  1. In the ML_GENERATE_TABLE routine, specify the table columns containing the input queries and for storing the generated text-based responses:

    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:

    CALL sys.ML_GENERATE_TABLE("demo_db.input_table.Input", "demo_db.output_table.Output", JSON_OBJECT("task", "generation", "model_id", "mistral-7b-instruct-v1", "language", "en"));
  2. View the contents of the output table:

    SELECT * FROM output_table;
    +----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id | Output                                                                                                                                                                                                                                                                                   |
    +----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    |  1 | {"text": " MySQL is an open-source relational database management system widely used for web, mobile and enterprise applications. It offers robust data storage, retrieval, and manipulation capabilities with support for various programming languages and platforms.", "error": null} |
    |  2 | {"text": " Artificial Intelligence (AI) is the simulation of human intelligence processes by computer systems. These processes include learning, reasoning, and self-correction. AI technologies include machine learning, natural language processing, and robotics.", "error": null}   |
    |  3 | {"text": " Machine learning is a subset of artificial intelligence that involves the development of algorithms and statistical models to enable systems to improve their performance on a specific task by learning from data, without being explicitly programmed.", "error": null}     |
    +----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

    As of MySQL 9.3.0, 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, ensure that you delete the database to avoid being billed for it:

DROP DATABASE demo_db;

To learn more about the available routine options, see ML_GENERATE_TABLE Syntax.