Sub topic 9
Teaching by Example with User and Assistant Pairs
This chapter assumes you can read Python lists and dictionaries, and that you know a chat message has a role and content. It also assumes you know the API is stateless, so every request carries the whole history. Here you will use that fact to show the model what you want instead of only describing it.
What few-shot messages are
A few-shot prompt includes a small number of worked examples before the real input. In a chat API, each example is a pair of messages: a user message with a sample input and an assistant message with the output you want. The model reads these earlier turns as if it had already answered that way, and it tends to continue in the same style.
The model does not learn anything permanent from these pairs. They only influence the current request, because they are part of the messages you send.
A worked example
This list classifies sentiment. The system message states the task and the output format. Two example pairs follow. The last message is the real input and has no answer yet.
messages = [
{"role": "system", "content": "Classify the sentiment of the text as positive, negative, or neutral. Reply with one word."},
{"role": "user", "content": "The delivery was fast and the box was intact."},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "The app crashes every time I open it."},
{"role": "assistant", "content": "negative"},
{"role": "user", "content": "The package arrived on Tuesday."},
]The model sees two inputs, each followed by a one-word label. For the final message, the most likely reply is a single lowercase word, probably neutral. The examples fix the format (one word, lowercase, no punctuation) more reliably than the instruction alone.
Choosing good examples
The examples are the instructions, so their quality matters.
- Cover the range of outputs. If all your examples are
positive, the model will lean towardpositive. - Keep the format identical across examples. If one label is capitalized and another is not, the model may copy either one.
- Use inputs that look like real ones. Examples that are much simpler than your real data teach the wrong difficulty level.
- Keep the count small. Two to five pairs is usually enough, and every pair costs tokens on every request.
- Order can matter. Models sometimes favor the label of the last example, so avoid ending on a long run of the same class.
Sending the examples to an API
Because the examples are ordinary messages, you send them like any other history. This function keeps the examples fixed and adds each new input.
from openai import OpenAI
client = OpenAI()
examples = [
{"role": "system", "content": "Classify the sentiment of the text as positive, negative, or neutral. Reply with one word."},
{"role": "user", "content": "The delivery was fast and the box was intact."},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "The app crashes every time I open it."},
{"role": "assistant", "content": "negative"},
]
def classify(text):
messages = examples + [{"role": "user", "content": text}]
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=messages,
)
return response.choices[0].message.content
print(classify("The package arrived on Tuesday."))Note that classify builds a fresh list each time and does not append the reply to examples. For a classifier, each call should be independent. If you appended every answer, the history would grow and earlier inputs could influence later ones.
Common errors
- Examples that contradict the instruction. If the system message says "one word" and an example answers in a sentence, the model usually follows the example.
- Missing assistant reply in an example. Two
usermessages in a row, with no answer between them, do not show a pair. The model sees two questions. - Leaking examples into the answer. Inputs that are too similar to an example can make the model copy its label. Vary the wording.
- Putting examples inside a long user message. This works, but separate message pairs make the structure explicit and are easier to edit and test.
Exercise
Exercise
You want a model to turn a product name into a short tagline, always in the form "Name: tagline" with no more than six words after the colon. You write a system message with this rule, but the model sometimes replies with full sentences. Describe how you would add few-shot messages to fix this, and name one mistake to avoid.
Show answer
Add two or three pairs after the system message. Each pair has a user message with a product name and an assistant message with a reply in exactly the form "Name: tagline", with six words or fewer after the colon. Then send the real product name as the final user message. Avoid examples that break the rule, such as one with a longer tagline or a different format, because the model tends to copy the examples over the instruction. Also avoid using only one style of product, so the model does not overfit to it.