Sentiment Analysis with GPT-4 Function Calling
Ryan Vanderpol
March 12, 2024
•
7 min read
Sentiment Analysis is certainly not a new concept, but with powerful modern AI tools it is becoming incredibly easy to do. Here we’ll go through what it is, common use cases for it, and how we can gather actionable data from natural language with just a few lines of code.
For those who are unfamiliar with the concept, Sentiment Analysis (sometimes called “opinion mining”) is the computational process of determining the emotional tone behind a series of words. This is used to gain an understanding of the attitudes, opinions, and emotions expressed within an online mention, be it in social media posts, reviews, forums, or other digital platforms. This emotional tone is typically categorized as positive, negative, or neutral.
GPT-4 is a Large Language Model (LLM) which, by default, returns unstructured natural language as its response. However, OpenAI recently released a new feature called Function Calling which you can use to instruct the LLM to return data in a structured manner (eg. JSON) so that the code you build to call the LLM can, in turn, pass the result off to another function with consistent results.
Using the tools we’ll discuss here, you can build automated sentiment analysis into your product or company process. Automated sentiment analysis, especially in bulk, can be a huge help for companies to prioritize product feedback and identify what customers to respond to.
Common Use Cases for Sentiment Analysis
There are a lot of use cases for performing Sentiment Analysis, but in general it is used to either get a sense of the average sentiment or trending sentiment about a topic, or to quickly identify negative sentiment that needs to be addressed across a broad dataset.
Here are some more practical examples of how sentiment analysis is used by companies today.
Brand Monitoring:
Companies use sentiment analysis to monitor social media and other online platforms to understand public opinion about their brand, products, or services. This helps in managing brand reputation and responding quickly to negative sentiment trends.
Customer Feedback Analysis:
Businesses analyze customer reviews and feedback on their products or services to glean insights about customer satisfaction and identify areas for improvement.
Market Research:
Sentiment analysis is used in market research to understand consumer attitudes towards products, services, or concepts. This can inform product development, marketing strategies, and competitive positioning.
Political Campaigns and Public Opinion:
Politicians and public organizations use sentiment analysis to gauge public opinion on policies, campaigns, and social issues. This can influence policy-making and campaign strategies.
Financial Markets:
In the financial sector, sentiment analysis of news articles, reports, and social media can predict market trends and potential stock movements. Trading performed on this information is at your own risk!
Build a Sentiment Analysis Profiler
Most of our previous articles about AI and Machine Learning tools have highlighted building in Python, which is probably the most common language used for these types of tasks, but many of the products we build use a Javascript stack, so we’ll do this one in Typescript this time.
We’ll be using OpenAI’s Function Calling, accessible via the tools
parameter. As of today, Function Calling is only accessible to specific GPT models, so we’ll be using gpt-4-1106-preview
. To use the GPT-4 model you’ll need an OpenAI account and API Key.
The code is actually fairly simple and looks as follows:
const determineSentiment = async (prompt: string): Promise<'positive' | 'negative' | 'neutral'> => {
const chatResponse = await openai.chat.completions.create({
model: "gpt-4-1106-preview",
messages: [
{
role: "user",
content: `Determine the sentiment of the following text: "${prompt}". Is it positive, negative, or neutral?`,
},
],
tools: [
{
type: "function",
function: {
name: "extractSentiment",
description: "Extract the sentiment from the user prompt.",
parameters: {
type: "object",
properties: {
sentiment: {
type: "string",
enum: ["positive", "negative", "neutral"],
description: "The sentiment of the text.",
},
},
},
},
},
],
});
const toolCalls = chatResponse.choices[0].message?.tool_calls;
if (toolCalls && toolCalls?.length > 0) {
return JSON.parse(toolCalls[0].function?.arguments).sentiment;
}
throw new Error("Failed to determine sentiment");
};
You can execute this function with a simple call like this:
const review = “This was the most delicious fried chicken sandwich I’ve ever had!”;
const sentiment: 'positive' | 'negative' | 'neutral' = await determineSentiment(review);
In this case, we should, of course, receive a result of positive
back from the AI. Function Calling and Typescript allows us to receive this data back from the LLM in a type-safe and structured way, allowing us to pass this data on to additional processes.
This is an incredibly simple example, but one that can be used to very easily farm actionable information from vast data sets. For example, perhaps negative
reviews get a ticket created in another system indicating that a customer service representative should follow up with the customer. Or maybe sentiment is tracked longitudinally to show how public sentiment for a particular product or topic trends over time.
A more complex example might be a review like the following:
Overall, I really love this product and have been using it for years. It's great! However, the new update is terrible and I hate it. I'm not sure if I will continue using it.
There is both positive and negative sentiment present in this statement and with a few adjustments to the LLM prompts we can explicitly tease out sentiment for each category; perhaps in this example we want to know how the latest product update is being received (not well, apparently).
Wrapping Up
Sentiment Analysis can give you incredibly valuable insights into your business or how the public is feeling about a topic. Getting this data in an automated way is now easier than ever using modern AI tools. Are you sitting on a backlog of customer feedback that is thousands of messages long? Let’s build an AI model that can translate it to actionable feedback for your team.
The team at Beta Acid is solving all kinds of interesting problems using Machine Learning. What can we help you build?
SHARE THIS STORY
Get in touch
Whether your plans are big or small, together, we'll get it done.
Let's get a conversation going. Shoot an email over to projects@betaacid.co, or do things the old fashioned way and fill out the handy dandy form below.