DEV Community

shashank ms
shashank ms

Posted on

Applications of LLM in Natural Language Processing

We will build a single-pass NLP analysis agent that takes raw text and returns structured results: summary, named entities, sentiment, topics, and a Spanish translation. This replaces five separate traditional NLP pipelines with one Oxlo.ai request, which is ideal for preprocessing documents, support tickets, or news feeds.

What you'll need

Step 1: Initialize the Oxlo.ai client

I instantiate the client exactly like the OpenAI SDK, just pointing the base URL at Oxlo.ai. Replace the placeholder with your key from the portal.

from openai import OpenAI

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

Step 2: Lock down the system prompt

The system prompt forces the model to behave like a deterministic NLP backend and emit only the JSON schema we expect. I also enable JSON mode in the API call, so malformed output is rare.

SYSTEM_PROMPT = """You are a precise NLP analysis engine.
Given any user text, respond with a single JSON object containing exactly these keys:
- summary: a concise summary of at most three sentences.
- entities: an array of objects with "text" and "type" (e.g., Person, Organization, Location).
- sentiment: an object with "label" (positive, negative, neutral) and "score" (0.0 to 1.0).
- topics: an array of up to five descriptive topic tags.
- translation_es: a faithful Spanish translation of the original text.

Output raw JSON only. Do not wrap it in markdown code fences."""

Step 3: Build the analysis function

This function sends the text to Oxlo.ai with JSON mode enabled, then parses the result. I use Llama 3.3 70B here because it handles mixed instructions reliably, but you can swap in qwen-3-32b if you are working primarily with multilingual sources.

import json

def analyze_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,
    )

    raw = response.choices[0].message.content
    return json.loads(raw)

Step 4: Feed it a sample document

I use a short news snippet so the output is easy to verify. In production you would stream files from S3 or a database.

SAMPLE_TEXT = """
Apple Inc. is reportedly in talks to acquire a small AI startup based in Toronto.
The deal, valued at roughly $150 million, would give Apple access to new on-device
language models. Investors reacted positively, sending Apple shares up 2% in early
trading on Thursday.
"""

Run it

Call the function and print the parsed JSON. Because Oxlo.ai uses flat per-request pricing, running this on a thousand long documents costs the same whether each prompt is one hundred tokens or ten thousand tokens. See https://oxlo.ai/pricing for current plan details.

if __name__ == "__main__":
    result = analyze_text(SAMPLE_TEXT)
    print(json.dumps(result, indent=2, ensure_ascii=False))

When I ran this, the output looked like this:

{
  "summary": "Apple is negotiating a $150 million acquisition of a Toronto AI startup to bolster its on-device language models, and its stock rose 2%.",
  "entities": [
    {"text": "Apple Inc.", "type": "Organization"},
    {"text": "Toronto", "type": "Location"},
    {"text": "$150 million", "type": "MonetaryValue"},
    {"text": "Thursday", "type": "Date"}
  ],
  "sentiment": {
    "label": "positive",
    "score": 0.85
  },
  "topics": [
    "mergers and acquisitions",
    "artificial intelligence",
    "on-device ML",
    "stock market"
  ],
  "translation_es": "Apple Inc. está supuestamente en conversaciones para adquirir una pequeña startup de IA con sede en Toronto. El acuerdo, valorado en aproximadamente 150 millones de dólares, le daría a Apple acceso a nuevos modelos de lenguaje en el dispositivo. Los inversores reaccionaron positivamente, enviando las acciones de Apple hasta un 2% en las primeras operaciones del jueves."
}

Next steps

Wire this function into a FastAPI endpoint and process uploaded PDFs or HTML pages. If you are batching large context windows, try switching the model to kimi-k2.6 or deepseek-v3.2 on Oxlo.ai, where request-based pricing keeps long-document costs predictable regardless of token count.

Top comments (0)