Sub topic 2
Few-Shot Examples, RAG, and Fine-Tuning
Assume you can call a chat-style language model API, read Python, and understand embeddings and context windows. This chapter focuses on choosing the right adaptation method after a basic prompt has failed.
Few-shot examples and output structure
Few-shot prompting means placing a few input-and-output examples in the prompt. The examples show the model the task, tone, labels, and expected format without changing model parameters.
def build_messages(question, examples):
messages = [{
"role": "system",
"content": (
"Classify each support ticket as billing, bug, or other. "
"Reply with one word."
),
}]
for text, label in examples:
messages += [
{"role": "user", "content": text},
{"role": "assistant", "content": label},
]
messages.append({"role": "user", "content": question})
return messages
examples = [
("I was charged twice.", "billing"),
("The export button crashes.", "bug"),
]
print(build_messages("My invoice is wrong.", examples))Use examples that represent common cases and difficult boundaries. Keep their labels consistent. If output must be machine-readable, specify a schema or exact fields. Ambiguous instructions and examples that disagree with each other are common causes of unreliable output.
RAG: giving the model facts at query time
Retrieval-augmented generation (RAG) retrieves relevant external text and adds it to the prompt. It is useful when facts are private, frequently updated, or too large to place in every prompt.
RAG does not teach the model the facts permanently. Each request depends on the quality of its retrieved context. Instruct the model to use the supplied context and to say when the answer is absent. This reduces unsupported answers, but it does not guarantee them.
Inside a retrieval pipeline
A typical pipeline splits documents into chunks, converts each chunk into an embedding, and stores the vectors. An embedding is a numeric representation of meaning. At query time, the system embeds the question, ranks chunks by similarity, and inserts the best results into the prompt.
Chunk size, overlap, metadata filters, ranking quality, and the number of retrieved chunks affect results. More chunks can add cost and distract the model. Test retrieval separately from answer quality.
Fine-tuning: changing the model’s behavior
Fine-tuning trains a model on labeled examples so its parameters change. It can improve a stable style, classification boundary, or strict output pattern that prompting cannot reliably enforce. It is usually a poor choice for rapidly changing facts; RAG is better for those.
| Need | First choice |
|---|---|
| Clear instructions or a few patterns | Prompting |
| Private or changing knowledge | RAG |
| Repeated behavior across many prompts | Fine-tuning |
Fine-tuning requires representative, clean data and a held-out evaluation set. Parameter-efficient fine-tuning, such as LoRA, updates a small set of additional parameters instead of the whole model, reducing training cost and storage.
Exercise
A support bot must answer questions using policies that change weekly and must use a fixed JSON format. Which approaches should you consider?
Show answer
Use RAG for the changing policies and prompting for the JSON format. Consider fine-tuning only if repeated evaluation shows prompting cannot maintain the required behavior.