Skip to content

Commit b762e39

Browse files
authored
Merge pull request #507 from realpython/prompt-engineering
Update Prompt engineering materials
2 parents 5fc38da + 96924f6 commit b762e39

File tree

7 files changed

+185
-176
lines changed

7 files changed

+185
-176
lines changed

prompt-engineering/README.md

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Create and activate a [virtual environment](https://realpython.com/python-virtua
2020
(venv) $ python -m pip install openai
2121
```
2222

23+
Or, to make sure that you're using the same versions as shown in the tutorial, you can install from `requirements.txt` instead:
24+
25+
```bash
26+
(venv) $ python -m pip install -r requirements.txt
27+
```
28+
2329
## Usage
2430

2531
Read support chat conversations from a file, sanitize the text, classify by sentiment, and format the output as JSON:
@@ -399,20 +405,34 @@ instruction_prompt = """
399405
Classify the sentiment of each conversation in >>>>>CONTENT<<<<<
400406
with "🔥" for negative and "✅" for positive.
401407
408+
Follow these steps when classifying the conversations:
409+
1. Does the customer use swear words or 😤?
410+
2. Does the customer seem aggravated or angry?
411+
412+
If you answer "Yes" to one of the above questions,
413+
then classify the conversation as negative with "🔥".
414+
Otherwise classify the conversation as positive with "✅".
415+
416+
Let's think step by step
417+
402418
#### START EXAMPLES
403419
404420
------ Example Inputs ------
405421
[Agent] 2023-07-24 : What can I help you with?
406422
[Customer] 2023-07-24 : I CAN'T CONNECT TO MY 😤 ACCOUNT
407423
[Agent] 2023-07-24 : Are you sure it's not your caps lock?
408424
[Customer] 2023-07-24 : 😤! You're right!
409-
The customer uses the 😤 emoji and seems aggravated, so the sentiment is negative. 🔥
425+
- Does the customer use swear words or 😤? Yes
426+
- Does the customer seem aggravated or angry? Yes
427+
- Sentiment: 🔥
410428
411429
[Agent] 2023-06-15 : Hello! How can I assist you today?
412430
[Customer] 2023-06-15 : I can't seem to find the download link for my purchased software.
413-
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
414-
[Customer] 2023-06-15 : It's ********. Thanks for helping me out!
415-
The customer does not use any swear words or 😤 emoji and does not seem aggravated or angry, so the sentiment is positive. ✅
431+
[Agent] 2023-06-15 : No problem, ****. Let me find that for you. Can you please provide your order number?
432+
[Customer] 2023-06-15 : It's ****. Thanks for helping me out!
433+
- Does the customer use swear words or 😤? No
434+
- Does the customer seem aggravated or angry? No
435+
- Sentiment: ✅
416436
417437
------ Example Outputs ------
418438
🔥
@@ -424,8 +444,8 @@ The customer does not use any swear words or 😤 emoji and does not seem aggrav
424444
425445
[Agent] 2023-06-15 : Hello! How can I assist you today?
426446
[Customer] 2023-06-15 : I can't seem to find the download link for my purchased software.
427-
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
428-
[Customer] 2023-06-15 : It's ********. Thanks for helping me out!
447+
[Agent] 2023-06-15 : No problem, ****. Let me find that for you. Can you please provide your order number?
448+
[Customer] 2023-06-15 : It's ****. Thanks for helping me out!
429449
430450
#### END EXAMPLES
431451
"""
@@ -444,21 +464,34 @@ Classify the sentiment of each conversation in >>>>>CONTENT<<<<<
444464
as "negative" and "positive".
445465
Return the output as valid JSON.
446466
447-
#### START EXAMPLES
467+
Follow these steps when classifying the conversations:
468+
1. Does the customer use swear words or 😤?
469+
2. Does the customer seem aggravated or angry?
448470
449-
------ Example Input ------
471+
If you answer "Yes" to one of the above questions,
472+
then classify the conversation as "negative".
473+
Otherwise classify the conversation as "positive".
450474
475+
Let's think step by step
476+
477+
#### START EXAMPLES
478+
479+
------ Example Inputs ------
451480
[Agent] 2023-07-24 : What can I help you with?
452481
[Customer] 2023-07-24 : I CAN'T CONNECT TO MY 😤 ACCOUNT
453482
[Agent] 2023-07-24 : Are you sure it's not your caps lock?
454483
[Customer] 2023-07-24 : 😤! You're right!
455-
The customer uses the 😤 emoji and seems aggravated, so the sentiment is negative.
484+
- Does the customer use swear words or 😤? Yes
485+
- Does the customer seem aggravated or angry? Yes
486+
- Sentiment: "negative"
456487
457488
[Agent] 2023-06-15 : Hello! How can I assist you today?
458489
[Customer] 2023-06-15 : I can't seem to find the download link for my purchased software.
459-
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
460-
[Customer] 2023-06-15 : It's ********. Thanks for helping me out!
461-
The customer does not use any swear words or 😤 emoji and does not seem aggravated or angry, so the sentiment is positive.
490+
[Agent] 2023-06-15 : No problem, ****. Let me find that for you. Can you please provide your order number?
491+
[Customer] 2023-06-15 : It's ****. Thanks for helping me out!
492+
- Does the customer use swear words or 😤? No
493+
- Does the customer seem aggravated or angry? No
494+
- Sentiment: "positive"
462495
463496
------ Example Output ------
464497
@@ -480,8 +513,8 @@ The customer does not use any swear words or 😤 emoji and does not seem aggrav
480513
"conversation": [
481514
"A: Hello! How can I assist you today?",
482515
"C: I can't seem to find the download link for my purchased software.",
483-
"A: No problem, ********. Let me find that for you. Can you please provide your order number?",
484-
"C: It's ********. Thanks for helping me out!"
516+
"A: No problem, ****. Let me find that for you. Can you please provide your order number?",
517+
"C: It's ****. Thanks for helping me out!"
485518
]
486519
}
487520
]
@@ -504,54 +537,56 @@ Classify the sentiment of each conversation in >>>>>CONTENT<<<<<
504537
as "negative" and "positive".
505538
Return the output as valid JSON.
506539
"""
507-
role_prompt = """You are a thoroughly trained machine learning model
508-
that is an expert at sentiment classification.
540+
role_prompt = """You are a thoroughly trained machine learning
541+
model that is an expert at sentiment classification.
509542
You diligently complete tasks as instructed.
510543
You never make up any information that isn't there."""
511544
positive_example = """
512545
[Agent] 2023-06-15 : Hello! How can I assist you today?
513546
[Customer] 2023-06-15 : I can't seem to find the download link for my purchased software.
514-
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
515-
[Customer] 2023-06-15 : It's ********. Thanks for helping me out!
547+
[Agent] 2023-06-15 : No problem, ****. Let me find that for you. Can you please provide your order number?
548+
[Customer] 2023-06-15 : It's ****. Thanks for helping me out!
549+
"""
550+
positive_reasoning = """
551+
- Does the customer use swear words or 😤? No
552+
- Does the customer seem aggravated or angry? No
553+
- Sentiment: "positive"
516554
"""
517-
positive_reasoning = """The customer does not use any swear words or 😤 emoji
518-
and does not seem aggravated or angry, so the sentiment is positive."""
519555
positive_output = """
520-
{
521-
"positive": [
522-
{
523-
"date": "2023-06-15",
524-
"conversation": [
525-
"A: Hello! How can I assist you today?",
526-
"C: I can't seem to find the download link for my purchased software.",
527-
"A: No problem, ********. Let me find that for you. Can you please provide your order number?",
528-
"C: It's ********. Thanks for helping me out!"
529-
]
530-
}
531-
]
532-
}
556+
"positive": [
557+
{
558+
"date": "2023-06-15",
559+
"conversation": [
560+
"A: Hello! How can I assist you today?",
561+
"C: I can't seem to find the download link for my purchased software.",
562+
"A: No problem, ****. Let me find that for you. Can you please provide your order number?",
563+
"C: It's ****. Thanks for helping me out!"
564+
]
565+
}
566+
]
533567
"""
534568
negative_example = """
535569
[Agent] 2023-07-24 : What can I help you with?
536570
[Customer] 2023-07-24 : I CAN'T CONNECT TO MY 😤 ACCOUNT
537571
[Agent] 2023-07-24 : Are you sure it's not your caps lock?
538572
[Customer] 2023-07-24 : 😤! You're right!
539573
"""
540-
negative_reasoning = """The customer uses the 😤 emoji and seems aggravated,
541-
so the sentiment is negative."""
574+
negative_reasoning = """
575+
- Does the customer use swear words or 😤? Yes
576+
- Does the customer seem aggravated or angry? Yes
577+
- Sentiment: "negative"
578+
"""
542579
negative_output = """
543-
{
544-
"negative": [
545-
{
546-
"date": "2023-07-24",
547-
"conversation": [
548-
"A: What can I help you with?",
549-
"C: I CAN'T CONNECT TO MY 😤 ACCOUNT",
550-
"A: Are you sure it's not your caps lock?",
551-
"C: 😤! You're right!"
552-
]
553-
}
554-
]
555-
}
580+
"negative": [
581+
{
582+
"date": "2023-07-24",
583+
"conversation": [
584+
"A: What can I help you with?",
585+
"C: I CAN'T CONNECT TO MY 😤 ACCOUNT",
586+
"A: Are you sure it's not your caps lock?",
587+
"C: 😤! You're right!"
588+
]
589+
}
590+
]
556591
"""
557592
```

prompt-engineering/app.py

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,17 @@
33
import tomllib
44
from pathlib import Path
55

6-
import openai
6+
from openai import OpenAI
77

8-
# Authenticate
9-
openai.api_key = os.getenv("OPENAI_API_KEY")
10-
11-
12-
class Settings(dict):
13-
"""Handle loading and accessing application settings from file."""
8+
__all__ = ["get_chat_completion"]
149

15-
@classmethod
16-
def load(cls, path) -> "Settings":
17-
"""Load TOML settings file and pass it to class constuctor."""
18-
with path.open("rb") as file:
19-
return cls(tomllib.load(file))
10+
# Authenticate
11+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
2012

21-
def __init__(self, *args, **kwargs) -> None:
22-
"""Add general settings and prompts as instance attributes."""
23-
super().__init__(*args, **kwargs)
24-
# Settings
25-
self.chat_models = self["general"]["chat_models"]
26-
self.model = self["general"]["model"]
27-
self.max_tokens = self["general"]["max_tokens"]
28-
self.temperature = self["general"]["temperature"]
29-
self.model_supports_chat_completions = self.model in self.chat_models
30-
# Prompts
31-
self.instruction_prompt = self["prompts"]["instruction_prompt"]
32-
self.role_prompt = self["prompts"]["role_prompt"]
33-
self.positive_example = self["prompts"]["positive_example"]
34-
self.positive_reasoning = self["prompts"]["positive_reasoning"]
35-
self.positive_output = self["prompts"]["positive_output"]
36-
self.negative_example = self["prompts"]["negative_example"]
37-
self.negative_reasoning = self["prompts"]["negative_reasoning"]
38-
self.negative_output = self["prompts"]["negative_output"]
13+
# Load settings file
14+
settings_path = Path("settings.toml")
15+
with settings_path.open("rb") as settings_file:
16+
SETTINGS = tomllib.load(settings_file)
3917

4018

4119
def parse_args() -> argparse.Namespace:
@@ -47,52 +25,46 @@ def parse_args() -> argparse.Namespace:
4725

4826
def main(args: argparse.Namespace) -> None:
4927
file_content = args.file_path.read_text("utf-8")
50-
settings = Settings.load(Path("settings.toml"))
51-
if settings.model_supports_chat_completions:
52-
print(get_chat_completion(file_content, settings))
53-
else:
54-
print(get_completion(file_content, settings))
55-
28+
print(get_chat_completion(file_content))
5629

57-
def get_completion(content: str, settings: Settings) -> str:
58-
"""Send a request to the /completions endpoint."""
59-
response = openai.Completion.create(
60-
model=settings.model,
61-
prompt=assemble_prompt(content, settings),
62-
max_tokens=settings.max_tokens,
63-
temperature=settings.temperature,
64-
)
65-
return response["choices"][0]["text"]
6630

67-
68-
def get_chat_completion(content: str, settings: Settings) -> str:
31+
def get_chat_completion(content: str) -> str:
6932
"""Send a request to the /chat/completions endpoint."""
70-
response = openai.ChatCompletion.create(
71-
model=settings.model,
72-
messages=assemble_chat_messages(content, settings),
73-
temperature=settings.temperature,
33+
response = client.chat.completions.create(
34+
model=SETTINGS["general"]["model"],
35+
messages=_assemble_chat_messages(content),
36+
temperature=SETTINGS["general"]["temperature"],
37+
seed=12345, # Doesn't do anything for older models
7438
)
75-
return response["choices"][0]["message"]["content"]
76-
77-
78-
def assemble_prompt(content: str, settings: Settings) -> str:
79-
"""Combine all text input into a single prompt."""
80-
return f">>>>>\n{content}\n<<<<<\n\n" + settings.instruction_prompt
81-
82-
83-
def assemble_chat_messages(content: str, settings: Settings) -> list[dict]:
84-
"""Combine all messages into a well-formatted dictionary."""
85-
return [
86-
{"role": "system", "content": settings.role_prompt},
87-
{"role": "user", "content": settings.negative_example},
88-
{"role": "system", "content": settings.negative_reasoning},
89-
{"role": "assistant", "content": settings.negative_output},
90-
{"role": "user", "content": settings.positive_example},
91-
{"role": "system", "content": settings.positive_reasoning},
92-
{"role": "assistant", "content": settings.positive_output},
39+
return response.choices[0].message.content
40+
41+
42+
def _assemble_chat_messages(content: str) -> list[dict]:
43+
"""Combine all messages into a well-formatted list of dicts."""
44+
messages = [
45+
{"role": "system", "content": SETTINGS["prompts"]["role_prompt"]},
46+
{"role": "user", "content": SETTINGS["prompts"]["negative_example"]},
47+
{
48+
"role": "system",
49+
"content": SETTINGS["prompts"]["negative_reasoning"],
50+
},
51+
{
52+
"role": "assistant",
53+
"content": SETTINGS["prompts"]["negative_output"],
54+
},
55+
{"role": "user", "content": SETTINGS["prompts"]["positive_example"]},
56+
{
57+
"role": "system",
58+
"content": SETTINGS["prompts"]["positive_reasoning"],
59+
},
60+
{
61+
"role": "assistant",
62+
"content": SETTINGS["prompts"]["positive_output"],
63+
},
9364
{"role": "user", "content": f">>>>>\n{content}\n<<<<<"},
94-
{"role": "user", "content": settings.instruction_prompt},
65+
{"role": "user", "content": SETTINGS["prompts"]["instruction_prompt"]},
9566
]
67+
return messages
9668

9769

9870
if __name__ == "__main__":
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
aiohttp==3.8.4
2-
aiosignal==1.3.1
3-
async-timeout==4.0.2
4-
attrs==23.1.0
5-
certifi==2023.5.7
6-
charset-normalizer==3.1.0
7-
frozenlist==1.3.3
8-
idna==3.4
9-
multidict==6.0.4
10-
openai==0.27.8
11-
python-dotenv==1.0.0
12-
requests==2.31.0
13-
tqdm==4.65.0
14-
urllib3==2.0.3
15-
yarl==1.9.2
1+
annotated-types==0.6.0
2+
anyio==4.3.0
3+
certifi==2024.2.2
4+
distro==1.9.0
5+
h11==0.14.0
6+
httpcore==1.0.4
7+
httpx==0.27.0
8+
idna==3.6
9+
openai==1.13.3
10+
pydantic==2.6.3
11+
pydantic-core==2.16.3
12+
sniffio==1.3.1
13+
tqdm==4.66.2
14+
typing-extensions==4.10.0

prompt-engineering/sanitized-testing-chats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[Agent] 2023-08-13: Good morning! How may I assist you?
1212
[Client] 2023-08-13: Hello, I'm having a problem with my mobile app, it keeps crashing.
13-
[Agent] 2023-08-13: I'm sorry to hear that, ********. Could you tell me what device you're using?
13+
[Agent] 2023-08-13: I'm sorry to hear that, ********. Could you tell me what device you're using?
1414
[Client] 2023-08-13: I have an iPhone 11.
1515

1616
[Agent] 2023-08-30: Good evening! How may I assist you today?

0 commit comments

Comments
 (0)