DEV Community

shashank ms
shashank ms

Posted on

LLM for Natural Language Processing in Text Analysis

We are going to build a batch feedback analyzer that turns raw customer comments into structured JSON. It extracts sentiment, themes, urgency, and action items using an LLM. This helps product and support teams spot problems without reading every ticket manually.

What you'll need

Step 1: Set up the client and sample data

I keep a small list of real-looking feedback strings so we can iterate quickly without external files. We initialize the OpenAI-compatible client pointing at Oxlo.ai.

from openai import OpenAI

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

# Sample raw feedback for analysis
feedback_batch = [
    "The checkout process was smooth but shipping took two weeks. Not acceptable.",
    "Love the new dashboard. It is intuitive and fast.",
    "App crashed when I tried to export my report. Please fix this ASAP.",
    "Documentation is confusing. I could not find the API rate limits anywhere.",
]

Step 2: Define the system prompt and analysis function

The prompt locks the model into returning only valid JSON with fixed keys. I use llama-3.3-70b because it follows structured instructions reliably at low temperature. A small cleanup helper strips any accidental markdown fences before parsing.

import json

SYSTEM_PROMPT = """You are a precise text analysis engine. Analyze the user feedback and return a single JSON object with these exact keys:
- sentiment: one of [Positive, Negative, Neutral]
- themes: array of up to 3 short topic labels
- urgency: one of [Low, Medium, High]
- action_item: one sentence describing what the business should do

Rules:
- Output ONLY valid JSON. No markdown, no explanations.
- Be concise. Labels should be 1-3 words."""

def analyze_feedback(text: str):
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": text},
        ],
        temperature=0.1,
    )
    
    raw = response.choices[0].message.content.strip()
    # Clean up accidental markdown fences
    if raw.startswith("

```"):
        raw = raw.split("\n", 1)[1].rsplit("```

", 1)[0].strip()
    return json.loads(raw)

Step 3: Process the feedback batch

We loop through the feedback list, call the analyzer for each entry, and catch parse errors so one bad response does not kill the run. Because Oxlo.ai uses flat request-based pricing, detailed at https://oxlo.ai/pricing, running this over hundreds of long tickets does not increase cost with input length.

results = []
for text in feedback_batch:
    try:
        parsed = analyze_feedback(text)
        parsed["source"] = text
        results.append(parsed)
    except Exception as e:
        print(f"Failed on: {text[:50]}... Error: {e}")

print(f"Processed {len(results)} entries successfully.")

Step 4: Aggregate and report

A few lines of standard library code tally sentiments, count themes, and surface every medium or high urgency action item.

from collections import Counter

sentiments = Counter(r["sentiment"] for r in results)
urgencies = Counter(r["urgency"] for r in results)
all_themes = [t for r in results for t in r["themes"]]
top_themes = Counter(all_themes).most_common(5)

print("=== Feedback Analysis Report ===")
print(f"Sentiment distribution: {dict(sentiments)}")
print(f"Urgency distribution: {dict(urgencies)}")
print("Top themes:")
for theme, count in top_themes:
    print(f"  - {theme}: {count}")

print("\n=== Action Items ===")
for r in results:
    if r["urgency"] in ["Medium", "High"]:
        print(f"[{r['urgency']}] {r['action_item']}")

Run it

Save everything in feedback_analyzer.py, replace YOUR_OXLO_API_KEY, and run python feedback_analyzer.py. You should see a concise report like this.

Processed 4 entries successfully.
=== Feedback Analysis Report ===
Sentiment distribution: {'Negative': 2, 'Positive': 1, 'Neutral': 1}
Urgency distribution: {'High': 2, 'Low': 1, 'Medium': 1}
Top themes:
  - shipping: 2
  - dashboard: 1
  - app crash: 1
  - documentation: 1

=== Action Items ===
[High] Investigate and resolve the app crash during report export.
[High] Review shipping logistics to reduce delivery times.
[Medium] Clarify API rate limit documentation in the developer docs.

Next steps

Swap the hardcoded list for a daily CSV export from your support desk and schedule the script with cron or GitHub Actions. If your feedback arrives in multiple languages, switch the model to qwen-3-32b or kimi-k2.6 on Oxlo.ai and keep the same prompt.

Top comments (0)