Building a Spam Classifier with BAML

In this tutorial, you’ll learn how to create a simple but effective spam classifier using BAML and OpenAI’s GPT models. By the end, you’ll have a working classifier that can distinguish between spam and legitimate messages.

Prerequisites

  • Basic understanding of BAML syntax
  • Access to OpenAI API (you’ll need an API key)

Step 1: Define the Classification Schema

First, let’s define what our classification output should look like. Create a new file called spam_classifier.baml and add the following schema:

1enum MessageType {
2 SPAM
3 NOT_SPAM
4}

This schema defines a simple classification with two possible labels: SPAM or NOT_SPAM.

Step 2: Create the Classification Function

Next, we’ll create a function that uses GPT-4 to classify text. Add this to your spam_classifier.baml file:

1function ClassifyText(input: string) -> MessageType {
2 client "openai/gpt-4o-mini"
3 prompt #"
4 Classify the message.
5
6 {{ ctx.output_format }}
7
8 {{ _.role("user") }}
9
10 {{ input }}
11 "#
12}

Let’s break down what this function does:

  • Takes an input as a string
  • Uses the gpt-4o-mini model
  • Provides clear guidelines for classification in the prompt
  • Returns a MessageType

Step 3: Test the Classifier

To ensure our classifier works correctly, let’s add some test cases:

1test BasicSpamTest {
2 functions [ClassifyText]
3 args {
4 input "Buy cheap watches now! Limited time offer!!!"
5 }
6}
7
8test NonSpamTest {
9 functions [ClassifyText]
10 args {
11 input "Hey Sarah, can we meet at 3 PM tomorrow to discuss the project?"
12 }
13}

This is what it looks like in the BAML Playground:

Try it yourself in the Interactive Playground!

Now that you have your classifier set up, try it with your own examples. Here are some messages you can test:

  1. “Meeting at 2 PM in the conference room”
  2. “CONGRATULATIONS! You’ve won $1,000,000!!!”
  3. “Can you review the document I sent yesterday?”
  4. “Make money fast! Work from home!!!”

Next Steps

  • Experiment with different prompt templates to improve accuracy
  • Add more spam indicators to the classification criteria
  • Create a more complex classification schema with confidence scores
  • Try using different GPT models to compare performance

Multi-Label Classification

While the spam classifier demonstrates single-label classification (where each input belongs to exactly one category), many real-world problems require multiple labels. Let’s build a support ticket classifier that can assign multiple relevant categories to each ticket.

Step 1: Define the Label Enum and Schema

Create a new file called ticket_classifier.baml and define the possible ticket categories as an enum:

1enum TicketLabel {
2 ACCOUNT
3 BILLING
4 GENERAL_QUERY
5}
6
7class TicketClassification {
8 labels TicketLabel[]
9}

Notice how this schema differs from our spam classifier:

  • We use an enum to define valid labels
  • The labels field is an array (TicketLabel[]), allowing multiple labels per ticket

Step 2: Create the Multi-Label Classification Function

Add the classification function to your ticket_classifier.baml file:

1function ClassifyTicket(ticket: string) -> TicketClassification {
2 client "openai/gpt-4o-mini"
3 prompt #"
4 You are a support agent at a tech company. Analyze the support ticket and select all applicable labels.
5
6 {{ ctx.output_format }}
7
8 {{ _.role("user") }}
9
10 {{ ticket }}
11 "#
12}

Key differences from the spam classifier:

  • The prompt includes examples showing both single and multiple labels
  • Examples demonstrate how labels can overlap
  • The model is instructed to consider all applicable labels

Step 3: Test Multi-Label Classification

Add test cases that cover both single-label and multi-label scenarios:

1test ClassifyTicketSingleLabel {
2 functions [ClassifyTicket]
3 args {
4 ticket "I need help resetting my password"
5 }
6}
7
8test ClassifyTicketMultiLabel {
9 functions [ClassifyTicket]
10 args {
11 ticket "My account is locked and I can't access my billing information"
12 }
13}

This is what it looks like in the BAML Playground:

Try it yourself!

Test the multi-label classifier with these examples:

  1. “How do I upgrade my subscription plan?”
  2. “I forgot my password and need to update my payment method”
  3. “What are the features included in the premium plan?”
  4. “My account is showing incorrect billing history”

Tips for Multi-Label Classification

  1. Balanced Examples: Include examples in your prompt that show both single and multiple labels
  2. Clear Descriptions: Add descriptive annotations to help the model understand each label
  3. Test Edge Cases: Include test cases that verify the model can handle:
    • Single label cases
    • Multiple label cases
    • Edge cases where no labels apply