सामग्री पर जाएँ

लेसन 11 · Prompting and Chat Formats

Prompting Llama and Getting Structured Output

Write clear prompts for Llama, use system messages and examples, ask for JSON, constrain output with schemas, then validate and repair replies in code.

Good prompts for Llama follow the same ideas as good prompts for any model: say what you want, give the needed context and show the shape of the answer. Smaller models are often less forgiving, so clarity matters even more.

A reliable structure

A practical prompt has these parts:

  1. Role and goal in the system message.
  2. Rules that always apply, written as positive instructions.
  3. Context, such as a document, separated by clear delimiters.
  4. The task itself.
  5. Output format.
TEXT
System: You label customer messages for a bicycle shop.
Labels: repair, order, return, other.
Reply with the label only. If unsure, reply other.

User: <message>
The chain keeps slipping when I climb hills.
</message>

Delimiters such as <message> help the model separate your instructions from data. They do not make the data safe.

Habits that help small models

  • One task per prompt. Split a long job into steps in your code.
  • Say what to do instead of listing many "do not" rules.
  • Give a few examples that cover different labels, including a hard one. See zero-shot and few-shot prompting.
  • Keep instructions short. A long rule list is easy to half-follow.
  • Allow "I don't know". Tell the model what to answer when the context lacks the answer.
  • Use low temperature for labels, extraction and code.

Few-shot examples as messages

With a chat model, you can give examples as earlier user and assistant turns:

Python Build a few-shot message list

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This list has no example for order. In your own prompts, include every label, because a missing one may be chosen less often.

Asking for JSON

Saying "reply in JSON" helps, but a model can still add a greeting, code fences or a missing bracket. Stronger tools exist:

  • Ollama's chat API has a format field that accepts json or a JSON schema.
  • llama.cpp's server supports grammars and JSON schemas, for example through response_format.
  • Other servers have similar features under their own names.
JSON
{
  "type": "object",
  "properties": {
    "label": {"type": "string", "enum": ["repair", "order", "return", "other"]},
    "urgent": {"type": "boolean"}
  },
  "required": ["label", "urgent"]
}

A schema fixes the shape, not the truth. A reply can also be cut off by a length limit, so you still need to check it.

Always validate

Python Stand-in: parse and validate a reply

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When validation fails, you can retry once with the error message added, then fall back to a safe default or a human. Limit retries, so a bad input cannot loop forever.

Reasoning prompts

For multi-step problems, asking the model to work step by step can help some models. It also uses more tokens. Test it on your data rather than assuming. Keep the final answer in a clearly marked place so your code can find it. Never treat written reasoning as proof that the answer is right.

Version your prompts

Store prompts in files, name versions and run your test set after each change. A prompt that works on one Llama size or generation may fail on another. Keep a short note of why each change was made, so you do not repeat a fix that failed before.

Improving a weak prompt

Here is a before and after. The first prompt is "Look at this email and tell me about it." The model has to guess the goal, the audience, the length and the format. A better version says: "You help a support team. Read the email between the tags. Return three lines: the customer's problem in one sentence, how urgent it is (low, medium or high) and the next action. If the email has no problem, write 'no problem' on the first line."

The second prompt answers the questions a new colleague would ask. When a result is wrong, find which question your prompt left open before you add warnings in capital letters.

Long documents

With long inputs, put the document first, clearly delimited, and the specific question after it. This also helps prefix reuse when many questions use one document. Make sure the whole input fits in the context you configured. If it does not, retrieve only the relevant parts instead of pasting everything, as shown in the retrieval lesson.

Languages

If your users write in another language, say which language the reply should use. Test in that language, with real spelling mistakes and mixed text. Quality often differs between languages, and a prompt that works well in English may need clearer examples elsewhere.

Test cases for prompts

Treat a prompt like code: it needs tests. Collect inputs that are typical, very short, very long, off topic, in another language and hostile. For each one, write what a good result must contain and what it must not contain. Many checks can be automatic, such as "the label is in the allowed list", "the reply has at most three lines" or "no email address appears". Other qualities, such as tone or usefulness, need a person.

Run the set before and after a change. If the new prompt fixes one case and breaks two others, you will see it immediately instead of hearing it from users. Keep the old version until the new one passes.

Know the limit of prompting. If a careful prompt still fails often, change the system around it: add retrieval, split the task, use a stronger model or let ordinary code handle the part that must be exact, such as arithmetic.

Practice idea. Add an order example to the message builder, then write two hard test messages.

Common mistakes

Do not rely on the system prompt to protect secrets. Users may be able to extract it. Never put passwords or keys into a prompt.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What does a JSON schema constraint help with?

    It limits structure and types, not whether values are correct.
  2. 2. Where do few-shot examples fit in a chat request?

    Paired turns show the model the pattern to follow.
  3. 3. Validation has failed again after your allowed retry. What is sensible?

    Bounded retries plus a safe fallback prevent loops and bad actions.

Summary

  • Give the role, rules, delimited context, task and output format.
  • Smaller models benefit from short, focused prompts and examples.
  • Use schema constraints where available, and still validate.
  • Version prompts and test them after every change.

Next: Llama Memory Requirements

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।