<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Gpt · QuickRef</title><link>https://qref.eknath.dev/tags/gpt/</link><description>Gpt updates from QuickRef</description><language>en-us</language><atom:link href="https://qref.eknath.dev/tags/gpt/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Sun, 01 Jan 2023 00:00:00 +0000</lastBuildDate><item><title>Prompt Engineering</title><link>https://qref.eknath.dev/ai/prompt-engineering/</link><guid isPermaLink="true">https://qref.eknath.dev/ai/prompt-engineering/</guid><pubDate>Sun, 01 Jan 2023 00:00:00 +0000</pubDate><category>prompt-engineering</category><category>ai</category><category>llm</category><category>gpt</category><category>claude</category><category>gemini</category><description>Essential techniques and patterns for effective AI prompt engineering with LLMs like GPT, Claude, and Gemini.</description><content:encoded><![CDATA[<h2 id="overview">Overview</h2>
<p>Prompt engineering is the art and science of crafting effective prompts for Large Language Models (LLMs) to achieve desired outcomes. It involves understanding how to structure, phrase, and format inputs to get the best possible responses from AI systems.</p>
<h2 id="core-principles">Core Principles</h2>
<h3 id="clarity-and-specificity">Clarity and Specificity</h3>
<pre tabindex="0"><code>❌ Poor: &#34;Write about dogs&#34;
✅ Good: &#34;Write a 300-word informative article about Golden Retriever training techniques for first-time dog owners&#34;
</code></pre><h3 id="context-setting">Context Setting</h3>
<pre tabindex="0"><code>❌ Poor: &#34;Explain recursion&#34;
✅ Good: &#34;Explain recursion to a computer science student who understands basic programming concepts but struggles with advanced algorithms. Use simple examples and analogies.&#34;
</code></pre><h3 id="role-definition">Role Definition</h3>
<pre tabindex="0"><code>❌ Poor: &#34;Help with my code&#34;
✅ Good: &#34;As an experienced Python developer, help me debug this function and explain the error in detail&#34;
</code></pre><h2 id="framework">Framework</h2>
<p>Getting the most out of generative AI is all about understanding inputs and outputs. What are you hoping to get from the tool? That’s the output. What kind of prompt is going to help you get it? That’s the input. And there’s a simple framework that helps you provide clear and specific directions to any gen AI tool.</p>
<p>The <strong>TCREI</strong> framework (a useful acronym for &ldquo;Thoughtfully Create Really Excellent Inputs&rdquo;) stands for:</p>
<ul>
<li><strong>T</strong>ask</li>
<li><strong>C</strong>ontext</li>
<li><strong>R</strong>eferences</li>
<li><strong>E</strong>valuate</li>
<li><strong>I</strong>terate</li>
</ul>
<h2 id="prompt-structure">Prompt Structure</h2>
<h3 id="basic-template">Basic Template</h3>
<pre tabindex="0"><code>[ROLE/CONTEXT]
[TASK/INSTRUCTION]
[SPECIFIC REQUIREMENTS]
[FORMAT/CONSTRAINTS]
[EXAMPLES (if needed)]
</code></pre><h3 id="example-structure">Example Structure</h3>
<pre tabindex="0"><code>Role: You are a senior software engineer with 10 years of experience
Task: Review this code and provide feedback
Requirements: Focus on security vulnerabilities and performance issues
Format: Provide a numbered list with explanations
Constraints: Keep explanations under 100 words each
</code></pre><h2 id="essential-techniques">Essential Techniques</h2>
<h3 id="1-zero-shot-prompting">1. Zero-Shot Prompting</h3>
<pre tabindex="0"><code>Prompt: &#34;Translate the following English text to French: &#39;Hello, how are you today?&#39;&#34;

Response: &#34;Bonjour, comment allez-vous aujourd&#39;hui ?&#34;
</code></pre><h3 id="2-few-shot-prompting">2. Few-Shot Prompting</h3>
<pre tabindex="0"><code>Prompt: &#34;Convert these statements to questions:
Statement: The weather is nice today.
Question: Is the weather nice today?

Statement: She likes coffee.
Question: Does she like coffee?

Statement: The meeting starts at 3 PM.
Question: &#34;

Response: &#34;Does the meeting start at 3 PM?&#34;
</code></pre><h3 id="3-chain-of-thought-cot">3. Chain of Thought (CoT)</h3>
<pre tabindex="0"><code>Prompt: &#34;Solve this step by step: If a train travels 120 miles in 2 hours, what&#39;s its speed in mph?&#34;

Response: &#34;Let me solve this step by step:
1. Distance = 120 miles
2. Time = 2 hours
3. Speed = Distance ÷ Time
4. Speed = 120 ÷ 2 = 60 mph
Therefore, the train&#39;s speed is 60 mph.&#34;
</code></pre><h3 id="4-self-consistency">4. Self-Consistency</h3>
<pre tabindex="0"><code>Prompt: &#34;Solve this problem using three different approaches: What&#39;s 15% of 240?&#34;

Approach 1: 240 × 0.15 = 36
Approach 2: (15/100) × 240 = 36
Approach 3: 240 ÷ 100 × 15 = 36
</code></pre><h3 id="5-tree-of-thoughts">5. Tree of Thoughts</h3>
<pre tabindex="0"><code>Prompt: &#34;Let&#39;s explore multiple solution paths for this problem:
Problem: How to increase website conversion rate?

Path 1: User Experience improvements
Path 2: Pricing strategy optimization
Path 3: Marketing message refinement

Analyze each path and recommend the best approach.&#34;
</code></pre><h2 id="advanced-patterns">Advanced Patterns</h2>
<h3 id="system-prompts">System Prompts</h3>
<pre tabindex="0"><code>System: You are a helpful assistant that always responds in JSON format.
User: What&#39;s the weather like?
Assistant: {
  &#34;response&#34;: &#34;I don&#39;t have access to real-time weather data&#34;,
  &#34;suggestion&#34;: &#34;Please check a weather website or app for current conditions&#34;
}
</code></pre><h3 id="constitutional-ai">Constitutional AI</h3>
<pre tabindex="0"><code>Initial Prompt: &#34;Write a social media post about politics&#34;
Constitutional Rule: &#34;Ensure the response is balanced, factual, and doesn&#39;t promote extreme viewpoints&#34;
Refined Response: [Balanced political content]
</code></pre><h3 id="prompt-chaining">Prompt Chaining</h3>
<pre tabindex="0"><code>Prompt 1: &#34;Analyze this customer feedback and identify key themes&#34;
Response 1: [Themes: pricing, support, features]

Prompt 2: &#34;Based on these themes: [themes], create action items for the product team&#34;
Response 2: [Specific action items]
</code></pre><h2 id="content-specific-techniques">Content-Specific Techniques</h2>
<h3 id="code-generation">Code Generation</h3>
<pre tabindex="0"><code>Prompt: &#34;Create a Python function that:
- Takes a list of numbers as input
- Returns the median value
- Handles edge cases (empty list, single element)
- Includes docstring and type hints
- Add unit tests&#34;
</code></pre><h3 id="creative-writing">Creative Writing</h3>
<pre tabindex="0"><code>Prompt: &#34;Write a short story (500 words) about a time traveler who accidentally changes history. 
Style: Humorous, first-person narrative
Setting: Modern day coffee shop
Constraint: Must include the phrase &#39;butterfly effect&#39; naturally
Tone: Light-hearted but with underlying tension&#34;
</code></pre><h3 id="technical-documentation">Technical Documentation</h3>
<pre tabindex="0"><code>Prompt: &#34;Create API documentation for this endpoint:
- Endpoint: POST /api/users
- Purpose: Create new user account
- Include: request/response examples, error codes, authentication requirements
- Format: OpenAPI specification
- Audience: Backend developers&#34;
</code></pre><h3 id="data-analysis">Data Analysis</h3>
<pre tabindex="0"><code>Prompt: &#34;Analyze this dataset and provide insights:
[Dataset description]
Please provide:
1. Summary statistics
2. Key trends and patterns
3. Potential outliers
4. Recommendations based on findings
5. Visualizations suggestions&#34;
</code></pre><h2 id="platform-specific-tips">Platform-Specific Tips</h2>
<h3 id="gpt-openai">GPT (OpenAI)</h3>
<pre tabindex="0"><code># Effective with:
- Role-playing scenarios
- Step-by-step instructions
- Creative tasks
- Code generation

# Best practices:
- Use clear delimiters (```, ---, ###)
- Specify output format explicitly
- Break complex tasks into steps
</code></pre><h3 id="claude-anthropic">Claude (Anthropic)</h3>
<pre tabindex="0"><code># Effective with:
- Analytical tasks
- Long-form content
- Safety-conscious responses
- Nuanced reasoning

# Best practices:
- Provide context about harm prevention
- Use structured thinking prompts
- Request explanations of reasoning
</code></pre><h3 id="gemini-google">Gemini (Google)</h3>
<pre tabindex="0"><code># Effective with:
- Multimodal tasks
- Factual information
- Real-time reasoning
- Integration tasks

# Best practices:
- Leverage multimodal capabilities
- Use specific, factual queries
- Provide clear context
</code></pre><h2 id="common-patterns">Common Patterns</h2>
<h3 id="question-answering">Question Answering</h3>
<pre tabindex="0"><code>Template: &#34;Based on the following context, answer the question concisely:

Context: [relevant information]
Question: [specific question]
Answer format: [bullet points/paragraph/etc.]
If unsure, say: &#39;I don&#39;t have enough information to answer this question.&#39;&#34;
</code></pre><h3 id="summarization">Summarization</h3>
<pre tabindex="0"><code>Template: &#34;Summarize the following text:
- Length: [word count/paragraph count]
- Audience: [target audience]
- Focus: [key aspects to emphasize]
- Style: [academic/casual/business]

Text: [content to summarize]&#34;
</code></pre><h3 id="classification">Classification</h3>
<pre tabindex="0"><code>Template: &#34;Classify the following into categories:
Categories: [list categories]
Criteria: [classification rules]
Output format: Category name and confidence level
If uncertain, provide top 2 categories.

Text to classify: [content]&#34;
</code></pre><h3 id="extraction">Extraction</h3>
<pre tabindex="0"><code>Template: &#34;Extract the following information from the text:
- [Field 1]: [description]
- [Field 2]: [description]
- [Field 3]: [description]

If a field is not found, return &#39;Not specified&#39;
Format as JSON.

Text: [source content]&#34;
</code></pre><h2 id="prompt-optimization">Prompt Optimization</h2>
<h3 id="iterative-refinement">Iterative Refinement</h3>
<pre tabindex="0"><code>Version 1: &#34;Write a blog post about AI&#34;
Version 2: &#34;Write a 800-word blog post about AI ethics for non-technical readers&#34;
Version 3: &#34;Write a 800-word blog post about AI ethics for non-technical readers, focusing on privacy concerns, bias issues, and practical implications for everyday users&#34;
</code></pre><h3 id="ab-testing-prompts">A/B Testing Prompts</h3>
<pre tabindex="0"><code>Prompt A: &#34;Explain machine learning simply&#34;
Prompt B: &#34;Explain machine learning using everyday analogies&#34;
Prompt C: &#34;Explain machine learning to a curious 12-year-old&#34;
</code></pre><h3 id="performance-metrics">Performance Metrics</h3>
<pre tabindex="0"><code>Evaluation Criteria:
- Accuracy: Does it answer correctly?
- Relevance: Is it on-topic?
- Clarity: Is it easy to understand?
- Completeness: Does it cover all aspects?
- Consistency: Are multiple runs similar?
</code></pre><h2 id="error-handling">Error Handling</h2>
<h3 id="common-issues">Common Issues</h3>
<pre tabindex="0"><code>Issue: Vague responses
Solution: Add specific constraints and examples

Issue: Inconsistent format
Solution: Provide clear format templates

Issue: Hallucination
Solution: Request citations and verify claims

Issue: Incomplete responses
Solution: Ask for step-by-step breakdown
</code></pre><h3 id="defensive-prompting">Defensive Prompting</h3>
<pre tabindex="0"><code>Template: &#34;Please [task]. Important constraints:
- Only use information from the provided context
- If you&#39;re unsure, explicitly state your uncertainty
- Cite sources when making claims
- If the task is unclear, ask for clarification
- Avoid speculation or assumptions&#34;
</code></pre><h2 id="best-practices">Best Practices</h2>
<h3 id="dos">Do&rsquo;s</h3>
<pre tabindex="0"><code>✅ Be specific and clear
✅ Provide context and examples
✅ Use structured format
✅ Test and iterate
✅ Define success criteria
✅ Use appropriate tone
✅ Break complex tasks into steps
</code></pre><h3 id="donts">Don&rsquo;ts</h3>
<pre tabindex="0"><code>❌ Don&#39;t be vague or ambiguous
❌ Don&#39;t overload with information
❌ Don&#39;t ignore safety considerations
❌ Don&#39;t expect perfect first attempts
❌ Don&#39;t forget to validate outputs
❌ Don&#39;t use offensive language
❌ Don&#39;t rely on single prompts for critical tasks
</code></pre><h2 id="evaluation-methods">Evaluation Methods</h2>
<h3 id="human-evaluation">Human Evaluation</h3>
<pre tabindex="0"><code>Criteria:
- Helpfulness (1-5 scale)
- Accuracy (1-5 scale)
- Clarity (1-5 scale)
- Relevance (1-5 scale)
- Overall quality (1-5 scale)
</code></pre><h3 id="automated-evaluation">Automated Evaluation</h3>
<pre tabindex="0"><code>Metrics:
- BLEU score (translation quality)
- ROUGE score (summarization quality)
- Perplexity (text quality)
- Custom scoring functions
</code></pre><h2 id="advanced-techniques">Advanced Techniques</h2>
<h3 id="meta-prompting">Meta-Prompting</h3>
<pre tabindex="0"><code>Prompt: &#34;Create a prompt that will help generate effective marketing copy for a SaaS product. The prompt should include:
- Target audience specification
- Product benefit focus
- Call-to-action guidance
- Brand voice requirements&#34;
</code></pre><h3 id="prompt-templates">Prompt Templates</h3>
<pre tabindex="0"><code>Template Engine:
&#34;As a {role}, {task} for {audience}. 
Focus on {key_aspects}. 
Use {tone} tone and {format} format.
Constraints: {limitations}&#34;

Usage:
role = &#34;UX designer&#34;
task = &#34;create wireframes&#34;
audience = &#34;mobile app users&#34;
key_aspects = &#34;usability and accessibility&#34;
tone = &#34;professional&#34;
format = &#34;annotated sketches&#34;
limitations = &#34;mobile-first design&#34;
</code></pre><h3 id="dynamic-prompting">Dynamic Prompting</h3>
<pre tabindex="0"><code>Conditional Logic:
&#34;If the user query is about {topic_A}, respond with {approach_A}.
If the user query is about {topic_B}, respond with {approach_B}.
If unclear, ask for clarification using this template: {clarification_template}&#34;
</code></pre><h2 id="troubleshooting">Troubleshooting</h2>
<h3 id="common-problems">Common Problems</h3>
<pre tabindex="0"><code>Problem: Inconsistent outputs
Solution: Add more constraints, use temperature settings

Problem: Too verbose
Solution: Specify length limits, use bullet points

Problem: Off-topic responses
Solution: Repeat key requirements, use system prompts

Problem: Factual errors
Solution: Request citations, use chain-of-thought reasoning
</code></pre><h3 id="debugging-techniques">Debugging Techniques</h3>
<pre tabindex="0"><code>1. Simplify the prompt
2. Test each component separately
3. Use explicit instructions
4. Add examples and counter-examples
5. Validate with different inputs
6. Monitor for edge cases
</code></pre><h2 id="tools-and-resources">Tools and Resources</h2>
<h3 id="prompt-testing-tools">Prompt Testing Tools</h3>
<pre tabindex="0"><code>- OpenAI Playground
- Anthropic Console
- Langchain Prompt Templates
- PromptLayer
- WeaveGPT
- Prompt Perfect
</code></pre><h3 id="evaluation-frameworks">Evaluation Frameworks</h3>
<pre tabindex="0"><code>- HELM (Holistic Evaluation of Language Models)
- BIG-bench
- EleutherAI Evaluation Harness
- Custom evaluation scripts
</code></pre><h2 id="quick-reference">Quick Reference</h2>
<h3 id="basic-prompt-structure">Basic Prompt Structure</h3>
<pre tabindex="0"><code>[Context] + [Task] + [Format] + [Constraints] = Effective Prompt
</code></pre><h3 id="essential-elements">Essential Elements</h3>
<pre tabindex="0"><code>- Clear objective
- Specific requirements
- Expected format
- Quality criteria
- Examples (when helpful)
- Constraints and limitations
</code></pre><h3 id="quality-checklist">Quality Checklist</h3>
<pre tabindex="0"><code>□ Is the task clearly defined?
□ Are requirements specific?
□ Is the format specified?
□ Are constraints mentioned?
□ Are examples provided?
□ Is the tone appropriate?
□ Are edge cases considered?
</code></pre><h2 id="see-also">See Also</h2>
<ul>
<li><a href="https://qref.eknath.dev/ai/managing-ai-agents">Managing AI Agents with AGENT.MD</a></li>
<li><a href="https://platform.openai.com/docs/guides/prompt-engineering">OpenAI Prompt Engineering Guide</a></li>
<li><a href="https://docs.anthropic.com/claude/docs/prompt-engineering">Anthropic Prompt Engineering</a></li>
<li><a href="https://ai.google.dev/docs/prompt_best_practices">Google AI Prompt Design</a></li>
<li><a href="https://www.promptengineering.org/">Prompt Engineering Institute</a></li>
<li><a href="https://python.langchain.com/docs/modules/model_io/prompts/">LangChain Prompt Templates</a></li>
</ul>
]]></content:encoded></item></channel></rss>