chriscelaya/minecraft-ai-training-tutorial
097
1---2base_model: unsloth/Qwen2.5-7B-bnb-4bit3tags:4- text-generation-inference5- transformers6- unsloth7- qwen28- trl9license: apache-2.010language:11- en12---13 14# Efficient Fine-Tuning of Large Language Models - Minecraft AI Assistant Tutorial 15 16This repository demonstrates how to fine-tune the **Qwen 7B** 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.17 18## ๐ Resources 19 20- **Source Code**: [GitHub Repository](https://github.com/while-basic/mindcraft)21- **Colab Notebook**: [Colab Notebook](https://colab.research.google.com/drive/1Eq5dOjc6sePEt7ltt8zV_oBRqstednUT?usp=sharing)22- **Blog Article**: [Walkthrough](https://chris-celaya-blog.vercel.app/articles/unsloth-training)23- **Dataset**: [Andy-3.5](https://huggingface.co/datasets/Sweaterdog/Andy-3.5)24- **Teaser**: [Video](https://www.youtube.com/watch?v=KUXY5OtaPZc)25 26## Overview 27 28This **readme.md** provides step-by-step instructions to: 291. Install and set up the **Unsloth framework**. 302. Initialize the **Qwen 7B** model with **4-bit quantization**. 313. Implement **LoRA Adapters** for memory-efficient fine-tuning. 324. Prepare the **Andy-3.5 dataset** with Minecraft-specific knowledge. 335. Configure and execute training in a resource-efficient manner. 346. Evaluate and deploy the fine-tuned AI assistant.35 36---37 38### Key Features 39 40- **Memory-Efficient Training**: Fine-tune large models on GPUs as low as T4 (Google Colab). 41- **LoRA Integration**: Modify only key model layers for efficient domain-specific adaptation. 42- **Minecraft-Optimized Dataset**: Format data using **ChatML templates** for seamless integration. 43- **Accessible Hardware**: Utilize cost-effective setups with GPU quantization techniques.44 45---46 47## Prerequisites 48 49- **Python Knowledge**: Familiarity with basic programming concepts. 50- **GPU Access**: T4 (Colab Free Tier) is sufficient; higher-tier GPUs like V100/A100 recommended. 51- **Optional**: [Hugging Face Account](https://huggingface.co/) for model sharing.52 53---54 55## Setup 56 57Install the required packages: 58```bash59!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"60!pip install --no-deps xformers trl peft accelerate bitsandbytes61```62 63---64 65## Model Initialization 66 67Load the **Qwen 7B** model with 4-bit quantization for reduced resource usage: 68 69```python70from unsloth import FastLanguageModel71import torch72 73model, tokenizer = FastLanguageModel.from_pretrained(74 model_name="unsloth/Qwen2.5-7B-bnb-4bit",75 max_seq_length=2048,76 dtype=torch.bfloat16,77 load_in_4bit=True,78 trust_remote_code=True,79)80```81 82---83 84## Adding LoRA Adapters 85 86Add LoRA to fine-tune specific layers efficiently: 87```python88model = FastLanguageModel.get_peft_model(89 model,90 r=16,91 target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "embed_tokens", "lm_head"],92 lora_alpha=16,93 lora_dropout=0,94 use_gradient_checkpointing="unsloth",95)96```97 98---99 100## Dataset Preparation 101 102Prepare the Minecraft dataset (**Andy-3.5**): 103```python104from datasets import load_dataset105from unsloth.chat_templates import get_chat_template106 107dataset = load_dataset("Sweaterdog/Andy-3.5", split="train")108tokenizer = get_chat_template(tokenizer, chat_template="chatml")109```110 111---112 113## Training Configuration 114 115Set up the training parameters: 116```python117from trl import SFTTrainer118from transformers import TrainingArguments119 120trainer = SFTTrainer(121 model=model,122 tokenizer=tokenizer,123 train_dataset=dataset,124 dataset_text_field="text",125 args=TrainingArguments(126 per_device_train_batch_size=16,127 max_steps=1000,128 learning_rate=2e-5,129 gradient_checkpointing=True,130 output_dir="outputs",131 fp16=True,132 ),133)134```135 136Clear unused memory before training: 137```python138import torch139torch.cuda.empty_cache()140```141 142---143 144## Train the Model 145 146Initiate training: 147```python148trainer_stats = trainer.train()149```150 151---152 153## Save and Share 154 155Save your fine-tuned model locally or upload to Hugging Face: 156```python157model.save_pretrained("andy_minecraft_assistant")158```159 160---161 162## Optimization Tips 163 164- Expand the dataset for broader Minecraft scenarios. 165- Adjust training steps for better accuracy. 166- Fine-tune inference parameters for more natural responses.167 168---169 170For more details on **Unsloth** or to contribute, visit [Unsloth GitHub](https://github.com/unslothai/unsloth). 171 172Happy fine-tuning! ๐ฎ173 174## Citation175 176@misc{celaya2025minecraft,177 author = {Christopher B. Celaya},178 title = {Efficient Fine-Tuning of Large Language Models - A Minecraft AI Assistant Tutorial},179 year = {2025},180 publisher = {GitHub},181 journal = {GitHub repository},182 howpublished = {\url{https://github.com/kolbytn/mindcraft}},183 note = {\url{https://chris-celaya-blog.vercel.app/articles/unsloth-training}}184}185 