DEV Community

shashank ms
shashank ms

Posted on

Building Language Understanding Models with LLM: A Step-by-Step Guide

We are going to build a language understanding layer that turns raw customer support messages into structured intent and entity JSON. This kind of module is the backbone of any agentic support system, and it runs entirely on an LLM hosted through Oxlo.ai.

What you'll need

Before starting, make sure you have the following ready.

  • Python 3.10 or newer installed locally.
  • The OpenAI Python SDK. Install it with pip install openai.
  • An Oxlo.ai API key from https://portal.oxlo.ai. Oxlo.ai is a drop-in OpenAI-compatible provider, so the same SDK works without modification.

Step 1: Define the schema and system prompt

Language understanding starts with a strict contract. I write a system prompt that tells the model exactly what fields to return and what each field means. I also pin the output to JSON so downstream code can rely on the structure.

SYSTEM_PROMPT = """
You are a language understanding engine. Analyze the user's support message and return a single JSON object with these exact keys:

- intent: one of [refund_request, technical_issue, billing_question, account_access, general_inquiry]
- entities: an object containing any relevant identifiers such as order_id, email, or product_name
- urgency: one of [low, medium, high, critical]
- summary: a one-sentence summary of the user's core need
- next_action: one of [escalate_to_human, send_kb_link, process_refund, verify_identity]

Rules:
1. Do not include markdown formatting or explanation outside the JSON.
2. If an entity is missing, use null.
3. If the message is ambiguous, set intent to general_inquiry and urgency to medium.
"""

Step 2: Set up the Oxlo.ai client

Because Oxlo.ai exposes a fully OpenAI-compatible endpoint, I import the standard client and point it at Oxlo.ai. I will use the general-purpose Llama 3.3 70B model, though Oxlo.ai also offers Qwen 3 32B, Kimi K2.6, and DeepSeek V3.2 if you need multilingual or reasoning variants.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="YOUR_OXLO_API_KEY"  # Replace with your key from https://portal.oxlo.ai
)

Step 3: Build the extraction function

Now I wrap the API call in a small function. I set the temperature low to reduce creativity, and I enable JSON mode so the model knows it must emit valid JSON.

import json

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

Step 4: Add schema validation

Even with JSON mode, I want to guard against missing keys. I use Pydantic to coerce and validate the model output so my application fails loudly and cleanly if the shape drifts.

from typing import Optional
from pydantic import BaseModel, Field

class SupportIntent(BaseModel):
    intent: str
    entities: dict
    urgency: str
    summary: str
    next_action: str

def parse_support_message(text: str) -> SupportIntent:
    raw_json = extract_intent(text)
    return SupportIntent.model_validate(raw_json)

Step 5: Batch process a conversation log

In production you rarely process one message at a time. Here is a short loop that classifies three incoming tickets and prints the structured results. This is where Oxlo.ai's request-based pricing shines: each message costs one request, regardless of how long the system prompt is.

tickets = [
    "I was charged twice for order #49281 and I need my money back immediately.",
    "Hey, how do I reset my password? I cannot log in.",
    "The app crashes every time I open the dashboard on my Pixel 7. This is blocking my work.",
]

for ticket in tickets:
    result = parse_support_message(ticket)
    print(result.model_dump_json(indent=2))

Run it

Save everything in a file named intent_parser.py, export your API key, and run python intent_parser.py. You should see structured output similar to this.

{
  "intent": "billing_question",
  "entities": {"order_id": "#49281"},
  "urgency": "high",
  "summary": "Customer was double-charged and wants a refund.",
  "next_action": "process_refund"
}
{
  "intent": "account_access",
  "entities": {},
  "urgency": "medium",
  "summary": "Customer needs help resetting their password.",
  "next_action": "send_kb_link"
}
{
  "intent": "technical_issue",
  "entities": {"device": "Pixel 7"},
  "urgency": "critical",
  "summary": "App crashes on dashboard open, blocking work.",
  "next_action": "escalate_to_human"
}

Next steps

Swap in qwen-3-32b or kimi-k2.6 if you need stronger multilingual or reasoning performance for the same pipeline. Because Oxlo.ai charges per request rather than per token, expanding the system prompt or adding few-shot examples does not increase your cost. See https://oxlo.ai/pricing for plan details.

From here, you can wire this parser into a FastAPI endpoint or connect it to a message queue like Redis Streams to build a real-time support agent. The module is just plain Python, so it drops into any async worker without vendor lock-in.

Top comments (0)