> ## Documentation Index
> Fetch the complete documentation index at: https://pearsdb.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Sentiment Analysis with MindsDB and OpenAI using MQL

## Introduction

In this blog post, we present how to create OpenAI models within MindsDB. This example is a sentiment analysis where we infer emotions behind a text. The input data is taken from our sample MongoDB database.

## Prerequisites

To follow along, you can sign up for an account at [cloud.mindsdb.com](https://cloud.mindsdb.com/register/nlp). Alternatively, head to [MindsDB documentation](https://docs.mindsdb.com/) and follow the instructions to manually set up a local instance of MindsDB via [Docker](/setup/self-hosted/docker) or [pip](/setup/self-hosted/pip/source).

## How to Connect MindsDB to a Database

We use a collection from our MongoDB public demo database, so let’s start by connecting MindsDB to it.

You can use [Mongo Compass](/connect/mongo-compass) or [Mongo Shell](/connect/mongo-shell) to connect our sample database like this:

```bash theme={null}
test> use mindsdb
mindsdb> db.databases.insertOne({
            'name': 'mongo_demo_db',
            'engine': 'mongodb',
            'connection_args': {
                "host": "mongodb+srv://user:MindsDBUser123!@demo-data-mdb.trzfwvb.mongodb.net/",
                "database": "public"
            }
        })
```

## Tutorial

In this tutorial, we create a predictive model to infer emotions behind a text, a task also known as sentiment analysis.

Now that we've connected our database to MindsDB, let’s query the data to be used in the example:

```bash theme={null}
mindsdb> use mongo_demo_db
mongo_demo_db> db.amazon_reviews.find({}).limit(3)
```

Here is the output:

```bash theme={null}
{
  _id: '63d013b5bbca62e9c7774b1d',
  product_name: 'All-New Fire HD 8 Tablet, 8 HD Display, Wi-Fi, 16 GB - Includes Special Offers, Magenta',
  review: 'Late gift for my grandson. He is very happy with it. Easy for him (9yo ).'
}
{
  _id: '63d013b5bbca62e9c7774b1e',
  product_name: 'All-New Fire HD 8 Tablet, 8 HD Display, Wi-Fi, 16 GB - Includes Special Offers, Magenta',
  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."
}
{
  _id: '63d013b5bbca62e9c7774b1f',
  product_name: 'All-New Fire HD 8 Tablet, 8 HD Display, Wi-Fi, 16 GB - Includes Special Offers, Magenta',
  review: '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.'
}
```

Let's create a model collection to identify sentiment for all reviews:

```bash theme={null}
mongo_demo_db> use mindsdb
mindsdb> db.models.insertOne({
            name: 'sentiment_classifier',
            predict: 'sentiment',
            training_options: {
                        engine: 'openai',
                        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}}.":'
                }
        })
```

In practice, the `insertOne` method triggers MindsDB to generate an AI collection called `sentiment_classifier` that uses the OpenAI integration to predict a field named `sentiment`. The model is created 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](/sql/project).

The `training_options` key specifies the parameters that this handler requires.

* The `engine` parameter defines that we use the `openai` engine.
* The `prompt_template` parameter conveys the structure of a message that is to be completed with additional text generated by the model.

<Note>
  Follow [this instruction](/integrations/ai-engines/openai#setup) to set up the OpenAI integration in MindsDB.
</Note>

Once the `insertOne` method has started execution, we can check the status of the creation process with the following query:

```bash theme={null}
mindsdb> db.getCollection('models').find({
            'name': 'sentiment_classifier'
        })
```

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 collection – you can query it either by specifying synthetic data in the actual query:

```bash theme={null}
mindsdb> db.sentiment_classifier.find({
            review: 'It is ok.'
        })
```

Here is the output data:

```bash theme={null}
{
  sentiment: 'neutral',
  review: 'It is ok.'
}
```

Or by joining with a collection for batch predictions:

```bash theme={null}
mindsdb> db.sentiment_classifier.find(
            {
                'collection': 'mongo_demo_db.amazon_reviews'
            },
            {
                'sentiment_classifier.sentiment': 'sentiment',
                'amazon_reviews.review': 'review'
            }
        ).limit(3)
```

Here is the output data:

```bash theme={null}
{
  sentiment: 'positive',
  review: 'Late gift for my grandson. He is very happy with it. Easy for him (9yo ).'
}
{
  sentiment: '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 yer old grandchildren. They basically use it to play Amazon games that you download.'
}
```

The `amazon_reviews` collection is used to make batch predictions. Upon joining the `sentiment_classifier` model with the `amazon_reviews` collection, the model uses all values from the `review` field.

## 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](https://cloud.mindsdb.com/register/nlp) 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](https://mindsdb.com/joincommunity), 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](https://mindsdb.com/blog/extract-insights-from-text-inside-databases-using-openai-gpt3-and-mindsdb-integration). Also, we've got a variety of tutorials that use MySQL and MongoDB:

* [Sentiment Analysis in MySQL](/nlp/sentiment-analysis-inside-mysql-with-openai)
* [Question Answering in MySQL](/nlp/question-answering-inside-mysql-with-openai)
* [Text Summarization in MySQL](/nlp/text-summarization-inside-mysql-with-openai)
* [Question Answering in MongoDB](/nlp/question-answering-inside-mongodb-with-openai)
* [Text Summarization in MongoDB](/nlp/text-summarization-inside-mongodb-with-openai)

## What's Next?

Have fun while trying it out yourself!

* Bookmark [MindsDB repository on GitHub](https://github.com/mindsdb/mindsdb).
* Sign up for a free [MindsDB account](https://cloud.mindsdb.com/register/nlp).
* Engage with the MindsDB community on [Slack](https://mindsdb.com/joincommunity) or [GitHub](https://github.com/mindsdb/mindsdb/discussions) to ask questions and share your ideas and thoughts.

If this tutorial was helpful, please give us a GitHub star [here](https://github.com/mindsdb/mindsdb).
