OpenAI sentiment analysis

I would like to create a sentiment analysis index on social media. I would like to understand how to use OpenAI existing models to enable the sentiment analysis.

Hi and welcome to the Developer Forum!

Can you explain a little more what you are trying to do?

Hi Foxabilo,
Thanks for getting back.
I want to apprehend how some international organisations are perceived on Twitter. I would like to perform a sentiment analysis by leveraging existing OpenAI models. At this stage, and before looking at the implementation, I need to understand how, in practice, people use those OpenAI models (e.g LLM) for their own purpose?
Best Regards,
Raphael.

You would pick your social media platform, obtain(pay) for API access to that, build a retrieval engine to pull back the specific posts and comments you are interested in for that platform using traditional code and possibly augment that with AI to decide if a post should be included and then you can run your sentiment analysis on each post, I’ll say off the bat, these things are usually non trivial to build and not cheap to run as you need one system per platform and you need to make as many API calls to the AI as there are comments.

Assuming you have the budget for that and a suitable budget for the building and maintenance (platforms are not static entities and you will have to modify interfaces over time) then you have a potentially valuable service.

If I were to hazard a guess at the size and budget required to build this then I’d say getting toward quarter to 1M side of things unless you plan on building everything yourself, but then speed and time to market comes into play.

2 Likes

Thanks for your detailed response, I find it very useful. Let me digest it - could I revert back once I get off the plane (in case I have a question)?

1 Like

Sure, your post will remain open for commenting at any time.

1 Like

When it comes to sentiment analysis, you can call OpenAI’s LLM from the API as follows and classify the results into positive, neutral, and negative categories.

This classification may not always be accurate.
If you feel the results are not accurate, you can fine-tune the model to get the desired outcomes.

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": "You will be provided with a tweet, and your task is to classify its sentiment as positive, neutral, or negative."
    },
    {
      "role": "user",
      "content": "I loved the new Batman movie!"
    }
  ],
  temperature=0.7,
  max_tokens=64,
  top_p=1
)

This only demonstrates the basic techniques that can be applied to actual social media platforms such as Twitter. Please note that building an appropriate system to collect social media data, call APIs, and perform sentiment analysis is a separate project that needs to be undertaken.

I hope this information will be of some help to you!

2 Likes

This is how I conducted my sentiment analysis:

def get_response(example):
  response = client.chat.completions.create(
    model="gpt-3.5-turbo-0125",
    messages=[
      {
        "role": "system",
        "content": "You are an expert in restaurant marketing with a specialism in sentiment analysis. \n\nI will give you some reviews and I want you to return one word based on the sentiment. Positive, Negative or Neutral. "
      },
      {
        "role": "user",
        "content": "With all the new upgrades everything looks great and clean great staff"
      },
      {
        "role": "assistant",
        "content": "Positive"
      },
      {
        "role": "user",
        "content": "It'd McDonalds. It is what it is as far as the food and atmosphere go. The staff here does make a difference. They are all friendly, accommodating and always smiling. Makes for a more pleasant experience than many other fast food places."
      },
      {
        "role": "assistant",
        "content": "Positive"
      },
      {
        "role": "user",
        "content": "We stopped by for a quick breakfast.  It was not crowded inside, but there was a pretty long drive thru.\nOur order was supposed to have 3 food items and 2 drinks. They missed a food item. The lady ignored me when I told her.My husband went to get it, she finished her task then handed it to him without a word or smile or anything."
      },
      {
        "role": "assistant",
        "content": "Negative"
      },
      {
        "role": "user",
        "content": "The morning crew seems fast and efficient. Night crew is a whole different experience, lines down the street, hour long waits. If this was a one time occurrence it wouldn't be so bad but this is a nightly problem. Beyond this the staff is so highly rude you leave wanting to crash your car through the building. Don't forget they will mistake your order almost every single time as well and serve it to you cold."
      },
      {
        "role": "assistant",
        "content": "Negative"
      },
      {
        "role": "user",
        "content": "Me and my girlfriend came tonight to pick up our food after 11pm for a mobile order, they need a better system if you are walking on foot. The food tasted great, loved the McDonald's deal ordering from the app."
      },
      {
        "role": "assistant",
        "content": "Neutral"
      },
      {
        "role": "user",
        "content": "Been frequenting this location for a few years.Morning,mid-day and night,the food is always hot,fresh and served with a smile."
      },
      {
        "role": "assistant",
        "content": "Positive"
      },
      {
        "role": "user",
        "content": "GOOD"
      },
      {
        "role": "assistant",
        "content": "Positive"
      },
      {
        "role": "user",
        "content": "Too many people around and an empte shelf"
      },
      {
        "role": "assistant",
        "content": "Negative"
      },
     {
      "role": "user",
      "content": f"""{example} """
    }
    ],
    temperature=1,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
  )
  return response.choices[0].message.content
#####
df1['GPT Sentiment'] = df1['review'].apply(get_response)

This way, I am running the call on every row iteratively, which is both computationally and monitarily inefficient for large data files. Do you have any suggestions on how I can do this in a more optimized manner?

Thank you

1 Like

very nice explanation from basics.

1 Like