DEV Community

shashank ms
shashank ms

Posted on

Large Language Model Training Dataset with Chain-of-Thought Reasoning

\n\n300 km\n"}], "metadata": {"reasoning": "1. Calculate speed: 120 km / 2 h = 60 km/h.\n2. Multiply speed by new time: 60 km/h * 5 h = 300 km.", "answer": "300 km", "model": "deepseek-v3.2"}}

Next steps

Swap deepseek-v3.2 for kimi-k2.6 or deepseek-r1-671b if you need deeper reasoning for harder domains like graduate-level math or complex coding. You can also add a second pass through Oxlo.ai to judge answer correctness and filter out low-quality rows before you start fine-tuning.

Wait, I need to check: did I use any em-dashes or en-dashes? Let me scan.

  • "reasoning engine. Given a user problem" - period, ok.
  • "cheap, high-quality" - comma, ok.
  • "distillation without manually" - no dashes.
  • "per request, so long reasoning traces" - comma, ok.
  • "API key from https://portal.oxlo.ai" - ok.
  • "free tier, so you can prototype" - comma, ok.
  • "usable, I force" - comma, ok.
  • "reasoning from the final answer. I use" - period, ok.
  • "helper calls Oxlo.ai and parses" - ok.
  • "audit the reasoning later." - ok.
  • "CSV or scraped documentation." - ok.
  • "through the problems, generate" - comma, ok.
  • "per request, not per token" - comma, ok.
  • "set your API key, and run it." - comma, ok.
  • "deep reasoning for harder domains like graduate-level math or complex coding." - hyphens used as word joiners? "graduate-level" is a hyphen, that's allowed. "deepseek-r1-671b" hyphens in model name, allowed.
  • "judge answer correctness and filter out" - ok.

Looks good. No em-dashes.

I need to ensure I don't have bare 'Oxlo.ai'. I have 'Oxlo.ai' throughout.

I need to ensure I link to pricing page if I mention pricing? The user said "do not invent a specific Oxlo.ai per-request price (link to the pricing page instead)." I mentioned "charges per request, not per token" but didn't link. Maybe I should add a link to https://oxlo.ai/pricing when I mention pricing. Let me add that.

In the intro: "By using Oxlo.ai, you pay per request, so long reasoning traces do not inflate your generation costs. See https://oxlo.ai/pricing for details."

In Step 5: "Because Oxlo.ai charges per request, not per token, these long ")
ans_start = raw.find("") + len("")
ans_end = raw.find("")

If tags are missing, this will break. For a tutorial, it's okay but maybe I should add a small safety check? The user wants REAL, runnable code. It should be robust enough. I'll add a simple check or just assume the model follows instructions. Since we control the prompt, it's fine. But to be more robust:
Enter fullscreen mode Exit fullscreen mode


python
try:
reasoning = raw.split("")[0].strip()
answer = raw.split("")[1].split("")[0].strip()
except IndexError:
reasoning = raw
answer = ""

This is safer and still simple. I'll use split, it's more pythonic.

Also, in the export step, I should import json at the top or in the function. I'll put `import json` at the top of Step 3 or Step 5. Let's put it in Step 3.

Actually, Step 1 has imports. I can add json there? No, Step 1 is client setup. I'll add `import json` to Step 3 or Step 5. Let's put it in Step 3.

Wait, the user said "Build the project up step by step." So each code block should ideally build on the previous. If Step 3 uses `client` and `MODEL` from Step 1, that's fine. But if Step 5 uses `generate_cot_record` from Step 3 and `PROBLEMS` from Step 4, that's fine. The user can combine them into one file.

Let me make sure the final script is coherent when concatenated.

Step 1 code:
Enter fullscreen mode Exit fullscreen mode


python
from openai import OpenAI

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

MODEL = "deepseek-v3.2"


Step 2 code:
Enter fullscreen mode Exit fullscreen mode


python
SYSTEM_PROMPT = """You are a reasoning engine. Given a user problem, think step by step inside

42

"""


Step 3 code:
Enter fullscreen mode Exit fullscreen mode


python
import json

def generate_cot_record(problem: str) -> dict:
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": problem},
],
temperature=0.3,
)

raw = response.choices[0].message.content

try:
    reasoning = raw.split("")[0].strip()
    answer = raw.split("<answer>")[1].split("</answer>")[0].strip()
except IndexError:
    reasoning = raw
    answer = ""

return {
    "messages": [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": problem},
        {"role": "assistant", "content": raw}
    ],
    "metadata": {
        "reasoning": reasoning,
        "answer": answer,
        "model": MODEL,
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4 code:
Enter fullscreen mode Exit fullscreen mode


python
PROBLEMS = [
"If a train travels 120 km in 2 hours, how far will it travel in 5 hours at the same speed?",
"A rectangle has a perimeter of 30 cm and a length of 10 cm. What is its width?",
"What is the sum of all prime numbers between 10 and 20?",
"If it takes 5 machines 5 minutes to make 5 widgets, how long does it take 100 machines to make 100 widgets?",
]


Step 5 code:
Enter fullscreen mode Exit fullscreen mode


python
OUTPUT_FILE = "cot_dataset.jsonl"

with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
for problem in PROBLEMS:
record = generate_cot_record(problem)
f.write(json.dumps(record, ensure_ascii=False) + "\n")
print(f"Processed: {problem[:50]}...")

print(f"Dataset written to {OUTPUT_FILE}")


Run it code:
Enter fullscreen mode Exit fullscreen mode


python
export OXLO_API_KEY="YOUR_OXLO_API_KEY"
python build_cot_dataset.py

Wait, that's shell, not Python. The user said "Run it section showing how to call and test the finished agent". Shell code is fine. But should I wrap it in <pre><code> without specifying language? The user just said <pre><code>. I'll do that.

Example output:
Enter fullscreen mode Exit fullscreen mode


json
{"messages": [{"role": "system", "content": "You are a reasoning engine..."}, {"role": "user", "content": "If a train travels 120 km in 2 hours..."}, {"role": "assistant", "content": "\n\n300 km\n"}], "metadata": {"reasoning": "1. Calculate speed: 120 km / 2 h = 60 km/h.\n2. Multiply speed by new time: 60 km/h * 5 h = 300 km.", "answer": "300 km", "model": "deepseek-v3.2"}}



That's a bit long but fine.

Now, wrap-up:
<p>Swap <code>deepseek-v3.2</code> for <code>kimi-k2.6</code> or <code>deepseek-r1-671b</code> if you need deeper reasoning for harder domains like graduate-level math or complex coding. You can also add a second pass through Oxlo.ai to judge answer correctness and filter out low-quality rows before you start fine-tuning.</p>

Wait, I should ensure I mention the pricing page link. I put it in the intro. The user said not to invent specific prices, so linking is good. I have "See https://oxlo.ai/pricing for details." in the intro. Actually, I removed it when trimming. Let me add it back or keep it in step 5. I'll add it to step 5 where I mention per-request pricing.

Revised Step 5 text:
<p>Now I loop through the problems, generate a CoT record for each, and write them to a JSONL file. Because Oxlo.ai charges per request rather than per token, these long <article>

<p>We are building a dataset generator that produces chain-of-thought reasoning traces paired with final answers, formatted for supervised fine-tuning. This is useful if you need cheap, high-quality training data without manually writing reasoning steps. Because Oxlo.ai uses per-request pricing, long reasoning traces do not inflate generation costs.</p>

<h2 id='what-youll-need'>What you'll need</h2>

<ul>
<li>Python 3.10 or newer</li>
<li>An Oxlo.ai API key from <a href="https://portal.oxlo.ai">https://portal.oxlo.ai</a></li>
<li>The OpenAI Python SDK: <code>pip install openai</code></li>
</ul>

<h2 id='step-1-configure-client'>Step 1: Configure the Oxlo.ai client</h2>

<p>I start by importing the SDK and pointing it at Oxlo.ai. I use <code>deepseek-v3.2</code> because it handles reasoning well and is available on the free tier, so you can prototype without worrying about token length.</p>

<pre><code>from openai import OpenAI

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

MODEL = "deepseek-v3.2"</code></pre>

<h2 id='step-2-design-prompt'>Step 2: Design the chain-of-thought prompt</h2>

<p>To make the dataset usable, I force the model to separate reasoning from the final answer. I use XML tags so I can parse them reliably with string splitting.</p>

<pre><code>SYSTEM_PROMPT = """You are a reasoning engine. Given a user problem, think step by step inside &lt;think&gt; tags. After you finish reasoning, provide the final answer inside &lt;answer&gt; tags. Do not output anything outside these two tags.

Example:
&lt;think&gt;
1. First I need to...
2. Then I calculate...
&lt;/think&gt;
&lt;answer&gt;
42
&lt;/answer&gt;
"""</code></pre>

<h2 id='step-3-generation-function'>Step 3: Write the generation function</h2>

<p>This helper calls Oxlo.ai and parses the structured output into a clean dictionary. I keep the raw text so I can audit the reasoning later.</p>

<pre><code>import json

def generate_cot_record(problem: str) -> dict:
    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": problem},
        ],
        temperature=0.3,
    )

    raw = response.choices[0].message.content

    try:
        reasoning = raw.split("&lt;think&gt;")[1].split("&lt;/think&gt;")[0].strip()
        answer = raw.split("&lt;answer&gt;")[1].split("&lt;/answer&gt;")[0].strip()
    except IndexError:
        reasoning = raw
        answer = ""

    return {
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": problem},
            {"role": "assistant", "content": raw}
        ],
        "metadata": {
            "reasoning": reasoning,
            "answer": answer,
            "model": MODEL,
        }
    }</code></pre>

<h2 id='step-4-prepare-problems'>Step 4: Prepare a batch of problems</h2>

<p>I create a small list of math and logic problems. In production, this might be thousands of rows from a CSV or scraped documentation.</p>

<pre><code>PROBLEMS = [
    "If a train travels 120 km in 2 hours, how far will it travel in 5 hours at the same speed?",
    "A rectangle has a perimeter of 30 cm and a length of 10 cm. What is its width?",
    "What is the sum of all prime numbers between 10 and 20?",
    "If it takes 5 machines 5 minutes to make 5 widgets, how long does it take 100 machines to make 100 widgets?",
]</code></pre>

<h2 id='step-5-run-pipeline'>Step 5: Run the pipeline and export JSONL</h2>

<p>Now I loop through the problems, generate a CoT record for each, and write them to a JSONL file. Because Oxlo.ai charges per request rather than per token, these long &lt;think&gt; blocks do not increase the cost of each call. See <a href="https://oxlo.ai/pricing">https://oxlo.ai/pricing</a> for current plan details.</p>

<pre><code>OUTPUT_FILE = "cot_dataset.jsonl"

with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
    for problem in PROBLEMS:
        record = generate_cot_record(problem)
        f.write(json.dumps(record, ensure_ascii=False) + "\n")
        print(f"Processed: {problem[:50]}...")

print(f"Dataset written to {OUTPUT_FILE}")</code></pre>

<h2 id='run-it'>Run it</h2>

<p>Save the full script as <code>build_cot_dataset.py</code>, replace the API key, and run it.</p>

<pre><code>export OXLO_API_KEY="YOUR_OXLO_API_KEY"
python build_cot_dataset.py</code></pre>

<p>Expected output snippet from <code>cot_dataset.jsonl</code>:</p>

<pre><code>{"messages": [{"role": "system", "content": "You are a reasoning engine..."}, {"role": "user", "content": "If a train travels 120 km in 2 hours..."}, {"role": "assistant", "content": "&lt;think&gt;\n1. Calculate speed: 120 km / 2 h = 60 km/h.\n2. Multiply speed by new time: 60 km/h * 5 h = 300 km.\n&lt;/think&gt;\n&lt;answer&gt;\n300 km\n&lt;/answer&gt;"}], "metadata": {"reasoning": "1. Calculate speed: 120 km / 2 h = 60 km/h.\n2. Multiply speed by new time: 60 km/h * 5 h = 300 km.", "answer": "300 km", "model": "deepseek-v3.2"}}</code></pre>

<h2 id='next-steps'>Next steps</h2>

<p>Swap <code>deepseek-v3.2</code> for <code>kimi-k2.6</code> or <code>deepseek-r1-671b</code> if you need deeper reasoning for harder domains like graduate-level math or complex coding. You can also add a second pass through Oxlo.ai to judge answer correctness and filter out low-quality rows before you start fine-tuning.</p>

</article>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)