Skip to content

Commit 3c96e20

Browse files
authored
Merge branch 'master' into python-asterisk-and-slash-special-parameters
2 parents fc0301b + 6eeab9d commit 3c96e20

28 files changed

+1169
-55
lines changed

prompt-engineering/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023 Real Python
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

prompt-engineering/README.md

Lines changed: 557 additions & 0 deletions
Large diffs are not rendered by default.

prompt-engineering/app.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import argparse
2+
import os
3+
import tomllib
4+
from pathlib import Path
5+
6+
import openai
7+
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."""
14+
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))
20+
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"]
39+
40+
41+
def parse_args() -> argparse.Namespace:
42+
"""Parse command-line input."""
43+
parser = argparse.ArgumentParser()
44+
parser.add_argument("file_path", type=Path, help="Path to the input file")
45+
return parser.parse_args()
46+
47+
48+
def main(args: argparse.Namespace) -> None:
49+
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+
56+
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"]
66+
67+
68+
def get_chat_completion(content: str, settings: Settings) -> str:
69+
"""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,
74+
)
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},
93+
{"role": "user", "content": f">>>>>\n{content}\n<<<<<"},
94+
{"role": "user", "content": settings.instruction_prompt},
95+
]
96+
97+
98+
if __name__ == "__main__":
99+
main(parse_args())

prompt-engineering/chats.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[support_tom] 2023-07-24T10:02:23+00:00 : What can I help you with?
2+
[johndoe] 2023-07-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
3+
[support_tom] 2023-07-24T10:03:30+00:00 : Are you sure it's not your caps lock?
4+
[johndoe] 2023-07-24T10:04:03+00:00 : Blast! You're right!
5+
6+
[support_amy] 2023-06-15T14:45:35+00:00 : Hello! How can I assist you today?
7+
[greg_stone] 2023-06-15T14:46:20+00:00 : I can't seem to find the download link for my purchased software.
8+
[support_amy] 2023-06-15T14:47:01+00:00 : No problem, Greg. Let me find that for you. Can you please provide your order number?
9+
[greg_stone] 2023-06-15T14:47:38+00:00 : It's 1245789. Thanks for helping me out!
10+
11+
[support_louis] 2023-05-05T09:22:12+00:00 : Hi, how can I help you today?
12+
[karen_w] 2023-05-05T09:23:47+00:00 : MY BLASTED ORDER STILL HASN'T ARRIVED AND IT'S BEEN A WEEK!!!
13+
[support_louis] 2023-05-05T09:24:15+00:00 : I'm sorry to hear that, Karen. Let's look into this issue.
14+
[support_louis] 2023-05-05T09:25:35+00:00: Can you please provide your order number so I can check the status for you?
15+
[karen_w] 2023-05-05T09:26:12+00:00: Fine, it's 9876543.
16+
[support_louis] 2023-05-05T09:26:45+00:00: Thank you, Karen. I see there was a delay in shipping. Your order will arrive within the next 2 days.
17+
18+
[support_jenny] 2023-06-18T17:35:28+00:00: Hello! How can I help you today?
19+
[alex_harper] 2023-06-18T17:36:05+00:00: I accidentally placed an order twice, can you help me cancel one?
20+
[support_jenny] 2023-06-18T17:36:25+00:00: Sure, Alex. Can you give me the order number you'd like to cancel?
21+
[alex_harper] 2023-06-18T17:36:55+00:00: Yes, it's 1122334. Thank you!
22+
[support_jenny] 2023-06-18T17:37:32+00:00: I've successfully canceled order number 1122334. You will receive a confirmation email shortly.
23+
24+
[support_ben] 2023-06-29T11:51:45+00:00: Good morning, what can I assist you with today?
25+
[lisa_beck] 2023-06-29T11:52:20+00:00: Hi there, I received a damaged item in my order. Can you help me return it?
26+
[support_ben] 2023-06-29T11:52:45+00:00: I'm sorry to hear that, Lisa. Can you provide your order number and specify the damaged item?
27+
[lisa_beck] 2023-06-29T11:53:22+00:00: Sure, order number is 5566778 and the damaged item is a coffee mug.
28+
29+
[support_rachel] 2023-05-04T08:16:37+00:00: How can I help you today?
30+
[mike_t] 2023-05-04T08:17:15+00:00: My coupon code isn't working at checkout. Can you help?
31+
[support_rachel] 2023-05-04T08:17:38+00:00: Of course, Mike. Please provide the coupon code you're trying to use.
32+
[mike_t] 2023-05-04T08:18:02+00:00: It's "HELLO10".
33+
[support_rachel] 2023-05-04T08:18:37+00:00: I've checked the code, and it seems to have expired. I apologize for the inconvenience. Here's a new code for you to use: "WELCOME15".
34+
35+
[support_vincent] 2023-06-15T20:43:55+00:00: Good evening! How may I assist you?
36+
[sara_winters] 2023-06-15T20:44:30+00:00: Hi there, I'm having trouble logging into my account. I've tried resetting my password, but it's not working.
37+
[support_vincent] 2023-06-15T20:44:52+00:00: I'm sorry to hear that, Sara. Let me help you. Can you please confirm your email address?
38+
[sara_winters] 2023-06-15T20:45:25+00:00: Sure, it's [email protected].
39+
40+
[support_david] 2023-06-24T16:28:43+00:00: Welcome! What can I do for you today?
41+
[jane_d] 2023-06-24T16:29:16+00:00: Hi, I need to change my delivery address for my recent order.
42+
[support_david] 2023-06-24T16:29:43+00:00: Alright, Jane. Please provide your order number.
43+
[jane_d] 2023-06-24T16:30:11+00:00: It's 3344556. Thanks for your help!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[support_tom] 2023-07-24 : What can I help you with?
2+
[Client] 2023-07-24 : I CAN'T CONNECT TO MY 😤 ACCOUNT
3+
[Agent] 2023-07-24 : Are you sure it's not your caps lock?
4+
[Client] 2023-07-24 : 😤! You're right!
5+
6+
[Agent] 2023-06-15 : Hello! How can I assist you today?
7+
[Client] 2023-06-15 : I can't seem to find the download link for my purchased software.
8+
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
9+
[Client] 2023-06-15 : It's ********. Thanks for helping me out!
10+
11+
[Agent] 2023-05-05 : Hi, how can I help you today?
12+
[Client] 2023-05-05 : MY 😤 ORDER STILL HASN'T ARRIVED AND IT'S BEEN A WEEK!!!
13+
[Agent] 2023-05-05 : I'm sorry to hear that, ********. Let's look into this issue.
14+
[Agent] 2023-05-05: Can you please provide your order number so I can check the status for you?
15+
[Client] 2023-05-05: Fine, it's ********.
16+
[Agent] 2023-05-05: Thank you, ********. I see there was a delay in shipping. Your order will arrive within the next 2 days.
17+
18+
[Agent] 2023-06-18: Hello! How can I help you today?
19+
[Client] 2023-06-18: I accidentally placed an order twice, can you help me cancel one?
20+
[Agent] 2023-06-18: Sure, ********. Can you give me the order number you'd like to cancel?
21+
[Client] 2023-06-18: Yes, it's ********. Thank you!
22+
[Agent] 2023-06-18: I've successfully canceled order number ********. You will receive a confirmation email shortly.
23+
24+
[Agent] 2023-06-29: Good morning, what can I assist you with today?
25+
[Client] 2023-06-29: Hi there, I received a damaged item in my order. Can you help me return it?
26+
[Agent] 2023-06-29: I'm sorry to hear that, ********. Can you provide your order number and specify the damaged item?
27+
[Client] 2023-06-29: Sure, order number is ******** and the damaged item is a coffee mug.
28+
29+
[Agent] 2023-05-04: How can I help you today?
30+
[Client] 2023-05-04: My coupon code isn't working at checkout. Can you help?
31+
[Agent] 2023-05-04: Of course, ********. Please provide the coupon code you're trying to use.
32+
[Client] 2023-05-04: It's "********".
33+
[Agent] 2023-05-04: I've checked the code, and it seems to have expired. I apologize for the inconvenience. Here's a new code for you to use: "********".
34+
35+
[Agent] 2023-06-15: Good evening! How may I assist you?
36+
[Client] 2023-06-15: Hi there, I'm having trouble logging into my account. I've tried resetting my password, but it's not working.
37+
[Agent] 2023-06-15: I'm sorry to hear that, ********. Let me help you. Can you please confirm your email address?
38+
[Client] 2023-06-15: Sure, it's ********.
39+
40+
[Agent] 2023-06-24: Welcome! What can I do for you today?
41+
[Client] 2023-06-24: Hi, I need to change my delivery address for my recent order.
42+
[Agent] 2023-06-24: Alright, ********. Please provide your order number.
43+
[Client] 2023-06-24: It's ********. Thanks for your help!
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[Agent] 2023-07-15: Hello! What can I help you with today?
2+
[Client] 2023-07-15: Hey, my promo code isn't applying the discount in my cart.
3+
[Agent] 2023-07-15: My apologies for the trouble, ********. Could you tell me the promo code you're trying to use?
4+
[Client] 2023-07-15: It's "SAVE20".
5+
6+
[Agent] 2023-07-24: Good day! How can I help you?
7+
[Client] 2023-07-24: Hi "********", I can't update my 😤 credit card information. Do you want my 😤 money or not?
8+
[Agent] 2023-07-24: I'm sorry for the inconvenience, ********. Can you please confirm your account's email?
9+
[Client] 2023-07-24: Sure, you have all my 😤 data already anyways. It's ********.
10+
11+
[Agent] 2023-08-13: Good morning! How may I assist you?
12+
[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?
14+
[Client] 2023-08-13: I have an iPhone 11.
15+
16+
[Agent] 2023-08-30: Good evening! How may I assist you today?
17+
[Client] 2023-08-30: Hi ********, I've forgotten my 😤 password and I can't login into my account.
18+
[Agent] 2023-08-30: I'm sorry for the trouble, ********. Could you confirm your email address so we can reset your password?
19+
[Client] 2023-08-30: Definitely, it's ********.
20+
21+
[Agent] 2023-09-01: Hello! How can I assist you this morning?
22+
[Client] 2023-09-01: Hi, I'm trying to make a purchase but it's not going through.
23+
[Agent] 2023-09-01: I'm sorry to hear that, ********. Can you tell me what error message you're receiving?
24+
[Client] 2023-09-01: It's saying "Payment method not valid".
25+
26+
[Agent] 2023-10-11: Good morning! How may I assist you?
27+
[Client] 2023-10-11: Hello, I'd like to know the status of my order.
28+
[Agent] 2023-10-11: Of course, ********. Could you please provide me with the order number?
29+
[Client] 2023-10-11: It's ********.
30+
31+
[Agent] 2023-10-19: Welcome! How can I assist you right now?
32+
[Client] 2023-10-19: 😤! There's no option to change my profile picture. What kind of 😤 joint are you running?
33+
[Agent] 2023-10-19: Let me help you with this, ********. Are you trying to update it from the mobile app or the website?
34+
[Client] 2023-10-19: I'm using the 😤 website
35+
36+
[Agent] 2023-10-29: Hello! What can I help you with today?
37+
[Client] 2023-10-29: Hi ********, I was charged twice for my last order.
38+
[Agent] 2023-10-29: I'm sorry to hear that, ********. Could you share your order number so I can look into this for you?
39+
[Client] 2023-10-29: Sure, it's ********.
40+
41+
[Agent] 2023-11-08: How can I help you today?
42+
[Client] 2023-11-08: Hi, I made an order last week but I need to change the sizing.
43+
[Agent] 2023-11-08: Certainly, ********. Could you provide me the order number?
44+
[Client] 2023-11-08: Yes, it's ********. Thanks!
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[general]
2+
chat_models = ["gpt-3.5", "gpt-3.5-turbo", "gpt-4"]
3+
model = "gpt-4"
4+
max_tokens = 100
5+
temperature = 0
6+
7+
[prompts]
8+
instruction_prompt = """
9+
Classify the sentiment of each conversation in >>>>>CONTENT<<<<< as "negative" and "positive".
10+
Return the output as valid JSON.
11+
"""
12+
role_prompt = """You are a thoroughly trained machine learning model that is an expert at sentiment classification.
13+
You diligently complete tasks as instructed. You never make up any information that isn't there."""
14+
positive_example = """
15+
[Agent] 2023-06-15 : Hello! How can I assist you today?
16+
[Customer] 2023-06-15 : I can't seem to find the download link for my purchased software.
17+
[Agent] 2023-06-15 : No problem, ********. Let me find that for you. Can you please provide your order number?
18+
[Customer] 2023-06-15 : It's ********. Thanks for helping me out!
19+
"""
20+
positive_reasoning = """The customer does not use any swear words or 😤 emoji
21+
and does not seem aggravated or angry, so the sentiment is positive."""
22+
positive_output = """
23+
{
24+
"positive": [
25+
{
26+
"date": "2023-06-15",
27+
"conversation": [
28+
"A: Hello! How can I assist you today?",
29+
"C: I can't seem to find the download link for my purchased software.",
30+
"A: No problem, ********. Let me find that for you. Can you please provide your order number?",
31+
"C: It's ********. Thanks for helping me out!"
32+
]
33+
}
34+
]
35+
}
36+
"""
37+
negative_example = """
38+
[Agent] 2023-07-24 : What can I help you with?
39+
[Customer] 2023-07-24 : I CAN'T CONNECT TO MY 😤 ACCOUNT
40+
[Agent] 2023-07-24 : Are you sure it's not your caps lock?
41+
[Customer] 2023-07-24 : 😤! You're right!
42+
"""
43+
negative_reasoning = """The customer uses the 😤 emoji and seems aggravated,
44+
so the sentiment is negative."""
45+
negative_output = """
46+
{
47+
"negative": [
48+
{
49+
"date": "2023-07-24",
50+
"conversation": [
51+
"A: What can I help you with?",
52+
"C: I CAN'T CONNECT TO MY 😤 ACCOUNT",
53+
"A: Are you sure it's not your caps lock?",
54+
"C: 😤! You're right!"
55+
]
56+
}
57+
]
58+
}
59+
"""

prompt-engineering/settings.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[general]
2+
chat_models = ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4"]
3+
model = "text-davinci-003"
4+
max_tokens = 2100
5+
temperature = 0
6+
7+
[prompts]
8+
instruction_prompt = """
9+
Remove personally identifiable information, only show the date,
10+
and replace all swear words with "😤"
11+
"""
12+
role_prompt = """
13+
"""
14+
positive_example = """
15+
"""
16+
positive_reasoning = """
17+
"""
18+
positive_output = """
19+
"""
20+
negative_example = """
21+
"""
22+
negative_reasoning = """
23+
"""
24+
negative_output = """
25+
"""

0 commit comments

Comments
 (0)