Prompt Engineering
Prompt Engineering is the practice of designing and refining inputs (prompts) to maximize the performance and accuracy of Large Language Models (LLMs). Because LLMs are probabilistic engines and not deterministic code, how you ask a question drastically changes the mathematical space the LLM searches to find your answer.
1. Zero-Shot Prompting
Asking a question with absolutely zero context or examples. - Prompt: "Determine the sentiment of this text: I love the new features." - When to use: For basic tasks using massive models (like GPT-4 or Claude 3.5 Sonnet) that already understand the concept deeply.
2. Few-Shot Prompting
Providing the model with 2 to 5 examples of the exact input/output format you expect before asking the actual question. - Why it works: It forces the LLM to statistically heavily weight the format you provided when calculating the probabilities for its final answer. Highly effective for extracting JSON from messy text.
3. Chain-of-Thought (CoT) Prompting
Adding the magic phrase: "Let's think step by step." - Why it works: LLMs do not have internal monologues. If you ask a complex math question, a standard LLM will immediately try to predict the final answer token and likely fail. By forcing it to output intermediate reasoning steps (producing tokens like "Step 1: First we carry the two..."), you give the model "scratchpad space" to calculate intermediate math before attempting the final answer.
4. Role Prompting
Assigning a persona to the model. - Prompt: "You are a senior cybersecurity engineer auditing a Linux server..." - Why it works: By defining a role, you narrow down the model's vast probability universe. It will stop weighting generic internet answers highly and instead pull mathematical weights from training data related to IT, security, and networking.
How to execute the examples:
Go to the Examples/ folder and run the script:
python GenAI_PromptEngineering.py