Sentiment Analysis on Reddit using OpenAI (ChatGPT) and Upstash Kafka
In this blog post, we will build an analysis app that utilizes sentiment analysis to evaluate social media posts. The app will send notifications to Slack whenever a new post containing a specific keyword is published. To build this tool, we will use Upstash Kafka, the Reddit API and the OpenAI API.
Project Description
The Kafka producer will send search requests for a specific keyword to the Reddit API every hour. Whenever a new post becomes available, the producer will extract the data and send it to the Upstash Kafka cluster.
The cluster will then trigger the consumer, which will retrieve the messages sent by the producer. The consumer will send the data to the OpenAI API with an appropriate prompt that instructs ChatGPT to analyze the content of the post and convert it into a notification format. The result of this analysis will then be sent to a Slack channel as a notification, using the Slack API.
Getting Started
Creating Upstash Kafka Cluster
First of all, we'll set up the Upstash Kafka. Navigate to Upstash Console and login. In the Kafka section, click Create Cluster button. Fill in the name and region fields, then create your first topic. You are all set!
Reddit API Request
In order to send requests to Reddit API, we first have to sign in to Reddit.Then go to your app preferences and click the Create app or Create another app button. Fill out the form like so:
When the app is created, create a new python file named producer.py
and copy/paste the CLIENT_ID
and CLIENT_SECRET
fields to this file from the Reddit app preferences.
For the Reddit API requests, we'll use praw library, which is Python Reddit API wrapper. It'll enable us the make the queries easier. Here's the example code for the requests:
With a given subreddit_query
and query
parameters, the get_reddit_posts
function will return the recent posts with author, title, text and url information. With that, we're now able to make requests to Reddit API. Next, we'll send the post data to Upstash Kafka Cluster.
Upstash Kafka Producer
In order to use Kafka Producers, you have to install the kafka-python
library. You can do that by typing in the command below in the terminal.
NOTE: Make sure that you've installed the kafka-python
library, not the deprecated kafka
library. That's a possible error one may face.
We have already created a cluster and a topic on Upstash Kafka. To proceed further, please go to the Upstash Console and navigate to the Connect to your cluster section. Once there, copy the code snippet from the Producer section and paste it into your producer.py
file.
"Once you have completed the previous step, the only thing left to do is to make the requests and send them to the Kafka cluster. When sending a message, it is important to specify the topic. Copy the code snippet below and add it to the end of your producer.py
file."
After you run the producer.py
file, you can see the message analytics on Upstash Console and verify that the messages are actually produced and sent to the queue.
Upstash Kafka Consumer
Creating a consumer client is similar to creating a producer client. First, create a consumer.py
file in the same folder as producer.py
. Then copy/paste the Consumer code snippet to consumer.py
from Connect to your cluster section in the Upstash Console
After creating consumer client, we'll complete the methods for OpenAI request, and Slack notification request.
OpenAI API Request
First, go to OpenAI platform, create an application and get an API key after you sign in. You may need to update your payment information.
Before making the request to ChatGPT, we must decide on a clear and unambiguous prompt that conveys our purpose. As an example, I have provided a prompt below for you to use. Please copy and paste it into your consumer.py
file
Finally, we'll create the analyze_post
method which will apply sentiment analysis on the Reddit posts.
Here's a demonstration on how this prompt and its response would look like:
Our OpenAI chat works just fine now, but we have to work on the Slack notification app before the final touch.
Slack API Request
To get started, go to the Slack API Console and navigate to the Your apps section. Click the Create New App button and provide an app name, then choose the workspace for your notification bot.
In the Slack app dashboard, navigate to the OAuth & Permissions section in the sidebar. Under the Scopes section, add the chat:write scope to your bot as a Bot Token Scope.
Now, you can add your bot to a channel in Slack by right-clicking on it from the sidebar. Your Slack bot is now ready to send notifications to the channel, but we first need to configure the backend.
To do this, open your consumer.py
file and import the necessary libraries. Then, initialize the Slack client at the beginning of the file
You may find your channel_id
from Right click on the channel > View channel details.
Now that all parts of the consumer is ready, it's time to assemble them with a final touch.
Final Touch
Before finalizing the project, we need to complete several tasks. These include pulling messages from the Upstash cluster, sending them to OpenAI for sentiment analysis, and then sending the result of the analysis to Slack as a notification. We have already configured the necessary clients, and here's how we will perform these operations.
After running the producer.py
file, run the consumer.py
file and you'll see that notifications are being sent to Slack channel.
Last Words
In conclusion, this project has provided valuable experience in integrating Upstash Kafka with third-party APIs. By leveraging the power of OpenAI, we were able to perform sentiment analysis on incoming messages and send the results to Slack as notifications.
To further enhance the analysis results, you can experiment with different OpenAI prompts or try different approaches to text preprocessing. Additionally, you can explore other integrations with Upstash Kafka, such as connecting to other messaging platforms or data sources.
I hope you found this blog informative and useful for your own projects. Thank you for reading!