### Introduction to Expert Prompting This cheatsheet provides a comprehensive guide to crafting effective prompts for Large Language Models (LLMs) and engaging in productive conversations to achieve optimal results. Mastering prompt engineering is crucial for leveraging the full potential of AI. ### Core Principles of Effective Prompting - **Clarity & Specificity:** Be unambiguous. Avoid vague language. - **Context:** Provide necessary background information. - **Constraint:** Define boundaries, format, length, or style requirements. - **Persona:** Assign a role to the LLM (e.g., "Act as a senior data scientist"). - **Goal-Oriented:** Clearly state the desired outcome. - **Iterative Refinement:** Prompting is a process of trial and error. #### Why these principles matter Adhering to these principles helps the LLM understand your intent, reduces ambiguity, and guides it towards generating more relevant and accurate responses. ### Anatomy of a Best-in-Class System Prompt A system prompt establishes the LLM's identity, constraints, and initial instructions. It's the foundation of your interaction. #### 1. Role Assignment (Persona) Define who the LLM is. This shapes its tone, knowledge base, and approach. - **Example:** "You are an expert quantum physicist specializing in string theory." - **Poor Example:** "Be smart." #### 2. Goal/Objective Clearly state the primary purpose of the LLM's existence for this session. - **Example:** "Your goal is to explain complex quantum phenomena in an accessible way to a college freshman." - **Poor Example:** "Talk about physics." #### 3. Constraints & Rules Specify what the LLM should and should not do. This includes tone, style, length, safety guidelines, and forbidden actions. - **Examples:** - "Responses must be concise, under 200 words." - "Avoid jargon where possible, or explain it clearly." - "Never speculate or invent information. If you don't know, state that you don't know." - "Only answer questions related to physics." - **Poor Example:** "Don't be bad." #### 4. Output Format (if applicable) If the output needs a specific structure (JSON, Markdown table, bullet points), specify it here. - **Example:** "Format all explanations as a bulleted list, followed by a short summary paragraph." - **Poor Example:** "Make it look good." #### 5. Examples (Few-shot learning) Provide one or more input/output examples to demonstrate the desired behavior. This is highly effective. - **Example:** - `User: Explain superposition.` - `Assistant: Superposition is like a coin spinning in the air – it's both heads and tails until it lands. In quantum mechanics, a particle can exist in multiple states simultaneously until measured.` #### System Prompt Template ``` You are an [Role/Persona] specializing in [Domain/Area]. Your primary goal is to [Specific Objective]. Adhere to the following rules and constraints: 1. [Constraint 1] 2. [Constraint 2] 3. [Constraint 3] [Optional: Output Format instructions] [Optional: Examples] ``` ### Crafting Effective User Prompts Once the system prompt sets the stage, user prompts drive the interaction. #### 1. The "Think Step-by-Step" (Chain of Thought) Encourage the LLM to process information logically before responding. This significantly improves accuracy for complex tasks. - **Technique:** Include phrases like "Let's think step by step," "Walk me through your reasoning," or "First, identify X, then Y." #### 2. Role-Playing for the LLM Reinforce or introduce specific roles for the LLM within the user prompt. - **Example:** "As a literary critic, analyze the symbolism in 'Moby Dick'." #### 3. Providing Context and Background Even with a system prompt, specific user prompts often need their own context. - **Example:** "Given the following financial data for Q3 2023: [data], summarize the key trends." #### 4. Specifying Desired Output Format Reiterate or introduce specific formatting for the current response. - **Example:** "List the pros and cons of renewable energy in a two-column markdown table." #### 5. Iterative Questioning & Refinement Don't expect perfection on the first try. Break down complex tasks into smaller questions. - **"What if...?"** Explore alternatives. - **"Can you elaborate on...?"** Get more detail. - **"Summarize this for a [audience]."** Adjust complexity. - **"Rewrite this in the style of [author/tone]."** Change style. #### 6. Temperature & Top-P (Advanced) These parameters control the creativity/randomness of the LLM. - **Temperature:** - **Low (e.g., 0.1-0.3):** More deterministic, focused, good for factual tasks. - **High (e.g., 0.7-1.0):** More creative, diverse, good for brainstorming, creative writing. - **Top-P:** Similar to temperature, controls the diversity of word choices. #### 7. Token Limits Be mindful of the LLM's context window. Long prompts or conversations can hit limits, causing the LLM to "forget" earlier parts. Summarize or break down tasks if necessary. ### How to Have a Productive Chat with an LLM Treat the LLM as an intelligent assistant, not just a search engine. #### 1. Establish the "Session Goal" Before you start, define what you want to achieve by the end of the conversation. - **Example:** "My goal is to draft a project proposal outline." #### 2. Start with a Strong System Prompt As detailed above, this sets the foundation for the entire interaction. #### 3. One Idea Per Prompt (Generally) Avoid asking too many distinct questions in a single prompt unless they are clearly related and require a single, integrated answer. This prevents the LLM from getting confused or only answering the last part. #### 4. Be Patient and Persistent If the first response isn't perfect, don't give up. - **"That's not quite right. I meant X, not Y."** - **"Can you rephrase your last point to focus on [specific aspect]?"** #### 5. Use Follow-up Questions to Drill Down Build on previous responses. - **User:** "Explain the basics of machine learning." - **LLM:** *Provides basic explanation.* - **User:** "Now, focusing on supervised learning, what are some common algorithms?" #### 6. Explicitly Correct Mistakes If the LLM makes an error, point it out clearly. This helps it self-correct within the current session. - **Example:** "You mentioned 'deep learning' in your last response, but we were discussing 'reinforcement learning.' Please correct that." #### 7. Request Summaries For long conversations, ask the LLM to summarize previous points to ensure alignment. - **Example:** "Based on our discussion so far, can you summarize the main arguments for and against nuclear power?" #### 8. Manage Conversation Flow If the conversation goes off-topic, gently steer it back. - **Example:** "Let's get back to our original goal of outlining the project proposal." #### 9. Give Positive and Negative Feedback - **Positive:** "Excellent explanation of recursion, thank you!" (Encourages similar quality) - **Negative (Constructive):** "The last point was unclear. Can you simplify it for a non-technical audience?" #### 10. End with a Clear "Thank You" or "End of Task" This helps the LLM understand the task is complete. ### Advanced Prompting Techniques #### 1. Few-Shot Prompting Provide 1-3 examples of input-output pairs before asking for the main task. This is incredibly powerful for teaching the LLM a new pattern or format. - **Example:** `Task: Classify sentiment.` `Text: "I love this product." -> Positive` `Text: "This is terrible." -> Negative` `Text: "It's okay, I guess." -> Neutral` `Text: "The service was exceptional." ->` #### 2. CoT (Chain of Thought) Prompting Explicitly ask the LLM to "think step-by-step" or "show its reasoning." This is especially useful for complex problems, allowing the LLM to break down the task internally. - **Example:** "Let's think step by step. What are the intermediate steps to calculate the definite integral of $x^2$ from 0 to 1? Then provide the final answer." #### 3. Self-Consistency Generate multiple CoT paths, then choose the most common answer. (More applicable for programmatic interaction with LLMs). #### 4. Generated Knowledge Prompting Ask the LLM to generate relevant knowledge first, then use that knowledge to answer the main question. - **Example:** "First, generate 3-5 key facts about the history of artificial intelligence. Then, use those facts to explain the current trends in AI development." #### 5. Reflection/Self-Correction Ask the LLM to critique its own answer or identify potential flaws, then revise. - **Example:** "Review your previous answer. Are there any assumptions you made that might be incorrect? How could you improve it?" #### 6. Tree of Thought (ToT) Similar to CoT, but explores multiple reasoning paths in a tree-like structure, evaluating the progress of each path. (More complex, often requires custom tooling). #### 7. Prompt Chaining/Decomposition Break down a complex task into smaller, manageable sub-tasks, prompting the LLM for each step sequentially. - **Example:** 1. "Extract all key entities from this article: [article text]." 2. "Categorize these entities into 'People', 'Organizations', and 'Locations'." 3. "Summarize the relationships between the categorized entities." ### Troubleshooting Common LLM Issues #### 1. Irrelevant or Off-Topic Responses - **Solution:** Refine your system prompt for clearer constraints. Add "Only answer questions related to X." Be more specific in your user prompt. #### 2. Vague or Generic Answers - **Solution:** Ask for specifics. Use "Give examples," "Elaborate on," "Provide details for." Assign a more expert persona. #### 3. Repetitive Output - **Solution:** Increase temperature or Top-P slightly. Ask for "diverse examples" or "alternative perspectives." #### 4. Hallucinations (Invented Information) - **Solution:** Lower temperature. Emphasize "Do not speculate. Only use provided information or widely accepted facts." Ask the LLM to cite sources if possible. #### 5. Refusal to Answer (Safety Filters) - **Solution:** Rephrase your prompt to avoid sensitive keywords. Ensure your request is ethical and not violating any safety guidelines. If it's a legitimate request, try to explain the context or academic purpose. #### 6. Not Following Instructions - **Solution:** Make instructions more prominent (e.g., use bolding). Break down complex instructions into simpler, numbered steps. Use few-shot examples. #### 7. Output Too Long/Short - **Solution:** Explicitly state word count or length constraints (e.g., "Summarize in 3 sentences," "Provide a detailed analysis, approximately 500 words"). #### 8. Poor Formatting - **Solution:** Provide explicit formatting instructions (e.g., "Use a markdown table," "List as bullet points," "Respond in JSON format"). ### Ethical Considerations in Prompting - **Bias:** LLMs can reflect biases present in their training data. Be mindful of how your prompts might elicit or amplify these biases. - **Misinformation:** Always verify critical information provided by an LLM, especially for factual or sensitive topics. - **Privacy:** Never input sensitive personal, confidential, or proprietary information into public LLMs unless explicitly cleared by your organization. - **Transparency:** When presenting LLM-generated content, consider disclosing its origin, especially in academic or professional contexts. - **Harmful Content:** Avoid prompting for, or generating, content that is illegal, unethical, hateful, or promotes violence. ### Conclusion & Next Steps Mastering prompt engineering is an ongoing journey. Experiment, iterate, and continuously refine your approach. The best way to learn is by doing. #### Key Takeaways: - **System Prompts:** Set the stage, define the persona and rules. - **User Prompts:** Drive interaction with clarity, context, and constraints. - **Conversation:** Treat it as a dialogue, iterate, and refine. - **Advanced Techniques:** Leverage few-shot, CoT, and self-correction for complex tasks. - **Ethics:** Be responsible and critical of LLM outputs. Keep practicing, and you'll become an expert prompt engineer in no time!