This repository demonstrates how to fine-tune the Codellama-34b model to create "Andy," an AI assistant for Minecraft. Using the Unsloth framework, this tutorial showcases efficient fine-tuning with 4-bit quantization and LoRA for scalable training on limited hardware.
This readme.md provides step-by-step instructions to:
Install the required packages:
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps xformers trl peft accelerate bitsandbytes
Load the Codellama-34b model with 4-bit quantization for reduced resource usage:
from unsloth import FastLanguageModel
import torch
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/codellama-34b-bnb-4bit",
max_seq_length=2048,
dtype=torch.bfloat16,
load_in_4bit=True,
trust_remote_code=True,
)
Add LoRA to fine-tune specific layers efficiently:
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "embed_tokens", "lm_head"],
lora_alpha=16,
lora_dropout=0,
use_gradient_checkpointing="unsloth",
)
Prepare the Minecraft dataset (Andy-3.5):
from datasets import load_dataset
from unsloth.chat_templates import get_chat_template
dataset = load_dataset("Sweaterdog/Andy-3.5", split="train")
tokenizer = get_chat_template(tokenizer, chat_template="chatml")
Set up the training parameters:
from trl import SFTTrainer
from transformers import TrainingArguments
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
dataset_text_field="text",
args=TrainingArguments(
per_device_train_batch_size=16,
max_steps=1000,
learning_rate=2e-5,
gradient_checkpointing=True,
output_dir="outputs",
fp16=True,
),
)
Clear unused memory before training:
import torch
torch.cuda.empty_cache()
Initiate training:
trainer_stats = trainer.train()
Save your fine-tuned model locally or upload to Hugging Face:
model.save_pretrained("andy_minecraft_assistant")
For more details on Unsloth or to contribute, visit Unsloth GitHub.
Happy fine-tuning! 🎮
@misc{celaya2025minecraft, author = {Christopher B. Celaya}, title = {Efficient Fine-Tuning of Large Language Models - A Minecraft AI Assistant Tutorial}, year = {2025}, publisher = {GitHub}, journal = {GitHub repository}, howpublished = {\url{https://github.com/kolbytn/mindcraft}}, note = {\url{https://chris-celaya-blog.vercel.app/articles/unsloth-training}} }
model-00001-of-00014.safetensors 5.0 GBmodel-00004-of-00014.safetensors 4.9 GBmodel-00006-of-00014.safetensors 4.9 GBmodel-00008-of-00014.safetensors 4.9 GBmodel-00010-of-00014.safetensors 4.9 GBmodel-00012-of-00014.safetensors 4.9 GBmodel-00002-of-00014.safetensors 4.9 GBmodel-00005-of-00014.safetensors 4.8 GBmodel-00007-of-00014.safetensors 4.8 GBmodel-00009-of-00014.safetensors 4.8 GBmodel-00011-of-00014.safetensors 4.8 GBmodel-00013-of-00014.safetensors 4.8 GBmodel-00003-of-00014.safetensors 4.8 GBmodel-00014-of-00014.safetensors 4.4 GBadapter_model.safetensors 3.6 GB有文件?提交种子。
提交种子