Simplifying AI Development: A Practical Guide to HeatWave GenAI’s RAG & Vector Store Features

December 10, 2024
Tags: ,

This article builds upon the concepts introduced in my previous blog posts, HeatWave GenAI: Sentiment Analysis Made Easy-PeasyHeatWave GenAI: Your AI-Powered Content Creation Partner, In-Database LLMs for Efficient Text Translation with HeatWave GenAI and HeatWave GenAI: Sentiment Analysis Made Easy-Peasy.
For a deeper understanding, also consider reading these articles.

HeatWave GenAI empowers you to interact with unstructured data seamlessly using natural language. Whether you’re performing content generation, summarization, sentiment analysis, or retrieval-augmented generation (RAG), HeatWave GenAI simplifies the process with its intuitive design and robust features.

Here’s why HeatWave GenAI stands out:

  • Natural-Language Queries: Communicate with your data effortlessly by running natural-language searches using in-database or external large language models (LLMs).
  • SQL Familiarity: The system leverages a SQL-based interface, making it accessible and easy to adopt for users already familiar with relational databases.
  • Integrated and Optimized Pipeline: All the necessary components—vector store generation, RAG-based vector search, and chatbot capabilities—are seamlessly integrated, ensuring smooth performance and ease of use.
  • End-to-End Generative AI Capabilities: HeatWave GenAI supports direct integration of generative AI into your applications, providing a complete pipeline for data retrieval, analysis, and interaction.

What is RAG?

RAG stands for Retrieval-Augmented Generation. It’s a technique that combines the power of Large Language Models (LLMs) with external knowledge bases. The primary goal of RAG is to enhance the quality, relevance, and accuracy of AI-generated responses by retrieving relevant information from these sources.

Essentially, it’s like giving an LLM access to a vast library, allowing it to provide more informative and contextually relevant answers.

How RAG Works

  1. Data Retrieval: The system retrieves relevant information from a connected knowledge base, vector store, or database using a query.
  2. Generation: The retrieved data is then used as context to guide the language model in generating a response.
  3. Output: The result is a response that blends the creative capabilities of the LLM with factual, targeted insights from the retrieved data.

Key Features of RAG

  • Accuracy: Responses are grounded in real-world data, reducing errors and hallucinations typical of standalone LLMs.
  • Relevance: Ensures the output directly addresses user queries by relying on contextually relevant information.
  • Scalability: Can handle large datasets stored in vector databases for quick, efficient searches.

Some use Cases for RAG

  • Customer Support: Enhancing chatbot responses with real-time company knowledge or product details.
  • Content Creation: Generating articles or summaries based on specific datasets.
  • Research: Assisting with document retrieval and summarization for academic or industry research.
  • Personalization: Tailoring recommendations or messages based on customer data.

What is a Vector Store?

Vectors are numerical representations that capture the meaning, context, or characteristics of data, often used to represent text, images, or other complex data types. By storing these vectors, vector stores enable efficient similarity search, making it possible to find similar items based on their underlying semantic meaning.

Vector stores are crucial for modern AI applications like recommendation systems, natural language processing, and retrieval-augmented generation (RAG). They provide a powerful tool for understanding and analyzing complex data.

How a Vector Store Works

  1. Data Vectorization: Input data, such as text or images, is transformed into vector embeddings using a model like a large language model (LLM) or an image encoder.
  2. Storage: These embeddings are stored in the vector store, often alongside metadata about the original data.
  3. Search and Retrieval: When a query is made, it is also converted into a vector, and the store performs similarity searches to retrieve the most relevant results based on distance measures like cosine similarity or Euclidean distance.

Use Cases for Vector Stores

  • Search and Retrieval: Powering AI-driven search engines that find relevant documents, images, or videos based on content rather than keywords.
  • Recommendation Systems: Suggesting products, movies, or music by comparing user preferences to stored vectors.
  • Chatbots and Virtual Assistants: Retrieving knowledge or context to answer user queries more accurately.
  • Content Matching: Identifying similar or related content, such as plagiarism detection or duplicate image recognition.

What is HeatWave?

Oracle’s HeatWave is a fully-managed Oracle Cloud Infrastructure service. It offered different solutions for different workloads:

  • HeatWave MySQL: built on MySQL, for your transactional needs.
  • HeatWave Analytics: get real-time analytics on your data.
  • HeatWave Lakehouse: query data in various formats in object storage
  • HeatWave AutoML: automate the pipeline to build, train, and explain ML models
  • HeatWave GenAI: integrated & automated GenAI with in-database LLMs

HeatWave GenAI  (from version 9.0.1 onward) is an powerful integrated platform that combines automated in-database large language models (LLMs), vector stores, and scalable vector processing. This simplifies content generation, enabling businesses to harness generative AI without requiring AI expertise, data transfer, or extra costs.

The key benefits of such architecture are:

  • Effortless LLM Integration: Simple SQL queries unlock AI power.
  • Seamless Database Integration: No complex external connections.
  • Simplified Architecture: Easy management and scalability.
  • Scalable Performance: Handles large datasets and high traffic.
  • Always Up-to-Date Data: In-database LLMs ensure data freshness.

HeatWave GenAI is available on major cloud platforms: Oracle Cloud Infrastructure (OCI), Amazon Web Services (AWS), and Microsoft Azure.

In this article I’m using HeatWave 9.1.0.

SQL > SELECT version();
+-------------+
| version()   |
+-------------+
| 9.1.0-cloud |
+-------------+

I’ll leverage HeatWave Lakehouse and GenAI capabilities.

HeatWave Lakehouse
Allows to process data stored in OCI Object Storage. Data are retrieved from Object Storage, transformed into a memory-optimized format, and stored in the HeatWave persistence layer. This ensures efficient data processing, with the data loaded into the HeatWave cluster for real-time analytics and insights.

HeatWave Generative AI
Enables natural-language interactions with unstructured data, using SQL-based queries for content generation, summarization, and retrieval-augmented generation (RAG). This makes it easy to generate and extract valuable insights from your data with minimal complexity.

Preparation

The data sources that will augmented the LLM are 6 pdf files based on the following Wikipedia pages:

They were loaded into an OCI Object Storage bucket.

The data sources that will augmented the LLM are 6 pdf files based on Wikipedia pages

Load data into the HeatWave cluster, generate vector embedding & load them into a vector store table

If you’re loading a vector store table on HeatWave for the first time, call the following method to create a schema used for task management:

sql> SELECT mysql_task_management_ensure_schema();

RAG & Vector Store preparation

There are many ways to do this, the easiest is probably to use the sys.HEATWAVE_LOAD stored procedure with a Pre-Authenticated Requests (PAR).

An alternative is to use sys.vector_store_load. More info here.

In short, the PDF files (stored in the Object Storage bucket) will be uploaded to the HeatWave cluster, where they will be split into smaller segments, transformed into numerical representations (vector embeddings), and stored in a dedicated vector store table for efficient search and retrieval.

-- Load unstructured data (pdf) to HeatWave Lakehouse.
-- Ingesting Files Using Auto Parallel Load
SET @dl_tables = '[ { "db_name": "Iris_AI",
"tables": [
{"table_name": "IrisAIembeddings", "engine_attribute": { "dialect": {"format": "pdf", "language": "en"},
"file": [ {"par": "https://objectstorage.eu-frankfurt.../Iris_AI/o/"}
]}}] }]';

SET @options = JSON_OBJECT('mode', 'normal');

CALL sys.HEATWAVE_LOAD(CAST(@dl_tables AS JSON), @options);

-- Checks (count embeddings):
SELECT count(*) FROM Iris_AI.IrisAIembeddings;

The vector store table is: Iris_AI.IrisAIembeddings. Its structure is:

SQL > 
SHOW CREATE TABLE Iris_AI.IrisAIembeddings\G
*************************** 1. row ***************************
       Table: IrisAIembeddings
Create Table: CREATE TABLE `IrisAIembeddings` (
  `document_name` varchar(1024) NOT NULL COMMENT 'RAPID_COLUMN=ENCODING=VARLEN',
  `metadata` json NOT NULL COMMENT 'RAPID_COLUMN=ENCODING=VARLEN',
  `document_id` int unsigned NOT NULL,
  `segment_number` int unsigned NOT NULL,
  `segment` varchar(1024) NOT NULL COMMENT 'RAPID_COLUMN=ENCODING=VARLEN',
  `segment_embedding` vector(384) NOT NULL COMMENT 'RAPID_COLUMN=ENCODING=VARLEN' /*!80021 ENGINE_ATTRIBUTE '{"model": "minilm"}' */,
  PRIMARY KEY (`document_id`,`segment_number`)
) ENGINE=Lakehouse DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci SECONDARY_ENGINE=RAPID /*!80021 ENGINE_ATTRIBUTE='{"file": [{"par": "https://objectstorage.eu-frankfurt.../Iris_AI/o/"}], "dialect": {"format": "pdf", "language": "en", "is_strict_mode": false}}' */

Some data:

SELECT document_name, metadata, document_id, segment_number, segment FROM Iris_AI.IrisAIembeddings ORDER BY RAND() LIMIT 2\G
*************************** 1. row ***************************
 document_name: https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_versicolor_Wikipedia.pdf
      metadata: {"TITLE": "Iris versicolor - Wikipedia", "PUBLISHER": "Skia/PDF m128", "CREATIONDATE": "  9/14/2024 02:36:16 PM", "REVISIONDATE": "  9/14/2024 02:36:16 PM", "USERDEFINEDPROP": "Creator Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/128.0.0.0 Safari/537.36"}
   document_id: 2
segment_number: 8
       segment: The purple iris is the official flower of Kappa Pi International Honorary Art Fraternity.[13]
Iris versicolor 'Blue Flag'
 
Iris versicolor
 
Iris versicolor – botanical illustration in American Medicinal Plants, 1887
 
Saint-Prosper-de-Champlain, Quebec, Canada
Batiscan, Quebec floral emblem, Canada[14]
*************************** 2. row ***************************
 document_name: https://objectstorage.eu-frankfurt.../Iris_versicolor_Wikispecies.pdf
      metadata: {"TITLE": "Iris versicolor - Wikispecies", "PUBLISHER": "Skia/PDF m128", "CREATIONDATE": "  9/14/2024 02:39:44 PM", "REVISIONDATE": "  9/14/2024 02:39:44 PM", "USERDEFINEDPROP": "Creator Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/128.0.0.0 Safari/537.36"}
   document_id: 3
segment_number: 6
       segment: References: BRUMMITT, R.K. 2001. TDWG – World Geographical Scheme for Recording Plant Distributions, 2nd Edition (https://github.com/tdwg/wgsrpd/blob/master/109-488-1 -ED/2nd%20Edition/TDWG_geo2.pdf)
LINNAEUS, C. 1753. Species Plantarum. Tomus I: 39 (http://www.biodiversitylibrary.org/ite m/13829#page/51/mode/1up). Reference page. 
Flora of North America Editorial Committee (2002). Flora of North America North of Mexico 26: 1-723. Oxford University Press, New York, Oxford.

We can find the PDF references:

SELECT DISTINCT document_name FROM Iris_AI.IrisAIembeddings;
+---------------------------------------------------------------------------------+
| document_name                                                                   |
+---------------------------------------------------------------------------------+
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_versicolor_Wikispecies.pdf |
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_virginica_Wikispecies.pdf  |
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_setosa_Wikispecies.pdf     |
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_virginica_Wikipedia.pdf    |
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_setosa_Wikipedia.pdf       |
| https://objectstorage.eu-frankfurt.../Iris_AI/o/Iris_versicolor_Wikipedia.pdf   |
+---------------------------------------------------------------------------------+

If you have ever wondered what a vector (stored in the VECTOR data type) might look like, you can use VECTOR_TO_STRING() or FROM_VECTOR()

SELECT VECTOR_DIM(segment_embedding), FROM_VECTOR(segment_embedding) FROM Iris_AI.IrisAIembeddings LIMIT 1\G
*************************** 1. row ***************************
 VECTOR_DIM(segment_embedding): 384
FROM_VECTOR(segment_embedding): [8.00615e-02,2.11577e-02,1.61691e-02,4.23411e-02,-3.24994e-02,4.45356e-02,-9.23082e-02,-3.37680e-02,-2.03409e-02,6.48912e-02,4.07830e-02,1.68436e-02,-3.48651e-02,5.55622e-02,-3.63239e-02,6.86629e-02,4.45186e-02,3.82741e-02,1.09397e-02,1.40209e-03,2.67049e-02,2.23335e-03,5.67095e-02,-2.48191e-02,-5.93457e-02,5.69686e-02,1.88271e-03,7.52686e-02,3.77259e-02,-2.84845e-02,-3.70396e-02,1.23335e-01,6.92697e-02,-4.33865e-02,6.80390e-03,-1.47766e-02,2.61221e-02,4.78904e-02,5.68956e-02,7.15472e-02,-5.28972e-02,-1.00536e-02,-1.24508e-02,-5.44387e-02,7.01239e-03,2.39283e-03,-4.52311e-02,-1.01765e-02,1.66316e-02,-5.89363e-03,-7.39317e-02,-4.40087e-02,-4.70511e-02,-7.55958e-04,7.00977e-02,1.76238e-02,-3.46344e-02,3.42340e-02,3.16051e-03,2.01610e-02,6.16689e-02,-3.29546e-02,-1.77070e-02,-3.53866e-02,-4.38236e-02,2.54502e-02,-9.36790e-02,-9.03236e-03,8.05398e-03,-1.30124e-01,-2.29885e-02,4.14870e-02,7.31113e-02,4.12350e-02,-5.03330e-02,1.53553e-03,1.31312e-02,1.68512e-02,-7.28380e-02,-2.29530e-02,-5.44221e-02,-2.94721e-02,1.01206e-01,-1.42966e-02,3.71721e-02,1.80400e-02,-2.11835e-02,2.84860e-02,-3.72609e-02,1.53701e-02,-4.35036e-02,-5.51637e-02,1.50130e-02,-3.49194e-02,-1.39024e-02,7.81750e-02,6.52888e-02,-8.32752e-02,-1.03520e-02,-1.13159e-02,-6.32495e-02,-1.73173e-02,-3.33632e-02,-1.90669e-02,-3.64169e-02,2.54803e-03,-8.01785e-02,-1.22022e-01,-2.22540e-02,-2.70522e-03,3.12901e-02,3.36771e-02,-6.77026e-02,5.30156e-02,2.64821e-02,-7.79997e-03,-5.18856e-02,-1.98439e-02,1.09730e-01,4.17542e-02,1.41583e-02,-8.88374e-03,1.00671e-02,9.43218e-03,3.44302e-03,5.37422e-02,-3.77832e-02,-1.26111e-02,2.97282e-02,-2.65987e-02,-2.07729e-02,-1.04494e-02,-8.58481e-03,-7.74056e-02,-4.44796e-02,-6.00447e-02,2.15521e-02,9.89563e-03,-5.55751e-03,3.89642e-02,-6.50692e-02,-7.91098e-02,-7.90359e-02,-3.78152e-03,-2.96150e-02,5.58467e-02,4.15553e-02,1.22073e-01,2.91632e-02,1.20790e-01,-1.12918e-03,1.55323e-02,-2.26794e-03,2.69730e-02,2.47722e-02,-1.58608e-01,-6.48694e-03,7.97874e-02,1.30401e-01,-7.63800e-02,5.34007e-02,1.20450e-01,9.73293e-03,3.36432e-02,-4.79591e-02,-8.73279e-02,8.55590e-03,5.65378e-02,4.04146e-02,2.00272e-02,-1.53599e-02,3.70622e-02,5.86816e-02,1.57900e-02,-3.75850e-02,-6.28287e-02,2.97302e-02,-6.33792e-02,-1.58955e-01,4.47092e-02,-2.49065e-03,4.68349e-02,6.57920e-03,4.96956e-02,-5.24389e-04,1.12109e-02,-5.98008e-02,8.31092e-03,4.62907e-04,7.37732e-03,-5.63335e-02,-3.69826e-02,6.56116e-02,4.68743e-02,-3.02025e-02,-1.07677e-02,-3.90057e-02,3.40080e-02,1.53216e-02,4.84835e-02,-1.91672e-02,-2.53384e-03,4.27504e-02,-1.87664e-02,6.07610e-02,4.13199e-02,4.74177e-03,-8.20841e-02,-2.18529e-02,-8.70064e-04,-3.18450e-02,9.38491e-03,-7.51281e-02,6.61539e-02,2.76727e-03,9.03535e-03,5.58634e-02,-1.13491e-01,1.05393e-01,7.08349e-02,-3.90654e-02,-9.73421e-02,-2.31931e-02,2.50503e-34,-1.33356e-03,-8.12520e-02,-3.57421e-02,6.36865e-03,6.98303e-02,-3.79885e-02,1.89998e-03,4.15160e-02,2.64551e-02,-7.81740e-02,-6.01936e-02,8.89259e-02,6.85453e-02,-4.35710e-02,6.15102e-03,-4.12384e-02,-2.30391e-02,8.04307e-02,-4.20083e-02,4.30390e-02,-6.42659e-02,-4.87679e-02,-2.36370e-02,-6.97454e-02,-3.80549e-02,8.03606e-03,4.82721e-02,-8.53376e-02,-1.30280e-01,-4.02262e-02,-1.85272e-02,-7.64051e-02,-6.02972e-02,-8.30899e-03,-3.24592e-02,-6.51084e-02,-3.45417e-02,-5.57729e-02,-2.41541e-02,9.03948e-02,-6.26660e-03,-2.82211e-02,7.14078e-02,-7.29852e-04,-9.84422e-02,-4.61796e-03,-8.73099e-02,6.90387e-02,-3.40576e-02,-4.15616e-02,1.83394e-02,1.00965e-02,-1.69803e-02,-6.09804e-02,-8.52834e-03,-7.98361e-02,3.18808e-04,4.31007e-02,4.49218e-02,3.58964e-02,-2.11669e-02,-2.59622e-02,-2.92579e-02,5.19722e-02,-2.18184e-02,1.11915e-02,7.29237e-02,-1.75118e-02,-1.87051e-02,-5.20259e-02,7.49990e-02,-3.41171e-02,-6.95442e-03,1.92275e-02,5.38307e-02,-1.38445e-02,1.54541e-03,-5.89235e-02,4.19111e-02,8.50421e-02,-1.25453e-02,2.32194e-02,3.06071e-02,-1.00385e-02,9.18060e-02,-1.00535e-02,-1.32639e-01,-5.87926e-03,5.28075e-02,2.70706e-02,-2.62010e-02,4.78546e-02,-5.04491e-02,5.85774e-02,8.84199e-02,8.55492e-32,3.13937e-02,-7.58448e-03,-1.08424e-02,5.32128e-02,7.68471e-03,-4.37979e-02,-8.93071e-02,5.37401e-02,-9.28643e-03,-1.31074e-01,-3.15308e-02,1.66151e-04,-2.33636e-02,6.18941e-02,4.06328e-04,-8.38102e-02,4.22972e-02,6.47196e-02,-3.49144e-02,-5.97541e-03,2.07894e-02,2.67512e-02,5.52530e-02,-8.61502e-02,-6.22585e-02,1.19297e-01,-3.34995e-02,-6.14199e-03,4.33000e-02,3.92206e-02,5.98244e-02,-1.97159e-02,5.18834e-02,1.14972e-02,1.28108e-02,2.56998e-02,-4.65825e-02,1.79345e-02,-5.45621e-02,-3.79862e-02,2.39628e-02,-5.79710e-02,2.82440e-02,1.96145e-02,9.74760e-03,-4.65814e-02,1.20515e-02,-5.32089e-02,9.85598e-02,-6.20520e-03,-4.63758e-02,-6.89194e-03,-1.66197e-02,-1.36524e-02,-9.35889e-02,3.43503e-02,3.12809e-02,-1.08981e-01,4.64091e-02,2.83066e-02,8.21929e-02,4.31020e-02,2.17023e-02,9.03776e-02]

VECTOR_DIM() gets the length of a vector, that is, the number of entries it contains.

More vector functions here.

Update Vector Store

You can update your vector store by either drop the table and recreate it or by using the refresh_external_tables clause.

-- Updating the Vector Store using Auto Parallel Load
SET @dl_tables = '[
{ "db_name": "DBName", "tables": [ { "table_name": "VectorStoreTableName" }] }
]';

SET @options = JSON_OBJECT('mode', 'normal', 'refresh_external_tables', true);

CALL sys.HEATWAVE_LOAD(CAST(@dl_tables AS JSON), @options);

Running Retrieval-Augmented Generation

The ML_RAG routine performs retrieval-augmented generation (RAG), but the LLM that you will use must be loaded.
You can get the list of the supported LLMs within HeatWave GenAI by querying the ML_SUPPORTED_LLMS table in sys schema:

-- List of supported LLMs
SELECT * FROM sys.ML_SUPPORTED_LLMS;
+--------------------------------------+------------+
| model_name                           | model_type |
+--------------------------------------+------------+
| llama2-7b-v1                         | generation |
| mistral-7b-instruct-v1               | generation |
| llama3-8b-instruct-v1                | generation |
| cohere.command-text-v14              | generation |
| cohere.command-light-text-v14        | generation |
| meta.llama2-70b-chat-v1              | generation |
| meta.llama3-70b-instruct-v1:0        | generation |
| cohere.command-r-plus                | generation |
| cohere.command-r-16k                 | generation |
| meta.llama-3-70b-instruct            | generation |
| qwen2-7b-instruct-v1                 | generation |
| all_minilm_l12_v2                    | embedding  |
| multilingual-e5-small                | embedding  |
| cohere.embed-english-light-v3.0      | embedding  |
| cohere.embed-multilingual-v3.0       | embedding  |
| cohere.embed-multilingual-light-v3.0 | embedding  |
| cohere.embed-english-v3.0            | embedding  |
+--------------------------------------+------------+

At the time of writing – December 2024 – the HeatWave in-database LLMs are:

  • mistral-7b-instruct-v1
  • llama3-8b-instruct-v1
  • llama2-7b-v1 is deprecated

the HeatWave in-Database Embedding models are:

  • all_minilm_l12_v2
  • multilingual-e5-small

The other models are from OCI Generative AI Service. They can also be used in the HeatWave workflow however their use will incur additional costs.

The comprehensive list of the languages, embedding models, and large language models (LLMs) that HeatWave GenAI supports is available here.

In order to run your RAG, you should load the model, specify some options like the vector store table, the distance metric, the language, and also options like temperature, top_k, top_p, repeat_penalty, etc…), then define your natural language query and finally run the sys.ML_RAG stored procedure:

-- Load the model: ex. Llama 3 8B
CALL sys.ML_MODEL_LOAD("llama3-8b-instruct-v1", NULL);

-- Specify the table for retrieving the vector embeddings, / ['distance_metric', {'COSINE'|'DOT'|'EUCLIDEAN'}]
SET @irisAI_model_info = JSON_OBJECT("vector_store", JSON_ARRAY("Iris_AI.IrisAIembeddings"), "distance_metric",
"COSINE", "model_options", JSON_OBJECT("model_id", "llama3-8b-instruct-v1", "language", "en", "temperature",0));

-- Define your natural language query
SET @query="Describe the appearance of the Iris versicolor flower";

-- Retrieve the prompt and print the output:
CALL sys.ML_RAG(@query, @output, @irisAI_model_info);

HeatWave GenAI currently supports 3 distance metrics: COSINE, DOT & EUCLIDEAN. Other options are available here.

The result of the query is stored in JSON format in the variable @output (in this example). This result includes the answer and also citations referring to segments from the loaded documents (my PDF files).
In MySQL, when dealing with JSON documents, it is usually important to know the following functions: JSON_PRETTY, JSON_UNQUOTE & JSON_EXTRACT

-- Print the outputs
SELECT JSON_PRETTY(@output)\G

*************************** 1. row ***************************
JSON_PRETTY(@output): {
  "text": "\n\nAccording to the provided context, the well-developed blue flower of Iris versicolor has 6 petals and sepals that are spread out nearly flat. The longer sepals are hairless and have a greenish-yellow blotch at their base.",
  "citations": [
    {
      "segment": "Iris versicolor is a flowering herbaceous perennial plant, growing 10–80 cm (4–31 in) high. It tends to form large clumps from thick, creeping rhizomes. The unwinged, erect stems generally have basal leaves that are more than 1 cm (1?2 in) wide. Leaves are folded on the midribs so that they form an overlapping flat fan. The well developed blue flower has 6 petals and sepals spread out nearly flat and have two forms. The longer sepals are hairless and have a greenish-yellow blotch at their base.",
      "distance": 0.1099172830581665,
      "document_name": "https://objectstorage.eu-frankfurt.../Iris_versicolor_Wikipedia.pdf"
    },
    {
      "segment": "Iris versicolor\nIris versicolor\nTaxonavigation: Asparagales \nClassification System: APG IV Superregnum: Eukaryota Regnum: Plantae Cladus: Angiosperms Cladus: Monocots Ordo: Asparagales\nFamilia: Iridaceae Subfamilia: Iridoideae Tribus: Irideae Genus: Iris Subgenus: I. subg. Limniris Sectio: I. sect. Limniris Series: Iris ser. Laevigatae Species: Iris versicolor\nIris versicolor L., Sp. Pl. 1: 39 (1753).",
      "distance": 0.2635544538497925,
      "document_name": "https://objectstorage.eu-frankfurt.../Iris_versicolor_Wikispecies.pdf"
    },
    {
      "segment": "Homotypic Iris versicolor f. virginica (L.) VOSS, Vilm. Blumengaertn., ed. 3. 1: 978. 1895. Iris versicolor var. virginica (L.) BAKER, J. Linn. Soc., Bot. 16: 141 (1877). Xiphion virginicum (L.) ALEF., Bot. Zeitung (Berlin) 21: 297 (1863).\nHeterotypic Iris albilinea ALEXANDER SMALL & ALEXANDER, Contr. New York Bot. Gard. 327: 349 (1931).\nTaxonavigation\nName\nSynonyms",
      "distance": 0.27319884300231934,
      "document_name": "https://objectstorage.eu-frankfurt.../Iris_virginica_Wikispecies.pdf"
    }
  ],
  "vector_store": [
    "Iris_AI.IrisAIembeddings"
  ]
}

To display only the answer:

-- Get the answer from the generated JSON result.
SELECT JSON_UNQUOTE(JSON_EXTRACT(@output, '$.text')) AS answer\G

*************************** 1. row ***************************
answer: 

According to the provided context, the well-developed blue flower of Iris versicolor has 6 petals and sepals that are spread out nearly flat. The longer sepals are hairless and have a greenish-yellow blotch at their base.

To display only the citations:

-- You can also get the citations referring to segments from the loaded documents.
SELECT JSON_UNQUOTE(JSON_EXTRACT(@output, '$.citations')) AS citations\G

*************************** 1. row ***************************
citations: [{"segment": "Iris versicolor is a flowering herbaceous perennial plant, growing 10–80 cm (4–31 in) high. It tends to form large clumps from thick, creeping rhizomes. The unwinged, erect stems generally have basal leaves that are more than 1 cm (1?2 in) wide. Leaves are folded on the midribs so that they form an overlapping flat fan. The well developed blue flower has 6 petals and sepals spread out nearly flat and have two forms. The longer sepals are hairless and have a greenish-yellow blotch at their base.", "distance": 0.1099172830581665, "document_name": "https://objectstorage.eu-frankfurt.../Iris_versicolor_Wikipedia.pdf"}, {"segment": "Iris versicolor\nIris versicolor\nTaxonavigation: Asparagales \nClassification System: APG IV Superregnum: Eukaryota Regnum: Plantae Cladus: Angiosperms Cladus: Monocots Ordo: Asparagales\nFamilia: Iridaceae Subfamilia: Iridoideae Tribus: Irideae Genus: Iris Subgenus: I. subg. Limniris Sectio: I. sect. Limniris Series: Iris ser. Laevigatae Species: Iris versicolor\nIris versicolor L., Sp. Pl. 1: 39 (1753).", "distance": 0.2635544538497925, "document_name": "https://objectstorage.eu-frankfurt.../Iris_versicolor_Wikispecies.pdf"}, {"segment": "Homotypic Iris versicolor f. virginica (L.) VOSS, Vilm. Blumengaertn., ed. 3. 1: 978. 1895. Iris versicolor var. virginica (L.) BAKER, J. Linn. Soc., Bot. 16: 141 (1877). Xiphion virginicum (L.) ALEF., Bot. Zeitung (Berlin) 21: 297 (1863).\nHeterotypic Iris albilinea ALEXANDER SMALL & ALEXANDER, Contr. New York Bot. Gard. 327: 349 (1931).\nTaxonavigation\nName\nSynonyms", "distance": 0.27319884300231934, "document_name": "https://objectstorage.eu-frankfurt.../Iris_virginica_Wikispecies.pdf"}]

Et voilà!
As simple as that.

Create your chatbot with HeatWave Chat

HeatWave Chat is an advanced conversational agent powered by LLMs. It utilizes RAG and a vector store to deliver accurate, data-driven insights. By simulating human-like conversations, it provides users with a dynamic and informative experience, offering deep knowledge and meaningful insights in a conversational format.
HeatWave Chat is designed to understand user inputs and respond naturally. It enhances text generation by leveraging chat history for follow-up questions and utilizes vector search to access knowledge stored in its built-in vector store. With all communication and processing securely managed within the HeatWave service, HeatWave Chat delivers fast and reliable responses.

In order to run your chat, you should load the model, specify some chat options like the vector store table, the distance metric, the language, and also options like temperaturetop_k, top_p, repeat_penalty, etc…), then define your natural language query and finally run the sys.HEATWAVE_CHAT stored procedure:

-- Load the model: ex. Mistral 7B
CALL sys.ML_MODEL_LOAD("mistral-7b-instruct-v1", NULL);

-- Setup HeatWave Chat options
SET @chat_options = JSON_OBJECT("tables", JSON_ARRAY(JSON_OBJECT("schema_name", "`Iris_AI`",
"table_name","`IrisAIembeddings`")), "model_options", JSON_OBJECT("model_id", "mistral-7b-instruct-v1", "language",
"en", "temperature", 0));

-- Query in natural language (en, fr, de, es, hi...)
SET @question = "Where is Iris setosa native to?";

-- Add your query to HeatWave Chat
CALL sys.HEATWAVE_CHAT(@question);
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| response                                                                                                                                                                                                                                                                                                                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  Iris setosa is native to a circum-arctic distribution ranging from coastal Aleutian islands, Alaska, Maine in USA, within Canada (including British Columbia, Newfoundland, Quebec, and the Yukon), within Russia (including near to the lower Lena River, Siberia), northeastern Asia, China (including Manchuria and east Jilin), Korea and southwards to Japan (including Honshu and Hokkaido). |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+



-- Another query in natural language (en, fr, de, es, hi...)
SET @question = "How is Iris virginica used in Seminole culture?";

-- Add your query to HeatWave Chat
CALL sys.HEATWAVE_CHAT(@question);
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| response                                                                                                                                                                                                                                                                                  |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  Answer: Iris virginica has a cultural significance in Seminole culture as it is one of the Iris species used to treat "shock following alligator-bite". However, there is limited information available on how Iris virginica is specifically used in Seminole culture for this purpose. |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

The HEATWAVE_CHAT routine leverages the @chat_options variable as a dynamic parameter store. This variable is populated with additional context and configuration settings, which are subsequently utilized to refine the response generation process.

SELECT JSON_PRETTY(@chat_options)\G
*************************** 1. row ***************************
JSON_PRETTY(@chat_options): {
  "tables": [
    {
      "table_name": "`IrisAIembeddings`",
      "schema_name": "`Iris_AI`"
    }
  ],
  "response": "\n\nAccording to the provided context, it is mentioned that Iris virginica may be one of the Iris species used by the Seminole tribe to treat \"shock following alligator-bite\". This suggests that the plant has been traditionally used by the Seminole people for medicinal purposes in treating a specific condition related to alligator bites.",
  "documents": [
    {
      "id": "https://objectstorage.eu-frankfurt.../Iris_virginica_Wikipedia.pdf",
      "title": "Iris_virginica_Wikipedia.pdf",
      "segment": "p://herb.umd.umich.edu/herb/search.pl?searchstring=Iris+virginica) 12. Hamel, Paul B. and Mary U. Chiltoskey 1975 Cherokee Plants and Their Uses -- A 400 Year History. Sylva, N.C. Herald Publishing Co. (p. 41) 13. \"USDA Plants Database\" (https://plants.usda.gov/home/plantProfile?symbol=IRVI).\nplants.usda.gov.\nKemper Center for Home Gardening.org: Iris virginica (Virginia iris) (http://www.missouribota nicalgarden.org/PlantFinder/PlantFinderDetails.aspx?kempercode=y390)",
      "distance": 0.2979528307914734
    },
    {
      "id": "https://objectstorage.eu-frankfurt.../Iris_virginica_Wikipedia.pdf",
      "title": "Iris_virginica_Wikipedia.pdf",
      "segment": "It may be one of the Iris species used by the Seminole to treat \"shock following alligator-bite\".[13]\nIris virginica is one of three iris species in Ronald Fisher's Iris flower data set.",
      "distance": 0.3149242401123047
    },
    {
      "id": "https://objectstorage.eu-frankfurt.../Iris_virginica_Wikispecies.pdf",
      "title": "Iris_virginica_Wikispecies.pdf",
      "segment": "Retrieved from \"https://species.wikimedia.org/w/index.php?title=Iris_virginica&oldid=8735713\"\nThis page was last edited on 5 April 2022, at 21:56.\nText is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.\nVernacular names",
      "distance": 0.3498268127441406
    }
  ],
  "chat_history": [
    {
      "user_message": "Where is Iris setosa native to?",
      "chat_query_id": "d2dd2c8e-b3e8-11ef-bdb3-020017081917",
      "chat_bot_message": " Iris setosa is native to a circum-arctic distribution ranging from coastal Aleutian islands, Alaska, Maine in USA, within Canada (including British Columbia, Newfoundland, Quebec, and the Yukon), within Russia (including near to the lower Lena River, Siberia), northeastern Asia, China (including Manchuria and east Jilin), Korea and southwards to Japan (including Honshu and Hokkaido)."
    },
    {
      "user_message": "How is Iris virginica used in Seminole culture?",
      "chat_query_id": "7d4ea547-b3e9-11ef-bdb3-020017081917",
      "chat_bot_message": " Answer: Iris virginica has a cultural significance in Seminole culture as it is one of the Iris species used to treat \"shock following alligator-bite\". However, there is limited information available on how Iris virginica is specifically used in Seminole culture for this purpose."
    }
  ],
  "model_options": {
    "language": "en",
    "model_id": "mistral-7b-instruct-v1",
    "temperature": 0
  },
  "request_completed": true
}

The list of all the parameters that you can set in the @chat_options variable is available here and here.

You can modify chat parameters using the @chat_options variable, without resetting the chat session:

-- Modifying a chat parameter, here the model, without resetting the chat session:

-- Load the new model if needed
CALL sys.ML_MODEL_LOAD("llama3-8b-instruct-v1", NULL);

SET @chat_options = JSON_SET(@chat_options, '$.model_options.model_id', "llama3-8b-instruct-v1");

or by resetting the chat session:

-- Modifying a chat parameter, here the model, and resetting the chat session:

-- Load the new model if needed
CALL sys.ML_MODEL_LOAD("llama3-8b-instruct-v1", NULL);

SET @chat_options = JSON_OBJECT("model_options", JSON_OBJECT("model_id", "llama3-8b-instruct-v1", "language", "en", "temperature", 0));

More examples here.

As you have seen, HeatWave GenAI simplifies LLM integration with SQL queries, streamlines database connections, and offers a user-friendly architecture that lets you quickly develop applications.

That’s all for today—it’s time to wrap things up! 🙂

Peroration

HeatWave GenAI offers a seamless and powerful platform for integrating generative AI capabilities into your data workflows.
By combining advanced features like retrieval-augmented generation (RAG), an integrated vector store, and user-friendly SQL-based interfaces, HeatWave GenAI empowers users to unlock insights from unstructured data with minimal complexity.
Its efficient and secure in-database architecture ensures real-time performance, scalability, and always up-to-date data access.

Whether you aim to streamline content generation, enhance search and retrieval tasks, or build interactive chatbots, HeatWave GenAI provides all the tools you need within a single ecosystem. By leveraging the tutorials and techniques shared in this guide, you can fully harness the capabilities of HeatWave GenAI to transform how you interact with data and elevate your applications to new heights.

Key Features, Unmatched Benefits
Here’s what sets HeatWave GenAI apart:

  • Effortless LLM Integration: Harness the power of large language models with simple SQL queries.
  • Seamless Database Integration: No need for intricate external connections, simplifying your workflow.
  • Simplified Architecture: Easy to manage and scale, ensuring a smooth user experience.
  • Always Up-to-Date Data: In-database LLMs guarantee data freshness for accurate results.

Stay tuned for more insights!

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

0

HeatWave GenAI: Sentiment Analysis Made Easy-Peasy

September 10, 2024
Tags: ,

This article builds upon the concepts introduced in my previous blog posts, HeatWave GenAI: Your AI-Powered Content Creation Partner and In-Database LLMs for Efficient Text Translation with HeatWave GenAI.
For a deeper understanding, also consider reading these articles.

So, you’ve probably heard about AI getting “smarter” by the day. Well, one cool thing it can do now is read between the lines of what people say. It’s like having a mind-reading machine for text!
This new AI tech, called generative AI (or GenAI), can dive deep into what people are saying and tell us if they’re feeling positive, negative, or neutral.
Let’s see how HeatWave GenAI, can help you to enhance your understanding of customer sentiment, improve decision-making, and drive business success.

What is AI sentiment Analysis?

You can find a more formal and complete definition on Wikipedia, but in few words, it is like having a computer that can understand human emotions. This technology can analyze text data and thus dive deep into what people are saying and tell us if they’re happy, sad, or even mad.

By understanding the sentiment behind words, AI can help businesses gauge customer satisfaction, track brand reputation, and even predict market trends.
It’s a powerful tool that has a wide range of applications in fields like marketing, social media, and customer service.

What is HeatWave?

HeatWave is a fully-managed Oracle Cloud Infrastructure service. It offered different solutions for different workloads:

  • HeatWave MySQL: built on MySQL, for your transactional needs.
  • HeatWave Analytics: get real-time analytics on your data.
  • HeatWave Lakehouse: query data in various formats in object storage
  • HeatWave AutoML: automate the pipeline to build, train, and explain ML models
  • HeatWave GenAI: integrated & automated GenAI with in-database LLMs

Oracle’s HeatWave GenAI (starting with version 9.0.1) is at the forefront of this revolution, offering an integrated platform that combines in-database large language models (LLMs), vector stores, and scale-out vector processing to streamline content generation.

The key benefits of such architecture are:

  • Effortless LLM Integration: Simple SQL queries unlock AI power.
  • Seamless Database Integration: No complex external connections.
  • Simplified Architecture: Easy management and scalability.
  • Scalable Performance: Handles large datasets and high traffic.
  • Always Up-to-Date Data: In-database LLMs ensure data freshness.

In this article I’m using HeatWave 9.0.1.

For the purpose of this article, let’s consider a scenario where you’re a mobile phone manufacturer. In today’s digital age, it’s crucial to keep a pulse on customer sentiment. Your company actively monitors various social networks to stay informed about what customers are saying about your products.
For example, short messages like:

  • Just got my hands on the latest mobile phone and I’m loving it! Thanks for creating such an awesome product!
  • A mobile phone that prioritizes functionality and reliability over flashy features.
  • Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!

You get the idea!

So, how can you harnessing HeatWave GenAI for effortless sentiment analysis?
Like we have seen in HeatWave GenAI: Your AI-Powered Content Creation Partner & In-Database LLMs for Efficient Text Translation with HeatWave GenAI the first step is to load the LLM in HeatWave memory.

Load the LLM in HeatWave memory

Use the ML_MODEL_LOAD stored procedure: CALL sys.ML_MODEL_LOAD(‘<LLM>’, NULL);
The model needs to be loaded only once before generating text. It remains accessible within HeatWave as long as the service is running. This means you don’t have to reload the model for each text generation.

At the time of writing (beginning of September 2024) the available models are Mistral 7B (mistral-7b-instruct-v1) and Llama 2 (llama2-7b-v1).

mysql>
SELECT @@version, @@version_comment;
+-------------+--------------------------+
| @@version   | @@version_comment        |
+-------------+--------------------------+
| 9.0.1-cloud | MySQL Enterprise - Cloud |
+-------------+--------------------------+


-- Load Mistral 7B model
CALL sys.ML_MODEL_LOAD('mistral-7b-instruct-v1', NULL);

Mistral is loaded. Now you can think about defining your prompt.

Define your prompt in natural language

To make it more convenient, let’s use some variables. The sort messages above are now:

mysql>
SET @customer_msgA="Just got my hands on the latest mobile phone and I'm loving it! Thanks for creating such an awesome product!" ;

SET @customer_msgB="A mobile phone that prioritizes functionality and reliability over flashy features." ;

SET @customer_msgC="Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!" ;

The prompt contains a message and the inference instructions.
Let’s start with a very simple instruction text, something like:
What is the feeling from the text above?

mysql>
SET @instructions=" What is the feeling from the text above?";

SET @prompt=CONCAT(@customer_msgA, @instructions);

The final prompt is the concatenation of 2 variables: @customer_msg{X} and @instructions.

In order to have the optimal result, maybe you’ll need to do some fine tuning to set up the inference.

Inference Setting

I use generation as a task and the Mistral model and I put temperature, top-k and top-p to zero because I do not want any creativity from the model 🙂 .

mysql>
SET @inferenceSetup  = '{"task": "generation", "temperature": 0, "top_k": 0, "top_p": 0, "model_id": "mistral-7b-instruct-v1"}';

We have now all what we need to do sentiment analysis.

Sentiment Analysis

Like we have seen in HeatWave GenAI: Your AI-Powered Content Creation Partner & In-Database LLMs for Efficient Text Translation with HeatWave GenAI we must use the ML_GENERATE function.

One way to display the result is to use the following query:

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt;

Result with: SET @customer_msgA=”Just got my hands on the latest mobile phone and I’m loving it! Thanks for creating such an awesome product!” ;

mysql>
/* msg: "Just got my hands on the latest mobile phone and I'm loving it! Thanks for creating such an awesome product!" */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt;
+---------------------------------------------------------+
| Answer                                                  |
+---------------------------------------------------------+
|  The feeling expressed in the text is happiness or joy. |
+---------------------------------------------------------+

With SET @customer_msgB=”A mobile phone that prioritizes functionality and reliability over flashy features.” ;

mysql>
/* msg: "A mobile phone that prioritizes functionality and reliability over flashy features." */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt\G
*************************** 1. row ***************************
Answer:  The feeling conveyed in the text is one of practicality and pragmatism, with a focus on the importance of functionality and reliability in a mobile phone.

With SET @customer_msgC=”Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!” ;

mysql>
/* msg: "Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!" */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt\G
*************************** 1. row ***************************
Answer:  The feeling conveyed in the text above is frustration and disappointment.

Amazing!
The model provide a summary of the sentiment expressed in the message.

But what if we want to use this feature more programmatically? Well we need to improve our prompt.

Sentiment Analysis in Code

Basically we can ask the LLM to generate the result in a specific format, other than raw text like JSON, CSV, … or an integer.

Let’s say we have the following code:

  • Positive feeling: 1.
  • Negative feeling: -1
  • Neutral feeling or unknown: 0

The goal is to automatically identify problematic messages — those that express non positive sentiments — and then take action.

To do this, we only need to rewrite our prompt, improving the instruction part. For example we can have something like:
What is the feeling from the text above? if it is positive write 1. If it is negative write -1. if it is neutral or if you don’t know write 0. Regarding to your analysis, write only -1 or 0 or 1

Still a basic prompt, far from being optimal, but good enough to keep this article simple and most important, to do the job.

mysql>
SET @instructions=" What is the feeling from the text above? if it is positive write 1. If it is negative write -1. if it is neutral or if you don't know write 0. Regarding to your analysis, write only -1 or 0 or 1";

All the other variables are the same than the previous use case.
Let’s see the result:

Result with: SET @customer_msgA=”Just got my hands on the latest mobile phone and I’m loving it! Thanks for creating such an awesome product!” ;

mysql>
/* msg: "Just got my hands on the latest mobile phone and I'm loving it! Thanks for creating such an awesome product!" */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt;
+--------+
| Answer |
+--------+
|  1     |
+--------+

With SET @customer_msgB=”A mobile phone that prioritizes functionality and reliability over flashy features.” ;

mysql>
/* msg: "A mobile phone that prioritizes functionality and reliability over flashy features." */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt;
+--------+
| Answer |
+--------+
|  0     |
+--------+

With SET @customer_msgC=”Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!” ;

mysql>
/* msg: "Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!" */

SELECT Answer->>"$.text" AS "Answer" FROM (SELECT sys.ML_GENERATE(@prompt, @inferenceSetup) AS Answer ) AS dt;
+--------+
| Answer |
+--------+
|  -1    |
+--------+

Awesome!
The model produces results based on the format chosen and needed in our code. It could have been a JSON format for example and the logic would have been the same.

With such output you can easily integrate HeatWave in your data pipeline.

Sentiment Analysis into your data workflow with HeatWave

Imagine we store our customer messages in a (MySQL) table. A possible workflow involves regularly scanning this table for negative feedback. Once identified, these messages can be referenced in ( or be moved to) a separate table. This new table can then be used to populate the customer service team’s dashboard. By tracking unsatisfied customers in this way, the team can work to improve overall customer satisfaction.

A simplified data architecture could be:

mysql>
CREATE SCHEMA sentimentAnalysis;


CREATE TABLE `sentimentAnalysis`.`tweet` (
  `tweet_id` int unsigned NOT NULL AUTO_INCREMENT,
  `customer_msg` varchar(4000) DEFAULT NULL,
  PRIMARY KEY (`tweet_id`)
);


CREATE TABLE `sentimentAnalysis`.`tweet_to_review` (
  `tweet_to_review_id` int unsigned NOT NULL AUTO_INCREMENT,
  `tweet_id_to_check` int unsigned NOT NULL,
  `analysis_result` tinyint NOT NULL,
  PRIMARY KEY (`tweet_to_review_id`)
);

Table tweet contains the customer messages and a message ID..
The problematic messages (ie status 0 and -1) are referenced into table tweet_to_review, using the message ID. We also store the result of the sentiment analysis (0 or -1). Support may prefer to focus first on messages that are definitely negative (status -1).

mysql>
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Just got my hands on the latest mobile phone and I'm loving it! Thanks for creating such an awesome product!");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Loving the features on my new mobile phone! It's like a whole new world in the palm of my hand.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Your mobile phone company is a joke. Can't believe I wasted my hard-earned money on your crappy products.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Hey, just wanted to say how much I love your product! It's been a game changer for me.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Recently upgraded my old mobile phone to something newer - let's see how it goes");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("A mobile phone that prioritizes functionality and reliability over flashy features.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Why does your mobile phone company insist on selling devices that are constantly glitching and breaking? Do better!");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Shoutout for making such an awesome product. I couldn't imagine my life without it!");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Just another day dealing with the incompetence of my mobile phone company. Seriously considering switching to a different provider.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("For those who want a device that just works.");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Big thanks for creating a top-notch mobile phone. You guys never disappoint!");
INSERT `sentimentAnalysis`.`tweet` (customer_msg) VALUES("Just got my hands on a new mobile phone and I'm excited to see what it can do!");

Some data inserted in tweet table.
Most of these data were generated using AI Tweet Generator.

mysql>
SELECT * FROM `sentimentAnalysis`.`tweet` LIMIT 5\G
*************************** 1. row ***************************
    tweet_id: 1
customer_msg: Just got my hands on the latest mobile phone and I'm loving it! Thanks for creating such an awesome product!
*************************** 2. row ***************************
    tweet_id: 2
customer_msg: Loving the features on my new mobile phone! It's like a whole new world in the palm of my hand.
*************************** 3. row ***************************
    tweet_id: 3
customer_msg: Your mobile phone company is a joke. Can't believe I wasted my hard-earned money on your crappy products.
*************************** 4. row ***************************
    tweet_id: 4
customer_msg: Hey, just wanted to say how much I love your product! It's been a game changer for me.
*************************** 5. row ***************************
    tweet_id: 5
customer_msg: Recently upgraded my old mobile phone to something newer - let's see how it goes

Sample of the data.

The workflow of our data pipeline is quite simple:

  1. Get the tweet content from the source (table tweet)
  2. Do the sentiment analysis using HeatWave GenAI
  3. Write problematic tweets (status <> 0) into the target (table tweet_to_review) for further processing

It can be done with your favourite programing language. It can also be done on the database side using HeatWave MySQL stored routines capabilities.
If you are a javascript fan, you can now write your stored routine in that language since HeatWave MySQL 9.0: JavaScript Stored Programs

That said, for the love of the (SQL) art, let’s try something in SQL. But before diving into the code, let’s break down the 3 steps above.

Get the tweet content from the source (table tweet)

In order to have a flexible stored procedure, the input parameters are:

  • source schema name: VARCHAR (sentimentAnalysis)
  • source table name: VARCHAR (tweet)
  • source column message: VARCHAR (customer_msg)
  • source column primary key name: VARCHAR (tweet_id)
  • tweet ID: INTEGER UNSIGNED (ex: 1, 2, 3….)
  • target schema name: VARCHAR (sentimentAnalysis)
  • target table name: VARCHAR (tweet_to_review)
  • target column result name: VARCHAR (analysis_result)

To get the tweet content from the source the query is:

SELECT <source column message>
FROM <source schema name>.<source table name>
WHERE <source column primary key name>=<tweet ID>
INTO @customer_msg

Do the sentiment analysis using HeatWave GenAI

We’ve already seen this part:

Prompt instruction:
instructions=” What is the feeling from the text above? if it is positive write 1. If it is negative write -1. if it is neutral or if you don’t know write 0. Regarding to your analysis, write only -1 or 0 or 1″;

Add the tweet to the prompt:
prompt=CONCAT(‘”‘, @customer_msg, instructions, ‘”‘);

Inference setup:
inferenceSetup = ‘\'{“task”: “generation”, “temperature”: 0, “top_k”: 0, “top_p”: 0, “model_id”: “mistral-7b-instruct-v1″}\”;

And the Inference:
SELECT Answer->>”$.text” AS “Answer” FROM (SELECT sys.ML_GENERATE(prompt, inferenceSetup) AS Answer ) AS dt INTO @answer ;

Write problematic tweets into the target for further processing

And finally store the ID of the problematic message (tweet ID) and its status (-1 or 0):

INSERT INTO <target schema name>.<target table name> (<target column message ID name>, <target column result name>) VALUES (<tweetID>, <@answer>)

How it works?

The stored routine is called: genai_sentimentAnalysis.
In the following examples, it is run with tweet ID: 1, 3 and 10:

mysql>
CALL genai_sentimentAnalysis("sentimentAnalysis", "tweet", "customer_msg", "tweet_id", 1, "sentimentAnalysis", "tweet_to_review", "analysis_result");
+-----------------------------------------------+
| INFO                                          |
+-----------------------------------------------+
| Tweet number 1 as been analyzed as  POSITIVE  |
+-----------------------------------------------+
1 row in set (2.4341 sec)

Query OK, 0 rows affected (2.4341 sec)


CALL genai_sentimentAnalysis("sentimentAnalysis", "tweet", "customer_msg", "tweet_id", 3, "sentimentAnalysis", "tweet_to_review", "analysis_result");
+-----------------------------------------------+
| INFO                                          |
+-----------------------------------------------+
| Tweet number 3 as been analyzed as  NEGATIVE  |
+-----------------------------------------------+
1 row in set (1.9574 sec)

Query OK, 0 rows affected (1.9574 sec)


CALL genai_sentimentAnalysis("sentimentAnalysis", "tweet", "customer_msg", "tweet_id", 10, "sentimentAnalysis", "tweet_to_review", "analysis_result");
+-----------------------------------------------+
| INFO                                          |
+-----------------------------------------------+
| Tweet number 10 as been analyzed as  NEUTRAL  |
+-----------------------------------------------+
1 row in set (1.8339 sec)

Query OK, 0 rows affected (1.8339 sec)

The stored procedure output is a log that is displayed on the console.
The first call generates 1 (tweet ID 1 is positive), the second -1 (tweet ID 3 is negative) and the third 0 (tweet ID 10 is neutral).

Let’s check the tweet_to_review table:

mysql>
SELECT tweet_id_to_check, analysis_result FROM tweet_to_review;
+-------------------+-----------------+
| tweet_id_to_check | analysis_result |
+-------------------+-----------------+
|                 3 |              -1 |
|                10 |               0 |
+-------------------+-----------------+

Only , tweet 3 and 10 are selected for the customer service team.

For their dashboard you can provide more details with a query like:

mysql>
SELECT tweet_id_to_check, analysis_result, customer_msg 
FROM tweet_to_review INNER JOIN tweet ON tweet_id=tweet_id_to_check 
ORDER BY analysis_result\G
*************************** 1. row ***************************
tweet_id_to_check: 3
  analysis_result: -1
     customer_msg: Your mobile phone company is a joke. Can't believe I wasted my hard-earned money on your crappy products.
*************************** 2. row ***************************
tweet_id_to_check: 10
  analysis_result: 0
     customer_msg: For those who want a device that just works.


Show me the code

Here the stored procedure used.
As a disclaimer, this code is not meant to run in production, it is for demonstration purpose only 🙂

mysql>
DELIMITER $$
CREATE DEFINER=`MyHeatwaveUser`@`%` PROCEDURE `genai_sentimentAnalysis`(IN _sourceSchema VARCHAR(64), IN _sourceTable VARCHAR(64), IN _sourceColMsg VARCHAR(64), IN _sourceColPK VARCHAR(64), IN _tweetID INTEGER UNSIGNED, IN _targetSchema VARCHAR(64), IN _targetTable VARCHAR(64), IN _targetColResult VARCHAR(64))
    SQL SECURITY INVOKER
BEGIN
    DECLARE sourceTableName, targetTableName, targetCols VARCHAR(129);
    DECLARE instructions, inferenceSetup VARCHAR(512);
    DECLARE prompt VARCHAR(5000);
    DECLARE rep VARCHAR(100);
    
    /* Get the tweet content from source table */
    SET sourceTableName = CONCAT("`",_sourceSchema, "`.`", _sourceTable, "`");
    SET @txtStmt = CONCAT("SELECT ", _sourceColMsg, " INTO @customer_msg FROM  ", sourceTableName, " WHERE ", _sourceColPK, " = ", _tweetID);
    PREPARE getTweet FROM @txtStmt;
    EXECUTE getTweet;
    DEALLOCATE PREPARE getTweet;
    
    /* Sentiment analysis using HeatWave GenAI */
    -- Prompt instruction
    SET instructions=" What is the feeling from the text above? if it is positive write 1. If it is negative write -1. if it is neutral or if you don't know write 0. Regarding to your analysis, write only -1 or 0 or 1";
	
    -- Add the tweet to the prompt
    SET prompt=CONCAT('"', @customer_msg, instructions, '"');
	
    -- Inference setup
    SET inferenceSetup  = '\'{"task": "generation", "temperature": 0, "top_k": 0, "top_p": 0, "model_id": "mistral-7b-instruct-v1"}\'';

    -- Inference: if positive retunr 1 / if negative return -1 / if neutral or unknown return 0
    SET @txtStmt = CONCAT(' SELECT Answer->>"$.text" AS Answer FROM (SELECT sys.ML_GENERATE(', prompt, ' , ', inferenceSetup, ' ) AS Answer ) AS dt INTO @answer');
    PREPARE analysis FROM @txtStmt;
    EXECUTE analysis;
    DEALLOCATE PREPARE analysis;

    /* Write problematic tweets (status <> 0) into the target table for further processing */	
    SET rep = CONCAT('Tweet number ', _tweetID, ' as been analyzed as ');
    IF @answer < 1 THEN
        BEGIN
            SET targetTableName = CONCAT("`",_targetSchema, "`.`", _targetTable, "`");
            SET targetCols = CONCAT("(tweet_id_to_check, ", _targetColResult,")");
            SET @txtStmt = CONCAT('INSERT INTO ', targetTableName, targetCols, ' VALUES (', _tweetID, ', ', @answer, ')');
            PREPARE ins FROM @txtStmt;
            EXECUTE ins;
            DEALLOCATE PREPARE ins;
            
            /* Logging */
            CASE @answer
                WHEN 0 THEN SELECT CONCAT(rep, ' NEUTRAL ') AS 'INFO';
                WHEN -1 THEN SELECT CONCAT(rep, ' NEGATIVE ') AS 'INFO';
            END CASE;
        END;
    ELSE SELECT CONCAT(rep, ' POSITIVE ') AS 'INFO';
    END IF;
       
END$$
DELIMITER ;

As you see, all the logic is coded inside the procedure. It would probably make sense to break the different tasks to separate procedures/functions.

Areas for improvement

As you can imagine, the prompt is crucial. While I’ve tried kept things simple for this tutorial, effective prompt engineering is essential for getting the desired results from an LLM.

Remember to review the LLM’s output carefully. Even though we expect an integer, the model might generate something unexpected (not handle in the stored procedure).

As of {HeatWave} MySQL 9.0.0, you can now write stored routines in JavaScript. However, stored procedures aren’t mandatory. Choose the language that best suits your needs and start experimenting! 🙂

Peroration

In this article, we’ve explored how to leverage Oracle HeatWave GenAI to perform sentiment analysis on text data. By integrating in-database large language models HeatWave offers an easy, streamlined and efficient solution for understanding customer sentiment.

We’ve covered the essential steps involved, from loading the LLM into HeatWave memory to crafting effective prompts and analyzing the model’s output. By following these guidelines, you can easily incorporate sentiment analysis into your data pipeline and gain valuable insights into customer feedback.

Remember that effective prompt engineering is key to obtaining accurate and meaningful results. Experiment with different prompts and refine your approach to achieve the desired outcomes.

With HeatWave GenAI, you have a powerful tool at your disposal for extracting valuable information from text data. By harnessing its capabilities, you can enhance your understanding of customer sentiment, improve decision-making, and drive business success.

Stay tuned for more insights!

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

1

In-Database LLMs for Efficient Text Translation with HeatWave GenAI

August 13, 2024
Tags: , ,

This article builds upon the concepts introduced in my previous blog post, HeatWave GenAI: Your AI-Powered Content Creation Partner. For a deeper understanding, consider reading that article first.

HeatWave offered different solutions for your different workload:

  • HeatWave MySQL: built on MySQL, for your transactional needs.
  • HeatWave Analytics: get real-time analytics on your data.
  • HeatWave Lakehouse: query data in various formats in object storage
  • HeatWave AutoML: automate the pipeline to build, train, and explain ML models
  • HeatWave GenAI: integrated & automated GenAI with in-database LLMs

In this article you’ll experience the power of In-database translation with HeatWave GenAI. I am using HeatWave 9.0.1.

As demonstrated in my previous article, HeatWave GenAI: Your AI-Powered Content Creation Partner, HeatWave GenAI streamlines content generation by seamlessly integrating LLMs into the database. This innovative approach eliminates the complexities of external connections, allowing for effortless AI integration via straightforward SQL queries.

While HeatWave GenAI excels at generating English text, its capabilities extend to translation as well. Let’s explore how we can effectively translate English content into French using this powerful tool.

Load the LLM in HeatWave memory

Use the ML_MODEL_LOAD stored procedure: CALL sys.ML_MODEL_LOAD(‘<LLM>’, NULL);
The model needs to be loaded only once before generating text. It remains accessible within HeatWave as long as the service is running. This means you don’t have to reload the model for each text generation.

At the time of writing (August 2024) the available models are Mistral 7B (mistral-7b-instruct-v1) and Llama 2 (llama2-7b-v1).

-- Load Mistral 7B model
mysql>
CALL sys.ML_MODEL_LOAD('mistral-7b-instruct-v1', NULL);

Define your prompt in natural language

We obviously need a text to translate:

mysql> 
-- Text for Translation
SET @ENtext = "Dracula is a gothic horror novel by Bram Stoker, published on 26 May 1897. An epistolary novel, the narrative is related through letters, diary entries, and newspaper articles. It has no single protagonist and opens with solicitor Jonathan Harker taking a business trip to stay at the castle of a Transylvanian nobleman, Count Dracula. Harker escapes the castle after discovering that Dracula is a vampire, and the Count moves to England and plagues the seaside town of Whitby. A small group, led by Abraham Van Helsing, investigate, hunt and kill Dracula.";

Text extracted from Wikipedia: https://en.wikipedia.org/wiki/Dracula

With the text ready for translation, we need to provide clear instructions to the model about the desired output.
Actually, the most difficult part is here. The quality of the translation will be highly impacted by the quality of your prompt.

For now, let’s try a very simple prompt like: “Translate the following text into French, ensuring natural and grammatically correct phrasing“.
It will be concatenated (CONCAT) with the provided English text :

mysql> 
-- Enhanced prompt
SET @finalPrompt = CONCAT("Translate the following text into French, ensuring natural and grammatically correct phrasing: ", @ENtext);

Inference setting

Here, you select the desired task, choose the appropriate model, and fine-tune parameters to influence the output.

mysql> 
-- Inference setting
SET @inferenceSetup  = '{"task": "generation", "model_id": "mistral-7b-instruct-v1"}';

For simplicity, this example uses default parameters. A more complex example is provided below.

Generate the translation using HeatWave GenAI

And finally, send the enhanced prompt to the LLM using the ML_GENERATE function. The second parameter of this function is a JSON object that allows you to specify the task, the LLM, and the tuning options.

mysql>
-- Translation
SELECT sys.ML_GENERATE(@finalPrompt, @inferenceSetup)\G

*************************** 1. row ***************************
sys.ML_GENERATE(@finalPrompt, @inferenceSetup): {"text": " Réponse : Le roman gothique horreur de Bram Stoker, intitulé « Dracula », a été publié le 26 mai 1897. C'est un roman épistolaire dont la narration se fait par les lettres, les entrées dans le journal et les articles de presse. Il n'a pas d'héroïne principale et commence avec l'avocat Jonathan Harker qui effectue une visite d'affaires au château d'un nobleman transylvanien, le comte Dracula. Harker s'évade du château après avoir découvert que Dracula est un vampire, et le comte se rend en Angleterre et pille la ville côtière de Whitby. Un petit groupe, dirigé par Abraham Van Helsing, enquête, chasse et tue Dracula."

The output in a JSON that is not really nice to read.
But SQL is very powerful and you can easily improve the output using JSON_TABLE function.

SELECT TranslationFR AS TranslationFR FROM ( SELECT sys.ML_GENERATE(@finalPrompt, @inferenceSetup) AS textGen ) AS dt INNER JOIN JSON_TABLE ( dt.textGen, "$" COLUMNS( TranslationFR text PATH "$.text") ) AS jt \G

*************************** 1. row ***************************
TranslationFR:  Réponse : Le roman gothique horreur de Bram Stoker, intitulé « Dracula », a été publié le 26 mai 1897. C'est un roman épistolaire dont la narration se fait par les lettres, les entrées dans le journal et les articles de presse. Il n'a pas d'héroïne principale et commence avec l'avocat Jonathan Harker qui effectue une visite d'affaires au château d'un nobleman transylvanien, le comte Dracula. Harker s'évade du château après avoir découvert que Dracula est un vampire, et le comte se rend en Angleterre et pille la ville côtière de Whitby. Un petit groupe, dirigé par Abraham Van Helsing, enquête, chasse et tue Dracula.

An alternative is the following query:

SELECT TranslationFR->>"$.text" AS "TranslationFR" FROM (SELECT sys.ML_GENERATE(@finalPrompt, @inferenceSetup) AS TranslationFR ) AS dt \G

*************************** 1. row ***************************
TranslationFR:  Réponse : Le roman gothique horreur de Bram Stoker, intitulé « Dracula », a été publié le 26 mai 1897. C'est un roman épistolaire dont la narration se fait par les lettres, les entrées dans le journal et les articles de presse. Il n'a pas d'héroïne principale et commence avec l'avocat Jonathan Harker qui effectue une visite d'affaires au château d'un nobleman transylvanien, le comte Dracula. Harker s'évade du château après avoir découvert que Dracula est un vampire, et le comte se rend en Angleterre et pille la ville côtière de Whitby. Un petit groupe, dirigé par Abraham Van Helsing, enquête, chasse et tue Dracula.

Although the translation isn’t perfect, French speakers should grasp the gist, even if it reads like a particularly polite Advance-fee scam. 😀

Let’s see how to improve that!

Areas for improvement

You now have all the essential components to implement real-time translation within your applications.

Although the generated output may not always align with expectations, you can refine results by adjusting the prompt or modifying inference parameters.

Inference parameters for text generation include:

  • task
  • model_id
  • temperature
  • max_tokens
  • top_k
  • top_p
  • repeat_penalty
  • stop_sequences
  • token_likelihoods
  • echo
  • logprobs
  • stream
  • context

While prompt engineering is a critical skill, it’s beyond the scope of this article. I strongly recommend dedicating time to mastering this discipline for optimal results.

Let’s try to do some fine tuning.

The text for translation is the same:

-- Text for Translation
SET @ENtext = "Dracula is a gothic horror novel by Bram Stoker, published on 26 May 1897. An epistolary novel, the narrative is related through letters, diary entries, and newspaper articles. It has no single protagonist and opens with solicitor Jonathan Harker taking a business trip to stay at the castle of a Transylvanian nobleman, Count Dracula. Harker escapes the castle after discovering that Dracula is a vampire, and the Count moves to England and plagues the seaside town of Whitby. A small group, led by Abraham Van Helsing, investigate, hunt and kill Dracula.";

The enhanced prompt contains more information:
You are an expert translator from English to French. Your task is to translate the original text from English to French. Ensure the French translation is accurate, natural-sounding, and grammatically correct. Preserve the original text’s meaning, style, and context. Only generate the French translation of the original text. \n – Original Text: “, @ENtext, ” \n – French Translation:

-- Enhanced prompt
SET @finalPrompt = CONCAT("You are an expert translator from English to French. Your task is to translate the original text from English to French. Ensure the French translation is accurate, natural-sounding, and grammatically correct. Preserve the original text's meaning, style, and context. Only generate the French translation of the original text. \n  - Original Text: ", @ENtext, " \n  - French Translation: "); 

I modified the inference parameters – temperature, repeat_penalty, top_k and top_p:

-- Inference setting
SET @inferenceSetup  = '{"task": "generation", "temperature": 0.9, "repeat_penalty": 1, "top_k": 0, "top_p": 0.2, "model_id": "mistral-7b-instruct-v1"}';

And finally, generate the translation:

-- Translation
SELECT TranslationFR->>"$.text" AS "TranslationFR" FROM (SELECT sys.ML_GENERATE(@finalPrompt, @inferenceSetup) AS TranslationFR ) AS dt \G

*************************** 1. row ***************************
TranslationFR:  Dracula est un roman gothique d'horreur de Bram Stoker, publié le 26 mai 1897. C'est un roman épistolaire, la narration se fait par des lettres, des entrées de journal et des articles de journaux. Il n'a pas de héros principaux et commence par le soliciteur Jonathan Harker qui se rend à l'abbaye du comte Dracula, un nobleman de Transylvanie. Harker s'évade de l'abbaye après avoir découvert que Dracula est un vampire, et le comte s'installe en Angleterre et pille la ville côtière de Whitby. Un petit groupe, dirigé par Abraham Van Helsing, enquête, chasse et tue Dracula.

The results show an improvement. Further refinements can be achieved by optimizing inference parameters and prompt engineering.

Peroration

This article explored the use of HeatWave GenAI for text translation. By integrating LLMs into the database, users can efficiently translate text. While this article provided a basic overview, further experimentation with prompts and parameters can enhance results.

By leveraging the power of in-database AI, organizations can unlock new opportunities for global communication and collaboration.

Stay tuned for more insights!

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

1

HeatWave GenAI: Your AI-Powered Content Creation Partner

August 7, 2024
Tags:

Generative artificial intelligence (GenAI) is reshaping the content creation landscape. By training on vast datasets, these “intelligent” systems can produce new, human-quality content across a multitude of domains.

Oracle’s HeatWave GenAI (starting with version 9.0.1) is at the forefront of this revolution, offering an integrated platform that combines in-database large language models (LLMs), vector stores, and scale-out vector processing to streamline content generation.

This article explores how HeatWave GenAI is empowering businesses to produce high-quality content rapidly and effectively, making it an indispensable tool for industries demanding speed, accuracy, and security.

HeatWave GenAI brings LLMs directly into your database, enabling powerful AI capabilities and natural language processing.

The key benefits of such architecture are:

  • Effortless LLM Integration: Simple SQL queries unlock AI power.
  • Seamless Database Integration: No complex external connections.
  • Simplified Architecture: Easy management and scalability.
  • Scalable Performance: Handles large datasets and high traffic.
  • Always Up-to-Date Data: In-database LLMs ensure data freshness.

Generate text-based content using HeatWave GenAI is a very simple 4 steps process:

  1. Load the LLM in HeatWave memory
  2. Define your prompt in natural language
  3. Inference setting
  4. Generate text-based content using HeatWave GenAI

Load the LLM in HeatWave memory

Use the ML_MODEL_LOAD stored procedure: CALL sys.ML_MODEL_LOAD(‘<LLM>’, NULL);
The model needs to be loaded only once before generating text. It remains accessible within HeatWave as long as the service is running. This means you don’t have to reload the model for each text generation.

At the time of writing (August 2024) the available models are Mistral 7B (mistral-7b-instruct-v1) and Llama 2 (llama2-7b-v1).

-- Load Mistral 7B model
mysql>
CALL sys.ML_MODEL_LOAD('mistral-7b-instruct-v1', NULL);

Define your prompt in natural language

Like with you prefered programming language, it is more convenient to use a session variable: SET @userPrompt=”My prompt”;

mysql>
SET @userPrompt="Explain what is Generative Artificial intelligence in one sentence";

or whatever your needs:

SET @userPrompt="How to connect to MySQL using Python 3?";
SET @userPrompt="Write a short email to schedule a meeting.";
...

As you likely know, the quality of the prompt significantly impacts the generated text.

Inference setting

Here, you select the desired task, choose the appropriate model, and fine-tune parameters to influence the output.

mysql> 
-- Inference setting
SET @inferenceSetup  = '{"task": "generation", "model_id": "mistral-7b-instruct-v1"}';

For simplicity, this example uses default parameters. A more complex example is provided below.

Generate text-based content using HeatWave GenAI

Last step is to pass the user prompt to the LLM using the ML_GENERATE function. The second parameter of this function is a JSON object that allows you to specify the task, the LLM, and some tuning options like the temperature.

mysql>
SELECT sys.ML_GENERATE(@userPrompt, @inferenceSetup)\G

The output in a JSON that is not really nice to read.
But SQL is very powerful and you can easily improve the output using JSON_TABLE function.

mysql>
SELECT GenContent AS GenContent FROM ( SELECT sys.ML_GENERATE(@userPrompt, @inferenceSetup) AS textGen ) AS dt INNER JOIN JSON_TABLE ( dt.textGen, "$" COLUMNS( GenContent text PATH "$.text") ) AS jt \G

You can influence LLM outputs by adjusting parameters like temperature, number of tokens, stop sequences, top_k, top_p, etc…

Text generation options include:

  • task
  • model_id
  • temperature
  • max_tokens
  • top_k
  • top_p
  • repeat_penalty
  • stop_sequences
  • token_likelihoods
  • echo
  • logprobs
  • stream
  • context


Below is a quick example:

mysql>
-- Define your prompt in natural language
SET @userPrompt="Explain what is Generative Artificial intelligence in one sentence";


-- Inference setting
SET @inferenceSetup  = '{"task": "generation", "temperature": 3, "repeat_penalty": 1, "top_k": 1, "max_tokens": 500, "model_id": "mistral-7b-instruct-v1"}';


-- Generate text-based content
SELECT GenContent AS GenContent FROM ( SELECT sys.ML_GENERATE(@userPrompt, @inferenceSetup) AS textGen ) AS dt INNER JOIN JSON_TABLE ( dt.textGen, "$" COLUMNS( GenContent text PATH "$.text") ) AS jt \G
*************************** 1. row ***************************
GenContent:  Generative Artificial Intelligence (AI) refers to a subset of AI that involves the creation of new, original content, such as images, music, or text, through the use of algorithms and machine learning models that can learn from and generate data.

And that’s all!
As you can see, it’s very simple and easy with HeatWave GenAI to create some exciting GenAI based applications like:

Enhancing Customer Experience

  • Intelligent Chatbots: Provide real-time, personalized customer support, answering queries, and resolving issues efficiently.
  • Sentiment Analysis: Understand customer feedback, identify trends, and tailor responses accordingly.

Boosting Content Creation

  • Content Generation: Create engaging blog articles, product descriptions, and social media posts at scale.
  • Personalized Recommendations: Offer tailored product suggestions based on customer preferences and behavior.
  • Social Media Management: Generate engaging content, schedule posts, and monitor performance.

Driving Business Insights

  • Text Classification: Categorize vast amounts of text data for efficient analysis and decision-making.
  • Market Research: Analyze customer sentiment, identify trends, and gain competitive insights.

Generative AI is a game-changer, offering endless possibilities to improve efficiency, enhance customer experiences, and drive business growth.

HeatWave GenAI: Your AI-Powered Content Creation Partner demo

Moving forward

While the examples provided offer a glimpse of generative AI’s potential, it’s important to note that significant improvements can be achieved through advanced prompt engineering techniques. Exploring these techniques in depth is beyond the scope of this article.

In upcoming articles, we’ll delve deeper into HeatWave GenAI’s capabilities, showcasing its application in text translation and Retrieval Augmented Generation (RAG).

Stay tuned for more insights!

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

1

Get Your HeatWave MySQL Data On-Premises with Replication

July 16, 2024
Tags:

MySQL Replication exist for centuries… OK, I’m exaggerating a little bit 🙂 but this feature is as much appreciated as it is useful, because is helping DBAs to easily build scalable and redundant architectures:

MySQL 3.23.15 and up features support for one-way, asynchronous replication, in which one
server acts as the master, while one or more other servers act as slaves

MySQL 3.23/4.0/4.1 Reference Manual

At least from MySQL 3.23… I told you it’s been there for a long time: MySQL first Public Releases

But what does replication do?
Quoting the documentation again (the most recent this time)

Replication enables data from one MySQL database server (the source) to be copied to one or more MySQL database servers (the replicas).
Replication is asynchronous by default; replicas do not need to be connected permanently to receive updates from a source.

MySQL 8.4 Reference Manual

In this article we will see how to replicate data from HeatWave MySQL to a on-premise standalone MySQL Instance.

Get Your HeatWave MySQL Data On-Premises with Replication
  • Source is HeatWave MySQL 8.4.1
    • IP: 137.235.181.51
    • Port: 3306
  • Replica is MySQL 8.4.1
    • IP: 88.65.229.255
    • Port: 3306
  • Replication user
    • ‘rplUser’@’88.65.229.%’

We will use 3 main components:

  • HeatWave MySQL as a source
  • The asynchronous replication capability of MySQL
  • A MySQL instance as a read replica

HeatWave MySQL

HeatWave is a fully-managed database service, powered by the integrated in-memory query accelerator. It is the only cloud-native database service that combines transactions, analytics, machine learning and generative AI services into HeatWave, delivering real-time, secure analytics without the complexity, latency, and cost of ETL duplication. It also includes HeatWave Lakehouse which allows users to query data stored in object storage in a variety of file formats.
HeatWave is developed, managed, and supported by the MySQL team in Oracle.

HeatWave MySQL accelerate MySQL query performance by orders of magnitude and get real-time analytics on your transactional data. HeatWave MySQL is built on MySQL Enterprise Edition.

The HeatWave MySQL endpoints are not directly accessible from the internet (i.e. private IP). That said, there are different possibilities to connect such as  a compute instance, VPN connection, Bastion session, or network load balancer.
In this article I’ll use the latter, which is present on a public subnet, enables you to connect to HeatWave MySQL over the internet.
However please note that it is not recommended, to make a database accessible over the internet, because it is a security risk.
You must restrict the authorized public IP addresses to a single IP address or a small range of IP addresses, and use in-transit encryption.
It is recommended to use a VPN connection.
You are warned!

I assuming your HeatWave MySQL instance is already created. If not, you can use the Console or the command-line interface to create an instance.
Using the console is pretty straightforward, you can find the details in the OCI documentation.

I also assuming that you have already setup your Network Load Balancer. If not I recommend you this great tutorial from my colleague Scott : Connecting to a MySQL HeatWave Database Instance Using an OCI Network Load Balancer.

MySQL Replication setup

Since MySQL replication is a familiar concept, widely covered over the years, let’s get right to the practical details that will help you set up your replication efficiently.
You’ll find all the juicy details in the MySQL 8.4 Reference Manual and some extra here and here.

Because the data stream will be over the Internet, we must, for obvious security reason, encrypted the connection.
More information on setting up Replication to Use Encrypted Connections.

Source configuration

On the source, create the replication user then grant that user the REPLICATION SLAVE privilege.
If you want to ensure that the source only accepts replicas that connect using encrypted connections (and btw you really want that), use the REQUIRE SSL option

-- On the Source
-- Create the replication user
CREATE USER 'rplUser'@'88.65.229.%' IDENTIFIED BY 'Q{P6@EH$L!YFje^9' REQUIRE SSL;
GRANT REPLICATION SLAVE ON *.* TO 'rplUser'@'88.65.229.%';

If the HeatWave MySQL instance (the source) contains existing data it is necessary to copy this data to the replica. There are different ways to dump the data from the source database, the recommended way in that context is to use MySQL Shell dump and load utilities.

You will most likely need dumpInstance and the relevant options are dependent of your context, the most important is usually compatibility.
MySQL Shell allows you to store your backup into an object store bucket and load from there, this is very convenient. For this article, the size of my dataset allows me to store it locally, and the only relevant option is dryRun.
More details in the documentation: Instance Dump Utility, Schema Dump Utility, and Table Dump Utility.

$ mysqlsh --js admin@137.235.181.51 

JS> util.dumpInstance("/backup/20240714", { dryRun: true })
dryRun enabled, no locks will be acquired and no files will be created.
...

If the result is satisfactory, then disable dry run:

JS> util.dumpInstance("/backup/20240714")
...

For your record, below an example of a dump that is stored in an object Storage bucket named MyBck_07 on Oracle Cloud Infrastructure:

util.dumpInstance("20240714", {osBucketName: "MyBck_07", osNamespace: "abcdefghijkl", ocimds: true })

Restore the dump on replica

The dump must be restored on the replica:

-- On the Replica
\sql SET GLOBAL  local_infile=1;

util.loadDump("/backup/20240714", {updateGtidSet:"replace", skipBinlog:true})

\sql SET GLOBAL  local_infile=0;

All the option details are available in the documentation. Please note that you can test you command with the dryRun option.

Also for your record, if you want to restore a dump for a bucket, your command will look like:

util.loadDump("20240714", {osBucketName: "MyBck_07", osNamespace: "abcdefghijkl"})

Setting the Source Configuration on the Replica

To set up the replica to communicate with the source for replication, configure the replica with the necessary connection information:

-- On the Replica
CHANGE REPLICATION SOURCE TO SOURCE_HOST = '137.235.181.51', SOURCE_AUTO_POSITION = 1, SOURCE_SSL=1;

START REPLICA USER = 'rplUser' PASSWORD = 'Q{P6@EH$L!YFje^9';

For a full list of options, see CHANGE REPLICATION SOURCE TO options.

Administration

On the replica, you can manage your replication as you normally do:

-- Checking Replication Status
SQL > SHOW REPLICA STATUS\G
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 137.235.181.51
                  Source_User: rplUser
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: binary-log.000088
          Read_Source_Log_Pos: 378
               Relay_Log_File: daz-Latitude-7400-relay-bin.000004
                Relay_Log_Pos: 557
        Relay_Source_Log_File: binary-log.000088
           Replica_IO_Running: Yes
          Replica_SQL_Running: Yes
...
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
...

Other useful commands on the replica, using performance_schema are:

SQL > SELECT * FROM performance_schema.replication_applier_status\G
*************************** 1. row ***************************
              CHANNEL_NAME: 
             SERVICE_STATE: ON
           REMAINING_DELAY: NULL
COUNT_TRANSACTIONS_RETRIES: 0
SQL > SELECT * FROM performance_schema.replication_connection_configuration\G
*************************** 1. row ***************************
                   CHANNEL_NAME: 
                           HOST: 137.235.181.51
                           PORT: 3306
                           USER: rplUser
              NETWORK_INTERFACE: 
                  AUTO_POSITION: 1
                    SSL_ALLOWED: YES
...
SQL > SELECT * FROM performance_schema.replication_connection_status\G
*************************** 1. row ***************************
               CHANNEL_NAME: 
                 GROUP_NAME: 
                SOURCE_UUID: 22f79ae9-991d-4b3c-82b9-b81bb2366635
                  THREAD_ID: 73
              SERVICE_STATE: ON
  COUNT_RECEIVED_HEARTBEATS: 17
   LAST_HEARTBEAT_TIMESTAMP: 2024-06-26 10:42:57.615232
   RECEIVED_TRANSACTION_SET: 22f79ae9-991d-4b3c-82b9-b81bb2366635:56
          LAST_ERROR_NUMBER: 0
         LAST_ERROR_MESSAGE: 
       LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00.000000
    LAST_QUEUED_TRANSACTION: 22f79ae9-991d-4b3c-82b9-b81bb2366635:56
...

On the source, you can check the status of connected replicas:

-- Check the status of connected replicas
SQL > SHOW PROCESSLIST \G
*************************** 6. row ***************************
     Id: 547
   User: rplUser
   Host: nlb-mhs-paris-dba.sub03221692542.vcnparisdba.oraclevcn.com:1234
     db: NULL
Command: Binlog Dump GTID
   Time: 428
  State: Source has sent all binlog to replica; waiting for more updates
   Info: NULL

Basic information about the replicas that were started with the --report-host option and are connected to the source:

SQL > SHOW REPLICAS \G
*************************** 1. row ***************************
   Server_Id: 1
        Host: Replica-8865229255
        Port: 3306
   Source_Id: 3640816068
Replica_UUID: 46c6de0c-2ff7-4b14-a80a-5dffea08864a

Other useful commands on the source, using performance_schema are:

SQL > SELECT 
   SERVICE_STATE, 
   HOST, 
   USER, 
   PORT
FROM performance_schema.replication_connection_configuration 
   INNER JOIN performance_schema.replication_applier_status 
      USING (CHANNEL_NAME) \G
*************************** 1. row ***************************
SERVICE_STATE: ON
         HOST: 137.235.181.51
         USER: rplUser
         PORT: 3306
SELECT
    concat(conn_status.channel_name, ' (', worker_id,')') AS channel,
    conn_status.service_state AS io_state,
    applier_status.service_state AS sql_state,
    format_pico_time(if(GTID_SUBTRACT(LAST_QUEUED_TRANSACTION, LAST_APPLIED_TRANSACTION) = "","0",
    abs(time_to_sec(if(time_to_sec(APPLYING_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP)=0,0,
    timediff(APPLYING_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP,now()))))) * 1000000000000) latency,
    format_pico_time((LAST_QUEUED_TRANSACTION_START_QUEUE_TIMESTAMP -
    LAST_QUEUED_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP) * 100000000000) transport_time,
    format_pico_time((LAST_QUEUED_TRANSACTION_END_QUEUE_TIMESTAMP -
    LAST_QUEUED_TRANSACTION_START_QUEUE_TIMESTAMP) * 1000000000000) time_to_relay_log,
    format_pico_time((LAST_APPLIED_TRANSACTION_END_APPLY_TIMESTAMP -
    LAST_APPLIED_TRANSACTION_START_APPLY_TIMESTAMP) * 1000000000000) apply_time,
    conn_status.LAST_QUEUED_TRANSACTION AS last_queued_transaction,
    applier_status.LAST_APPLIED_TRANSACTION AS last_applied_transaction
FROM
    performance_schema.replication_connection_status AS conn_status JOIN performance_schema.replication_applier_status_by_worker AS applier_status 
        ON applier_status.channel_name = conn_status.channel_name
\G
*************************** 1. row ***************************
                 channel:  (1)
                io_state: ON
               sql_state: ON
                 latency:   0 ps
          transport_time:   0 ps
       time_to_relay_log:   0 ps
              apply_time:   0 ps
 last_queued_transaction: 
last_applied_transaction: 
...

Peroration

This article guides you through setting up replication between a HeatWave MySQL instance (source) and an on-premise standalone MySQL instance (replica).
It highlights key steps like creating a replication user, securing the connection, and using MySQL Shell utilities for data transfer.
While a Network Load Balancer can be used for the connection, a VPN is recommended for better security.

Resources

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

Comments Off on Get Your HeatWave MySQL Data On-Premises with Replication

MySQL HeatWave Report – December 2022 update

January 16, 2023
MySQL HeatWave

The following is just a summary of new features in MySQL HeatWave.

Highlight

Slides

MySQL HeatWave Report – December 2022 update by Olivier DASINI

Download this presentation and others on my Speaker Deck account.

Video

MySQL HeatWave Report – December 2022 update by Olivier DASINI

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

0

Iris Data Set with MySQL HeatWave Machine Learning & Zeppelin

July 18, 2022
Tags: , ,

MySQL – The world’s most popular open source database – is also probably the most popular database in the Cloud.
MySQL HeatWave database service is THE MySQL PaaS, a fully managed cloud service in Oracle Cloud Infrastructure (OCI).

The only MySQL service 100% developed, managed and supported by the MySQL Team.

In short:

MySQL HeatWave is a massively parallel, high performance, in-memory query accelerator that accelerates MySQL performance by orders of magnitude for analytics workloads, mixed workloads, and Machine Learning.

MySQL HeatWave Architecture

For your analytics needs you can explore and visualize your data using a data visualization tool like Tableau, Qlik, Oracle Analytics Cloud, Apache Superset (see: Explore & visualize your MySQL HeatWave data with Superset)…

For your Machine Learning needs you can use a notebook like Jupyter, Apache Zeppelin (see: Interactively explore & visualize your MySQL HeatWave data with Apache Zeppelin),…

In this article I will show you how easy it is to use the Machine Learning capabilities of MySQL HeatWave. We will use the famous Iris dataset and we’re going to build, train, deploy and explain a machine learning model within MySQL HeatWave using Apache Zeppelin.

Prerequisites

To be able to reproduce this tutorial, you must have a MySQL HeatWave instance running (details here and also RTFM).
The data used in this article are available on my GitHub account here.

You need a MySQL client to load the data into MySQL HeatWave. I’m using MySQL Shell.
Furthermore if you want to use Zeppelin (although optional because we’re going to run mainly sql queries) , it must be properly setup.
Please read: Interactively explore & visualize your MySQL HeatWave data with Apache Zeppelin
You can also download the Zeppelin notebook here.

Finally, you might need to install some packages.
ex: (pip install pandas numpy unlzw3 sklearn seaborn matplotlib jupyter grpcio protobuf)

MySQL HeatWave ML

MySQL HeatWave ML makes it easy to use machine learning, whether you are a novice user or an experienced ML practitioner

You provide the data, and HeatWave ML analyzes the characteristics of the data and creates an optimized machine learning model that you can use to generate predictions and explanations. 

HeatWave ML supports supervised machine learning. That is, it creates a machine learning model by analyzing a labeled dataset to learn patterns that enable it to predict labels based on the features of the dataset. HeatWave ML supports both classification and regression models. 
More information here.

In-database Machine Learning with MySQL HeatWave
In-database Machine Learning with MySQL HeatWave

All HeatWave ML operations are initiated by running CALL or SELECT statements, which can be easily integrated into your applications.

HeatWave ML routines include:

Load the data

In a real life scenario, your data are already inside your MySQL instance. Although you might need to prepare them upfront, because the relevant data set must be inside a single table.

I’m using MySQL Shell (installed on the server that host Zeppelin) to load the data into the iris_ML schema. The dump file – MySQL_HeatWave-iris_ML.sql – was previously uploaded to the server into /home/ubuntu.

mysqlsh --version

mysqlsh myUser:MyP4s$W0rd@10.0.1.2 --sql -f /home/ubuntu/MySQL_HeatWave-iris_ML.sql

The dump contains 4 tables:

  • iris : the reference table ie source of truth, production data
  • iris_test : contains the test dataset
  • iris_train : contains the training dataset
  • iris_validate : contains the validation dataset

Check HeatWave ML status

HeatWave ML is enable by default as soon as your MySQL HeatWave Cluster is active.

The rapid_ml_status variable provides the status of HeatWave ML:

SHOW GLOBAL STATUS LIKE 'rapid_ml_status';

HeatWave ML is up and running \o/

Data exploration

The first step of any project based on data is to look at the data.
Summarize and visualize the data will help you to have a better understanding of the project.

It may surprise you but SQL provides some commands and functions for data exploration, although much less extensive than R or Python 🙂 :

-- Tables structure 
DESCRIBE iris_ML.iris;  -- Reference table (production table)
DESCRIBE iris_ML.iris_train; -- Table that contains the training dataset
DESCRIBE iris_ML.iris_test; -- Test dataset table
DESCRIBE iris_ML.iris_validate; -- Validation dataset table
-- Number of rows
SELECT count(*) FROM iris_ML.iris;
SELECT count(*) FROM iris_ML.iris_train;
SELECT count(*) FROM iris_ML.iris_test;
SELECT count(*) FROM iris_ML.iris_validate;

iris simulate the live production table. It contains the original data (150 rows) and actually it will not be used (as is) by HeatWave ML.
To keep things simple as possible in this article, iris as the same structure than iris_train. But in a real life scenario it will most likely not the case.
This production table may have additional columns like for example: a primary key, timestamp, etc… any useful business related information that are not relevant for building our model.

iris_train contains the training dataset a subset of iris table (120 rows), used to train the machine learning model.
This table is typically a subset of the reference table(s).

iris_test contains the test dataset a subset of iris table (30 rows), different than iris_train. Its structure is almost similar to iris_train but without the target column (class) .

iris_validate contains the validation dataset (30 rows). Same data than iris_test but same structure than iris_train in other words this table has the target column (class).

General requirements for HeatWave ML here.

-- Data sample
SELECT * FROM iris_ML.iris LIMIT 10;


-- Class distribution
SELECT class, count(*) FROM iris_ML.iris GROUP BY class;


-- Summary Statistics
SELECT MIN(sepal_length), MAX(sepal_length), ROUND(AVG(sepal_length), 2), ROUND(STD(sepal_length), 2) FROM iris_ML.iris;
SELECT MIN(sepal_width), MAX(sepal_width), ROUND(AVG(sepal_width), 2), ROUND(STD(sepal_width), 2) FROM iris_ML.iris;
SELECT MIN(petal_length), MAX(petal_length), ROUND(AVG(petal_length), 2), ROUND(STD(petal_length), 2) FROM iris_ML.iris;
SELECT MIN(petal_width), MAX(petal_width), ROUND(AVG(petal_width), 2), ROUND(STD(petal_width), 2) FROM iris_ML.iris;

Class distribution is well balanced:

  • Iris-virginica: 50 rows
  • Iris-setosa: 50 rows
  • Iris-versicolor: 50 rows

Data visualization

Visualize your data is probably the more convenient way to explore and understand them.

Below a little Python script to generate some graphs.

Beforehand, I had to edit the Zeppelin python interpreter – zeppelin.python – and replace python by python3:

Update zeppelin.python:  replace python by python3
zeppelin.python: python3:

The interpreter is: %python.ipython
The script is going to connect to MySQL HeatWave, thus you must update the database information (host, database, user, password)

%python.ipython

## Edit python interpreter on Zeppelin - zeppelin.python: python3  instead of python
## Update host, database, user and password (from you're MySQL HeatWave)



import mysql.connector as connection
import pandas as pd
import seaborn as sns
sns.set_palette('husl')
%matplotlib inline
import matplotlib.pyplot as plt


try:
    mydb = connection.connect(host="10.0.1.2", database='iris_ML', user="<user>", passwd="<password>", port=3306)
    query = "SELECT * FROM iris;"
    data = pd.read_sql(query,mydb)
    mydb.close() #close the connection
except Exception as e:
    my_conn.close()
    print(str(e))

data.head()

data.info()

data.describe()

data['class'].value_counts()

g = sns.pairplot(data, hue='class', markers='*')

plt.show()

HeatWave Machine Learning (ML) inclut tout ce dont les utilisateurs ont besoin pour créer, former, déployer et expliquer des modèles d’apprentissage automatique dans MySQL HeatWave, sans coût supplémentaire.

Dans ce webinaire vous apprendrez

The information above come from the Iris table (reference table). It could interesting to compare these data with the training dataset (iris_train) and the test dataset in order to evaluate the quality of these samples.

Replace query = “SELECT * FROM iris;” by query = “SELECT * FROM iris_train;” and then query = “SELECT * FROM iris_test;”

Training a Model

Now we have a better understanding of the data, let’s moving forward and train the model. We’re having a classification problem.

The ML_TRAIN routine, when run on a training dataset, produces a trained machine learning (ML) model.

# Train the model using ML_TRAIN

CALL sys.ML_TRAIN('iris_ML.iris_train', 'class', JSON_OBJECT('task', 'classification'), @iris_model);

You can show the current model, selecting the session variable @iris_model:

SELECT @iris_model;

The lifetime duration of a session variable is… the session lifetime duration. So when the session is closed the session variable content is lost.

ML_TRAIN stores the machine learning model – @iris_model – in the MODEL_CATALOG table:

# Model information

SELECT model_id, model_handle, model_owner, target_column_name, train_table_name, model_type, task, model_object_size  FROM ML_SCHEMA_admin.MODEL_CATALOG;

You can take the last created model using the following query:

SELECT model_handle FROM ML_SCHEMA_admin.MODEL_CATALOG ORDER BY model_id DESC LIMIT 1 INTO @iris_model;

When the model is created (using ML_TRAIN) you should load it into HeatWave ML (ML_MODEL_LOAD):

CALL sys.ML_MODEL_LOAD(@iris_model, NULL);

Row Prediction

HeatWave ML allows you to make prediction for individual rows or the entire table.

Row(s) predictions are generated by running ML_PREDICT_ROW.
Data are specified in JSON format.

# Predict 1 Row

SET @row_input = JSON_OBJECT( 
   "sepal_length", 7.3, 
   "sepal_width", 2.9, 
   "petal_length", 6.3, 
   "petal_width", 1.8
);  


SELECT sys.ML_PREDICT_ROW(@row_input, @iris_model);

Row Explanation

Being able to understand and explain a prediction is important in order to trust your model and to be able to explain the results. It might also be required by your local regulation.

ML_EXPLAIN_ROW generates explanations for one or more rows of data. Explanations help you understand which features have the most influence on a prediction.
Feature importance is presented as a value ranging from -1 to 1.

  • A positive value indicates that a feature contributed toward the prediction.
  • A negative value indicates that the feature contributed toward a different prediction
# Prediction explanation for 1 Row

SET @row_input = JSON_OBJECT( 
   "sepal_length", 7.3, 
   "sepal_width", 2.9, 
   "petal_length", 6.3, 
   "petal_width", 1.8
);    


SELECT sys.ML_EXPLAIN_ROW(@row_input, @iris_model);

Table Prediction

ML_PREDICT_TABLE generates predictions for an entire table and saves the results to an output table.

# Generate predictions for a table 

-- CALL sys.ML_MODEL_LOAD(@iris_model, NULL); -- Uncomment if the model is not yet loaded

DROP TABLE IF EXISTS iris_ML.iris_predictions; -- Useful if a table prediction was already generated

CALL sys.ML_PREDICT_TABLE('iris_ML.iris_test', @iris_model, 'iris_ML.iris_predictions');

A new table called iris_predictions is created. You can display Its first 5 rows with:

SELECT * FROM iris_ML.iris_predictions LIMIT 5;

Table Explanation

ML_EXPLAIN_TABLE explains predictions for an entire table and saves results to an output table.

# Prediction explanation for a table 

-- CALL sys.ML_MODEL_LOAD(@iris_model, NULL); -- Uncomment if the model is not yet loaded

DROP TABLE IF EXISTS iris_ML.iris_explanations;  -- Usueful if a table explanation was already done

CALL sys.ML_EXPLAIN_TABLE('iris_ML.iris_test', @iris_model, 'iris_ML.iris_explanations');

A new table called iris_explanations is created. You can display Its first 5 rows with:

SELECT * FROM iris_ML.iris_explanations LIMIT 5;

Scores

Scoring the model allows to assess the model’s reliability.

Models with a low score can be expected to perform poorly, producing predictions and explanations that cannot be relied upon. A low score typically indicates that the provided feature columns are not a good predictor of the target values.

HeatWave ML supports a variety of scoring metrics to help you understand how your model performs across a series of benchmarks. 
Details here.

ML_SCORE returns a computed metric indicating the quality of the model.

-- This example uses the accuracy: Computes the fraction of labels a model predicts correctly

CALL sys.ML_SCORE('iris_ML.iris_validate', 'class', @iris_model, 'accuracy', @accuracy_score);

SELECT @accuracy_score;

Other metrics are also available. See here.

Here we go!
We’ve seen the machine learning life cycle.

Machine Learning Life Cycle

I told you that Machine Learning with MySQL HeatWave was easy 🙂

You only need to use a limited set of SQL routines:

During the last 20 years MySQL has democratized the usage of transactional databases.
Now with MySQL HeatWave we are in the process of democratizing Analytics and Machine Learning.

With MySQL HeatWave,
valorizing your data has never been so easy!

Resources

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

Iris Versicolor
Iris Versicolor
1

Interactively explore & visualize your MySQL HeatWave data with Apache Zeppelin

July 5, 2022
Tags: ,

MySQLThe world’s most popular open source database – is a very good and safe choice as a transactional database, on-premise and in the cloud.

In the article – Explore & visualize your MySQL HeatWave data with Superset – I introduced MySQL HeatWave – a fully managed database service, that combines transactions, analytics, and machine learning services into one MySQL Database, delivering real-time, secure analytics without the complexity, latency, and cost of ETL duplication.
MySQL HeatWave is a native MySQL solution thus current MySQL applications work without changes.

Enabling a HeatWave Cluster also provides access to HeatWave Machine Learning (ML), which is a fully managed, highly scalable, cost-efficient, machine learning solution for data stored in MySQL.
HeatWave ML provides a simple SQL interface for training and using predictive machine learning models, which can be used by novice and experienced ML practitioners alike.
Machine learning expertise, specialized tools, and algorithms are not required.
With HeatWave ML, you can train a model with a single call to an SQL routine. 

Below some numbers comparing MySQL HeatWave ML vs AWS Redshift ML:

MySQL HeatWave  ML is 25x faster than Redshift ML
25x faster on average without compromising accuracy
MySQL HeatWave ML is 25x faster than Redshift ML
MySQL HeatWave  ML is 1% of cost of Redshift ML
1% of cost and no additional cost for MySQL HeatWave customers
MySQL HeatWave ML is 1% of cost of Redshift ML
Better performance with HeatWave ML with larger cluster size
Better performance with HeatWave ML with larger cluster size

For more details & raw numbers read: Performance comparison of HeatWave ML with Redshift ML.

MySQL HeatWave is currently available on Oracle Cloud Infrastructure (OCI). Availability on Amazon Web Services (AWS) and Microsoft Azure is coming (Oracle’s “HeatWave” is going multicloud: How hot will things get for MySQL?).

MySQL HeatWave ML can also takes models tuned in MySQL and use them to answer queries from popular machine learning notebook services for code and data, such as Jupyter and Apache Zeppelin.

Apache Zeppelin is an open source web-based notebook that allows data ingestion, data discovery, data analytics, data visualization and data collaboration.
Apache Zeppelin supports many interpreters such as MySQL, Python, JDBC, Shell, Spark, …
More: https://zeppelin.apache.org/

In this article I will show you how to properly configure Apache Zeppelin in order to take advantage of the Analytics and Machine Learning capabilities of MySQL HeatWave the MySQL in-memory query accelerator.

MySQL HeatWave

I’m using OCI and I will not go into all the installation details (RTFM). Below the main steps.

Using your favorite Internet browser, open your OCI console, select a compartment and go to Databases / MySQL / DB Systems.

Create a DB Systems and select HeatWave.

The rest is pretty straightforward!

Please note the Show Advanced Options link at the bottom of the page.

It’s Data Import tab is useful if you want to create an instance with data.

More information in this article: Discovering MySQL Database Service – Episode 5 – Create a MySQL DB system from a MySQL Shell dump

Now we must add the HeatWave cluster.

On the left, under Resources click HeatWave.

Add the HeatWave Cluster…

Set the Node Count

I would recommend to estimate the node count, click Estimate Node Count then Generate Estimate:

Copy the Load Command.
It will be used to load the chosen data into the HeatWave cluster.

Apply the node count estimate:

Finally create the cluster by pushing the button: Add HeatWave Cluster

When the cluster is running, the very last step is to offload the data to the cluster by running the stored procedure (i.e. the “Load Command”) copied previously using your favorite MySQL client.

e.g. Offload to the HeatWave cluster all the tables inside the airportdb schema:
CALL sys.heatwave_load(JSON_ARRAY(‘airportdb’), NULL);

Now you have a MySQL HeatWave cluster up and running with some data loaded into the cluster;
Cheers!

Apache Zeppelin

I assume that you already know what is Apache Zeppelin. If not RTFM.

Please note this is a test installation, probably not suitable for a production environment.

Requirements

Apache Zeppelin officially supports and is tested, among other, on Ubuntu 20.04, so I’ve installed Zeppelin on a VM Ubuntu 20.04 from Oracle Cloud Infrastructure (OCI).

In addition to the OS, you also need the good version of OpenJDK or Oracle JDK.

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.4 LTS
Release:	20.04
Codename:	focal
$ java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1)
OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1, mixed mode, sharing)

I’m good to go!

Binary

I downloaded the binary package with all interpreters. Available here.

wget https://dlcdn.apache.org/zeppelin/zeppelin-0.10.1/zeppelin-0.10.1-bin-all.tgz

Unpack it in a directory of your choice and you’re ready to go!

tar xvf zeppelin-0.10.1-bin-all.tgz
mv zeppelin-0.10.1-bin-all/ zeppelin
cd zeppelin

Firewalls

Depending on your architecture, you may have to deal with some firewalls.

In OCI I must update my Ingress Rules for the Default Security List (OCI virtual firewall) for the protocol TCP and the port you are going to use, 8080 in this article:

On the Ubuntu side I also had to update IPtable.

Starting Apache Zeppelin

By default Zeppelin is listening at 127.0.0.1:8080, so you can’t access it when it is deployed on another remote machine.
To access a remote Zeppelin, you need to change zeppelin.server.addr to 0.0.0.0 in conf/zeppelin-site.xml.

~/zeppelin$ cp conf/zeppelin-site.xml.template conf/zeppelin-site.xml

~/zeppelin$ vim conf/zeppelin-site.xml
...

When the update is done:

~/zeppelin$ grep -m2  -a4  "<property>" conf/zeppelin-site.xml
-->

<configuration>

<property>
  <name>zeppelin.server.addr</name>
  <value>0.0.0.0</value>
  <description>Server binding address</description>
</property>

<property>
  <name>zeppelin.server.port</name>
  <value>8080</value>
  <description>Server port.</description>
</property>

Address is 0.0.0.0 and Port is 8080.

Now I can start Zeppelin:

~/zeppelin$ bin/zeppelin-daemon.sh start
~/zeppelin$ bin/zeppelin-daemon.sh status
Zeppelin is running                                        [  OK  ]

Yippee!

MySQL Connector J

The Zeppelin SQL interpreter needs the JDBC MySQL Connector, available at https://dev.mysql.com/downloads/connector/j/

Obviously, MySQL customers can use the commercial version available on MOS.

$ wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java_8.0.29-1ubuntu20.04_all.deb

$ sudo dpkg -i mysql-connector-java_8.0.29-1ubuntu20.04_all.deb

The JDBC MySQL Connector is now installed!

Setup the Zeppelin interpreter

Create a new interpreter:

Enter a name for the Interpreter Name box and select jdbc as Interpreter group:

  • Interpreter Name: MySQL HeatWave
  • Interpreter group: jdbc

JDBC interpreter lets you create a JDBC connection to any data sources seamlessly. Inserts, Updates, and Upserts are applied immediately after running each statement.

Properties

  • default.url : jdbc:mysql://<MySQL HeatWave IP Address>:3306/
  • default.user : <MySQL User>
  • default.password: <MySQL Password>
  • default.driver : com.mysql.jdbc.Driver

Dependencies

  • Dependencies artifact : mysql:mysql-connector-java:8.0.29

Don’t forget to save the configuration 🙂

To sum up, the interpreter configuration looks like:

MySQL HeatWave usage

I can now create a notebook – Check MySQL HeatWave (download the notebooks) – in order to check if HeatWave is enable:

The Default Interpreter must be MySQL_HeatWave:

For the check, let’s run these 2 queries:

SHOW VARIABLES LIKE 'rapid_bootstrap';

SHOW STATUS WHERE Variable_name IN ('rapid_cluster_status', 'rapid_ml_status', 'rapid_cluster_ready_number', 'rapid_load_progress');

The HeatWave cluster is up and ML is enable.
All good!

Analytics

Out of curiosity, let’s try some analytic queries.

I already loaded the airportdb sample database (see here) into the MySQL DB system.

Let’s create a new note – Airportdb Analytics Queries (download the notebooks):

airportdb must now be loaded into the HeatWave cluster in order to take advantage of the tremendous performance:

CALL sys.heatwave_load(JSON_ARRAY('airportdb'), NULL) 

And here are the queries:

-- Number of Tickets > $500.00, Grouped By Price
SELECT booking.price, count(*) 
FROM airportdb.booking 
WHERE booking.price > 500 
GROUP BY booking.price 
ORDER BY booking.price 
LIMIT 10;


-- Average Age of Passengers By Country, Per Airline
SELECT airline.airlinename, AVG(datediff(departure,birthdate)/365.25) as avg_age, count(*) as nb_people
FROM airportdb.booking, airportdb.flight, airportdb.airline, airportdb.passengerdetails
WHERE booking.flight_id=flight.flight_id AND
    airline.airline_id=flight.airline_id AND
    booking.passenger_id=passengerdetails.passenger_id AND
    country IN ("SWITZERLAND", "FRANCE", "ITALY")
GROUP BY airline.airlinename
ORDER BY airline.airlinename, avg_age
LIMIT 10;

Great results, respectively less than 0 second and 1 second.

Without HeatWave the query execution times would be respectively 10 and 17 seconds:

Machine Learning

What about the Machine Learning possibilities of MySQL HeatWave ML?

Well this article is already long enough, so it will be the topic of the next article 🙂

Stay tune!

Resources

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

0

Explore & visualize your MySQL HeatWave data with Superset

June 9, 2022

MySQLThe world’s most popular open source database – is probably the best choice for a transactional database especially when considering the costs / performance ratio, and that on-premise or in the cloud.

But what about business intelligence (BI) needs? Analytics workloads?

We’ve got you covered now with the versatile MySQL HeatWave – a fully managed database service, that combines transactions, analytics, and machine learning services into one MySQL Database, delivering real-time, secure analytics without the complexity, latency, and cost of ETL duplication.
MySQL HeatWave is a native MySQL solution thus current MySQL applications work without changes.

MySQL HeatWave is also fast, super fast, easy to use and provides an incredible price/performance ratio…

Details & more numbers: Performance comparison of MySQL HeatWave with Snowflake, Amazon Redshift, Amazon Aurora, and Amazon RDS for MySQL.

MySQL HeatWave is currently available on Oracle Cloud Infrastructure (OCI). Availability on Amazon Web Services (AWS) and Microsoft Azure is coming (Oracle’s “HeatWave” is going multicloud: How hot will things get for MySQL?).

Indeed, having a fast and easy to use database is valuable. But analytics also imply that you need to explore and visualize your data.
There are plenty of tools available on the market, one of the them is Apache Superset, an open source modern data exploration and visualization platform.

In this article I will show you how to properly configure Apache Superset in order to take advantage of a high performance, in-memory query accelerator: MySQL HeatWave.

MySQL HeatWave

I’m on OCI and I will not go into all the installation details (RTFM). Below the main steps.

Using your favorite Internet browser, open your OCI console, select a compartment and go to Databases / MySQL / DB Systems.

Create a DB Systems and select HeatWave.

The rest is pretty straightforward!

Please note the Show Advanced Options link at the bottom of the page.

It’s Data Import tab is useful if you want to create an instance with data.

More information in this article: Discovering MySQL Database Service – Episode 5 – Create a MySQL DB system from a MySQL Shell dump

Now we must add the HeatWave cluster.

On the left, under Resources click HeatWave.

Add the HeatWave Cluster…

Set the Node Count

I would recommend to estimate the node count, click Estimate Node Count then Generate Estimate:

Copy the Load Command.
It will be used to load the chosen data into the HeatWave cluster.

Apply the node count estimate:

Finally create the cluster by pushing the button: Add HeatWave Cluster

When the cluster is running, the very last step is to offload the data to the cluster by running the stored procedure (i.e. the “Load Command”) copied previously using your favorite MySQL client.

e.g. Offload to the HeatWave cluster all the tables inside the airportdb schema:
CALL sys.heatwave_load(JSON_ARRAY(‘airportdb’), NULL);

Apache Superset

I assume that you know what is Apache Superset. If not RTFM.
I also assume that it is already installed. You’ll find the necessary information in their documentation: Installing Superset from Scratch or Installing Superset Locally Using Docker Compose.

it is worth noting that I had some issues during the installation process following the documentation.

For the record, below what I’ve done using a VM Ubuntu 20.04 from Oracle Cloud Infrastructure (OCI).
Please note this is a test installation, not suitable for a production environment.

Install the required dependencies (Ubuntu or Debian):

$ sudo apt update && sudo apt -y install build-essential libssl-dev libffi-dev python3-dev python3-pip libsasl2-dev libldap2-dev default-libmysqlclient-dev libprotobuf17 python3-protobuf

Upgrade pip (and prevent some issues)

$ pip3 install --upgrade pip

$ pip3 install zipp==3.8.0

$ pip3 install Flask-WTF==0.14.3

Install Superset

$ pip3 install apache-superset

Binaries are in $HOME/.local/bin
I decided to put the following information into my ~/.bashrc (however not sure it is a good practice, but good enough for testing)

export PATH="$HOME/.local/bin:$PATH"
export FLASK_APP=superset

then

$ source ~/.bashrc

Initialize the database:

$ superset db upgrade

$ superset fab create-admin


# If you want to load the embeded examples
# superset load_examples

# Create default roles and permissions
$ superset init

Please not that you will probably have to update your firewall.

If like me you are on OCI you must update the security list (OCI virtual firewall) for the protocol TCP and the port you are going to use, 8088 in this article

On Ubuntu you may have to update IPtable.

Start Superset

Superset is listening on port 8088

$ superset run -h 0.0.0.0 -p 8088 --with-threads --reload

If everything worked, you should be able to navigate to hostname:port in your browser and login using the username and password you created.

If you have everything on your laptop, you can remove -h 0.0.0.0 and then only local connections will be allowed (i.e. navigate to localhost:8088).

Install MySQL Connector Python 8.0

Unlike what is written here, I would recommend to use MySQL 8.0, to download and use the official MySQL Python connector available on the MySQL website.

Go to https://dev.mysql.com/downloads/connector/python/ for the latest version.

$ wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-py3_8.0.29-1ubuntu20.04_amd64.deb

$ sudo dpkg -i mysql-connector-python-py3_8.0.29-1ubuntu20.04_amd64.deb

Obviously, MySQL customers can use the commercial version available on MOS.

Configure Superset for MySQL HeatWave

Using the Apache Superset UI you can add a new backend database.

Go to Data / Databases

Then click + DATABASE

Add your MySQL HeatWave database.
There are 2 important parts:

1/ SQLALCHEMY URI :

mysql+mysqlconnector://<MySQL user>:<MySQL password>@<MySQL host>

You need the MySQL user, password and IP.

2/ ENGINE PARAMETERS

In order to take advantage of the power of MySQL HeatWave, autocommit must be enabled.

If autocommit is disabled, queries are not offloaded and execution is performed on the MySQL DB System. In other words, the queries will be much slower. Details here.

Go to Advanced / Other / ENGINE PARAMETERS

{“isolation_level”:”AUTOCOMMIT”}

You all set 🙂

Thanks to MySQL HeatWave, you can now add your datasets and build awesome and very fast dashboards, explore and visualize your data at the speed of light.

Resources

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

3

Data Migration from MariaDB to MySQL

March 9, 2022

Yet another customer wanted to migrate from MariaDB to MySQL. So I decided to write down the overall data migration process.

Indeed each migration are a unique story so I won’t provide you a “How to migrate”…
The idea is to highlight the general path and help you to avoid common pitfalls and thus help you save some precious time.

Context

  • Source database
    • MariaDB 10.3
    • Around 8GB of data
    • Only InnoDB tables
    • Many user accounts (important information)
  • Target databases
    • MySQL Database Service 8.0.28
    • MySQL 8.0.28

In fact, the (real) target was MySQL Database Service the MySQL PaaS on Oracle Cloud Infrastructure, but I will also show the workflow for on-premise MySQL.

Please find below a reenactment of the events…

MySQL Shell

The first step is to install the MySQL DBA BFF: MySQL Shell.
MySQL Shell is an advanced client and code editor for MySQL. You can manage your data using SQL, JavaScript and Python.

In addition to data manipulation (btw MySQL 8.0 also provides a NoSQL CRUD API to handle JSON documents), there are also a lot of things you can do with MySQL Shell like create a MySQL cluster (InnoDB Cluster, InnoDB ReplicaSet, InnoDB ClusterSet),  verify whether your MySQL server instances is ready for upgrade, dump and restore, etc… :

Believe me if you try it, you will love it 🙂


It is way better than the old mysql text client and some of these features work with MariaDB (at least the ones needed for the migration).

Easy to install.
e.g.

[opc@mariadb10-3 ~]$ 
sudo yum install -y mysql-shell

The source dataset

Let’s have a look on the dataset.

Unfortunately MariaDB does not have the sys schema
We cannot use this very elegant query:

-- MySQL 5.7 / 8.0+

SELECT sys.format_bytes(sum(data_length)) DATA,
       sys.format_bytes(sum(index_length)) INDEXES,
       sys.format_bytes(sum(data_length + index_length)) 'TOTAL SIZE'
FROM information_schema.TABLES ORDER  BY data_length + index_length
;

So back in the time and let’s use the information_schema instead and the following ugly query:

-- MySQL (old version) / MariaDB --

SQL>
SELECT 
    CONCAT( SUM( ROUND( DATA_LENGTH / (1024 / 1024) ,2)), 'MB' ) AS Data,
    CONCAT( SUM( ROUND( INDEX_LENGTH / (1024 / 1024) ,2)), 'MB' ) AS Indexes,
    CONCAT( SUM( ROUND( (DATA_LENGTH + INDEX_LENGTH) / (1024 * 1024) ,2)), 'MB' ) AS 'Total Size' 
FROM information_schema.TABLES ORDER BY DATA_LENGTH + INDEX_LENGTH
;

+-----------------+-----------------+------------+
| Data            | Indexes         | Total Size |
+-----------------+-----------------+------------+
| 3044154215.00MB | 5688978432.00MB | 8328.61MB  |
+-----------------+-----------------+------------+

Approximately 8GB of data.

What about the storage engines?

Again because of lack of sys schema we cannot use this pretty query:

-- MySQL 5.7 / 8.0+

SELECT count(*) as '# TABLES', sys.format_bytes(sum(data_length)) DATA, 
       sys.format_bytes(sum(index_length)) INDEXES,   
       sys.format_bytes(sum(data_length + index_length)) 'TOTAL SIZE', 
       engine `ENGINE` FROM information_schema.TABLES 
WHERE TABLE_SCHEMA NOT IN ('sys','mysql', 'information_schema', 'performance_schema', 'mysql_innodb_cluster_metadata')
GROUP BY engine
ORDER BY engine;

So let’s back in the time and use instead:

-- MySQL (old version) / MariaDB --

SQL>
SELECT 
   count(*) as '# TABLES',
   CONCAT( SUM( ROUND( DATA_LENGTH / (1024 / 1024) ,2)), 'MB' ) AS Data,
   CONCAT( SUM( ROUND( INDEX_LENGTH / (1024 / 1024) ,2)), 'MB' ) AS Indexes,
   CONCAT( SUM( ROUND( (DATA_LENGTH + INDEX_LENGTH) / (1024 * 1024) ,2)), 'MB' ) AS 'Total Size',
   engine `ENGINE` FROM information_schema.TABLES 
WHERE TABLE_SCHEMA NOT IN ('sys','mysql', 'information_schema', 'performance_schema', 'mysql_innodb_cluster_metadata')
GROUP BY engine
ORDER BY engine
;

+----------+-----------------+-----------------+------------+--------+
| # TABLES | Data            | Indexes         | Total Size | ENGINE |
+----------+-----------------+-----------------+------------+--------+
|        2 | NULL            | NULL            | NULL       | NULL   |
|       26 | 3043442688.00MB | 5688705024.00MB | 8327.66MB  | InnoDB |
+----------+-----------------+-----------------+------------+--------+

All tables are in InnoDB \o/
That’s how DBA’s life should be 🙂

Migrate the data

The source is MariaDB 10.3 and the target is MySQL 8.0, on-premise or using MySQL Database Service.

With such amount of data ~10GB a dump is not an issue especially if you use MySQL Shell utilities (please forget about the aging mysqldump – it deserves a nice retirement).

Migrate to MySQL on-premise

Because I need the whole instance, I’m going to use MySQL Shell dumpInstance utility.

An important option of dumpInstance is compatibility. It can

  • enforce the InnoDB storage engine (force_innodb) ie change CREATE TABLE statements to use the InnoDB storage engine for any tables that do not already use it
  • ignore any missing primary keys (ignore_missing_pks),
  • automatically add primary keys in invisible columns (create_invisible_pks)
  • etc…

Here, I will only use force_innodb. (if your memory is better than mine you remember that all the tables are already in InnoDB, so not really needed in this case, but I must admit that I’m paranoid :-O).
Joke aside, it is in my opinion a good habit to use this option.

If you are worried about primary keys below a query that would help you to check your schema:

-- Find tables without PK

SELECT tables.table_schema , tables.table_name , tables.engine  
FROM information_schema.tables  LEFT JOIN (     
   SELECT table_schema , table_name
   FROM information_schema.statistics
   GROUP BY table_schema, table_name, index_name 
   HAVING SUM( 
     case when non_unique = 0 and nullable != 'YES' then 1 else 0 end ) = count(*) ) puks   
   ON tables.table_schema = puks.table_schema 
  AND tables.table_name = puks.table_name   
  WHERE puks.table_name IS null 
  AND tables.table_type = 'BASE TABLE' 
  AND tables.TABLE_SCHEMA NOT IN ('mysql', 'performance_schema', 'information_schema', 'sys');

The MySQL Shell dump instance command for dumping data from MariaDB to MySQL 8.0 on-premise looks like:

JS>
util.dumpInstance("/bkp/dumpMariadb1", {threads: 8, compatibility: ["force_innodb"]})
  • The dump will be stored in the /bkp/dumpMariadb1 directory
  • threads: 8 -> 8 parallel threads to dump chunks of data from the MariaDB instance
  • compatibility: force_innodb -> update to InnoDB for any tables that do not already use it

Depending on your context you may have to tune this command.

Move the dump on the MySQL 8.0 host and then restore the dump.

In MySQL 8.0 there is a special ninja technique 🙂 to load a dump at the speed of light… All right, I may be exaggerating a bit, but at least at the speed of the sound by disabling crash recovery aka disabling redo logging.

DISCLAIMER: This feature is intended only for loading data into a new MySQL instance. Do not disable redo logging on a production system. It is permitted to shutdown and restart the server while redo logging is disabled, but an unexpected server stoppage while redo logging is disabled can cause data loss and instance corruption.

The commands to restore the dump on MySQL 8.0 on-premise look like:

JS>
\sql SET GLOBAL local_infile=1;
\sql ALTER INSTANCE DISABLE INNODB REDO_LOG;

util.loadDump("/restore/dumpMariadb1", {threads: 8, ignoreVersion: true , skipBinlog: true, loadUsers: false})

\sql ALTER INSTANCE ENABLE INNODB REDO_LOG;
\sql SET GLOBAL  local_infile=0;
  • local_infile=1 -> allows local data loading
  • ALTER INSTANCE DISABLE INNODB REDO_LOG -> disable redo logging to speed up data loading
  • The dump has been moved to the MySQL instance at /restore/dumpMariadb1
  • threads: 8 -> 8 parallel threads to  upload chunks of data to the MySQL instance
  • ignoreVersion: true -> allow the import of the MariaDB dump into MySQL (MariaDB is taken for a MySQL 5.5 version)
  • skipBinlog: true -> skips binary logging for the sessions used during the restore
  • loadUsers: false -> do not import users and their roles and grants. This one is very important because MariaDB users are not compatibles. Please note that the default is false (paranoid mode enable) 

Migrate to MySQL Database Service

As a prerequisites I’m assuming that you have the basic Oracle Cloud Infrastructure and MySQL Database Service knowledge.
If it is not yet the case, please read my Discovering MySQL Database Service series.

To migrate your data into MySQL Database Service (MDS), MySQL Shell is again your best friend to export the data.
You can either store the data locally or very easily in an OCI object storage bucket.

If the data are located in a bucket you can then use the MDS data Import option to automatically create a new MDS instance populated with your data.
If you choose the “local” storage, the import will be manual only.

The main thing to keep in mind during the import process is that you will most likely have a user account issue (MariaDB users are not compatibles), so my recommendation is to take a MySQL Shell dumps with all the business data (i.e. mysql, information_schema, performance_schema, sys are not include) without the user accounts.

Because I need the whole instance, in both case I’m using MySQL Shell dumpInstance utility.

Backup the database into a bucket

I’m going to store my dump inside an Object Storage bucket so I must create the bucket before the dump. This is very easy by using the OCI interface.

If you are not familiar with OCI Object Storage you can follow this tutorial: Discovering MySQL Database Service – Episode 4 – Dump your MySQL data into an Object Storage bucket.

In order to put these data into the OCI bucket, we must make some configuration for allowing MySQL Shell to easily connect to the OCI object storage service.

To this end, we will create an OCI CLI configuration file.

The CLI is a small-footprint tool that you can use on its own or with the Console to complete Oracle Cloud Infrastructure tasks. The CLI provides the same core functionality as the Console, plus additional commands. Some of these, such as the ability to run scripts, extend Console functionality.

To be clear, we will not use the CLI but its configuration file. The configuration file name and default location (on Linux) is /home/opc/.oci/config.

It should have the following information:

  • user: OCID of the user calling the API.
  • fingerprint: Fingerprint for the public key that was added to this user.
  • key_file: Full path and filename of the private key.
  • tenancy: OCID of your tenancy.
  • region: An Oracle Cloud Infrastructure region.

For more details please click here.

As an example, mine looks like:

[DEFAULT]
user=ocid1.user.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
fingerprint=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
key_file=/home/opc/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
compartment=ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
region=eu-frankfurt-1

You may be wondering how to get this information? Fair question!
Don’t panic I already explained everything in this tutorial: Discovering MySQL Database Service – Episode 4 – Dump your MySQL data into an Object Storage bucket

My Bucket is named: migrationMariadbNoUser

The MySQL Shell dump instance command for migrating data from MariaDB to MDS and stored data into a bucket looks like:

JS>
util.dumpInstance("", {users: false, osBucketName: "migrationMariadbNoUser", osNamespace: "abcdefghijkl", ocimds: true,  threads: 8, compatibility: ["force_innodb", "strip_restricted_grants", "strip_definers", "strip_tablespaces"]})
  • users: false -> exclude users and their roles and grants in the dump
  • osBucketName: “migrationMariadbNoUser” -> name of the Object Storage bucket to which the dump is to be written
  • osNamespace: “abcdefghijkl” -> namespace where the Object Storage bucket named by osBucketName is located
  • ocimds: true -> enables checks and modifications for compatibility with MDS
  • threads: 8 -> 8 parallel threads to dump chunks of data from the MariaDB instance
  • compatibility: ->
    • force_innodb -> change CREATE TABLE statements to use the InnoDB 
    • strip_restricted_grants -> remove specific privileges that are restricted by MDS
    • strip_definers -> remove the DEFINER clause from views, routines, events, and triggers
    • strip_tablespaces -> all tables are created in their default tablespace

Complete documentation is available here.

The command above is storing the dump, compatible with MDS, into the bucket migrationMariadbNoUser.

Backup the database locally

If your dataset is not big and/or you are too lazy to spend a little time in configuration, the alternative is to dump your data locally.
The main drawback is that you will have to import the data into MDS manually – but this should not be a big deal if your dataset is not huge.

If you want to store the dump locally the command looks like:

JS>
util.dumpInstance("/bkp/dumpMariadbNoUser", {users: false, threads: 8, compatibility: ["force_innodb", "strip_restricted_grants", "strip_definers", "strip_tablespaces", "create_invisible_pks"]})

The command is close to the one for the on-prem.
Please note – users: false – option. You can also dump the user and then run the import without loading the users (default behavior).

In fact, the most import difference compare to an on-prem data migration are the compatibility options. Because MDS is a PaaS you have less flexibility than the on-prem MySQL so more compatibility needs.

Restore into MySQL Database Service from a bucket

You have 2 solutions, the automated one, easiest but less flexible or the manual.

The first one is the MDS Data Import feature using the OCI interface.

MySQL Data Service - Data Import
MySQL Data Service – Data Import

You’ll find the “How To” in: Discovering MySQL Database Service – Episode 5 – Create a MySQL DB system from a MySQL Shell dump

More details in the documentation.

If you want to speed up your import you can disable the Crash Recovery (during the import and enable it when it is finished). Some details here.

You can also restore the dump manually, in a new fresh MDS instance.

The command looks like:

JS>
util.loadDump("", {osBucketName: "migrationMariadbNoUser", osNamespace: "abcdefghijkl", threads: 8, ignoreVersion: true})

Please note that in this case, you won’t be able to speed up the restore by disabling the crash recovery system (Redo Logs).

Restore into MySQL Database Service from a local dump

In this case we cannot use the crash recovery trick nor the Data Import feature.
Anyway the MySQL Shell dumpInstance function will still do the job.

The command looks like:

JS>
util.loadDump("/restore/dumpMariadbNoUser", {threads: 8, ignoreVersion: true})
  • threads: 8 -> 8 parallel threads to  upload chunks of data to the MySQL instance
  • ignoreVersion: true -> allow the import of the MariaDB dump into MySQL (MariaDB is taken for a MySQL 5.5 version)

loadUsers: false is not necessary here because the dump was made with users: false.

And don’t forget to re-create your users 🙂
You have plenty of authentication plugins to create secure user accounts.
We also provide Multifactor Authentication.

Btw if security is one of your concern (and it should be) below some MySQL password management features:

Database migration is not only about data migration, so you still have some work to do. But now you should have a better understanding of the overall data migration process.

Follow me on Linkedin

Watch my videos on my YouTube channel and subscribe.

My Slideshare account.

My Speaker Deck account.

Thanks for using HeatWave & MySQL!

0