Question Answering using MindsDB and OpenAI GPT-4
Answering Questions about The Great Pyramids
Introduction
Human minds are always curious and full of questions. They generally tend to seek the answers to their questions from any physical or online resource which is really time-consuming. It doesn't really work when you need to find the answer in an instant.
Well, the solution to this is applying AI to do the job. Question answering is one of the most challenging tasks in natural language processing (NLP) as it needs to find the most accurate and precise answer to the question instantly.
In this blog, you will explore how you can leverage the capability of MindsDB along with the OpenAI engine to build a robust Question-Answering model that can specifically answer questions about the Great Pyramids.
Data Setup
The data for this is quite simple. You will just have to collect some questions about the Great Pyramids and store them as a CSV file that you can use later while working with this model.
Connecting the Data
Now that you have prepared the CSV file with the set of questions about the Great Pyramids, it's time to upload it on MindsDB Cloud. You can simply follow the steps below to successfully add it as a Table.
Click on the
Add
button on the MindsDB Cloud Editor and then tap theUpload File
button from the menu that appears.Now select the CSV file from your system in the
Import a file
section, give a name for the table in theDatasource name
field and then click onSave and Continue
to create the Table.The control now takes you back to the MindsDB Editor where you can find two queries listed. Execute the first one and check the results.
SHOW TABLES FROM files;
This should list all the available tables in the current instance. Look out for the table you just created. This confirms that the data is successfully imported.
Understanding the Data
It's time to explore the data in your table now. You can simply use the next query on the Cloud Editor and that should return the data records from your new table.
SELECT * FROM files.Pyramids LIMIT 10;
This returns the first 10 records from the Pyramids
table.
This table has only two columns namely, Questions
that contains a set of questions and the context
column that contains the context for these questions.
You will use this table as the input to perform batch answering of questions, later in this tutorial.
Creating an OpenAI GPT-4 Model
Now you are ready to create the model that can answer your questions about the Great Pyramids. This is an example of Question Answering with Contenxt
type. Here you will provide a context to the model i.e., The Great Pyramids
while you train it. You can also choose to train a model without any context that can serve as a general purpose Question-Answering model.
With MindsDB, you have to just execute a simple SQL statement and that creates the model for you.
CREATE MODEL model_name
PREDICT target_column
USING
engine = 'engine_name',
model_name = 'name_of_the_model',
question_column = 'name_of_the_column_that_will_pass_the_question',
context_column = 'name_of_the_column_that_will_pass_the_context';
Well, it's time for you to understand more about all the parameters that we will use in the CREATE MODEL
statement.
model_name: Name of the model as per your choice.
target_name: Name of the column that you want to retrieve the result in i.e., the answer here.
engine: Name of the engine you want to use. (
openai
here)model_name: Name of the specific model from the engine. (
gpt-4
here)question_column: Name of the column/parameter that will serve the questions.
context_column: Name of the column/parameter that will serve the context.
Now if you will replace the placeholders with the real values, then the actual CREATE MODEL
will look like this.
CREATE MODEL openai_question_answer_model
PREDICT answer
USING
engine = 'openai',
model_name = 'gpt-4',
question_column = 'Questions',
context_column = 'context';
This returns the specific row for this newly created openai_question_answer_model
from the models
table upon successful execution.
Status of the Model
The model may take some time to complete its training and be ready for use. In the meantime, you can check its training status by using the SQL statement below.
Select status from models
where name='openai_question_answer_model';
Please wait until the status of the complete
to use it for answering the questions.
Answering Questions
Well, now comes the most interesting part where you will use the model and get answers to your questions easily about the Great Pyramids.
Answering Single Questions
You can now try to fetch an answer for a single question with the statement below.
SELECT Questions, answer
FROM openai_question_answer_model
WHERE context='The Great Pyramids' AND Questions='When were they built?';
The model should successfully return with two columns, one that contains the question
and the other that contains its corresponding answer.
You can notice that in the above query, the question didn't specify What
was built. The model picks up the context i.e., The Great Pyramids
and understands that the user is trying to find out when the Great Pyramids were built.
Answering a Batch of Questions
You can move on to the next part where you will try to fetch answers for a batch of questions that are already present in your Pyramids
table.
You can achieve this by joining the model with the table and then querying the answers. Follow the statement below to do this successfully.
SELECT input.context, input.Questions, output.answer AS Answers
FROM files.Pyramids AS input
JOIN openai_question_answer_model AS output LIMIT 3;
The model automatically picks up the context
from the Context
column in the Pyramids
table so you don't need to explicitly provide it.
The above query will return you with the answers to the first 3 questions from the Pyramids
table.
Conclusion
In this article, you learnt how to use MindsDB and OpenAI's GPT-4 to build a powerful and accurate question-answering system about the Great Pyramids. You also got to know how you can connect your data to MindsDB Cloud and use it for batch processing using the trained model.
This is definitely not the end. You can always choose any dataset or any topic and actively use MindsDB's AutoML capabilities to the fullest. Feel free to use your creative ideas and share, what amazing things you're building, with the community.
Lastly, before you leave please drop a like if this article was worth your time and feel free to drop any feedback you may have in the comments below.