Lesson 9 ยท Beginner

Structuring Better Prompts with Role, Context, and Examples

Go beyond simple instructions by using role prompts, context blocks, and few-shot examples to guide the model more effectively.

Read the explanation carefully, then review the examples and coding section. The goal is to understand both the concept and how it appears inside a real application workflow.

Explanation

Role prompts help frame the perspective or behavior of the model, such as tutor, analyst, or editor.

Context blocks provide facts, policy rules, or product details that the model should use in its answer.

Few-shot prompting improves reliability by showing the model examples of the desired style or format.

Why this topic matters in practice

In generative AI products, the model is only one part of the system. The surrounding workflow determines whether the output is useful, safe, and maintainable. This lesson matters because it helps you connect the idea to tasks such as tutoring, search, copilots, business assistants, and production automation.

Examples

Tutor workflow

A role prompt can ask the model to explain technical ideas in simple steps without jargon.

Company knowledge

A context block can include product features so the model answers using the right details.

Structured extraction

One or two examples can show how you want fields mapped into JSON.

Few-shot prompt assembly

The code below is intentionally concise so the underlying pattern stays clear. It focuses on the application logic you can reuse, even if you later switch model providers or deployment environments.

examples = [
    {"input": "Cloud computing", "output": "Cloud computing means using remote servers over the internet."},
    {"input": "Machine learning", "output": "Machine learning means teaching computers to learn from data."},
]

def build_few_shot_prompt(term):
    prompt = "Explain each term in one simple sentence.\n\n"
    for item in examples:
        prompt += f"Term: {item['input']}\nAnswer: {item['output']}\n\n"
    prompt += f"Term: {term}\nAnswer:"
    return prompt

print(build_few_shot_prompt("Neural network"))

How the coding section works

  • The examples teach the model what 'simple sentence' means in practice.
  • Few-shot prompting is especially useful when the required output style is specific.
  • Keep examples short and representative so they fit within context limits.

Implementation advice

When turning this lesson into a real feature, think beyond the code snippet itself. Decide what inputs should be allowed, how you will validate outputs, how you will recover from errors, and how you will measure whether the feature is actually helping users. Those surrounding choices often determine whether an AI feature feels polished or unreliable.

Summary / key takeaways

  • Role, context, and examples make prompts more controllable.
  • Few-shot prompting is powerful when the output format must be consistent.
  • Prompt structure often matters more than prompt length.

Exercises

  1. Create a role prompt for an AI study coach.
  2. Add a third example to the sample code and test the changed prompt.
  3. Write a context block for a fictional school admission assistant.