liveBook · Manning

Chapter 5: Fine-tuning LLMs for improved performance

What you will learn in chapter 5 of Building Reliable AI Systems

The introduction explains why specialized tasks may call for fine-tuning and defines it in contrast to prompting and RAG.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why specialized tasks may call for fine-tuning and defines it in contrast to prompting and RAG.

Key terms: fine-tuning, pretrained LLM, custom data, prompting, retrieval-augmented generation (RAG)

5.1 Choosing the right approach: Prompting, RAG, or fine-tuning

Introduces fine-tuning as training a model on task-specific examples and contrasts its strength with prompting and RAG.

Key terms: fine-tuning, model’s weights, domain- or task-specific data, prompting, retrieval-augmented generation (RAG)

5.1.1 When to use each approach: A practical decision framework

This section gives a three-step decision framework for choosing prompting, RAG, or fine-tuning based on test results, data freshness, and consistency needs.

Key terms: prompting test, quality bar, real examples, system prompt, data-freshness question, RAG, fine-tuning, fresh data, consistency challenge, specialized domain knowledge

5.1.2 Hybrid approach: Combining RAG and fine-tuning

This section explains how fine-tuning and RAG complement each other, especially when an application needs both consistent behavior and current, verifiable information.

Key terms: fine-tuning, RAG, hybrid model, source attribution, verifiable answers, document references, agent tools

5.2 Case studies: Fine-tuning in the real world

5.2.1 Medicine: Med-PaLM 2

The Med-PaLM 2 case shows how medical fine-tuning can improve exam performance and alignment with medical standards, while warning that benchmark success does not guarantee reliability in practice.

Key terms: fine-tuning, PaLM 2, medical standards, appropriate caution, benchmark performance, real-world reliability, novel cases, brittle

5.2.2 Finance: BloombergGPT and FinGPT

Financial-domain models can outperform general-purpose models on finance tasks, but domain training can erode general knowledge if the data is not carefully mixed.

Key terms: BloombergGPT, FinGPT, domain specialization, market sentiment analysis, catastrophic forgetting, general-purpose examples

5.2.3 Code generation: Codex

The Codex example shows how fine-tuning can adapt a model to code, while highlighting the need for human review and high-quality training data.

Key terms: fine-tuning, source code, pattern matching, boilerplate generation, human review, training data, quality, preparation

5.3 Understanding the fine-tuning process

This introduction previews the three phases of fine-tuning—data preparation, model selection, and training—before they are explained in later sections.

5.3.1 Data preparation

This section explains how to collect, curate, format, and validate fine-tuning data so a model learns the intended behavior rather than errors or accidental patterns.

Key terms: task-specific text, annotation, training data, quality over quantity, bias, balance and coverage, prompt-completion paradigm, prompt, completion, role labels

5.3.2 Fine-tuning closed-source models (OpenAI)

This section explains why and how to fine-tune an OpenAI model with custom chat data, from uploading examples to using and monitoring the resulting model.

Key terms: cloud API service, open source model, training infrastructure, branded voice, return policy, fine-tuning, chat format, .jsonl, fine-tuning job, fine_tuned_model

5.3.3 Understanding key fine-tuning approaches

This section compares full fine-tuning, LoRA, and QLoRA by how they adapt a model, the resources they require, and their risk of catastrophic forgetting.

Key terms: full fine-tuning, catastrophic forgetting, Parameter-Efficient Fine-Tuning (PEFT), LoRA, base model, adapters, QLoRA, 4-bit quantization, float16, reminder examples

5.3.4 Building a customer-support assistant

This section builds a customer-support assistant with QLoRA and explains the data, model, adapter, training, and testing choices that make the example practical on consumer hardware.

Key terms: Bitext dataset, instruction-response format, [INST], [/INST], LLaMA-2-7B, QLoRA, 4-bit quantization, float16, pre-quantized, LoRA

5.4 Knowledge distillation for LLMs

The section introduces knowledge distillation as a way to retain much of a powerful LLM’s behavior while making production use faster and cheaper.

Key terms: knowledge distillation, teacher model, student model, training data

5.4.1 How knowledge distillation works in LLMs

This section explains how a smaller LLM can learn from a teacher’s responses and how to evaluate the resulting student for deployment.

Key terms: knowledge distillation, teacher model, student model, sequence-level distillation, input–output pairs, cost, latency, flexibility, commodity hardware, inference cost

5.4.2 Best practices

This section gives practical guidance for knowledge distillation and explains why a smaller student model can make deployment more efficient.

Key terms: teacher, student, diverse examples, evaluate frequently, retrain, knowledge distillation, speed, efficiency, scalability

5.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Embeddings and vector search Chapter 6: Creating effective AI agents →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.