Skip to content

Commit cd5cb27

Browse files
committed
📝 Add prompt engineering guidelines fragments
1 parent b289c40 commit cd5cb27

File tree

4 files changed

+1210
-51
lines changed

4 files changed

+1210
-51
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# Compact Guide to Prompt Engineering with AI Assistants
2+
3+
## Introduction
4+
5+
This guide provides strategies and best practices to effectively interact with AI assistants, enhancing their output accuracy and quality.
6+
7+
## Table of Contents
8+
9+
1. [Be Clear, Direct, and Detailed](#be-clear-direct-and-detailed)
10+
2. [Use Examples to Guide Behavior](#use-examples-to-guide-behavior)
11+
3. [Encourage Step-by-Step Thinking](#encourage-step-by-step-thinking)
12+
4. [Use XML Tags to Structure Prompts](#use-xml-tags-to-structure-prompts)
13+
5. [Assign a Role Using System Prompts](#assign-a-role-using-system-prompts)
14+
6. [Prefill Responses for Output Control](#prefill-responses-for-output-control)
15+
7. [Chain Prompts for Complex Tasks](#chain-prompts-for-complex-tasks)
16+
8. [Tips for Long Contexts](#tips-for-long-contexts)
17+
18+
---
19+
20+
## Be Clear, Direct, and Detailed
21+
22+
Treat the AI assistant as a new employee who needs explicit instructions. Provide precise and detailed prompts to get better responses.
23+
24+
- **Golden Rule:** If a colleague with minimal context finds your prompt confusing, the assistant likely will too.
25+
- **How to Be Clear:**
26+
- Provide context (purpose, audience, workflow, goals).
27+
- Be specific about what you want.
28+
- Use numbered lists or bullet points for instructions.
29+
30+
**Example: Anonymizing Customer Feedback**
31+
32+
*Clear Prompt:*
33+
34+
```markdown
35+
Your task is to anonymize customer feedback for our quarterly review.
36+
37+
Instructions:
38+
1. Replace all customer names with "CUSTOMER_[ID]".
39+
2. Replace email addresses with "EMAIL_[ID]@example.com".
40+
3. Redact phone numbers as "PHONE_[ID]".
41+
4. Leave product names intact.
42+
5. If no PII is found, copy the message verbatim.
43+
6. Output only the processed messages, separated by "---".
44+
45+
Data to process: {{FEEDBACK_DATA}}
46+
```
47+
48+
---
49+
50+
## Use Examples to Guide Behavior
51+
52+
Including relevant examples helps the assistant understand the desired output format and style.
53+
54+
- **Benefits:**
55+
- Improves accuracy and consistency.
56+
- Helps with complex tasks requiring specific formats.
57+
58+
- **Crafting Examples:**
59+
- Make them relevant and diverse.
60+
- Wrap examples in `<example>` tags.
61+
62+
**Example: Analyzing Customer Feedback**
63+
64+
*Prompt with Example:*
65+
66+
```markdown
67+
Our CS team needs to categorize feedback. Use categories: UI/UX, Performance, Feature Request, Integration, Pricing, Other. Rate sentiment and priority.
68+
69+
<example>
70+
Input: The new dashboard is confusing and slow.
71+
Category: UI/UX, Performance
72+
Sentiment: Negative
73+
Priority: High
74+
</example>
75+
76+
Now, analyze this feedback: {{FEEDBACK}}
77+
```
78+
79+
---
80+
81+
## Encourage Step-by-Step Thinking
82+
83+
For complex tasks, instruct the assistant to think through the problem step-by-step.
84+
85+
- **Benefits:**
86+
- Increases accuracy and coherence.
87+
- Helps with debugging by revealing the thought process.
88+
89+
- **How to Prompt:**
90+
- Include phrases like "Think step-by-step."
91+
- Use structured prompts with `<thinking>` and `<answer>` tags.
92+
93+
**Example: Financial Advice**
94+
95+
*Prompt:*
96+
97+
```markdown
98+
You're a financial advisor. A client has $10,000 to invest and needs it in 5 years for a house down payment. Option A: Volatile stock with 12% annual return. Option B: Bond with guaranteed 6% annual return. Which do you recommend? Think step-by-step.
99+
```
100+
101+
*Assistant's Response:*
102+
103+
```markdown
104+
<thinking>
105+
[Step-by-step analysis]
106+
</thinking>
107+
108+
<answer>
109+
[Recommendation]
110+
</answer>
111+
```
112+
113+
---
114+
115+
## Use XML Tags to Structure Prompts
116+
117+
XML tags help organize prompts with multiple components, improving clarity and reducing errors.
118+
119+
- **Benefits:**
120+
- Clearly separates instructions, context, and examples.
121+
- Enhances parseability of outputs.
122+
123+
- **Best Practices:**
124+
- Be consistent with tag names.
125+
- Nest tags appropriately.
126+
127+
**Example: Generating Financial Reports**
128+
129+
*Prompt:*
130+
131+
```markdown
132+
You're a financial analyst at AcmeCorp.
133+
134+
<data>{{SPREADSHEET_DATA}}</data>
135+
136+
<instructions>
137+
1. Include sections: Revenue Growth, Profit Margins, Cash Flow.
138+
2. Highlight strengths and areas for improvement.
139+
</instructions>
140+
141+
Use a concise and professional tone.
142+
143+
<formatting_example>{{Q1_REPORT}}</formatting_example>
144+
```
145+
146+
---
147+
148+
## Assign a Role Using System Prompts
149+
150+
Setting a role in the system prompt tailors the assistant's responses to fit a specific persona or expertise.
151+
152+
- **Benefits:**
153+
- Enhances accuracy in specialized tasks.
154+
- Adjusts tone and focus.
155+
156+
- **How to Assign a Role:**
157+
158+
Include a role description in the system parameter of your API call.
159+
160+
```python
161+
import anthropic
162+
163+
client = anthropic.Anthropic()
164+
165+
response = client.messages.create(
166+
model="assistant-model",
167+
max_tokens=2048,
168+
system="You are the General Counsel of a Fortune 500 tech company.",
169+
messages=[
170+
{"role": "user", "content": "Analyze this contract..."}
171+
]
172+
)
173+
174+
print(response.content)
175+
```
176+
177+
---
178+
179+
## Prefill Responses for Output Control
180+
181+
By prefilling part of the assistant's response, you can guide the format and content of the output.
182+
183+
- **Benefits:**
184+
- Controls formatting (e.g., JSON, XML).
185+
- Helps maintain character consistency.
186+
187+
- **How to Prefill:**
188+
189+
Include the initial text in the assistant's response field before generating the reply.
190+
191+
**Example:**
192+
193+
*Prefill:*
194+
195+
```markdown
196+
Assistant: {
197+
```
198+
199+
*Assistant's Completion:*
200+
201+
```markdown
202+
"name": "Product Name",
203+
"price": "$49.99",
204+
"colors": ["black", "white"]
205+
}
206+
```
207+
208+
---
209+
210+
## Chain Prompts for Complex Tasks
211+
212+
Break down complex tasks into smaller subtasks handled sequentially or in parallel.
213+
214+
- **Benefits:**
215+
- Improves accuracy and clarity.
216+
- Easier to debug and refine.
217+
218+
- **How to Chain Prompts:**
219+
- Identify and separate subtasks.
220+
- Use XML tags to pass outputs between prompts.
221+
- Keep each subtask focused.
222+
223+
**Example: Legal Contract Analysis**
224+
225+
1. **Prompt 1:** Analyze the contract and output findings in `<risks>` tags.
226+
2. **Prompt 2:** Draft an email based on `<risks>`.
227+
3. **Prompt 3:** Review the email for tone and clarity.
228+
229+
---
230+
231+
## Tips for Long Contexts
232+
233+
When working with large amounts of data, follow these guidelines.
234+
235+
- **Place Long Data at the Top:** Include extensive documents before your query.
236+
- **Structure with XML Tags:** Organize multiple documents using tags for clarity.
237+
- **Ground Responses in Quotes:** Ask the assistant to reference specific parts of the data.
238+
239+
**Example Structure:**
240+
241+
```markdown
242+
<documents>
243+
<document>
244+
<source>Document 1</source>
245+
<content>{{DOC1_CONTENT}}</content>
246+
</document>
247+
<document>
248+
<source>Document 2</source>
249+
<content>{{DOC2_CONTENT}}</content>
250+
</document>
251+
</documents>
252+
253+
[Your query or task]
254+
```

0 commit comments

Comments
 (0)