Few-shot examples and structured JSON output
Course contents (20 lessons)
- Getting to know Meta AI and Llama
- What Meta AI is and where you can use it
- Meta AI versus Llama: the assistant and the models
- Your first chat with Meta AI
- Prompt basics that work
- The anatomy of a good prompt
- Context, tone, length and follow-ups
- Everyday uses
- Writing and replying to messages
- Captions, posts and bios
- Planning and study help
- Customer replies for a small business
- Images, groups and privacy
- Writing image generation prompts
- Group chats, etiquette and privacy
- Working with Llama models directly
- Running a Llama model on your own computer
- System prompts and chat templates
- Parameters: temperature, top_p and max tokens
- Few-shot examples and structured JSON output
- Advanced prompting
- Prompting Llama for code
- Retrieval-augmented prompting: answering from your own documents
- Evaluating and comparing answers
- Safety, limits and a final project
- Safety, limits and responsible use
- Final project: build a study buddy assistant
Few-shot prompting
A zero-shot prompt gives only an instruction. A few-shot prompt adds a handful of examples of input and the output you want. The model copies the pattern. This is one of the most reliable techniques, especially with smaller local models.
Classify each customer message as COMPLAINT, QUESTION or PRAISE.
Message: "Where is my order?"
Label: QUESTION
Message: "The zip broke on the first day."
Label: COMPLAINT
Message: "Lovely service, thank you!"
Label: PRAISE
Message: "Do you have this in blue?"
Label:
Why it works
The model sees the exact format, including the word "Label:" at the end, and continues with one word. It does not have to guess what categories mean or how to write the answer. Three to five examples are usually enough. Choose examples that differ from each other and include the tricky cases.
Rules for good examples
- Make them consistent. If one example uses capital letters for labels and another does not, the model will be unsure.
- Cover every category at least once.
- Put examples that look like your real data, not neat textbook sentences.
- Watch the order. Models can lean toward the label of the last example.
Structured output with JSON
If a program will read the answer, free text is a problem. JSON is a simple structure that code can parse. Ask for it in a precise way:
Extract the details from the message and reply with JSON only, no other text.
Use exactly these keys: "name" (string), "date" (YYYY-MM-DD or null), "guests" (number or null).
If a value is not in the message, use null.
Message: "Hi, this is Aisha, I would like a table for 4 on 2026-03-08."
JSON:
A good reply looks like this:
{"name": "Aisha", "date": "2026-03-08", "guests": 4}
Why it works
It names every key and its type, gives a rule for missing values and forbids extra text. Ending the prompt with "JSON:" nudges the model to start writing the object at once. Adding one full example of input and output makes the result even more steady.
Making it reliable
- Use low temperature, close to 0, for extraction.
- Give the date format explicitly. Otherwise you get "8th March" one time and "08/03" the next.
- Some tools have a JSON mode or let you give a schema, which forces valid output. Ollama's API, for example, has a format option for JSON. Check the current docs of your tool for the exact fields.
- Always validate in code. Parse the reply. If parsing fails, retry once with a short message such as "Your last reply was not valid JSON. Reply with JSON only."
A small parsing example
When you call a local Ollama model from code, you send a request to its local address and read the answer. Here is the idea in plain steps. Send the prompt with temperature 0. Take the text of the reply. Strip any code fences. Parse it as JSON. Check that the keys exist and the types are right. If anything fails, retry once, then log the failure and ask a person.
That last step matters. Never let a program silently act on output you could not validate.
Do not trust the content blindly
Valid JSON is not the same as correct JSON. The model may put a plausible but wrong date in a perfectly formatted object. For important data, compare the extracted values against the source text, or use a second check.
The common mistake
The common mistake is asking for JSON but allowing chatty text around it: "Sure! Here is your JSON:". The extra words break the parser. Say "JSON only" and, in code, extract the part between the first opening brace and the last closing brace as a fallback.
Try it yourself
Write a few-shot prompt that classifies ten real messages from your phone into three categories of your choice. Then write an extraction prompt that returns JSON with four fields from a short paragraph. Run each five times at temperature 0.1 and count how many outputs are valid and correct. Fix the prompt until you get five out of five.
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account