Prompt Engineering Fundamentals
dlab.berkeley.edu
This workshop
📝 Poll 1: How often do you use LLMs like Claude or ChatGPT, and what do you use them for?
LLM chatbots
What is a “large amount of text”?
How do LLM chatbots work?
How are words predicted?
Are models probabilistic or deterministic?
What is prompt engineering?
The art of effectively communicating with AI to elicit desired results.
What is prompt engineering?
Before you do prompt engineering…
Model selection matters
📝 Poll 2: Model selection
Reasoning models
Reasoning or not?
Kinds of input
Prompting techniques
Let’s start from a barebones prompt
Suppose we’re interested in searching for some datasets on environmental discourse as a potential for a final project.
We could try…
https://chatgpt.com/share/68a6638d-4030-8012-837b-6b241d245350
Let’s start from a barebones prompt
Or, in newer models, we may trigger “thinking”, which can produce some interesting results…
While this is good, we can do better!
1. Write clear instructions
Example: Write clear instructions
Worse | Better |
How do I add numbers in Excel? | How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called "Total". |
Summarize the meeting notes. | Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any. |
Analyze this agreement for potential risks and liabilities: {{CONTRACT}}. Focus on indemnification, limitation of liability, and IP ownership clauses. | Analyze this software licensing agreement for legal risks and liabilities. We’re a multinational enterprise considering this agreement for our core data infrastructure. <agreement> {{CONTRACT}} </agreement> |
Exercise: Write clear instructions
PROMPT
Summarize this.�https://arxiv.org/abs/1706.03762
2. Specify output
3. Provide examples (few-shot)
4. Apply constraints
5. Provide a role
Worse | Better |
Analyze this software licensing agreement for potential risks: <contract> {{CONTRACT}} </contract> Focus on indemnification, liability, and IP ownership. | You are the General Counsel of a Fortune 500 tech company. We’re considering this software licensing agreement for our core data infrastructure: <contract> {{CONTRACT}} </contract> Analyze it for potential risks, focusing on indemnification, liability, and IP ownership. Give your professional opinion. |
Exercise: Provide a role
ROLE PROMPT
• You are a friendly high-school teacher…
• You are a meticulous patent lawyer…
• You are a sarcastic stand-up comic…
PROMPT
Explain quantum computing to me.
📝 Poll 3: Strengths and Weaknesses
Tasks
Classify each as an LLM strength or weakness.
Privacy
(Preventing) hallucination
Prompt injection
Models do not really distinguish between instructions and input
Tips
1. Provide rich context
2. Ask for abundance
Tips
3. Use interactive approaches
4. Advanced features to leverage
Tips
5. Common pitfalls to avoid
Exercise: Iterative Prompt Refinement
Your task is to create a prompt that makes an LLM effectively evaluate customer service email responses. You'll start with a basic prompt and iteratively improve it using the techniques we've covered.
Goal:
Build a prompt that can accurately assess customer service emails on a scale of 1-10, considering factors like helpfulness, tone, completeness, and professionalism.
Test Case Materials
Customer Inquiry: "I ordered a laptop 5 days ago (Order #12345) and it still hasn't shipped. The website said 2-3 day processing. I need this for an important presentation next week. What's going on?"
Three Response Examples to Evaluate:
Response A: "Hi! Thanks for reaching out. I see your order #12345 for the laptop. There was a slight delay in our warehouse, but I've personally escalated this and it will ship tomorrow with expedited delivery at no extra charge. You'll receive tracking info within 24 hours. I've also added a 15% discount to your account for the inconvenience. I understand how important this is for your presentation - please let me know if you need anything else!"
Response B: "Your order is delayed. It will ship soon. Check your email for tracking."
Response C: "I apologize for any inconvenience. Unfortunately, that item is currently backordered and we don't have an estimated ship date. You can cancel your order if you'd like a refund, or wait for it to become available. Let me know what you'd prefer."
Stage 1: Basic Prompt
Create your initial prompt without any special techniques:
Your Prompt:
[Write your basic prompt here]
Test Results: Rate how well it evaluates the three responses. What's missing?
Stage 2: Add Clear Instructions
Revise your prompt with specific, clear instructions about what to evaluate and how.
Techniques to Apply:
Test Results: How did the clarity improve the evaluations?
Stage 3: Add Examples (Few-Shot)
Include 1-2 example evaluations to show the LLM exactly what you want.
Techniques to Apply:
Test Results: Are the evaluations more aligned with your expectations?
Stage 4: Apply Constraints
Add specific constraints and boundaries to prevent unwanted outputs.
Techniques to Apply:
Test Results: Are the scores more reliable and consistent?
Stage 5: Provide a Role
Give the LLM a specific role/persona to enhance its performance.
Techniques to Apply:
Final Test Results: Compare your final prompt's performance to your initial version.
Reflection Questions
Biggest Improvement: Which technique made the most dramatic improvement to your prompt's performance?
Unexpected Challenges: What aspects of the task were harder to prompt for than expected?
Trade-offs: Did any techniques conflict with each other or require balancing?
Real-world Application: How would you adapt this approach for other evaluation tasks in your work?
The Future of Data Science
Join D-Lab’s Mailing List
Stay up to date with upcoming workshops, and campus job and funding opportunities!