AI EducademyAIEducademy
AcademicsLabBlogAbout
Sign In
AI EducademyAIEducademy

Free AI education for everyone, in every language.

Learn

  • Academics
  • Lessons
  • Lab
  • Dashboard
  • About

Community

  • GitHub
  • Contribute
  • Code of Conduct

Support

  • Buy Me a Coffee โ˜•

Free AI education for everyone

MIT Licence. Open Source

Programsโ€บ๐Ÿ•๏ธ AI Canopyโ€บLessonsโ€บPrompt Engineering Mastery โ€” The Art of Talking to AI
โœจ
AI Canopy โ€ข Advancedโฑ๏ธ 40 min read

Prompt Engineering Mastery โ€” The Art of Talking to AI

Why Prompts Matter โ€” Garbage In, Garbage Out ๐Ÿ—‘๏ธ

You've learned how LLMs work under the hood. Now comes the skill that matters most in practice: prompt engineering โ€” crafting instructions that get the results you actually want.

The same model can give you a brilliant answer or a useless one depending entirely on how you ask:

โŒ Bad prompt:   "Tell me about climate"
   โ†’ Vague, unfocused 2,000-word essay

โœ… Good prompt:  "Explain three key causes of climate change
                  in bullet points, suitable for a 14-year-old.
                  Keep it under 100 words."
   โ†’ Focused, useful, right-sized answer
๐Ÿค”
Think about it:

Think of an LLM like a brilliant expert who takes instructions very literally. If you say "write something about dogs", you'll get a random essay. If you say "write a 200-word comparison of Golden Retrievers and Labradors for a pet adoption website, focusing on family-friendliness", you'll get exactly what you need.


Core Techniques: Zero-Shot, Few-Shot, Chain-of-Thought ๐ŸŽฏ

Zero-Shot Prompting

Give the model a task with no examples. Relies entirely on the model's training.

Prompt: "Classify this review as POSITIVE, NEGATIVE, or NEUTRAL:
'The battery life is amazing but the camera is disappointing.'"

Response: "NEUTRAL"

Works well for straightforward tasks where the model already understands the format.

Few-Shot Prompting

Provide a few examples before your actual request. The model learns the pattern from your examples:

Prompt:
"Classify these reviews:

Review: 'Absolutely love this product!'  โ†’ POSITIVE
Review: 'Broke after two days.'          โ†’ NEGATIVE
Review: 'It's okay, works fine.'         โ†’ NEUTRAL

Review: 'The design is sleek but the software crashes often.'
Sentiment: "

Response: "NEGATIVE"

Few-shot prompting was a GPT-3 breakthrough: just 2โ€“3 examples dramatically improve accuracy on new tasks โ€” no fine-tuning required.

Chain-of-Thought (CoT) Prompting

Ask the model to think step by step. This dramatically improves reasoning on complex problems:

โŒ Without CoT:
"If a train travels 120 km in 2 hours, then speeds up by 50%,
how far does it travel in the next 3 hours?"
โ†’ Model might guess incorrectly

โœ… With CoT:
"If a train travels 120 km in 2 hours, then speeds up by 50%,
how far does it travel in the next 3 hours?
Let's think step by step."

โ†’ Step 1: Original speed = 120 km / 2 hours = 60 km/h
โ†’ Step 2: Speed increase of 50% = 60 ร— 1.5 = 90 km/h
โ†’ Step 3: Distance in 3 hours = 90 ร— 3 = 270 km
โ†’ Answer: 270 km โœ“

System Prompts and Role-Playing ๐ŸŽญ

A system prompt sets the model's behaviour, personality, and constraints before the user ever sends a message.

messages = [
    {
        "role": "system",
        "content": """You are a senior Python code reviewer.
        Rules:
        - Only comment on bugs, security issues, and performance
        - Ignore style and formatting
        - Be direct and specific
        - Rate severity: LOW / MEDIUM / HIGH / CRITICAL
        - If the code is clean, say 'LGTM' and nothing more"""
    },
    {
        "role": "user",
        "content": "Review this code:\n\ndef login(user, pwd):\n    query = f'SELECT * FROM users WHERE name=\"{user}\" AND pass=\"{pwd}\"'\n    return db.execute(query)"
    }
]

The model would flag the SQL injection vulnerability as CRITICAL โ€” because the system prompt told it exactly what to look for and how to respond.

Effective System Prompt Structure

1. ROLE:        Who the model should act as
2. TASK:        What it should do
3. CONSTRAINTS: Rules and boundaries
4. FORMAT:      How to structure the output
5. EXAMPLES:    Optional โ€” what good output looks like
๐Ÿ’ก

System prompts are your most powerful tool for consistent, reliable AI behaviour. Invest time in crafting them โ€” a great system prompt can turn a general-purpose model into a specialised expert.


RAG: Retrieval Augmented Generation ๐Ÿ“š

LLMs have a knowledge cutoff and can hallucinate. RAG fixes both by giving the model access to your own data at query time: Index documents โ†’ Retrieve relevant chunks โ†’ Augment the prompt โ†’ Generate a grounded answer.

# Simplified RAG example
def answer_with_rag(question, document_store):
    relevant_chunks = document_store.search(question, top_k=3)
    context = "\n\n".join(chunk.text for chunk in relevant_chunks)

    prompt = f"""Answer based ONLY on the provided context.
If the context doesn't contain the answer, say "I don't have that information."

Context:
{context}

Question: {question}
Answer:"""

    return llm.generate(prompt)
๐Ÿค”
Think about it:

RAG is like giving a student an open-book exam. The student (LLM) still does the reasoning, but can look up facts โ€” making answers much more accurate and trustworthy.


Structured Output: JSON Mode and Function Calling ๐Ÿ”ง

For production apps, you need structured data, not free-form text.

# JSON Mode โ€” force structured responses
response = client.chat.completions.create(
    model="gpt-4o",
    response_format={"type": "json_object"},
    messages=[
        {"role": "system", "content": "Extract product info as JSON: name, price, category, in_stock"},
        {"role": "user", "content": "The Sony WH-1000XM5 costs $349, available in Electronics."}
    ]
)
# Returns: {"name": "Sony WH-1000XM5", "price": 349, "category": "Electronics", "in_stock": true}

Function calling goes further โ€” letting the model decide when to invoke external tools (weather APIs, databases, calculators) by describing available functions in a schema. The model returns a structured call rather than free text.


Advanced Techniques ๐Ÿง 

Self-Consistency

Ask the same question multiple times (higher temperature), then take the majority answer. Converging reasoning paths signal higher confidence.

Tree of Thought

Instead of one chain of reasoning, explore multiple branches and evaluate feasibility before continuing deeper โ€” prune weak branches, expand promising ones. This excels at planning and complex problem-solving tasks.


Hands-On: Building a Prompt Library ๐Ÿ› ๏ธ

Create reusable prompt templates for common tasks:

PROMPT_LIBRARY = {
    "summarise": """Summarise the following text in {length} bullet points.
Focus on key facts and actionable insights.
Text: {text}""",

    "code_review": """Review this {language} code for:
1. Bugs and logic errors
2. Security vulnerabilities
3. Performance issues
Ignore style. Rate each issue as LOW/MEDIUM/HIGH/CRITICAL.
Code:
```{language}
{code}
```""",

    "explain": """Explain {concept} to a {audience}.
Use analogies from everyday life.
Keep it under {word_limit} words.
End with a "Test yourself" question.""",

    "extract_data": """Extract the following fields from the text as JSON:
Fields: {fields}
If a field is not found, use null.
Text: {text}""",
}

def build_prompt(template_name, **kwargs):
    template = PROMPT_LIBRARY[template_name]
    return template.format(**kwargs)

# Usage examples
prompt = build_prompt(
    "explain",
    concept="quantum computing",
    audience="curious 15-year-old",
    word_limit=150
)
print(prompt)
๐Ÿ’ก

A prompt library is a living document โ€” iterate and improve your templates based on results. Version-control them like code, test them with different inputs, and share the best ones with your team.


Quick Recap ๐ŸŽฏ

  1. Prompt quality determines output quality โ€” be specific, structured, and clear
  2. Zero-shot works for simple tasks; few-shot adds examples; chain-of-thought enables complex reasoning
  3. System prompts define the model's role, rules, and output format for consistent behaviour
  4. RAG grounds LLM answers in your own data, reducing hallucinations and adding recency
  5. Structured output (JSON mode, function calling) makes LLMs production-ready
  6. Advanced techniques like self-consistency and tree of thought push accuracy further
  7. Build a prompt library โ€” reusable, tested templates save time and improve quality

What's Next? ๐Ÿš€

You've now mastered the three pillars of modern AI practice: deep architectures, language models, and prompt engineering. In the upcoming lessons, we'll tackle AI system design โ€” how to architect production AI applications that are reliable, scalable, and responsible. ๐Ÿ•๏ธ

Lesson 3 of 30 of 3 completed
โ†Large Language Models โ€” The Engines Behind Modern AI๐ŸŒฒ AI Forestโ†’