Gen AI — Prompt Engineering
What is prompt engineering?
Designing inputs to get desired outputs from AI models.
Basic principles
- Be specific: Clear instructions
- Provide context: Background information
- Set constraints: Output format, length
- Use examples: Show desired output
Bad vs good prompts
# Bad
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Write about dogs"}]
)
# Good
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": """
Write a 100-word informative paragraph about golden retrievers.
Include their temperament, exercise needs, and suitability for families.
Use a friendly, accessible tone.
"""}]
)
Prompt templates
def create_prompt(topic, audience, length):
return f"""
Write a {length}-word explanation about {topic}.
Target audience: {audience}.
Tone: Professional but accessible.
Include one practical example.
"""
Role prompting
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an expert Python developer."},
{"role": "user", "content": "Write a function to sort a list"}
]
)
Output formatting
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": """
List 3 Python libraries for web scraping.
Format as:
1. Library name - Brief description
2. Library name - Brief description
3. Library name - Brief description
"""}]
)
Mini Practice
- Rewrite a bad prompt to be specific
- Create a prompt template
- Use role prompting
- Specify output format
Up Next
Continue with Zero-Shot - No examples needed.
Related Topics
Frequently Asked Questions about Prompt Engineering
What is Prompt Engineering in Gen AI?
Prompt Engineering is a fundamental concept in Gen AI. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Prompt Engineering?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Prompt Engineering.
Why is Prompt Engineering important in Gen AI?
Prompt Engineering is essential for Gen AI development. Understanding this concept will help you write better code and solve real-world problems more effectively.