Here’s how you can adapt the previous recipe to fine-tune exactly the model you mentioned (“unsloth/Qwen3-14B-Base-unsloth-bnb-4bit”) and incorporate the “year” as part of your prompt. We’ll cover:
- Data formatting (JSON)
- Loading “unsloth/Qwen3-14B-Base-unsloth-bnb-4bit” in Unsloth
- LoRA setup and training
- Tips on prompting with the year
1) Data formatting
Choose one of these two styles. In both cases you prepend (or embed) the year explicitly in your “prompt”.
A) Instruction / Input / Output
Save a JSONL (or .json) like this:
[
{
"instruction": "Given this excerpt from the year 1888, answer: What does the author think of new technology?",
"text": "The mechanical marvels of our age, while impressive in their ingenuity…",
"output": "The author admires the ingenuity but worries it might erode human values."
},
{
"instruction": "Summarize this 1955 diary entry:",
"text": "The atom, once purely academic…",
"output": "It argues that atomic power brings both great promise (medicine, energy) and huge risk (nuclear war)."
}
]
B) Prompt / Completion
[
{
"prompt": "Year: 1888\nText: The mechanical marvels of our age, while impressive in their ingenuity…\n\n",
"completion": "The author admires the ingenuity but worries it might erode human values.\n"
},
{
"prompt": "Year: 1955\nText: The atom, once purely academic…\n\n",
"completion": "It argues that atomic power brings both great promise (medicine, energy) and huge risk (nuclear war).\n"
}
]
Which to choose?
- Instruction/Input/Output gives you more flexibility to vary tasks (summaries, Q&A, style transfer).
- Prompt/Completion is simpler if you just want “given this → continue/answer.”
2) Loading the Unsloth base model
from unsloth import FastLanguageModel
from transformers import AutoTokenizer
model_name = "unsloth/Qwen3-14B-Base-unsloth-bnb-4bit"
tokenizer = AutoTokenizer.from_pretrained(model_name,
padding_side="right",
truncation_side="left")
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = model_name,
tokenizer = tokenizer,
load_in_4bit = True,
bnb_4bit_use_double_quant = True,
bnb_4bit_quant_type = "nf4",
bnb_4bit_compute_dtype = "float16",
device_map = "auto",
max_seq_length = 2048
)
3) Wrapping with LoRA & training
import torch
from datasets import load_dataset
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from transformers import TrainingArguments
from trl import SFTTrainer
# 3.1 Prepare model for k-bit + LoRA
model = prepare_model_for_kbit_training(model)
lora_cfg = LoraConfig(
r = 16,
lora_alpha = 32,
lora_dropout = 0.05,
task_type = "CAUSAL_LM",
target_modules= ["q_proj","k_proj","v_proj","o_proj","up_proj","down_proj"]
)
model = get_peft_model(model, lora_cfg)
# 3.2 Load & tokenize your JSON dataset
ds = load_dataset("json", data_files="historical_data.json", split="train")
def preprocess(batch):
# adjust if you used instruction/text/output vs prompt/completion
if "instruction" in batch:
inputs = [i + "\n" + t for i,t in zip(batch["instruction"], batch["text"])]
targets = batch["output"]
else:
inputs = batch["prompt"]
targets = batch["completion"]
tok_inputs = tokenizer(inputs, padding=True, truncation=True, max_length=2048)
tok_labels = tokenizer(targets, padding=True, truncation=True, max_length=1024)
labels = tok_labels["input_ids"]
# mask padding
labels = [
[(lb if mask else -100) for lb,mask in zip(label, attn)]
for label,attn in zip(labels, tok_labels["attention_mask"])
]
return {
"input_ids": tok_inputs["input_ids"],
"attention_mask": tok_inputs["attention_mask"],
"labels": labels
}
ds = ds.map(preprocess, batched=True, remove_columns=ds.column_names)
# 3.3 Training arguments
train_args = TrainingArguments(
output_dir = "qwen3-historical-lora",
per_device_train_batch_size = 1,
gradient_accumulation_steps = 4,
max_steps = 1000,
learning_rate = 2e-4,
logging_steps = 50,
save_steps = 250,
fp16 = True,
optim = "adamw_paged",
lr_scheduler_type = "cosine",
warmup_ratio = 0.05
)
trainer = SFTTrainer(
model = model,
train_dataset = ds,
tokenizer = tokenizer,
args = train_args,
# packing=False because Qwen padding needs care
packing = False
)
trainer.train()
model.save_pretrained("qwen3-historical-lora")
tokenizer.save_pretrained("qwen3-historical-lora")
4) Prompting tips
– Always start your prompt with the year, e.g.:
Year: 1888
Text: The mechanical marvels of our age…
→ [model generates answer]
– Or embed as an “instruction”:
Instruction: “Based on the excerpt from 1888, what does the author think…?”
Text: “The mechanical marvels…”
– If you have very long texts, you can truncate or split them by chapter/page.
– Validation: After training, try prompts from eras you held out (e.g. 1920 diaries) and see if the model’s tone & content shift appropriately.
That’s the end-to-end setup using exactly the unsloth/Qwen3-14B-Base-unsloth-bnb-4bit model.
Next steps:
- Create/curate your JSON (with “year” baked into prompt/instruction).
- Run the above fine-tuning script.
- Qualitatively inspect the generations by varying the year.
Let me know if you hit any snags! 🚀