Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add use case of LLM on food review running on colab #991

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ parts:
title: Image Search [FAISS]
- file: source/usecases/qa-video.rst
title: Q&A from Videos [ChatGPT + HuggingFace]
- file: source/usecases/food-review.rst
title: Sentiment Analysis and Response [ChatGPT + PostgreSQL]
- file: source/usecases/02-object-detection.ipynb
title: Object Detection
- file: source/usecases/03-emotion-analysis.ipynb
Expand Down
147 changes: 147 additions & 0 deletions docs/source/usecases/food-review.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
ChatGPT + Postgres Tutorial
===========================

.. raw:: html

<embed>
<table align="left">
<td>
<a target="_blank" href="https://colab.research.google.com/github/georgia-tech-db/eva/blob/staging/tutorials/14-food-review-tone-analysis-and-response.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" /> Run on Google Colab</a>
</td>
<td>
<a target="_blank" href="https://github.com/georgia-tech-db/eva/blob/staging/tutorials/14-food-review-tone-analysis-and-response.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" /> View source on GitHub</a>
</td>
<td>
<a target="_blank" href="https://github.com/georgia-tech-db/eva/raw/staging/tutorials/14-food-review-tone-analysis-and-response.ipynb"><img src="https://www.tensorflow.org/images/download_logo_32px.png" /> Download notebook</a>
</td>
</table><br><br>
</embed>

In this tutorial, we demonstrate how to use EvaDB + ChatGPT to analyze the tone of food reviews stored in PostgreSQL. Then, based on the analysis, we further use
EvaDB + ChatGPT to address negative reviews by proposing a solution to the customer.

For this use case, we assume user has a Postgres server running locally. You can also check our notebook above to skip Postgres setup.

1. Connect to EvaDB
-------------------

.. code-block:: python

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

2. Connect to an Existing Postgres Database
-------------------------------------------

.. tab-set::

.. tab-item:: Python

.. code-block:: python

params = {
"user": "eva",
"password": "password",
"host": "localhost",
"port": "5432",
"database": "evadb",
}
query = f"CREATE DATABASE postgres_data WITH ENGINE = 'postgres', PARAMETERS = {params};"
cursor.query(query).df()

.. tab-item:: SQL

.. code-block:: sql

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

3. Sentiment Analysis of Food Review using ChatGPT
---------------------------------------------

We then use EvaDB + ChatGPT to analyze whether the review is "positive" or "negative" with customized ChatGPT prompt. For this use case,
we assume reviews have been already loaded into the table inside PostgreSQL.
You can check our `Jupyter Notebook <https://github.com/georgia-tech-db/eva/blob/staging/tutorials/14-food-review-tone-analysis-and-response.ipynb>`_ for how to load data.

.. tab-set::

.. tab-item:: Python

.. code-block:: python

cursor.query("""
SELECT ChatGPT(
"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: postive.",
review)
FROM postgres_data.review_table;
""").df()

.. tab-item:: SQL

.. code-block:: sql

SELECT ChatGPT(
"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: postive.",
review)
FROM postgres_data.review_table;

This will return tone analysis results for existing reviews.

.. code-block::

+------------------------------+
| chatgpt.response |
|------------------------------|
| negative |
| positive |
| negative |
+------------------------------+

4. Response to Negative Reviews using ChatGPT
---------------------------------------------

.. tab-set::

.. tab-item:: Python

.. code-block:: python

cursor.query("""
SELECT ChatGPT(
"Respond the the review with solution to address the review's concern",
review)
FROM postgres_data.review_table
WHERE ChatGPT(
"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: postive.",
review) = "negative";
""").df()

.. tab-item:: SQL

.. code-block:: sql

SELECT ChatGPT(
"Respond the the review with solution to address the review's concern",
review)
FROM postgres_data.review_table
WHERE ChatGPT(
"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: postive.",
review) = "negative";

This query will first filter out positive reviews and then apply ChatGPT again to create response to negative reviews. This will give results.

.. code-block::

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 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 ... |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Check out our `Jupyter Notebook <https://github.com/georgia-tech-db/evadb/blob/staging/tutorials/14-food-review-tone-analysis-and-response.ipynb>`_ for working example.
Loading