DEV Community

shashank ms
shashank ms

Posted on

Integrating LLM with Natural Language Generation Models: A Step-by-Step Guide

Natural language generation (NLG) has moved far beyond template-based mail-merge. Modern pipelines pair large language models (LLMs) for reasoning and planning with dedicated NLG layers that enforce style, structure, and compliance. This combination gives you the contextual depth of models like DeepSeek R1 671B MoE or Llama 3.3 70B while keeping output tightly controlled. In this guide, we will build a two-stage NLG pipeline, orchestrate it with Python, and run it on Oxlo.ai's request-based inference platform.

Why Combine LLMs with NLG?

Using a single LLM for end-to-end generation is simple, but it puts all responsibilities, reasoning, style adherence, and factual grounding, into one black box. When you separate concerns, the LLM acts as a planner or knowledge extractor, and the NLG layer acts as a controlled surface realizer. The result is more reliable formatting, reduced hallucination, and easier auditing.

Oxlo.ai hosts models that fit both roles. For the reasoning stage, DeepSeek R1 671B MoE or Kimi K2.6 can extract complex relationships from long documents. For the generation stage, Llama 3.3 70B or Qwen 3 32B can render text under strict stylistic constraints. Because Oxlo.ai supports JSON mode and function calling, you can lock the LLM output to a schema before it ever reaches the NLG stage.

Architecture Patterns

Most production NLG pipelines fall into one of three patterns.

  • Structured extraction then realization. The LLM reads source material and emits structured data, for example, a JSON object of key metrics. A template engine or second model pass turns that data into prose. This is ideal for financial reporting, medical summaries, and legal briefs.
  • Draft and refine. The LLM produces a rough draft. A second model, often with a narrower context window and a strict system prompt, edits for tone, shortens sentences, or enforces terminology. This works well for marketing copy and technical documentation.
  • Tool-augmented generation. The LLM uses function calling to query databases or calculators. The NLG layer then narrates the results. Oxlo.ai's function calling support makes this pattern straightforward to implement.

Step-by-Step Implementation

Follow these steps to wire the pipeline together.

1. Define the schema

Decide exactly what the LLM must extract. If you are generating incident reports, the schema might include severity, root cause, affected systems, and remediation steps. A fixed schema prevents drift between the reasoning and generation stages.

2. Select your models

On Oxlo.ai, choose a reasoning model that matches your complexity. DeepSeek R1 671B MoE and Kimi K2.6 handle advanced chain-of-thought extraction. For simpler tasks, Llama 3.3 70B is a fast general-purpose option. For multilingual NLG, Qwen 3 32B supports agent workflows across dozens of languages. All are accessible through the same OpenAI-compatible endpoint.

3. Lock output with JSON mode

Use the response_format={"type": "json_object"} parameter to force valid JSON. Alternatively, define a tool schema so the model returns arguments you can validate against a Pydantic model. Oxlo.ai supports both approaches on its chat completions endpoint.

4. Build the NLG stage

Pass the structured JSON into a Jinja2 template, or feed it into a second chat completion with a rigid system prompt. The second approach is useful when the surface text must vary based on audience, for example, executive summary versus engineer-facing details.

5. Orchestrate and stream

Wrap the two calls in a Python function. If you need lower time-to-first-token, enable streaming on the NLG stage. Oxlo.ai delivers streaming responses with no cold starts on popular models, so the pipeline remains responsive even under load.

Code Example: A Two-Stage Report Generator

The following Python script uses the OpenAI SDK pointed at Oxlo.ai. Stage one extracts structured insights from raw server logs. Stage two renders those insights into a Markdown report with a controlled voice.

import os
import json
from openai import OpenAI

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

RAW_LOGS = """
2024-06-12 14:23:00 ERROR db-master-01 connection pool exhausted
2024-06-12 14:24:15 WARN  api-gateway-03 latency p99 > 2000ms
2024-06-12 14:30:00 INFO  db-master-01 failover to replica successful
"""

# Stage 1: Structured extraction with an LLM
extraction = client.chat.completions.create(
    model="deepseek-r1-671b",  # DeepSeek R1 671B MoE on Oxlo.ai
    messages=[
        {
            "role": "system",
            "content": (
                "You are a site reliability engineer. Analyze the logs and return "
                "a JSON object with keys: incidents, root_causes, and actions_taken."
            )
        },
        {"role": "user", "content": RAW_LOGS}
    ],
    response_format={"type": "json_object"}
)

data = json.loads(extraction.choices[0].message.content)

# Stage 2: NLG renders structured data into prose
nlg = client.chat.completions.create(
    model="llama-3.3-70b",  # Llama 3.3 70B on Oxlo.ai
    messages=[
        {
            "role": "system",
            "content": (
                "You are a technical writer. Write a concise Markdown incident report. "
                "Use passive voice for root causes. Limit the summary to 100 words."
            )
        },
        {
            "role": "user",
            "content": json.dumps(data, indent=2)
        }
    ],
    stream=False
)

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

Notice that the only change required to run this on Oxlo.ai is setting base_url="https://api.oxlo.ai/v1". The SDK, JSON mode, and response parsing remain identical to other OpenAI-compatible providers.

Cost and Latency Considerations

Multi-stage NLG pipelines often shuttle large documents through several model calls. On token-based providers, long inputs directly inflate costs because you pay for every input token on every stage. Oxlo.ai uses request-based pricing: one flat cost per API request regardless of prompt length. For long-context extraction followed by a separate generation pass, this can yield substantial savings compared to token-based billing.

Latency is also predictable. Oxlo.ai serves popular models with no cold starts, so the time between your request and the first streamed chunk depends on model speed and queue depth, not a container boot cycle. If you need guaranteed throughput for high-volume NLG, the Enterprise tier offers dedicated GPUs. See the Oxlo.ai pricing page for plan details.

Conclusion

Integrating an LLM with a dedicated NLG layer separates reasoning from presentation, giving you both depth and control. By using JSON mode to guarantee structure and a second model call to enforce style, you can build pipelines that are auditable, consistent, and cost-effective. Oxlo.ai's OpenAI-compatible API, broad model catalog, and flat per-request pricing make it a strong backend for these workloads, especially when your inputs are long or your agents require many sequential generation steps.

Top comments (0)