Question Answering with MindsDB and OpenAI using SQL
Introduction
In this blog post, we present how to create OpenAI models within MindsDB. In this example, we ask a question to a model and get an answer. The input data is taken from our sample MySQL database.
Prerequisites
To follow along, you can sign up for an account at cloud.mindsdb.com. Alternatively, head to MindsDB documentation and follow the instructions to manually set up a local instance of MindsDB via Docker or pip.
Tutorial
In this tutorial, we create a predictive model to answer questions in a specified domain.
We use a table from our MySQL public demo database, so let’s start by connecting MindsDB to it:
CREATE DATABASE mysql_demo_db
WITH ENGINE = 'mysql',
PARAMETERS = {
"user": "user",
"password": "MindsDBUser123!",
"host": "db-demo-data.cwoyhfn6bzs0.us-east-1.rds.amazonaws.com",
"port": "3306",
"database": "public"
};
Now that we’ve connected our database to MindsDB, let’s query the data to be used in the example:
SELECT *
FROM mysql_demo_db.questions
LIMIT 3;
Here is the output:
+------------------+--------------------------------------------------------+-------------+
| article_title | question | true_answer |
+------------------+--------------------------------------------------------+-------------+
| Alessandro_Volta | Was Volta an Italian physicist? | yes |
| Alessandro_Volta | Is Volta buried in the city of Pittsburgh? | no |
| Alessandro_Volta | Did Volta have a passion for the study of electricity? | yes |
+------------------+--------------------------------------------------------+-------------+
Let’s create a model table to answer all questions from the input dataset:
CREATE MODEL question_answering_model
PREDICT answer
USING
engine = 'openai',
prompt_template = 'answer the question of text:{{question}} about text:{{article_title}}';
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.
In practice, the CREATE MODEL
statement triggers MindsDB to generate an AI table called question_answering_model
that uses the OpenAI integration to predict a column named answer
. The model lives inside the default mindsdb
project. In MindsDB, projects are a natural way to keep artifacts, such as models or views, separate according to what predictive task they solve. You can learn more about MindsDB projects here.
The USING
clause specifies the parameters that this handler requires.
- The
engine
parameter defines that we use theopenai
engine. - The
prompt_template
parameter conveys the structure of a message that is to be completed with additional text generated by the model.
If you’re using a local deployment, in order to use the OpenAI integration,
you need to install the openai
Python package. You can do this by running
the following command: bash pip install openai
We use the OpenAI engine to create a model in MindsDB. Please note that the api_key
parameter is optional on cloud.mindsdb.com but mandatory for local usage/on-premise. You can obtain an OpenAI API key by signing up for OpenAI’s API services on their website. Once you have signed up, you can find your API key in the API Key section of the OpenAI dashboard. You can then pass this API key to the MindsDB platform when creating models.
To create a question_answering_model
model in MindsDB, you can run the following code:
CREATE MODEL question_answering_model
PREDICT answer
USING
engine = 'openai',
prompt_template = 'answer the question of text:{{question}} about text:{{article_title}}',
api_key = 'YOUR_OPENAI_API_KEY'; -- MANDATORY FOR LOCAL MODE
Alternatively, you can create a MindsDB ML engine that includes the API key, so you don’t have to enter it each time:
CREATE ML_ENGINE openai2
FROM openai
USING
api_key = 'YOUR_OPENAI_API_KEY';
Please note that it is required to provide an OpenAI API key when using MindsDB Pro version.
If you want to use the OpenAI API key provided by MindsDB, please confirm your email. Alternatively, you have the option to utilize your own OpenAI API key by specifying it in the api_key
parameter.
Once the CREATE MODEL
statement has started execution, we can check the status of the creation process with the following query:
DESCRIBE question_answering_model;
It may take a while to register as complete depending on the internet connection. Once the creation is complete, the behavior is the same as with any other AI table – you can query it either by specifying synthetic data in the actual query:
SELECT article_title, question, answer
FROM question_answering_model
WHERE question = 'Was Abraham Lincoln the sixteenth President of the United States?'
AND article_title = 'Abraham_Lincoln';
Here is the output data:
+------------------+-------------------------------------------------------------------+------------------------------------------------------------------------+
| article_title | question | answer |
+------------------+-------------------------------------------------------------------+------------------------------------------------------------------------+
| Abraham_Lincoln | Was Abraham Lincoln the sixteenth President of the United States? | Yes, Abraham Lincoln was the sixteenth President of the United States. |
+------------------+-------------------------------------------------------------------+------------------------------------------------------------------------+
Or by joining with another table for batch predictions:
SELECT input.article_title, input.question, output.answer
FROM mysql_demo_db.questions AS input
JOIN question_answering_model AS output
LIMIT 3;
Here is the output data:
+------------------+--------------------------------------------------------+--------------------------------------------------------+
| article_title | question | answer |
+------------------+--------------------------------------------------------+--------------------------------------------------------+
| Alessandro_Volta | Was Volta an Italian physicist? | Yes, Volta was an Italian physicist. |
| Alessandro_Volta | Is Volta buried in the city of Pittsburgh? | No, Volta is not buried in the city of Pittsburgh. |
| Alessandro_Volta | Did Volta have a passion for the study of electricity? | Yes, Volta had a passion for the study of electricity. |
+------------------+--------------------------------------------------------+--------------------------------------------------------+
The questions
table is used to make batch predictions. Upon joining the question_answering_model
model with the questions
table, the model uses all values from the article_title
and question
columns.
Leverage the NLP Capabilities with MindsDB
By integrating databases and OpenAI using MindsDB, developers can easily extract insights from text data with just a few SQL commands. These powerful natural language processing (NLP) models are capable of answering questions with or without context and completing general prompts.
Furthermore, these models are powered by large pre-trained language models from OpenAI, so there is no need for manual development work. Ultimately, this provides developers with an easy way to incorporate powerful NLP capabilities into their applications while saving time and resources compared to traditional ML development pipelines and methods. All in all, MindsDB makes it possible for developers to harness the power of OpenAI efficiently!
MindsDB is now the fastest-growing open-source applied machine-learning platform in the world. Its community continues to contribute to more than 70 data-source and ML-framework integrations. Stay tuned for the upcoming features - including more control over the interface parameters and fine-tuning models directly from MindsDB!
Experiment with OpenAI models within MindsDB and unlock the ML capability over your data in minutes. Remember to sign-up for a free demo account and follow the tutorials, perhaps this time using your data.
Finally, if MindsDB’s vision to democratize ML sounds exciting, head to our community Slack, where you can get help and find people to chat about using other available data sources, ML frameworks, or writing a handler to bring your own!
Follow our introduction to MindsDB’s OpenAI integration here. Also, we’ve got a variety of tutorials that use MySQL and MongoDB:
- Sentiment Analysis in MySQL
- Text Summarization in MySQL
- Sentiment Analysis in MongoDB
- Question Answering in MongoDB
- Text Summarization in MongoDB
What’s Next?
Have fun while trying it out yourself!
- Bookmark MindsDB repository on GitHub.
- Sign up for a free MindsDB account.
- Engage with the MindsDB community on Slack or GitHub to ask questions and share your ideas and thoughts.
If this tutorial was helpful, please give us a GitHub star here.