Prompt Engineering Masterclass Tutorial: Modern Prompting Techniques

# Masterclass Prompt Engineering ## Daftar Isi 1. [Pendahuluan](#pendahuluan) 2. [Prasyarat](#prasyarat) 3. [Memahami Dasar-Dasar Prompt Engineering](#memahami-dasar-dasar-prompt-engineering) 4. [Ze...

By Ruby Abdullah · · tutorial
Prompt EngineeringLLMChain-of-ThoughtFew-ShotAIGPT

Prompt Engineering Masterclass

Table of Contents

  • Introduction
  • Prerequisites
  • Understanding Prompt Engineering Fundamentals
  • Zero-Shot Prompting
  • Few-Shot Prompting
  • Chain-of-Thought Prompting
  • Self-Consistency Prompting
  • Tree-of-Thought Prompting
  • Structured Output and JSON Mode
  • System Prompts and Prompt Templates
  • Prompt Evaluation
  • Common Pitfalls and How to Avoid Them
  • Best Practices
  • Conclusion

  • Introduction

    Prompt engineering is the discipline of crafting effective inputs to large language models (LLMs) to elicit accurate, relevant, and useful responses. As LLMs become integral to production systems, mastering prompt engineering is no longer optional -- it is a core skill for AI engineers, data scientists, and software developers alike.

    This tutorial provides a comprehensive, hands-on guide to advanced prompting techniques. You will learn how to apply zero-shot, few-shot, chain-of-thought, self-consistency, and tree-of-thought prompting strategies. You will also learn how to enforce structured outputs, design reusable prompt templates, evaluate prompt quality, and avoid common mistakes.

    All examples use Python with the OpenAI API, but the principles apply to any LLM provider.


    Prerequisites

    • Python 3.9 or higher
    • An OpenAI API key (or compatible LLM API)
    • Basic understanding of LLMs and API calls
    • Installed packages:

    pip install openai langchain pydantic
    

    Set your API key as an environment variable:

    export OPENAIAPIKEY="sk-your-key-here"
    


    Understanding Prompt Engineering Fundamentals

    A prompt is the textual input provided to an LLM. The quality of the output is directly proportional to the clarity, specificity, and structure of the prompt. There are several dimensions to consider:

    • Instruction clarity: Be explicit about what you want.
    • Context: Provide relevant background information.
    • Input/output format: Specify the desired format.
    • Constraints: Define boundaries (length, style, language).
    • Role assignment: Tell the model who it should act as.

    import openai
    
    

    client = openai.OpenAI()

    def callllm(prompt: str, system: str = "", model: str = "gpt-4o", temperature: float = 0.7) -> str:

    """Utility function to call the LLM with a given prompt."""

    messages = []

    if system:

    messages.append({"role": "system", "content": system})

    messages.append({"role": "user", "content": prompt})

    response = client.chat.completions.create(

    model=model,

    messages=messages,

    temperature=temperature,

    )

    return response.choices[0].message.content


    Zero-Shot Prompting

    Zero-shot prompting means asking the model to perform a task without providing any examples. The model relies entirely on its pre-trained knowledge.

    When to Use

    • The task is straightforward and well-known (e.g., summarization, translation).
    • You want to minimize prompt length and cost.
    • The model has strong prior knowledge of the domain.

    Example: Sentiment Analysis

    prompt = """Classify the sentiment of the following review as 'positive', 'negative', or 'neutral'.
    
    

    Review: "The product arrived on time and works exactly as described. Very satisfied with my purchase."

    Sentiment:"""

    result = callllm(prompt, temperature=0.0)

    print(result) # Expected: positive

    Example: Text Summarization

    prompt = """Summarize the following paragraph in exactly two sentences.
    
    

    Paragraph: "Machine learning is a subset of artificial intelligence that focuses on building

    Related Articles

    Complete Azure OpenAI Service Tutorial: GPT and LLMs on Azure

    Tutorial Lengkap Azure OpenAI Service: Enterprise AI dengan Model GPT Azure OpenAI Service menyediakan akses REST API ke...

    Complete LlamaIndex Tutorial: Building RAG Applications with LLMs

    Tutorial Lengkap LlamaIndex: Membangun Aplikasi RAG dengan LLM LlamaIndex adalah framework data yang powerful untuk memb...

    Complete Ollama Tutorial: Deploy LLMs Locally

    Tutorial Lengkap Ollama: Deploy LLM Secara Lokal Ollama adalah tool open-source yang memudahkan Anda menjalankan Large L...

    TRL Tutorial: LLM Post-Training with SFT, DPO, and Reward Modeling

    Post-Training LLM dengan TRL: SFT, Reward Modeling, dan DPO Setelah sebuah base language model selesai dipretraining, mo...