Sentiment Analysis with MindsDB and OpenAI using SQL

MindsDB

Sentiment Analysis with MindsDB and OpenAI using SQL

Learn how to perform sentiment analysis using MindsDB and OpenAI models with SQL commands.

Connect on Social Media
Access Platform

Sentiment Analysis with MindsDB and OpenAI using SQL

MindsDB is revolutionizing how developers approach sentiment analysis with its seamless integration of OpenAI models. In this guide, we will walk you through the process of creating an OpenAI model within MindsDB to perform sentiment analysis on text data.

Introduction

Sentiment analysis is a powerful tool for understanding emotions behind text. With MindsDB, you can leverage SQL commands to create predictive models that analyze sentiments efficiently. Let’s dive into how to set this up!

Prerequisites

Before we start, ensure you have MindsDB installed locally via Docker or Docker Desktop.

Connecting to the Database

To begin, connect MindsDB to your MySQL database. Use the following SQL command:

CREATE DATABASE mysql_demo_db WITH ENGINE = 'mysql', PARAMETERS = { "user": "user", "password": "MindsDBUser123!", "host": "samples.mindsdb.com", "port": "3306", "database": "public" };

Querying the Data

Now that we’re connected, let’s query some sample data:

SELECT * FROM mysql_demo_db.amazon_reviews LIMIT 3;

Sample Output

product_namereview
All-New Fire HD 8 TabletLate gift for my grandson. He is very happy with it. Easy for him (9yo).
All-New Fire HD 8 TabletI'm not super thrilled with the proprietary OS on this unit, but it does work okay.
All-New Fire HD 8 TabletI purchased this Kindle Fire HD 8 for my grandchildren. They love it!

Creating the Sentiment Analysis Model

Next, we’ll create a model to classify sentiments:

  1. Create an OpenAI engine:

    CREATE ML_ENGINE openai_engine FROM openai USING openai_api_key = 'your-openai-api-key';
    
  2. Create the sentiment classifier model:

    CREATE MODEL sentiment_classifier_model PREDICT sentiment USING engine = 'openai_engine', prompt_template = 'describe the sentiment of the reviews strictly as "positive", "neutral", or "negative". "I love the product":positive "It is a scam":negative "{{review}}."';
    

Checking Model Status

To check the status of your model creation, run:

DESCRIBE sentiment_classifier_model;

Making Predictions

Once your model is ready, you can make predictions:

Single Prediction

SELECT review, sentiment FROM sentiment_classifier_model WHERE review = 'It is ok.';

Batch Predictions

SELECT input.review, output.sentiment FROM mysql_demo_db.amazon_reviews AS input JOIN sentiment_classifier_model AS output LIMIT 3;

Sample Output

reviewsentiment
Late gift for my grandson. He is very happy with it.positive
I'm not super thrilled with the proprietary OS on this unit, but it does work okay.positive
I purchased this Kindle Fire HD 8 for my grandchildren. They love it!positive

Conclusion

MindsDB makes it easy to harness the power of OpenAI for sentiment analysis. By integrating databases and AI, developers can extract insights from text data with minimal effort. If you’re excited about the potential of MindsDB, join our community on Slack or GitHub to learn more and share your experiences!

Call to Action

Ready to dive into sentiment analysis with MindsDB? Start experimenting with OpenAI models today and unlock the full potential of your data!