Skip to content

1.2 Classification

Understanding SYSTEM and USER in AI Conversations

For a classification example, you'd typically want to provide the model with a scenario where it needs to classify a given text into predefined categories. Here's how you could set up system_message and user_message for such a task:

  • system_message: This part should explain the task at hand, including the categories available for classification. It sets the context for what the AI is expected to do.

  • user_message: This should contain the text that needs to be classified. It's the input based on which the AI will make its classification.

Given this setup, let's create an example where the task is to classify customer feedback into categories such as "Positive", "Negative", or "Neutral".

System Message

system_message = """Classify the customer feedback into one of the following categories: Positive, Negative, or Neutral."""

User Message For the user_message, let's create a piece of feedback that needs classification:

user_message = """I recently purchased a product from your store. The shopping experience was fantastic, and the product quality exceeded my expectations!"""

How it works

When interacting with OpenAI's GPT models via the Chat Completion API, messages within the conversation are structured to accurately simulate an exchange between a user and the AI system. Each message is represented as a dictionary with at least two key components: role and content. The role key specifies the originator of the message, while the content key contains the actual text of the message. Distinguishing between {'role': 'system'} and {'role': 'user'} is crucial for designing interactions that elicit the desired response from the AI model.

{'role': 'user'}

Messages tagged with {'role': 'user'} signify that the message is coming from the user's perspective, akin to the user asking a question or making a statement to the AI. These are typically queries, commands, or inputs that the AI is expected to respond to. By marking a message as originating from the user, you're simulating a user's part in the conversation, guiding the AI on how to frame its response.

{'role': 'system'}

Conversely, messages marked with {'role': 'system'} provide instructions, context, or constraints to the AI model, simulating system-generated messages. These can dictate the AI's behavior, personality, or response style, such as instructing the AI to respond in the style of a specific author or adhere to certain guidelines. System messages influence how the AI understands and responds to subsequent user messages.

The sequence of messages between system and user roles guides the AI model in generating contextually relevant responses, aligned with both direct user inputs and overarching system instructions. This dynamic exchange allows the AI to adapt its responses based on the provided context and instructions, enabling more nuanced and interactive conversations.

Example

Consider a scenario where you want the AI to generate a poem about a happy carrot in a whimsical style. The message sequence might look like this:

  1. System Message: Sets the context or provides instructions.
  2. {'role': 'system', 'content': "You are an assistant that responds in the style of a whimsical poet."}
  3. User Message: Poses a query or request.
  4. {'role': 'user', 'content': "Write me a very short poem about a happy carrot."}

This conversation flow allows for crafting nuanced and contextually aware interactions with AI, enabling creative and effective use of language models across various applications.

Classification Example

Here's how you could assemble the complete example with these messages:

import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())  # read local .env file

openai.api_key = os.environ['OPENAI_API_KEY']

def classify(messages, model="gpt-3.5-turbo", temperature=0, max_tokens=500):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
        max_tokens=max_tokens,
    )
    return response.choices[0].message["content"]

delimiter = "####"
system_message = """Classify the customer feedback into one of the following categories: Positive, Negative, or Neutral."""

user_message = """I recently purchased a product from your store. The shopping experience was fantastic, and the product quality exceeded my expectations!"""

messages = [  
    {'role':'system', 'content': system_message},    
    {'role':'user', 'content': f"{delimiter}{user_message}{delimiter}"},  
] 

response = classify(messages)
print(response)

More Classification Examples

Below are 10 examples of prompts designed for classification tasks, covering a variety of topics and categories. Each example includes a brief description of the task and the categories to classify into, followed by a specific system_message and a generic user_message that would be classified:

1. Email Classification

Categories: Work, Personal, Spam

  • System Message: "Classify the following email as Work, Personal, or Spam."
  • User Message: "Great deals on our latest electronics! Click now to save big."

2. Movie Review Sentiment Analysis

Categories: Positive, Negative, Neutral

  • System Message: "Determine if the sentiment of the following movie review is Positive, Negative, or Neutral."
  • User Message: "The film had stunning visuals, but the storyline was predictable and lacked depth."

3. News Article Topic Classification

Categories: Politics, Technology, Sports, Entertainment

  • System Message: "Classify the topic of the news article as Politics, Technology, Sports, or Entertainment."
  • User Message: "The latest smartphone model features groundbreaking technology that's set to change the industry."

4. Product Review Rating

Categories: 1 Star, 2 Stars, 3 Stars, 4 Stars, 5 Stars

  • System Message: "Based on the content of the product review, classify it into a rating category from 1 Star to 5 Stars."
  • User Message: "While the product design is innovative, the frequent malfunctions and poor customer service make it hard to recommend."

5. Customer Inquiry Intent

Categories: Billing, Technical Support, Sales, General Inquiry

  • System Message: "Identify the intent of the customer inquiry as Billing, Technical Support, Sales, or General Inquiry."
  • User Message: "Can you explain the different plans available and any ongoing promotions?"

6. Text Genre Identification

Categories: Fiction, Non-Fiction, Poetry, News

  • System Message: "Identify the genre of the following text as Fiction, Non-Fiction, Poetry, or News."
  • User Message: "In the heart of the city, amidst the bustling streets, there lay a quiet garden untouched by time."

7. Social Media Post Tone

Categories: Serious, Humorous, Inspirational, Angry

  • System Message: "Classify the tone of the following social media post as Serious, Humorous, Inspirational, or Angry."
  • User Message: "Nothing beats starting the day with a smile. Remember, happiness is contagious!"

8. Research Paper Field

Categories: Biology, Computer Science, Psychology, Mathematics

  • System Message: "Classify the field of the following research paper abstract as Biology, Computer Science, Psychology, or Mathematics."
  • User Message: "This study explores the algorithmic complexity of sorting mechanisms and their computational efficiency."

9. Food Review Flavor Profile

Categories: Sweet, Salty, Sour, Bitter, Umami

  • System Message: "Classify the flavor profile described in the food review as Sweet, Salty, Sour, Bitter, or Umami."
  • User Message: "The dish presented a perfect balance of umami, with a subtle hint of sweetness that enhanced the overall taste."

10. Emergency Call Type

Categories: Fire, Medical, Crime, Other

  • System Message: "Classify the type of emergency based on the following call transcript as Fire, Medical, Crime, or Other."
  • User Message: "There's a lot of smoke coming from the building next door, and we can see flames. Please send help quickly!"

For each of these examples, you can adjust the user_message to fit the specific scenario you want to classify, ensuring the AI model is provided with a clear context and task in the system_message.

Theory questions:

  1. What are the key components of a message in the context of interacting with OpenAI's GPT models, and why is distinguishing between them important?
  2. Explain how the role of 'system' messages differs from that of 'user' messages in AI conversations.
  3. Provide an example of how a 'system' message can dictate the AI's behavior or response style.
  4. How does the sequence of messages between 'system' and 'user' roles influence the AI model's response?
  5. In the classification task example provided, what categories are available for classifying customer feedback?
  6. Describe a scenario where classifying the sentiment of a movie review would be beneficial. What categories could be used for classification?
  7. How can classifying the topic of a news article help in content management or recommendation systems? Give examples of categories that could be used.
  8. Discuss the importance of classifying customer inquiries in a business setting. What categories could be used to streamline customer service processes?
  9. Explain the role of 'user_message' in the context of AI classification tasks. How should it be structured to aid the AI in making accurate classifications?
  10. How does classifying the tone of social media posts benefit content moderation or marketing strategies? Provide examples of tone categories that could be used.