liveBook · Manning

Chapter 2: Tuning for a specific domain

What you will learn in chapter 2 of Domain-Specific Small Language Models

This chapter introduction previews customizing open source foundation models with your own data and notes that most of the book focuses on inference rather than tuning.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews customizing open source foundation models with your own data and notes that most of the book focuses on inference rather than tuning.

2.1 Data preparation

This section establishes that task-specific Transformer fine-tuning begins by formatting the training dataset, then previews the data-preparation examples that follow.

Key terms: fine-tuning, Transformer model, dataset for training

2.1.1 Data preparation for BERT fine-tuning

This section prepares labeled text for BERT classification fine-tuning by tokenizing and encoding it, then organizing it into training and validation data loaders.

Key terms: classification task, class label, list of tuples, bert-base-uncased tokenizer, tokens, [CLS], [SEP], padding, truncating, token

2.1.2 Data preparation for GPT fine-tuning

This section prepares context–completion pairs for GPT-2 fine-tuning by tokenizing and formatting them, then placing them in a custom training data loader.

Key terms: context, prompt, target, completion, GPT-2 tokenizer, special tokens, separator token, padding, truncating, token IDs

2.1.3 Data preparation for RAG

This section prepares text for RAG by embedding and indexing it with FAISS, then demonstrates how to find and interpret similar text.

Key terms: RAG, embeddings, sentence_transformers, FAISS index, normalize, query embedding, Euclidean (L2) distance, k, distances, ann

2.2 Retrieval-augmented generation

This section explains how RAG supplies retrieved information to an LLM and when to consider it instead of prompt engineering.

Key terms: retrieval-augmented generation, retrieval phase, content generation phase, external knowledge base, embeddings, prompt engineering, few-shot, chain-of-thought, RAG, fine-tuning

2.3 Fine-tuning

This section walks through full fine-tuning of DistilBERT for question answering, from preparing a labeled dataset to training and extracting an answer.

Key terms: fine-tuning, pretrained LLM, full fine-tuning, parameters, question, context, answers, start and end tokens, preprocess_function, DistilBERT-base

2.4 LoRA

This section explains why LoRA reduces fine-tuning costs and walks through preparing, training, saving, and using a LoRA-adapted FLAN-T5 summarizer.

Key terms: PEFT, LoRA, pretrained weights, full fine-tuning, FLAN-T5, samsum, tokenizer, 8-bit precision, LoraConfig, checkpoint

2.5 RAG or fine-tuning?

This section compares when RAG and fine-tuning are useful for specializing an LLM and explains why the choice depends on the use case.

Key terms: RAG, up-to-date information, vector database, data pipelines, fine-tuning, task-specific dataset, domain nuances, PEFT, interpretability, hallucinations

2.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Small language models Chapter 3: End-to-end transformer fine-tuning →

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.