DEV Community

shashank ms
shashank ms

Posted on

Introduction to LLMs for Natural Language Generation Tasks

We are building a lightweight sales-report generator that turns structured weekly JSON metrics into readable executive summaries. It helps data teams stop writing repetitive narratives by hand. I will walk through the full script, from first API call to batched output.

What you'll need

Before starting, grab an API key from the Oxlo.ai portal. You will also need Python 3.10 or newer and the OpenAI SDK.

pip install openai

Step 1: Initialize the client and test the endpoint

I always start with a single ping to confirm the base URL and key are configured. I use llama-3.3-70b here because it handles instruct tasks reliably.

from openai import OpenAI

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

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Say hello and confirm you are ready."},
    ],
)

print(response.choices[0].message.content)

Step 2: Lock in the system prompt

The system prompt is the only place where I describe tone, length, and formatting rules. Keeping it separate from the user payload lets me reuse it across every report.

from openai import OpenAI

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

SYSTEM_PROMPT = """You are a data-narrative engine.
Convert the provided weekly sales JSON into a concise executive paragraph of no more than four sentences.
Use a neutral, professional tone.
Never include raw JSON keys in the output.
If growth is negative, frame it as a challenge rather than a failure."""

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": '{"region": "Test", "revenue": 1000, "growth": 0.05}'},
    ],
)

print(response.choices[0].message.content)

Step 3: Format the user message from raw data

I want to pass structured JSON without hand-writing prose each time. A small helper function injects the metrics into a consistent template so the model sees the same schema every request.

import json
from openai import OpenAI

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

SYSTEM_PROMPT = """You are a data-narrative engine.
Convert the provided weekly sales JSON into a concise executive paragraph of no more than four sentences.
Use a neutral, professional tone.
Never include raw JSON keys in the output.
If growth is negative, frame it as a challenge rather than a failure."""

def build_user_message(data: dict) -> str:
    return f"Weekly sales metrics:\n

```json\n{json.dumps(data, indent=2)}\n```

\nGenerate the summary paragraph."

sample_data = {
    "region": "North America",
    "period": "2024-W23",
    "revenue_usd": 1_240_000,
    "growth_pct": -0.03,
    "top_product": "Enterprise SaaS Bundle",
    "units_sold": 842
}

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": build_user_message(sample_data)},
    ],
)

print(response.choices[0].message.content)

Step 4: Batch process multiple reports and write to disk

In production I usually generate dozens of regional summaries at once. A loop feeds each district's metrics to the API and appends the results to a markdown file. Because Oxlo.ai charges per request rather than per token, sending large JSON payloads does not inflate cost the way token-based providers do. You can see the exact rate on the pricing page.

import json
from openai import OpenAI

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

SYSTEM_PROMPT = """You are a data-narrative engine.
Convert the provided weekly sales JSON into a concise executive paragraph of no more than four sentences.
Use a neutral, professional tone.
Never include raw JSON keys in the output.
If growth is negative, frame it as a challenge rather than a failure."""

def build_user_message(data: dict) -> str:
    return f"Weekly sales metrics:\n

```json\n{json.dumps(data, indent=2)}\n```

\nGenerate the summary paragraph."

regions = [
    {
        "region": "North America",
        "period": "2024-W23",
        "revenue_usd": 1_240_000,
        "growth_pct": -0.03,
        "top_product": "Enterprise SaaS Bundle",
        "units_sold": 842
    },
    {
        "region": "EMEA",
        "period": "2024-W23",
        "revenue_usd": 980_000,
        "growth_pct": 0.07,
        "top_product": "Cloud Backup Pro",
        "units_sold": 1205
    },
    {
        "region": "APAC",
        "period": "2024-W23",
        "revenue_usd": 760_000,
        "growth_pct": 0.12,
        "top_product": "Mobile Analytics SDK",
        "units_sold": 3100
    }
]

with open("weekly_summaries.md", "w", encoding="utf-8") as f:
    for data in regions:
        resp = client.chat.completions.create(
            model="llama-3.3-70b",
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": build_user_message(data)},
            ],
        )
        summary = resp.choices[0].message.content
        f.write(f"## {data['region']} - {data['period']}\n\n{summary}\n\n")

Run it

Save the script as generate_reports.py and run it from your terminal.

python generate_reports.py

After a few seconds, weekly_summaries.md should contain paragraphs like this:

## North America - 2024-W23

North America generated $1.24 million in revenue during 2024-W23, though the region faced a 3% growth challenge compared to the prior period. The Enterprise SaaS Bundle remained the top performer with 842 units sold, indicating solid demand for core infrastructure solutions. Leadership should investigate pipeline velocity to address the slight contraction and stabilize momentum heading into the next quarter.

## EMEA - 2024-W23

EMEA reached $980,000 in revenue for 2024-W23, marking a healthy 7% growth over the previous period. Cloud Backup Pro led sales with 1,205 units sold, reflecting continued trust in data-resilience offerings. The upward trajectory suggests the regional strategy is resonating with mid-market buyers.

## APAC - 2024-W23

APAC delivered $760,000 in revenue during 2024-W23, achieving an impressive 12% growth rate. The Mobile Analytics SDK was the standout product, moving 3,100 units and signaling strong developer adoption. This accelerating performance positions APAC as the fastest-growing region this quarter.

Wrap-up and next steps

This pattern works for any structured-to-narrative task. Two concrete moves from here: wire the script into a cron job that pulls fresh metrics from your warehouse each Monday morning, or switch to kimi-k2.6 on Oxlo.ai when you want to attach full CSV dumps or multi-week context windows without worrying about token costs scaling with input length.

Top comments (0)