DEV Community

shashank ms
shashank ms

Posted on

Practical Applications of LLM in Natural Language Processing

In this tutorial I am building a support ticket NLP agent that reads raw customer messages, classifies intent, extracts entities, scores sentiment, and drafts a structured response. It helps support teams buried in unstructured text get immediate triage. We will wire everything to Oxlo.ai so you pay per request, not per token, which keeps costs flat even when tickets carry long conversation history.

What you'll need

Python 3.10 or newer, the OpenAI SDK, and an Oxlo.ai API key from https://portal.oxlo.ai. Install the SDK with pip.

pip install openai

Step 1: Setup and system prompt

First, I instantiate the client pointing at Oxlo.ai and define the system prompt that grounds the agent. I use Llama 3.3 70B as the general-purpose workhorse.

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 support ticket NLP processor. Your job is to analyze raw customer messages and output structured JSON.

Follow these rules:
1. Classify the ticket into one of: Billing, Technical, Account, General.
2. Extract entities: product_name, account_id, and any dates mentioned.
3. Score sentiment as Positive, Neutral, or Negative.
4. Score urgency as Low, Medium, or High.
5. Draft a brief internal note summarizing the next step.

Return ONLY valid JSON with keys: category, entities, sentiment, urgency, internal_note."""

Step 2: Ingest and preprocess raw text

I use a small helper that accepts a raw ticket string, trims excessive whitespace, and passes it to the model. This keeps the pipeline clean.

def preprocess_ticket(raw_text: str) -> str:
    # collapse whitespace and strip noise
    cleaned = " ".join(raw_text.split())
    return cleaned

raw_ticket = """
Hi, my AcmeWidget 3000 stopped working yesterday.  
I tried rebooting but the LED is still red.  
My account is AC-99442-BX and I need this fixed before Friday's demo.  
This is really frustrating.
"""

Step 3: Extract structured features

Now I call Oxlo.ai with JSON mode enabled so the model returns parseable output. Because Oxlo.ai charges per request, attaching a long ticket history does not inflate the cost.

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

ticket_text = preprocess_ticket(raw_ticket)
result = analyze_ticket(ticket_text)
print(json.dumps(result, indent=2))

Step 4: Generate a draft reply

With structured data in hand, I run a second request to produce a customer-facing response. I switch to Qwen 3 32B for multilingual fluency in case the ticket arrives in another language.

def draft_reply(ticket_text: str, analysis: dict) -> str:
    reply_prompt = f"""You are a senior support agent. Write a concise, empathetic reply to the customer.
Ticket context: {ticket_text}
Analysis: {json.dumps(analysis)}

Rules:
- Acknowledge the specific issue.
- Reference the product and timeline if mentioned.
- Offer the exact next step.
- Keep it under 120 words."""

    response = client.chat.completions.create(
        model="qwen-3-32b",
        messages=[
            {"role": "system", "content": "You write customer support replies."},
            {"role": "user", "content": reply_prompt},
        ],
    )
    
    return response.choices[0].message.content.strip()

reply = draft_reply(ticket_text, result)
print(reply)

Step 5: Wrap the pipeline

Finally, I bundle both stages into one callable agent that returns the structured analysis and the draft reply together.

class TicketAgent:
    def __init__(self, client: OpenAI):
        self.client = client

    def process(self, raw_text: str) -> dict:
        cleaned = preprocess_ticket(raw_text)
        analysis = analyze_ticket(cleaned)
        reply = draft_reply(cleaned, analysis)
        return {
            "analysis": analysis,
            "draft_reply": reply,
            "model_pipeline": ["llama-3.3-70b", "qwen-3-32b"],
        }

agent = TicketAgent(client)
output = agent.process(raw_ticket)
print(json.dumps(output, indent=2))

Run it

Save the complete script as ticket_agent.py, export your key, and execute it.

export OXLO_API_KEY="sk-oxlo.ai-..."
python ticket_agent.py

Expected output looks like this:

{
  "analysis": {
    "category": "Technical",
    "entities": {
      "product_name": "AcmeWidget 3000",
      "account_id": "AC-99442-BX",
      "dates": ["Friday"]
    },
    "sentiment": "Negative",
    "urgency": "High",
    "internal_note": "Escalate to hardware team for red LED failure before Friday demo."
  },
  "draft_reply": "Thank you for reaching out, and I am sorry your AcmeWidget 3000 is showing a red LED after rebooting. I see your account is AC-99442-BX and that you have a demo on Friday. I am escalating this to our hardware team now and will send you a replacement unit overnight so you are covered before your demo.",
  "model_pipeline": ["llama-3.3-70b", "qwen-3-32b"]
}

Next steps

Wire the agent into a FastAPI endpoint so it processes tickets from your CRM webhooks in real time. If you need deeper reasoning for complex policy checks, swap the analysis step to Kimi K2.6 on Oxlo.ai without changing any client code. For pricing details, see https://oxlo.ai/pricing.

Top comments (0)