Large Language Models
MindsDB and OpenAI

MindsDB lets you create models that utilize features provided by OpenAI GPT-3.

How to Bring the OpenAI Model to MindsDB

We use the CREATE MODEL statement to bring the OpenAI models to MindsDB.

MindsDB provides an OpenAI engine by default. But if you want to use your own OpenAI engine, please create it using the command below and passing your OpenAI API key:

CREATE ML_ENGINE openai2
FROM openai
USING
api_key = 'YOUR_OPENAI_API_KEY';

MindsDB Pro Please note that it is required to provide an OpenAI API key when using MindsDB Pro version.

Default Model

When you create an OpenAI model in MindsDB, it uses the gpt-3.5-turbo model by default. But you can use the gpt-4 model as well by passing it to the model-name parameter in the USING clause of the CREATE MODEL statement.

Let’s go through the available operation modes.

Please note that the examples presented here use SQL. To see how to create OpenAI models in Mongo database using MQL, check out this example on sentiment classification.

Operation Mode 1: Answering Questions without Context

Here is how to create a model that answers questions without context:

CREATE MODEL project_a.openai_test_a
PREDICT answer
USING
    engine = 'openai',
    question_column = 'question',
    model_name = 'model_name'
    api_key = 'YOUR_OPENAI_API_KEY';

Where:

ExpressionDescription
project_a.openai_test_aThe model name is openai_test_a and it resides inside the project_a project. Learn more about MindsDB projects here.
answerIt is the value to be predicted.
engineThe openai engine is used.
question_columnIt is the column that stores input data.
model_nameOptional. By default, the text-davinci-002 model is used. If you prefer to use a cheaper model or a model that was fine-tuned outside of MindsDB, use this parameter.
api_keyOptional. If it is left blank, or the env value is passed, then MindsDB fetches the OPENAI_API_KEY environment variable value.

Let’s look at an example.

CREATE MODEL project_a.openai_test_a
PREDICT answer
USING
    engine = 'openai',
    question_column = 'question';

On execution, we get:

Query successfully completed

Now we can query for answers.

SELECT question, answer
FROM project_a.openai_test_a
WHERE question = 'Where is Stockholm located?';

On execution, we get:

+---------------------------+-------------------------------+
|question                   |answer                         |
+---------------------------+-------------------------------+
|Where is Stockholm located?|Stockholm is located in Sweden.|
+---------------------------+-------------------------------+

Operation Mode 2: Answering Questions with Context

Here is how to create a model that answers questions with context:

CREATE MODEL project_a.openai_test_b
PREDICT answer
USING
    engine = 'openai',
    question_column = 'question',
    context_column = 'context',
    api_key = 'YOUR_OPENAI_API_KEY';

Where:

ExpressionDescription
project_a.openai_test_bThe model name is openai_test_b and it resides inside the project_a project.
answerIt is the value to be predicted.
engineThe openai engine is used.
question_columnIt is the column that stores input data being a question.
context_columnIt is the column that stores input data being a context.
api_keyOptional. If it is left blank, or the env value is passed, then MindsDB fetches the OPENAI_API_KEY environment variable value.

Let’s look at an example.

CREATE MODEL project_a.openai_test_b
PREDICT answer
USING
    engine = 'openai',
    question_column = 'question',
    context_column = 'context';

On execution, we get:

Query successfully completed

Now we can query for answers.

SELECT context, question, answer
FROM project_a.openai_test_b
WHERE context = 'Answer with a joke'
AND question = 'How to cook soup?';

On execution, we get:

+-------------------+------------------+---------------------------------------------------------+
|context            |question          |answer                                                   |
+-------------------+------------------+---------------------------------------------------------+
|Answer with a joke |How to cook soup? |How do you cook soup? You put it in a pot and heat it up!|
+-------------------+------------------+---------------------------------------------------------+

Operation Mode 3: Prompt Completion

Here is how to create a model that offers the most flexible mode of operation. It answers any query provided in the prompt_template parameter.

Good prompts are the key to getting great completions out of large language models like the ones that OpenAI offers. For best performance, we recommend you read their prompting guide before trying your hand at prompt templating.

CREATE MODEL project_a.openai_test_c
PREDICT answer
USING
    engine = 'openai',
    prompt_template = 'input your query here',
    max_tokens = 100,
    temperature = 0.3,
    api_key = 'YOUR_OPENAI_API_KEY';

Where:

ExpressionDescription
project_a.openai_test_cThe model name is openai_test_c and it resides inside the project_a project.
answerIt is the value to be predicted.
engineThe openai engine is used.
prompt_templateIt is the column that stores a query to be answered. Please note that this parameter can be overridden at prediction time.
max_tokensIt defines the maximum token cost of the prediction. Please note that this parameter can be overridden at prediction time.
temperatureIt defines how risky the answers are. The value of 0 marks a well-defined answer, and the value of 0.9 marks a more creative answer. Please note that this parameter can be overridden at prediction time.
api_keyOptional. If it is left blank, or the env value is passed, then MindsDB fetches the OPENAI_API_KEY environment variable value.

Let’s create a model:

CREATE MODEL project_a.openai_test_c
PREDICT answer
USING
    engine = 'openai',
    prompt_template = 'Context: {{context}}. Question: {{question}}. Answer:',
    max_tokens = 100,
    temperature = 0.3;

Let’s look at an example that uses parameters provided at model creation time.

SELECT context, question, answer
FROM project_a.openai_test_c
WHERE context = 'Answer accurately'
AND question = 'How many planets exist in the solar system?';

On execution, we get:

+-------------------+-------------------------------------------+----------------------------------------------+
|context            |question                                   |answer                                        |
+-------------------+-------------------------------------------+----------------------------------------------+
|Answer accurately  |How many planets exist in the solar system?| There are eight planets in the solar system. |
+-------------------+-------------------------------------------+----------------------------------------------+

Now let’s look at an example that overrides parameters at prediction time.

SELECT instruction, answer
FROM project_a.openai_test_c
WHERE instruction = 'Speculate extensively'
USING
    prompt_template = '{{instruction}}. What does Tom Hanks like?',
    max_tokens = 100,
    temperature = 0.5;

On execution, we get:

+----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|instruction           |answer                                                                                                                                                                                                                         |
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|Speculate extensively |Some people speculate that Tom Hanks likes to play golf, while others believe that he enjoys acting and directing. It is also speculated that he likes to spend time with his family and friends, and that he enjoys traveling.|
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Example

Let’s go through a sentiment classification example to understand better how to bring OpenAI models to MindsDB as AI tables.

CREATE MODEL mindsdb.sentiment_classifier                           
PREDICT sentiment
USING
  engine = 'openai',              
  prompt_template = 'predict the sentiment of the text:{{review}} exactly as either positive or negative or neutral';

On execution, we get:

Query successfully completed

Where:

ExpressionsValues
project_namemindsdb
predictor_namesentiment_classifier
target_columnsentiment
engineopenai
prompt_templatepredict the sentiment of the text:{{review}} exactly as either positive or negative or neutral

In the prompt_template parameter, we use a placeholder for a text value that comes from the review column, that is, text:{{review}}.

Before querying for predictions, we should verify the status of the sentiment_classifier model.

SELECT *
FROM mindsdb.models
WHERE name = 'sentiment_classifier';

On execution, we get:

+--------------------+------+-------+-------+--------+--------+---------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
|NAME                |ENGINE|PROJECT|VERSION|STATUS  |ACCURACY|PREDICT  |UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS                                                                                                                                       |TAG    |
+--------------------+------+-------+-------+--------+--------+---------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
|sentiment_classifier|openai|mindsdb|1      |complete|[NULL]  |sentiment|up_to_date   |22.12.4.3      |[NULL]|[NULL]           |{'target': 'sentiment', 'using': {'prompt_template': 'predict the sentiment of the text:{{review}} exactly as either positive or negative or neutral'}}|[NULL] |
+--------------------+------+-------+-------+--------+--------+---------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+-------+

Once the status is complete, we can query for predictions.

SELECT output.sentiment, input.review
FROM example_db.demo_data.amazon_reviews AS input
JOIN mindsdb.sentiment_classifier AS output
LIMIT 3;

Don’t forget to create the example_db database before using one of its tables, like in the query above.

CREATE DATABASE example_db
WITH ENGINE = "postgres",
PARAMETERS = {
    "user": "demo_user",
    "password": "demo_password",
    "host": "3.220.66.106",
    "port": "5432",
    "database": "demo"
    };

On execution, we get:

+----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sentiment                              | review                                                                                                                                                                                                                                                                                                                                                                            |
+----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| positive                               | Late gift for my grandson. He is very happy with it. Easy for him (9yo ).                                                                                                                                                                                                                                                                                                         |
| The sentiment of the text is positive. | I'm not super thrilled with the proprietary OS on this unit, but it does work okay and does what I need it to do. Appearance is very nice, price is very good and I can't complain too much - just wish it were easier (or at least more obvious) to port new apps onto it. For now, it helps me see things that are too small on my phone while I'm traveling. I'm a happy buyer.|
| positive                               | I purchased this Kindle Fire HD 8 was purchased for use by 5 and 8 yer old grandchildren. They basically use it to play Amazon games that you download.                                                                                                                                                                                                                           |
+----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+