Classification
Text Classification using LLM
This tutorial showcases how to implement text classification tasks—specifically, single-label and multi-label classifications—using LLM (via OpenAI API), PHP’s enums
and classes.
!!! tips “Motivation”
Text classification is a common problem in many NLP applications, such as spam detection or support ticket categorization. The goal is to provide a systematic way to handle these cases using language models in combination with PHP data structures.
Single-Label Classification
Defining the Structures
For single-label classification, we first define an enum
for possible labels and a PHP class for the output.
Classifying Text
The function classify
will perform the single-label classification.
Testing and Evaluation
Let’s run an example to see if it correctly identifies a spam message.
Multi-Label Classification
Defining the Structures
For multi-label classification, we introduce a new enum class and a different PHP class to handle multiple labels.
Classifying Text
The function multi_classify
executes multi-label classification using LLM.
Testing and Evaluation
Finally, we test the multi-label classification function using a sample support ticket.