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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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# πŸ› οΈ Complete Command Reference

## 1. Create Project Directory

```bash
mkdir -p /root/ai-tuning
cd /root/ai-tuning



cat /etc/os-release


/opt/python310/bin/python3 -m venv /root/ai-tuning/.venv310
source /root/ai-tuning/.venv310/bin/activate
pip install numpy==1.26.4
pip install pyarrow==20.0.0
pip install torch==2.6.0
pip install transformers==4.46.3
pip install datasets==2.21.0
pip install peft==0.13.2
pip install accelerate==1.0.1
pip install requests



python - <<'PY'
import numpy
import pyarrow
import torch
import transformers
import datasets
import peft
import accelerate

print("NumPy       :", numpy.__version__)
print("PyArrow     :", pyarrow.__version__)
print("PyTorch     :", torch.__version__)
print("Transformers:", transformers.__version__)
print("Datasets    :", datasets.__version__)
print("PEFT        :", peft.__version__)
print("Accelerate  :", accelerate.__version__)
print("CUDA        :", torch.cuda.is_available())
PY

. Hugging Face Login
hf auth whoami
hf auth login


Create DevOps Dataset
/root/ai-tuning/devops_dataset.jsonl
ls -lh /root/ai-tuning/devops_dataset.jsonl
wc -l /root/ai-tuning/devops_dataset.jsonl


Validate JSONL
python3 -m json.tool devops_dataset.jsonl


python3 - <<'PY'
import json

file = "/root/ai-tuning/devops_dataset.jsonl"

errors = 0

with open(file, encoding="utf-8") as f:
    for line_no, line in enumerate(f, 1):
        line = line.strip()

        if not line:
            continue

        try:
            json.loads(line)
        except Exception as e:
            print(f"Invalid JSON at line {line_no}: {e}")
            errors += 1

if errors == 0:
    print("JSONL validation successful")
else:
    print(f"Validation failed: {errors} errors")
PY






Create Train / Validation Split


python3 - <<'PY'
import json
import random

input_file = "/root/ai-tuning/devops_dataset.jsonl"

train_file = "/root/ai-tuning/train.jsonl"
validation_file = "/root/ai-tuning/validation.jsonl"

with open(input_file, encoding="utf-8") as f:
    data = [json.loads(line) for line in f if line.strip()]

random.seed(42)
random.shuffle(data)

split = int(len(data) * 0.8)

train = data[:split]
validation = data[split:]

with open(train_file, "w", encoding="utf-8") as f:
    for item in train:
        f.write(json.dumps(item, ensure_ascii=False) + "\n")

with open(validation_file, "w", encoding="utf-8") as f:
    for item in validation:
        f.write(json.dumps(item, ensure_ascii=False) + "\n")

print("Total      :", len(data))
print("Train      :", len(train))
print("Validation :", len(validation))
PY





Check Training Script
ls -lh /root/ai-tuning/train_lora.py
less /root/ai-tuning/train_lora.py


. Run LoRA Training
cd /root/ai-tuning

/opt/python310/bin/python3 train_lora.py

Check LoRA Output
ls -lah /root/ai-tuning/lora-output
ls -lh /root/ai-tuning/lora-output/adapter_model.safetensors
cat /root/ai-tuning/lora-output/adapter_config.json
du -sh /root/ai-tuning/lora-output


Test LoRA Adapter
find /root/ai-tuning/lora-output -maxdepth 1 -type f -printf "%f\n"


Merge LoRA
cd /root/ai-tuning

/opt/python310/bin/python3 merge_lora.py

Check Merged Model
du -sh /root/ai-tuning/merged-qwen-devops

ls -lh /root/ai-tuning/merged-qwen-devops
ls -lh /root/ai-tuning/merged-qwen-devops/*.safetensors



Clone llama.cpp
cd /root/ai-tuning

git clone https://github.com/ggml-org/llama.cpp.git


cd /root/ai-tuning/llama.cpp
git status



Build llama.cpp
docker run --rm -it \
  -v /root/ai-tuning:/workspace \
  ubuntu:22.04




apt-get update
apt-get install -y \
    build-essential \
    cmake \
    git \
    python3 \
    python3-pip \
    python3-dev
cd /workspace/llama.cpp
cmake -B build -DCMAKE_BUILD_TYPE=Release
ls -lh build/bin/llama-cli




Convert Merged Model to GGUF
python3 /workspace/llama.cpp/convert_hf_to_gguf.py \
    /workspace/merged-qwen-devops \
    --outfile /workspace/qwen-devops-f16.gguf \
    --outtype f16



ls -lh /root/ai-tuning/qwen-devops-f16.gguf


Quantize F16 β†’ Q4_K_M
/workspace/llama.cpp/build/bin/llama-quantize \
    /workspace/qwen-devops-f16.gguf \
    /workspace/qwen-devops-q4_k_m.gguf \
    Q4_K_M




ls -lh /workspace/qwen-devops-*.gguf
Test GGUF Directly

/workspace/llama.cpp/build/bin/llama-cli \
    -m /workspace/qwen-devops-q4_k_m.gguf


Check Docker

docker --version
docker ps | grep ollama


docker exec $OLLAMA_CONTAINER ollama --version
docker exec $OLLAMA_CONTAINER ollama list



Create Model Directory

docker exec $OLLAMA_CONTAINER mkdir -p /models
docker exec $OLLAMA_CONTAINER ls -ld /models




Copy GGUF into Ollama Container

docker cp \
    /root/ai-tuning/qwen-devops-q4_k_m.gguf \
    $OLLAMA_CONTAINER:/models/qwen-devops-q4_k_m.gguf

docker exec $OLLAMA_CONTAINER \
    ls -lh /models/qwen-devops-q4_k_m.gguf

docker cp \
    /root/ai-tuning/Modelfile-qwen-devops \
    $OLLAMA_CONTAINER:/tmp/Modelfile

docker exec $OLLAMA_CONTAINER \
    cat /tmp/Modelfile


Create Ollama Fine-Tuned Model

docker exec -it $OLLAMA_CONTAINER \
    ollama create devops-qwen \
    -f /tmp/Modelfile

docker exec $OLLAMA_CONTAINER ollama list


Run Fine-Tuned Model


docker exec -it $OLLAMA_CONTAINER \
    ollama run devops-qwen



Ollama API


curl http://localhost:11434/api/tags

curl http://localhost:11434/api/generate \
  -d '{
    "model": "devops-qwen",
    "prompt": "How do I check disk usage in Linux?",
    "stream": false
  }'


Streaming API
curl http://localhost:11434/api/generate \
  -d '{
    "model": "devops-qwen",
    "prompt": "How do I troubleshoot Kubernetes CrashLoopBackOff?",
    "stream": true
  }'

API Health Check
curl -s http://localhost:11434/api/tags | python3 -m json.tool















Complete Pipeline Commands
# 1. Project
cd /root/ai-tuning

# 2. Activate Python
source .venv310/bin/activate

# 3. Validate dataset
python3 -c "import json; [json.loads(x) for x in open('devops_dataset.jsonl')]; print('OK')"

# 4. Train
/opt/python310/bin/python3 train_lora.py

# 5. Check adapter
ls -lh lora-output/adapter_model.safetensors

# 6. Merge
/opt/python310/bin/python3 merge_lora.py

# 7. Check merged model
du -sh merged-qwen-devops

# 8. Check GGUF
ls -lh qwen-devops-*.gguf

# 9. Check Ollama
docker exec 4333edae24c6 ollama list

# 10. Create model
docker exec -it 4333edae24c6 \
    ollama create devops-qwen -f /tmp/Modelfile

# 11. Run
docker exec -it 4333edae24c6 \
    ollama run devops-qwen

# 12. Benchmark
/opt/python310/bin/python3 benchmark_ollama.py


Final Verification Checklist

echo "===== SYSTEM ====="
uname -a
free -h
nproc

echo "===== PYTHON ====="
/opt/python310/bin/python3 --version

echo "===== DATASET ====="
wc -l /root/ai-tuning/devops_dataset.jsonl
wc -l /root/ai-tuning/train.jsonl
wc -l /root/ai-tuning/validation.jsonl

echo "===== LORA ====="
ls -lh /root/ai-tuning/lora-output/adapter_model.safetensors

echo "===== MERGED ====="
du -sh /root/ai-tuning/merged-qwen-devops

echo "===== GGUF ====="
ls -lh /root/ai-tuning/qwen-devops-*.gguf

echo "===== DOCKER ====="
docker ps

echo "===== OLLAMA ====="
docker exec 4333edae24c6 ollama list

echo "===== BENCHMARK ====="
ls -lh /root/ai-tuning/benchmark_results.json








Final Result

                    DevOps Dataset
                          β”‚
                          β–Ό
                  Train / Validation
                          β”‚
                          β–Ό
                Qwen2.5-3B-Instruct
                          β”‚
                          β–Ό
                    LoRA Training
                          β”‚
                          β–Ό
                   LoRA Adapter
                          β”‚
                          β–Ό
                    Merge LoRA
                          β”‚
                          β–Ό
                  Merged Qwen Model
                          β”‚
                          β–Ό
                       GGUF
                          β”‚
                          β–Ό
                     Q4_K_M
                          β”‚
                          β–Ό
                  Ollama / Docker
                          β”‚
                          β–Ό
                 devops-qwen:latest
                          β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β–Ό                       β–Ό
         Ollama API               Benchmark
              β”‚                       β”‚
              β–Ό                       β–Ό
          n8n / RAG          Base vs Fine-Tuned
              β”‚
              β–Ό
       DevOps AI Assistant



Production Target



                    User
                     β”‚
                     β–Ό
              Chat Interface
                     β”‚
                     β–Ό
                n8n / FastAPI
                     β”‚
                     β–Ό
                RAG Retriever
                     β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
              β–Ό             β–Ό
        Vector DB       DevOps Docs
              β”‚
              β–Ό
       Relevant Context
              β”‚
              β–Ό
       devops-qwen:latest
              β”‚
              β–Ό
       DevOps AI Response
              β”‚
              β–Ό
             User



The final architecture combines:

LoRA
 +
Qwen2.5-3B
 +
GGUF
 +
Q4_K_M
 +
Ollama
 +
Docker
 +
RAG
 +
n8n / FastAPI