Article | A sentiment engine using ChatGPT's completions API

A sentiment engine using ChatGPT's completions API

OpenAI's completions API opens up endless possibilities to integrate powerful analysis of text based data and convert it to a structured response, suitable for processing by an application.

We have recently had a number of examples where we've been able to leverage this technology. One task was to determine the sentiment of someone's comment before displaying that comment on a public website.

AI image

Traditionally, you may have been inclined to do this with an ever growing stop/allow list. This task has now been trivialised by the use of OpenAI's API.

Consider an Obituary where users can add thoughts and comments onto a public website. We need to weed out any offensive, derogatory or inappropriate comments before posting the comment on the Obituary page.

We also need to structure the result so that it is easily processable by the consuming application.

A simple script can be written to complete this task using the ChatGPT's Completions API.

comment = args[1] # users comment to be published
result = openai.ChatCompletion.create(
    model=model_id,
    messages = [
        {"role": "user", "content": comment},
        {
            "role": "system",
            "content": """The context is an Obituary
              displayed on a web page. Any one can
              add a comment onto the persons obituary.
              You are to determine if the comment is
              safe, appropriate and respectful to
              display on the website."""
        },
        {
            "role": "system",
            "content": """Please output a JSON document
              with keys `publish` and `reason`. The
              `publish` field will contain the boolean
              true or false, and the reason will be why
              it qualifies or not for the public website"""
        },
    ]
 )
print(result.choices[0].message.content)

With the user comment

Rest in peace

{
  "publish": true,
  "reason": "The comment is safe, appropriate, and respectful."
}

ChatGPT has an impressive capability to understand language semantics, evaluate the context and provide a value enhanced system result.

Experimentation is key to engineer the prompt in such a way that produces the best result. The more data you can provide in the context, the better the result.

Consider the following comment:

I am glad you lived a long and beautiful life

While this may be appropriate for an aged adult, it would be inappropriate for a child. ChatGPT was able to handle situations like this by adding the persons age into the context provided.

We can solve this by providing the subjects age into the system context as follows:

{
  "role": "system", 
  "content": "The person was {age} years old"
}

We were able to engineer ChatGPT to provide the desired response:

{
   "publish": false,
   "reason": "The comment is inappropriate as it refers to the
     deceased as a young child, implying that their life was cut short.
     Furthermore, it is not respectful to speculate on the length or 
     happiness of someone's life based solely on their age."
}

"Whenever you are dealing with unstructured data sources (images, pdfs, sound, text) AI can play a part in simplifying your solution. " Marcus Baguley

This is just a taster to help you think about the data you are processing and how that might change using generative AI and emerging API capabilities. The are many use cases you can apply this solution to.

Please chat to us if you want to know how you can best leverage AI technologies to solve your business problems.

Message sent
Message could not be sent
|