VOOZH about

URL: https://dev.to/shashank_ms_6a35baa4be138/llm-for-text-classification-23bn

⇱ LLM for Text Classification - DEV Community


We are going to build a zero-shot text classifier that routes customer support tickets into categories like Billing, Technical, and Account. This is for teams that need to automate triage without maintaining a dedicated ML pipeline or labeling thousands of examples.

What you'll need

Before starting, grab an Oxlo.ai API key from https://portal.oxlo.ai. You also need Python 3.10 or newer and the OpenAI SDK installed with pip install openai.

Step 1: Set up the Oxlo.ai client

I always start by verifying the connection with a simple call. This confirms the API key and base URL are correct before I build logic around it.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
 model="llama-3.3-70b",
 messages=[{"role": "user", "content": "Say OK"}],
 max_tokens=10
)

print(response.choices[0].message.content)

Step 2: Define the system prompt and schema

The system prompt is the entire classifier. I constrain the output to a JSON object with a label, a confidence score, and a short reason so the result is parseable and deterministic.

SYSTEM_PROMPT = """You are a text classification engine.
Analyze the user message and classify it into exactly one of these categories:
- Billing: payment issues, invoices, refunds, subscription changes
- Technical: bugs, errors, crashes, integration problems
- Account: login issues, password resets, profile updates, user management
- General: anything that does not fit the above

Respond with a JSON object containing:
- "category": one of the four labels above
- "confidence": a float between 0.0 and 1.0
- "reason": a one-sentence explanation for the classification

Do not include markdown formatting or any text outside the JSON object."""

Step 3: Build the classifier function

I wrap the API call in a function that accepts a text string and returns a Python dict. I use JSON mode to enforce valid output and keep temperature low for consistency.

import json

def classify_text(text: str) -> dict:
 response = client.chat.completions.create(
 model="llama-3.3-70b",
 messages=[
 {"role": "system", "content": SYSTEM_PROMPT},
 {"role": "user", "content": text},
 ],
 response_format={"type": "json_object"},
 temperature=0.1,
 max_tokens=150
 )
 
 raw = response.choices[0].message.content
 return json.loads(raw)

# Test with a single example
result = classify_text("I was charged twice last month and need a refund.")
print(json.dumps(result, indent=2))

Step 4: Batch process a dataset

In practice, classification runs over a list of inputs. I loop through a batch, collect results, and handle any API errors so one bad request does not kill the job.

tickets = [
 "My account password reset email never arrives.",
 "The API returns a 500 error when I submit large payloads.",
 "Can I upgrade my plan and get an invoice for last quarter?",
 "Hello, I hope you are having a nice day."
]

results = []
for ticket in tickets:
 try:
 label = classify_text(ticket)
 results.append({"ticket": ticket, **label})
 except Exception as e:
 results.append({"ticket": ticket, "error": str(e)})

for r in results:
 print(r)

Run it

Save the full script as classifier.py, set your API key, and run it. You should see structured JSON output for every ticket. Here is the complete file and what the output looks like on my end.

from openai import OpenAI
import json

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a text classification engine.
Analyze the user message and classify it into exactly one of these categories:
- Billing: payment issues, invoices, refunds, subscription changes
- Technical: bugs, errors, crashes, integration problems
- Account: login issues, password resets, profile updates, user management
- General: anything that does not fit the above

Respond with a JSON object containing:
- "category": one of the four labels above
- "confidence": a float between 0.0 and 1.0
- "reason": a one-sentence explanation for the classification

Do not include markdown formatting or any text outside the JSON object."""

def classify_text(text: str) -> dict:
 response = client.chat.completions.create(
 model="llama-3.3-70b",
 messages=[
 {"role": "system", "content": SYSTEM_PROMPT},
 {"role": "user", "content": text},
 ],
 response_format={"type": "json_object"},
 temperature=0.1,
 max_tokens=150
 )
 return json.loads(response.choices[0].message.content)

tickets = [
 "My account password reset email never arrives.",
 "The API returns a 500 error when I submit large payloads.",
 "Can I upgrade my plan and get an invoice for last quarter?",
 "Hello, I hope you are having a nice day."
]

for ticket in tickets:
 result = classify_text(ticket)
 print(f"{result['category']:12} | {result['confidence']:.2f} | {ticket[:50]}...")

Example output:

Account | 0.95 | My account password reset email never arrives....
Technical | 0.92 | The API returns a 500 error when I submit large...
Billing | 0.89 | Can I upgrade my plan and get an invoice for la...
General | 0.78 | Hello, I hope you are having a nice day....

Wrap-up

Two concrete ways to push this further. First, add a few-shot examples to the system prompt if you need finer granularity, such as distinguishing Security from Technical. Second, wire this function into an async worker so you can classify thousands of tickets without blocking. Oxlo.ai handles the inference, and because it uses flat per-request pricing, your cost stays predictable even when the input tickets are long. See the details at https://oxlo.ai/pricing.