Nowadays, the task of text classification is something trivial. For example, 10 years ago, we could not have even imagined that it would become an everyday task to optimize certain business processes.

What is the complexity of the task?

Let's imagine a certain task, to classify news that is related to real estate. We are engaged in selling/buying and it is important for us to be aware of new laws. We need a dataset on which we can train a model. This is where the big complex problem arises. How do we label the data for training? We can of course mark the data using keywords or other algorithms, but keep in mind that the quality of the data affects the accuracy of the final model.

What has changed? 

The advent of reasonably precise large language models (LLMs) enables us to label data with high accuracy. The cherry on the cake will be, the ability to generate training data from the air - literally. So we can ask the model to “classify”, and “generate”.

When do you need your model?

Of course, you have already asked yourself the question, why not use LLM as a basis for classification and it's not a bad idea. The main disadvantages of this approach will be: dependent accuracy (the model should be tested for different tasks), confidentiality, and cost. The bottom line is that if we have an ongoing task, some process where it has to be done consistently, accurately, quickly, and cheaply - our own model.

LLM classification example:

import abc
from typing import Any
from functools import lru_cache

from openai import OpenAI, LengthFinishReasonError
from pydantic import BaseModel

# Base interface
class BaseLLMClient(abc.ABC):

    @classmethod
    @abc.abstractmethod
    def execute_prompt(
        cls, prompt: str,
        model="gpt-3.5-turbo", timeout=60
    ) -> dict:
        raise NotImplementedError

# OpenAI implementation
@lru_cache(maxsize=None)
def get_client():
    return OpenAI(
        api_key="PUT_KEY",
    )
    
class GPTResponse(BaseModel):
    output: Any

class GPTBooleanResponse(GPTResponse):
    output: bool

class ChatGPT(BaseLLMClient):
    @classmethod
    def execute_prompt(
        cls, prompt: str, model="gpt-3.5-turbo", timeout=60, resp_type=GPTBooleanResponse,
        top_p=0.9, temperature=0.15,
    ) -> GPTBooleanResponse:
        client = get_client()
        response = client.beta.chat.completions.parse(
            messages=[{"role": "system", "content": prompt}],
            model=model,
            n=1,
            response_format=resp_type,
            timeout=timeout,
            top_p=top_p,
            temperature=temperature,
            frequency_penalty=0,
            presence_penalty=0,
        )
        return response.choices[0].message.parsed

PROMPT = """
You are an assistant designed to classify news content based on its relevance to real estate. Your task is to categorize the given text into two boolean categories:

- `true` — if the text is a real estate news article (i.e., it directly relates to real estate topics such as property sales, market trends, housing, commercial real estate, etc.).
- `false` — if the text is not related to real estate news or if it is not an article/news at all.

---

**INSTRUCTIONS:**

1. **Input:** Receive a piece of text that may represent a news article or general content.

2. **Classification:** Analyze the content carefully and determine whether it qualifies as real estate news:
   - Return `true` only if it is clearly about real estate.
   - Return `false` if it is unrelated, ambiguous, lacks enough information, or not an article/news.
   
3. **Output Format:** Return **only** a JSON object with a single key `"output"` holding the boolean classification value. No extra explanation or formatting beyond JSON is needed.  
   **Example output:**
   ```json
   {{
     "output": true
   }}
   ```
---

**TEXT FOR CLASSIFICATION:**  
``` 
{text} 
```
"""

EXAMPLE_ARTICLE = """
Europe’s real estate sector is recovering at pace following years of subdued activity, with investment volumes rising by a quarter over the past 12 months, according to new research from commercial property group CBRE.

Investment in European real estate rose 6% annually to 45 billion euros ($51 billion) in the first quarter of 2025 as improved macroeconomic sentiment and lower interest rates took hold. Investment volumes were up 25% annually over the year to 213 billion euros.
"""

ChatGPT.execute_prompt(prompt=PROMPT.format(text=EXAMPLE_ARTICLE), model="gpt-4.1-mini")
# Output: GPTBooleanResponse(output=True)


Bert-based classification example:

- Tensor Flow
- Hugging Face


Conclusion

For most text classification tasks - we can generate on-the-fly training data and create a suitable model for the task. In the case where we need a one-time or non-permanent task, using LLM is a very suitable tool to use