Classification of Customer queries

Tutorial 2

Jan Kirenz

Classification of Customer queries

Learn how to classify different customer queries

Setup

Python

import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())  

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

Helper function

def get_completion_from_messages(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"]

System message

delimiter = "####"

system_message = f"""
You will be provided with customer service queries. \
The customer service query will be delimited with \
{delimiter} characters.
Classify each query into a primary category \
and a secondary category. 
Provide your output in json format with the \
keys: primary and secondary.

Primary categories: Billing, Technical Support, \
Account Management, or General Inquiry.

Billing secondary categories:
Unsubscribe or upgrade
Add a payment method
Explanation for charge
Dispute a charge

Technical Support secondary categories:
General troubleshooting
Device compatibility
Software updates

Account Management secondary categories:
Password reset
Update personal information
Close account
Account security

General Inquiry secondary categories:
Product information
Pricing
Feedback
Speak to a human

"""

User message 1

user_message = f"""\
I want you to delete my profile and all of my user data"""
messages = [
    {'role': 'system',
     'content': system_message},
    {'role': 'user',
     'content': f"{delimiter}{user_message}{delimiter}"},
]

Output

response = get_completion_from_messages(messages)
print(response)
  • Output: {“primary”: “Account Management”, “secondary”: “Close account”}

User message 2

user_message = f"""\
Tell me more about your flat screen tvs"""
messages = [
    {'role': 'system',
     'content': system_message},
    {'role': 'user',
     'content': f"{delimiter}{user_message}{delimiter}"},
]

Output

response = get_completion_from_messages(messages)
print(response)

{ “primary”: “General Inquiry”, “secondary”: “Product information” }

Acknowledgments

This tutorial is mainly based on the excellent course “Building Systems with the ChatGPT API” provided by Isa Fulford from OpenAI and Andrew Ng from DeepLearning.AI

What’s next?

Congratulations! You have completed this tutorial 👍

Next, you may want to go back to the lab’s website