How to Integrate the Power of GPT-3 and MindsDB: A Step-by-Step Guide to Enhance Your App's Capabilities

How to Integrate the Power of GPT-3 and MindsDB: A Step-by-Step Guide to Enhance Your App's Capabilities

Artificial Intelligence (AI) is present in the majority of applications we use today. With the development of ML platforms like MindsDB and Language Models like GPT-3 the possibilities are endless. In this article, we'll explore how to integrate the power of GPT-3 and MindsDB into your app and show you how to take advantage of their unique features to enhance your user experience.

What is GPT-3?

GPT-3 is a language model developed by OpenAI that has 175 billion parameters, making it the largest language model ever made at the time of writing this article. As a language model, GPT-3 can perform a wide range of tasks, such as generating text, summarizing content, analyzing sentiment, translating languages, and more. Its massive size and pre-trained knowledge enable it to produce human-like responses to prompts and make it an indispensable tool for various natural language processing tasks.

What is MindsDB?

MindsDB is a revolutionary open-source platform that streamlines the development and deployment of predictive models. With MindsDB, developers and data scientists can easily build and deploy machine learning models within their databases using just a few lines of code, eliminating the need for complex infrastructure and saving valuable time. By integrating with databases, MindsDB enables faster and more efficient deployment of machine learning models, allowing businesses to quickly gain insights and make data-driven decisions.

Prerequisites

  1. Basic Programming Knowledge.

  2. MongoDB Compass

Getting started with MindsDB.

As previously noted, MindsDB operates directly within the database, meaning that access to a data source is necessary to proceed. Fortunately, the MindsDB team has provided some sample data for us to work with. In this case, we will be using a MongoDB database to perform the machine learning operations.

We will be making use of the MindsDB Cloud Editor. Register for a free here account to get started:

After registering with MindsDB you should be taken to a screen similar to this.

The next step would be to connect our MongoDB Compass to MindsDB. You can do that by following the instructions on this page. But, if you do not have a MongoDB database to connect to MindsDB do not worry, MindsDB provides us with sample data we can use.

Using MindsDB Sample Data

  1. Open up your MongoDB shell and run the following command

     use mindsdb
    
  2. Next type into the shell

     db.databases.insertOne({
         'name': 'mongo_test_db',
         'engine': 'mongodb',
         'connection_args': {
             "host": "mongodb+srv://admin:201287aA@cluster0.myfdu.mongodb.net/admin?authSource=admin&replicaSet=atlas-5koz1i-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true",
             "database": "test_data"
             }
     })
    
  3. Create the OpenAI model

     db.models.insertOne({
         name: 'sentiment_classifier_openai_mql',
         predict: 'sentiment',
         training_options: {
                 engine: 'openai',
                 prompt_template: 'predict the sentiment of the text:{{review}} exactly as either positive or negative or neutral'
                }
     })
    

result of running the above command should be

{ acknowledged: true,
  insertedId: ObjectId("63c19c3fe1d9855caa931df6") }
  1. Confirm the above tasks were executed successfully by running the command below

     db.getCollection('models').find({'name': 'sentiment_classifier_openai_mql'})
    

result of running the above command should be

{ NAME: 'sentiment_classifier_openai_mql',
  ENGINE: 'openai',
  PROJECT: 'mindsdb',
  VERSION: 1,
  STATUS: 'complete',
  ACCURACY: null,
  PREDICT: 'sentiment',
  UPDATE_STATUS: 'up_to_date',
  MINDSDB_VERSION: '22.12.4.3',
  ERROR: null,
  SELECT_DATA_QUERY: null,
  TRAINING_OPTIONS: '{\'target\': \'sentiment\', \'using\': {\'prompt_template\': \'predict the sentiment of the text:{{review}} exactly as either positive or negative or neutral\'}}',
  TAG: null,
  _id: ObjectId("000000000000002836398080") }

Running NLP Queries with MindsDB.

MindsDB lets you run 3 types of NLP operations using GPT-3.

  1. Answering Questions without Context

  2. Answering Questions with Context

  3. Prompt Completion

Making a Single Prediction.

Running the command below

db.sentiment_classifier_openai_mql.find({review: 'It is ok.'})

We get the result

{
  sentiment: 'The sentiment of the text is neutral.',
  review: 'It is ok.'
}

Making Bulk Predictions.

Running the command below

db.sentiment_classifier_openai_mql.find(
    {'collection': 'mongo_test_db.amazon_reviews'},
    {'sentiment_classifier_openai_mql.sentiment': 'sentiment',
     'amazon_reviews.review': 'review'
    }
)

We get the result

[
    {
      sentiment: 'positive',
      review: 'Late gift for my grandson. He is very happy with it.             Easy for him (9yo ).'
    }
    {
      sentiment: 'The sentiment of the text is positive.',
      review: "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."
    }
    {
      sentiment: 'positive',
      review: 'I purchased this Kindle Fire HD 8 was purchased for use by 5 and 8 year old grandchildren. They basically use it to play Amazon games that you download.'
    }
]

To get your hands dirty with more examples check out this resource.

References

  1. MindsDB Documentation

Happy building.

To the moon ๐Ÿš€.