File size: 6,025 Bytes
747a8c1 2577920 3ebf504 33fe1a9 3ebf504 33fe1a9 3ebf504 33fe1a9 3ebf504 33fe1a9 3ebf504 33fe1a9 3ebf504 33fe1a9 3ebf504 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | ---
tags:
- llm
- lora
- qlora
- llama
- llama-2
- instruction-tuning
- docker
- devops
- code-generation
---
# lora-llama2-finetuned
This model is a fine-tuned instruction-following Large Language Model (LLM) specialized in generating, analyzing, and explaining Dockerfiles. It was adapted from the Llama 2 7B Chat base model using the QLoRA efficient fine-tuning method.
1. Model Description
Model ID:
[Arsh014/lora-llama2-finetuned]
Base Model:
* Architecture: Llama 2 7 Billion Parameters
* Base Model Name: NousResearch/Llama-2-7b-chat-hf
Intended Use:
The primary function of this model is to serve as an expert assistant for containerization tasks. It performs best when prompted with an instruction about a specific application stack or a Dockerfile snippet that needs analysis.
* Generation: Creating valid Dockerfiles from natural language descriptions (e.g., "Create a Dockerfile for a multi-stage Rust application").
* Explanation: Providing step-by-step breakdowns of existing Dockerfiles.
* Refactoring: Suggesting best practices or optimizations for Docker commands.
Limitations & Ethical Considerations:
Critical Note on Scale: This model was fine-tuned on a very limited dataset (20 training examples).
* Generalization: Performance may be poor on instructions that deviate significantly from the training examples, and the model may exhibit signs of overfitting.
* Security: Generated Dockerfiles may contain insecure commands, outdated dependencies, or other security vulnerabilities. Always review and validate generated code before use in a production environment.
* Bias: The model inherits potential biases from its base model, Llama 2.
2. Training Details
The model was fine-tuned using the QLoRA (Quantized Low-Rank Adaptation) technique, which loads the base model in 4-bit precision and only trains a small set of adapter weights.
Configuration:
* Fine-Tuning Method: QLoRA (Efficiently trains adapters on a quantized base model.)
* LoRA Rank (r): 16 (Defines the rank of the update matrices.)
* LoRA Alpha (lora_alpha): 32 (Scaling factor for the LoRA weights.)
* Target Modules: ["q_proj", "v_proj"] (Only query and value attention projection layers were targeted.)
* Max Sequence Length: 512 tokens (Determines the input/output capacity.)
* Training Epochs: 3 (Number of passes over the entire dataset.)
* Final Validation Loss: 1.706886 (Indicates the loss on the small test set.)
3. Training Data
The model was trained on a custom instruction-tuning dataset designed to teach the model to follow specific prompts related to Dockerfiles.
Dataset Structure:
* Local File: /content/dockerfile_finetune.jsonl
* Format: Instruction-Response pairs, formatted for chat fine-tuning.
* Training Size: 20 examples
* Test Size: 3 examples
Prompt Template (REQUIRED for optimal results):
The inference pipeline must use the following template:
### Instruction:
[The user's request or question]
### Input:
[The context, such as an existing Dockerfile or code snippet]
### Response:
[The model's generated Dockerfile, explanation, or analysis]
4. How to Use (Inference)
Since this is a QLoRA adapter, you must load the base model (NousResearch/Llama-2-7b-chat-hf) and then merge the adapter weights from this repository.
Prerequisites:
pip install torch transformers accelerate bitsandbytes peft
Inference Code (Python):
(This section contains detailed Python code using transformers and peft to load and run the model. This code is essential for usage and should be copied directly.)
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
# --- Configuration ---
BASE_MODEL = "NousResearch/Llama-2-7b-chat-hf"
ADAPTER_MODEL = "[YOUR_USERNAME/YOUR_REPO_NAME]" # REPLACE ME
# 1. Load the base model in 4-bit (QLoRA)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="auto",
torch_dtype=torch.float16,
load_in_4bit=True, # Critical for QLoRA
)
# 2. Load the LoRA Adapter Weights
try:
model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
print(f"Successfully loaded LoRA adapters from {ADAPTER_MODEL}")
except Exception as e:
print(f"Error loading adapter: {e}. Ensure the adapter ID is correct.")
# Exit or handle error if adapter fails to load
# 3. Inference Function using the correct prompt template
def generate_docker_response(instruction: str, input_text: str = None) -> str:
# Construct the instruction-tuning prompt template
prompt = f"### Instruction:\n{instruction}\n\n"
if input_text:
prompt += f"### Input:\n{input_text}\n\n"
prompt += "### Response:\n"
# Tokenize and generate
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=512,
do_sample=True,
top_p=0.9,
temperature=0.7,
eos_token_id=tokenizer.eos_token_id
)
# Decode and clean the output
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the content after the "### Response:" tag
response_start = response.find("### Response:\n")
if response_start != -1:
return response[response_start + len("### Response:\n"):].strip()
return response
# --- Example Usage ---
instruction = "Generate a Dockerfile for a simple Go web service that compiles a main.go file and runs it."
print("--- Generating Dockerfile ---")
print(generate_docker_response(instruction))
print("\n--- Explaining a Dockerfile ---")
dockerfile_input = """
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
COPY --from=build /app/dist /app/dist
CMD ["npm", "start"]
"""
instruction = "Explain this multi-stage Dockerfile step-by-step."
print(generate_docker_response(instruction, dockerfile_input)) |