Prompt Engineering Deep Dive
A comprehensive technical reference with real-world examples, code, and advanced patterns.
1. Model Configuration
Critical settings to control output behavior.
Temperature
0.1 (Precise): Math, Logic, Code.
0.7+ (Creative): Writing, Ideation.
Top P (Nucleus)
Restricts token pool to top % probability mass. Lower = more focused.
Max Tokens
Hard limit on output size (e.g., 4096 or 8192).
Harm Block
BLOCK_NONE to BLOCK_HIGH. Controls content filtering stringency.
2. The Chain of Thought (CoT) Family
The foundation of reasoning. Forcing the model to "show its work".
A. Standard Chain of Thought
Math & LogicBy simply asking the model to explain step-by-step, accuracy on math and logic problems increases dramatically.
2. Identify values: Dist = 120km, Time = 2hr
3. Calculate: 120 / 2 = 60
4. Answer: 60 km/h
cot_prompt = ChatPromptTemplate.from_messages([
("system", "Solve problems step-by-step."),
("human", "Calculate speed: {input}"),
])
# Result: AI outputs the numbered steps aboveB. Zero-Shot CoT
Triggering reasoning without examples using magic phrases.
"Let's think step by step."
Use Case: When you don't have training data but need better logic.
prompt = "Q: {input}\nA: Let's think step by step."C. Few-Shot CoT
Providing 1-3 full examples to guide the pattern.
prompt = """Example 1: ...
Example 2: ...
Q: {input}"""3. Advanced Architectures
Complex structures for complex problems.
🤔 ReAct (Reason + Act)
The gold standard for Agents. It combines internal reasoning (Thought) with external tool use (Action).
Key Components
- Thought: Model "talks to itself" to plan.
- Action: Emits a standardized command to a tool.
- Observation: The output from the code/API tool.
react_template = """
Thought: {agent_scratchpad}
Action: ...
Observation: ...
"""🌳 Tree of Thoughts (ToT)
Problem solving by searching. Generate multiple branches, evaluate, and backtrack.
Branch 2: Bike Lanes... (Weather?)
Branch 3: Mixed Bus/Light Rail... (Balanced)
prompt = "Generate 3 approaches. Evaluate each. Pick best."
🔄 Self-Consistency
"Majority Voting". Ask the same question 3-5 times (high temp), pick answer that appears most.
results = [chain.invoke(x) for x in range(3)] final = max(set(results), key=results.count)
🏗️ Least-to-Most Prompting
Complex TasksDecompose a huge problem into sub-problems. Solve the easy ones first to build context for the hard ones.
2. Get Data (Yahoo Finance)
3. Clean Data (Outliers)
4. Feature Eng (RSI, MACD)
5. Train Model (LSTM)
prompt_1 = "Break this into steps." prompt_2 = "Solve step 1." prompt_3 = "Given step 1 answer, solve step 2..."
Specialized Techniques Reference
4. Ethics & Optimization
Building responsible and effective AI.
Optimization Strategies
- • Iterative Refinement: The "loop" of improvement.
- • A/B Testing: Compare Version A vs Version B systematically.
- • Prompt Libraries: Version control your best prompts.
- Metrics: Relevance, Accuracy, Coherence.
API Safety Settings
safety_settings={
HarmCategory.HATE_SPEECH: BLOCK_NONE,
HarmCategory.DANGEROUS: BLOCK_MEDIUM,
}Control filters for Hate, Dangerous, Sexual, and Harassment content.