DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Natural Language Processing

We are going to build a support ticket triage agent that reads unstructured customer messages, extracts named entities, classifies the issue type, and assigns an urgency score. This saves support teams from manually sorting through hundreds of daily emails or chat logs. Because the agent runs on a single API request per ticket, it is easy to scale without unpredictable token costs.

What you'll need

  • Python 3.10 or newer
  • The OpenAI SDK installed with pip install openai
  • An Oxlo.ai API key from https://portal.oxlo.ai

Step 1: Configure the Oxlo.ai client

I always start by verifying the connection. The Oxlo.ai endpoint is a drop-in replacement for the OpenAI SDK, so the setup is minimal. Replace YOUR_OXLO_API_KEY with the key from your portal.

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 hello"},
    ],
)

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

Step 2: Design the system prompt

The system prompt is the agent's instruction manual. I keep it strict: JSON output only, no markdown, with exactly four fields. This removes the need for regex parsing later.

SYSTEM_PROMPT = """You are an NLP triage engine. Analyze the user message and return a single JSON object with these keys:
- category: one of Billing, Technical, Account, or General.
- entities: an array of objects with "name" and "type" (Person, Product, or Organization).
- sentiment: one of Positive, Neutral, or Negative.
- urgency: an integer from 1 to 5, where 5 means the customer cannot work.

Rules:
- Do not include markdown code fences.
- Use only the JSON object as your response.
- If no entities are present, return an empty array."""

Step 3: Build the triage function

Now I wrap the call in a function that sends the ticket text and parses the JSON response. I use Llama 3.3 70B because it follows structured instructions reliably, but you can swap in Qwen 3 32B for multilingual tickets or Kimi K2.6 for deeper reasoning.

import json

def triage_ticket(ticket_text: str) -> dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": ticket_text},
        ],
        temperature=0.1,
    )

    raw = response.choices[0].message.content.strip()
    # Sometimes the model returns newlines before the JSON.
    return json.loads(raw)

# Test with a realistic ticket
ticket = "Acme Corp cannot access the dashboard after the v2.4 update. Sarah Johnson says the export button is broken and it is blocking their quarterly report."
result = triage_ticket(ticket)
print(json.dumps(result, indent=2))

Step 4: Batch process a backlog

Most support teams do not deal with one ticket at a time. I process a list of tickets and collect the results in a list. Because Oxlo.ai charges per request rather than per token, running this on twenty long emails costs the same as twenty short ones. That predictability matters when you are clearing a backlog.

tickets = [
    "My invoice from Widgets Inc is wrong. I was charged twice for the Pro plan.",
    "The API returns a 500 error every time I send a request with Japanese characters.",
    "Love the new design. Great work by the team at StartupXYZ.",
]

results = []
for t in tickets:
    try:
        parsed = triage_ticket(t)
        results.append(parsed)
    except json.JSONDecodeError:
        results.append({"error": "Failed to parse", "raw": t})

for r in results:
    print(r)

Step 5: Filter and alert on high urgency

Finally, I surface anything that needs immediate attention. This snippet prints only tickets with an urgency of 4 or 5, which you could wire to a Slack webhook or PagerDuty trigger.

critical = [r for r in results if r.get("urgency", 0) >= 4]

if critical:
    print(f"ALERT: {len(critical)} critical ticket(s) require immediate attention.")
    for c in critical:
        print(f"- Category: {c['category']}, Sentiment: {c['sentiment']}, Entities: {c['entities']}")
else:
    print("No critical tickets found.")

Run it

Save the script as triage.py, set your API key in the environment or directly in the client, and run python triage.py. You should see output similar to this.

$ python triage.py

{
  "category": "Technical",
  "entities": [
    {"name": "Acme Corp", "type": "Organization"},
    {"name": "Sarah Johnson", "type": "Person"}
  ],
  "sentiment": "Negative",
  "urgency": 5
}

ALERT: 1 critical ticket(s) require immediate attention.
- Category: Technical, Sentiment: Negative, Entities: [{'name': 'Acme Corp', 'type': 'Organization'}, {'name': 'Sarah Johnson', 'type': 'Person'}]

Next steps

Connect the triage agent to your support inbox via a simple webhook so new tickets are classified as they arrive. If you plan to process large volumes, look at Oxlo.ai's request-based pricing at https://oxlo.ai/pricing. It keeps long-context NLP workloads predictable because the cost does not scale with ticket length.

Top comments (0)