Start learning free

LESSON 31 OF 129 · AI ENGINEERING: STRUCTURED OUTPUT

Classify the Message

Practice

Classification is one of the most useful jobs you can give a model: text goes in, one label from a short list comes out. The Corner Shop's support team wants every customer message in one of four boxes: refund, shipping, product or other.

A schema that only says "the category is a string" is not enough. The model then makes up a label for each message: delivery for one, shipping for the next, Refund with a capital letter. Every answer is valid JSON, but a program can't count labels like these.

An enum fixes that. It is a list of the only values a field may have. Here is one on another field, a T-shirt size:

"size": {
    "type": "string",
    "enum": ["S", "M", "L"],
}

With it, the model can only answer "S", "M" or "L", spelled exactly like that. Labels from a fixed list can be counted with a dictionary. Your finished program must print:

refund: I want my money back for the lamp. It arrived broken.
shipping: Where is my package? I ordered ten days ago.
product: Does the teapot work on an induction stove?
refund: Please refund order 3. I bought it by mistake.
shipping: Can you ship to Canada?
other: Hi! Do you have a job for a student this summer?
refund 2
shipping 2
product 1
other 1

Your task: classify already sends one message to the model with a schema. Finish it so it returns the reply's category. Loop over inbox and print each label with its message. Add an enum with the four labels to the schema. Then count the labels in a dictionary named counts and print each category with its count.


Assignment
Follow the Coding Tutorial to classify the six messages with an enum and count the messages in each category.


Hint
The reply's text is at reply.content[0].text, and json.loads turns it into a dictionary. The enum goes inside the "category" dictionary, next to "type". Start every count at 0.


STEP 1 OF 10
    Ctrl + Enter to check