C.W.K.
Stream
Lesson 01 of 05 · published

Project: Custom Instruction Follower

~28 min · project, structured-output, json, instruction-follower

Level 0Observer
0 XP0/43 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

The goal

Train a small model to consistently output structured JSON for a product-review analysis tool. The model must produce parseable JSON with the right fields on the first try, every time.

The recipe

  • Base model: Llama 3.1 8B Instruct (good at instruction following).
  • Training data: 500–1,000 product reviews paired with hand-crafted JSON outputs covering positive, negative, mixed, sarcastic, very short, and very long inputs.
  • Method: QLoRA, r=16, all-linear, 3 epochs.
  • Expected outcome: 99%+ JSON format compliance vs ~90% with prompting alone.

The 'adversarial' diet

The single biggest determinant of robustness: include 'adversarial' examples in your training data — reviews that are ambiguous, very short, contain sarcasm, mix multiple sentiments, or arrive in unexpected formats. The model becomes robust to real-world variation only by seeing real-world variation during training.

Code

Training data shape for the JSON-output instruction follower·json
{"messages": [
  {"role": "system", "content": "Analyze product reviews. Always respond in JSON with: sentiment (positive|negative|mixed), confidence (0..1), key_points (list), summary (one sentence)."},
  {"role": "user", "content": "This laptop is amazing! Great battery life and the screen is gorgeous. Only downside is the keyboard could be better."},
  {"role": "assistant", "content": "{\"sentiment\": \"positive\", \"confidence\": 0.85, \"key_points\": [\"excellent battery life\", \"gorgeous screen\", \"mediocre keyboard\"], \"summary\": \"Highly positive review praising battery and display, with minor keyboard criticism.\"}"}
]}
{"messages": [
  {"role": "system", "content": "Analyze product reviews. Always respond in JSON with: sentiment (positive|negative|mixed), confidence (0..1), key_points (list), summary (one sentence)."},
  {"role": "user", "content": "meh"},
  {"role": "assistant", "content": "{\"sentiment\": \"mixed\", \"confidence\": 0.4, \"key_points\": [\"insufficient detail\"], \"summary\": \"Review too short to determine clear sentiment.\"}"}
]}

External links

Exercise

Pick a structured-output task you have at work. Curate 200 examples (mix happy-path with at least 30% adversarial). Fine-tune Llama 3.1 8B with QLoRA following the recipe. Measure JSON parseability before and after on 50 held-out reviews. Report the lift.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.