Sentiment Analysis#

Run on Google Colab View source on GitHub Download notebook


In this tutorial, we present how to use OpenAI models in EvaDB to analyse sentiment in text data. In particular, we focus on analysing sentiments expressed by customers in food reviews. EvaDB makes it easy to do sentiment analysis using its built-in ChatGPT AI function. In this tutorial, besides classifying sentiment, we will also use another query to generate responses to customers for addressing negative reviews.

We will assume that the input data is loaded into a PostgreSQL database. To load the food review data into your database, see the complete sentiment analysis notebook on Colab.


To follow along, you will need to set up a local instance of EvaDB via pip.

Connect to EvaDB#

After installing EvaDB, use the following Python code to establish a connection and obtain a cursor for running EvaQL queries.

import evadb
cursor = evadb.connect().cursor()

Connect EvaDB to PostgreSQL Database Server#

We will assume that you have a PostgreSQL database running locally that contains the data needed for analysis. Follow these instructions to install PostgreSQL.


If find it challenging to install the PostgreSQL database on your machine, here is an alternative for quick prototyping.

You can use an embedded SQLite database. If you go with the sqlite database, alter the SQL commands in this tutorial to use the sqlite engine and the evadb.db SQLite database file as explained in the SQLite page.

EvaDB lets you connect to your favorite databases, data warehouses, data lakes, etc., via the CREATE DATABASE statement. In this query, we connect EvaDB to an existing PostgreSQL server:

CREATE DATABASE postgres_data
WITH ENGINE = 'postgres',
    "user": "eva",
    "password": "password",
    "host": "localhost",
    "port": "5432",
    "database": "evadb"

Sentiment Analysis of Reviews using ChatGPT#

We run the following query to analyze whether the review is positive or negative with a custom ChatGPT prompt. Here, the query runs on the review column in the review_table that is a part of the PostgreSQL database.

    "Is the review positive or negative? Only reply 'positive' or 'negative'. Here are examples. The food is very bad: negative. The food is very good: positive.",
FROM postgres_data.review_table;

This query returns the sentiment of the reviews in the table:

|             chatgpt.response |
|                     negative |
|                     positive |
|                     negative |

Respond to Negative reviews using ChatGPT#

Let’s next respond to negative food reviews using another EvaQL query that first retrieves the reviews with negative sentiment, and processes those reviews with another ChatGPT function call that generates a response to address the concerns shared in the review.

        "Respond the the review with solution to address the review's concern",
FROM postgres_data.review_table
    "Is the review positive or negative. Only reply 'positive' or 'negative'. Here are examples. The food is very bad: negative. The food is very good: positive.",
    review) = "negative";

While running this query, EvaDB first retrieves the negative reviews and then applies ChatGPT to derive a response. Here is the query’s output DataFrame:

|                                                                                                                                                                                                                                                                             chatgpt.response |
| Dear valued customer, Thank you for bringing this matter to our attention. We apologize for the inconvenience caused by the excessive saltiness of your fried rice. We understand how important it is to have a satisfying dining experience, and we would like to make it right for you ... |
| Dear [Customer's Name], Thank you for bringing this issue to our attention. We apologize for the inconvenience caused by the missing chicken sandwich in your takeout order. We understand how frustrating it can be when an item is missing from your meal. To address this concern, we ... |

Leverage Text Processing AI Engines with EvaDB#

By integrating databases and AI engines using EvaDB, developers can easily extract insights from text data with just a few EvaQL queries. These powerful natural language processing (NLP) models from OpenAI and HuggingFace are capable of complex text processing tasks (e.g., answering complex questions with context obtained from a column in a table).

EvaDB makes it easy for developers to easily incorporate powerful NLP capabilities into their AI-powered applications while saving time and resources compared to traditional AI development pipelines.

What’s Next?#

👋 If you are excited about our vision of bringing AI inside databases, consider:

Language Models (🦙) and Databases

Language Models (🦙) and Databases#