Prompt Engineering
When you talk to an AI model (like Gemini or ChatGPT), the quality of your question decides the quality of the answer. Writing better questions for AI models is called Prompt Engineering.
This is one of the most important skills for AI engineers in 2027.
1. Why Prompts Matter
Bad Prompt:
"Tell me about Python"
→ The AI gives a very long, generic answer that is not useful.
Good Prompt:
"Explain what a Python list is in 3 simple sentences. Use an everyday example. The audience is a complete beginner who has never coded before."
→ The AI gives a focused, helpful, and clear answer.
The difference between a junior and senior AI engineer is the quality of their prompts.
2. The Anatomy of a Great Prompt
Every good prompt has these parts:
1. ROLE → Who should the AI act as?
2. TASK → What should it do?
3. CONTEXT → What background info does it need?
4. FORMAT → How should it structure the output?
5. TONE → How should it speak?
Example:
ROLE: You are a friendly Python tutor who teaches in simple English.
TASK: Explain what a for loop is.
CONTEXT: The student has just learned about variables and print statements.
FORMAT: Use a real-world analogy first, then show a code example with comments.
TONE: Encouraging and simple, like talking to a friend.
3. System Prompts vs User Prompts
When building AI applications, there are two types of messages:
System Prompt (Hidden Instructions)
Sets the AI's behavior. The user never sees this.
system_prompt = """You are a Python coding assistant.
- Always respond in simple English
- Include code examples
- Keep answers under 200 words"""
User Prompt (The User's Question)
The actual question from the user.
user_prompt = "How do I read a CSV file in Python?"
Putting Them Together (API Call):
payload = {
"contents": [
{"role": "user", "parts": [{"text": system_prompt}]},
{"role": "model", "parts": [{"text": "Understood! I will follow these rules."}]},
{"role": "user", "parts": [{"text": user_prompt}]}
]
}
4. Few-Shot Prompting (Teaching by Example)
Instead of explaining what you want, show the AI examples of the output you expect:
Convert the following sentences to Python variable names:
Input: "user full name" → Output: user_full_name
Input: "total items in cart" → Output: total_items_in_cart
Input: "maximum retry count" → Output: maximum_retry_count
Now convert this:
Input: "student exam score"
The AI will respond: student_exam_score
Why this works: The AI learns the pattern from your examples and applies it to new inputs.
5. Chain-of-Thought (Make AI Think Step by Step)
For complex problems, tell the AI to think step by step before giving the final answer:
Without Chain-of-Thought:
"What is 17 × 23?"
→ AI might give a wrong answer directly
With Chain-of-Thought:
"What is 17 × 23? Think step by step before giving the final answer."
AI response:
Step 1: 17 × 20 = 340
Step 2: 17 × 3 = 51
Step 3: 340 + 51 = 391
Final answer: 391
This technique works great for coding, debugging, and math problems.
6. Building a Simple Chatbot with Memory
import requests
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
# Store conversation history
conversation = [
{"role": "user", "parts": [{"text": "You are a helpful Python tutor. Keep answers short and simple."}]},
{"role": "model", "parts": [{"text": "Got it! I am your Python tutor. Ask me anything!"}]}
]
def chat(user_message):
# Add user message to history
conversation.append({"role": "user", "parts": [{"text": user_message}]})
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
payload = {"contents": conversation}
response = requests.post(url, json=payload)
data = response.json()
ai_reply = data["candidates"][0]["content"]["parts"][0]["text"]
# Add AI response to history (this is the "memory")
conversation.append({"role": "model", "parts": [{"text": ai_reply}]})
return ai_reply
# Have a conversation
print(chat("What is a list in Python?"))
print(chat("Give me an example"))
print(chat("How do I add items to it?"))
The "memory" trick: By sending the entire conversation history with each request, the AI remembers what was said before.
Summary
| Technique | What it Does | When to Use |
|---|---|---|
| Role + Task + Format | Structures your prompt clearly | Every time |
| System Prompt | Sets AI behavior invisibly | When building apps |
| Few-Shot | Teaches by showing examples | When you want a specific output format |
| Chain-of-Thought | Makes AI reason step by step | For complex problems |
| Conversation Memory | AI remembers past messages | When building chatbots |
- Good prompts = good AI output. Garbage prompt in, garbage answer out.
- These techniques work with any AI model — Gemini, Claude, GPT, Llama, etc.
Coming Soon
This AI & ML module is currently under development. Stay tuned for the advanced premium curriculum!