DEV Community

shashank ms
shashank ms

Posted on

Unlocking the Potential of LLMs in Natural Language Processing

We will build a Document Intelligence Agent that runs multiple NLP tasks, summarization, entity extraction, sentiment analysis, and theme detection, over unstructured text in a single API call. This is useful for support teams, researchers, and product managers who need to process long documents without maintaining separate pipelines for each task. Because Oxlo.ai charges a flat rate per request rather than per token (see https://oxlo.ai/pricing), running this on long documents costs the same as short snippets, which makes it practical for real workloads.

What you'll need

Step 1: Initialize the Oxlo.ai client

Set up the OpenAI SDK to point at Oxlo.ai. I keep my key in an environment variable, but you can paste it directly for local testing.

from openai import OpenAI
import os

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

Step 2: Define the NLP agent system prompt

The system prompt turns the general-purpose model into a structured NLP engine. I ask for JSON so downstream code can consume the output directly.

SYSTEM_PROMPT = """You are an NLP analysis engine. When given text, produce:
1. A 2-sentence summary
2. Sentiment classification: positive, negative, or neutral
3. Key entities mentioned (people, organizations, products)
4. Top 3 themes or topics

Return your analysis as valid JSON with keys: summary, sentiment, entities, themes."""

Step 3: Create the core analysis function

This function sends text to Oxlo.ai and returns the raw response. I use llama-3.3-70b because it handles mixed instructions and long context reliably. The temperature is kept low so sentiment labels stay consistent.

def analyze_text(text: str) -> str:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": f"Analyze this text:\n\n{text}"},
        ],
        temperature=0.2,
    )
    return response.choices[0].message.content

Step 4: Enforce structured output with JSON mode

Oxlo.ai supports the same response_format parameter as the OpenAI SDK, so we can enforce valid JSON and parse it safely.

import json

def analyze_text_structured(text: str) -> dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": f"Analyze this text:\n\n{text}"},
        ],
        response_format={"type": "json_object"},
        temperature=0.2,
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

Step 5: Build a batch processor for multiple documents

Real NLP workloads rarely stop at one document. This loop processes a list and collects results. I added basic error handling so one bad response does not kill the entire job.

from typing import List

def batch_analyze(documents: List[str]) -> List[dict]:
    results = []
    for doc in documents:
        try:
            result = analyze_text_structured(doc)
            results.append(result)
        except Exception as e:
            results.append({"error": str(e), "document_preview": doc[:50]})
    return results

Run it

Here is a complete script that exercises the pipeline on two realistic inputs. The first is a product announcement, the second is a user complaint. Both contain enough text that token-based billing would already add up, but on Oxlo.ai they each count as a single request.

if __name__ == "__main__":
    docs = [
        "Oxlo.ai launched a new request-based pricing model. Developers no longer need to count tokens for long-context workloads. The API is fully compatible with the OpenAI SDK, which means you can switch your base_url and start processing immediately. Early users report that agentic workflows with large prompts are now significantly cheaper to run at scale.",
        "The old token-based billing was unpredictable. Our monthly costs spiked when we added agentic workflows with large prompts. We needed a flat per-request option that did not punish us for sending full conversation history or long documents to the model."
    ]
    
    for item in batch_analyze(docs):
        print(json.dumps(item, indent=2))

Example output:

{
  "summary": "Oxlo.ai introduced flat per-request pricing and OpenAI SDK compatibility, making long-context workloads more predictable for developers.",
  "sentiment": "positive",
  "entities": ["Oxlo.ai", "OpenAI SDK"],
  "themes": ["pricing models", "developer tools", "API compatibility"]
}
{
  "summary": "The author describes unpredictable costs with token-based billing and expresses a need for flat per-request pricing.",
  "sentiment": "negative",
  "entities": [],
  "themes": ["cost management", "token billing", "agentic workflows"]
}

Next steps

Pipe this agent into a Slack webhook or email listener to process incoming support threads automatically. For multilingual documents, swap the model to qwen-3-32b. For deeper reasoning over complex legal or medical text, use kimi-k2.6.

Top comments (0)