Skip to content

Latest commit

 

History

History
215 lines (160 loc) · 10.9 KB

generative-ai-tables.mdx

File metadata and controls

215 lines (160 loc) · 10.9 KB
title sidebarTitle
Generative AI Tables
Generative AI Tables

MindsDB empowers organizations to harness the power of AI by abstracting AI models as Generative AI Tables. These tables are capable of learning from the input data and generating predictions from the underlying model upon being queried. This abstraction makes AI highly accessible, enabling development teams to use their existing SQL skills to build applications powered by AI.

What are Generative AI Tables?

Generative AI is a subfield of artificial intelligence that trains AI models to create new content, such as realistic text, forecasts, images, and more, by learning patterns from existing data.

MindsDB revolutionizes machine learning within enterprise databases by introducing the concept of Generative AI tables. These essentially represent AI models as virtual database tables, capable of producing realistic outputs given certain inputs.

-- Example of querying an AI table
-- This AI table is trained to generate customer support responses.

SELECT 
  response   -- AI generated output 
FROM 
  ai_response_model  -- A generative AI model as a virtual table
WHERE 
  customer_message = "Dear Team, I spilled water on the computer I bought on June 1st 2024, order #3418276, and it is no longer working, what should I do?"

Generative AI Tables streamline the process of making predictions based on your data, thereby allowing developers to tap into the comprehensive ecosystem of state-of-the-art Generative AI using simple SQL statements.

To better understand this concept, let’s have a deeper exploration in the use case of generating automated responses to emails coming from customers.

First let's look at the data that we have. In this case, imagine that you have a table with incoming emails.

SELECT message_id, customer_message
FROM postgres_demo_db.customer_messages;

On execution, we get:

+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| message_id | customer_message                                                                                                                                  |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| 1          | "I've been having trouble with my product; it's not working as it should. I would like to request a return. My order number is 12345."            |
| 2          | "My account settings won't update no matter what I try. Can you help?"                                                                            |
| 3          | "My product stopped working after just a week of use. I didn't expect this from your brand. Can I get a refund?"                                  |
| 4          | "I suspect my account has been hacked. I noticed some unusual activity. Please assist me in securing it."                                         |
| 5          | "There seems to be an issue with the settings on my account, they're not saving correctly."                                                       |
| 6          | "I believe my account has been compromised. Please help me regain control and secure my account."                                                 |
| 7          | "I'm not satisfied with the product I received. It's not working as advertised. I'd like to return it, please. My order number is 56789."         |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+

Creating a Generative AI-Table

We can for example create an OpenAI model that will generate a response given some prompt_template instructions written in plain English.

CREATE MODEL ai_response_model
PREDICT response
USING
    engine = ‘openai’,
    prompt_template = '

Generate a simple customer response depending on the following options:

- If it is a message about returns and there is an order number, the message should be as follows (populate order_id in the URL accordingly):
"We are sorry about the issues with your product. Please follow this link to process your return: http://website.com/returns?order_id=<order_id>."
- If the message is about returns but has no order number, respond by asking for the order number and provide a link to see the list of their orders: http://website.com/my_orders.
- If the message is about the account being compromised, send them a link to immediately change their password: http://website.com/change_password.
- Otherwise, simply respond with [NEEDS_ATTENTION].

	The message is the following: {customer_message}
';

Now that we generated our AI Table, we can query for messages and their generated responses using JOINs.

SELECT i.message_id,  m.response, i.customer_message
FROM postgres_demo_db.customer_messages AS i
JOIN ai_response_model AS m;

Results in:

+--------------+-------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------+
| i.message_id | m.response                                                                                                                                | i.customer_message                                                                                                                     |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------+
| 1            | We are sorry about the issues with your product. Please follow this link to process your return http://website.com/returns?order_id=12345 | "I've been having trouble with my product; it's not working as it should. I would like to request a return. My order number is 12345." |
| 2            | [NEEDS_ATTENTION]                                                                                                                         | "My account settings won't update no matter what I try. Can you help?"                                                                 |
| 3            | Please provide an order number, follow this link: http://website.com/my_orders                                                            | "My product stopped working after just a week of use. I didn't expect this from your brand. Can I get a refund?"                       |
| 4            | We understand your concern. Please change your password immediately using this link http://website.com/change_password                    | "I suspect my account has been hacked. I noticed some unusual activity. Please assist me in securing it."                              |
| 5            | [NEEDS_ATTENTION]                                                                                                                         | "There seems to be an issue with the settings on my account, they're not saving correctly."                                            |
| 6            | We understand your concern. Please change your password immediately using this link http://website.com/change_password                    | "I believe my account has been compromised. Please help me regain control and secure my account."                                      |
| 7            | We are sorry about the issues with your product. Please follow this link to process your return http://website.com/returns?order_id=56789 | "I'm not satisfied with the product I received. It's not working. I'd like to return it, please. My order number is 56789."           |
+--------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+

Difference between AI Tables and Standard Tables

To understand the difference, we can go over a much simpler example, here we will see how traditional database tables are designed to give you a deterministic response given some input, and Generative AI Tables are designed to generate an approximate response given some input.

Let’s consider the following income_table table that stores the income and debt values.

SELECT income, debt
FROM income_table;

On execution, we get:

+------+-----+
|income|debt |
+------+-----+
|60000 |20000|
|80000 |25100|
|100000|30040|
|120000|36010|
+------+-----+

A simple visualization of the data present in the income_table table is as follows:

Income vs Debt

Querying the income table to get the debt value for a particular income value results in the following:

SELECT income, debt
FROM income_table
WHERE income = 80000;

On execution, we get:

+------+-----+
|income|debt |
+------+-----+
|80000 |25100|
+------+-----+

And here is what we get:

Income vs Debt chart

But what happens when querying the table for an income value that is not present there?

SELECT income, debt
FROM income_table
WHERE income = 90000;

On execution, we get:

Empty set (0.00 sec)

When the WHERE clause condition is not fulfilled for any of the rows, no value is returned.

Income vs Debt query

When a table doesn’t have an exact match, the query returns an empty set or null value. This is where the AI Tables come into play!

Let’s create a debt_model model that allows us to approximate the debt value for any income value. We train the debt_model model using the data from the income_table table.

CREATE MODEL mindsdb.debt_model
FROM income_table
PREDICT debt;

On execution, we get:

Query OK, 0 rows affected (x.xxx sec)

MindsDB provides the CREATE MODEL statement. On execution of this statement, the predictive model works in the background, automatically creating a vector representation of the data that can be visualized as follows:

Income vs Debt model

Let’s now look for the debt value of some random income value. To get the approximated debt value, we query the mindsdb.debt_model model instead of the income_table table.

SELECT income, debt
FROM mindsdb.debt_model
WHERE income = 90000;

On execution, we get:

+------+-----+
|income|debt |
+------+-----+
|90000 |27820|
+------+-----+

And here is how it looks:

Income vs Debt model