Prompt Engineering

  • 初始设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv('OPENAI_API_KEY')

# OpenAI library version 0.27.0
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]

# OpenAI library version 1.0.0
client = openai.OpenAI()
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0
)
return response.choices[0].message.content
  • Use delimiters to clearly indicate distinct parts of the input • Delimiters can be anything like: ``, """, < >, ,:`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
text = f"""
You should express what you want a model to do by \\
providing instructions that are as clear and \\
specific as you can possibly make them. \\
This will guide the model towards the desired output, \\
and reduce the chances of receiving irrelevant \\
or incorrect responses. Don't confuse writing a \\
clear prompt with writing a short prompt. \\
In many cases, longer prompts provide more clarity \\
and context for the model, which can lead to \\
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \\
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)
  • Ask for a structured output
1
2
3
4
5
6
7
8
prompt = f"""
Generate a list of three made-up book titles along \\
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
  • Ask the model to check whether conditions are satisfied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \\
water boiling. While that's happening, \\
grab a cup and put a tea bag in it. Once the water is \\
hot enough, just pour it over the tea bag. \\
Let it sit for a bit so the tea can steep. After a \\
few minutes, take out the tea bag. If you \\
like, you can add some sugar or milk to taste. \\
And that's it! You've got yourself a delicious \\
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \\
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …

Step N - …

If the text does not contain a sequence of instructions, \\
then simply write \\"No steps provided.\\"

\\"\\"\\"{text_1}\\"\\"\\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)

text_2 = f"""
The sun is shining brightly today, and the birds are \\
singing. It's a beautiful day to go for a \\
walk in the park. The flowers are blooming, and the \\
trees are swaying gently in the breeze. People \\
are out and about, enjoying the lovely weather. \\
Some are having picnics, while others are playing \\
games or simply relaxing on the grass. It's a \\
perfect day to spend time outdoors and appreciate the \\
beauty of nature.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \\
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …

Step N - …

If the text does not contain a sequence of instructions, \\
then simply write \\"No steps provided.\\"

\\"\\"\\"{text_2}\\"\\"\\"
"""
response = get_completion(prompt)
print("Completion for Text 2:")
print(response)
  • "Few-shot" prompting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
prompt = f"""
Your task is to answer in a consistent style.

<child>: Teach me about patience.

<grandparent>: The river that carves the deepest \\
valley flows from a modest spring; the \\
grandest symphony originates from a single note; \\
the most intricate tapestry begins with a solitary thread.

<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)
  • Specify the steps required to complete a task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
text = f"""
In a charming village, siblings Jack and Jill set out on \\
a quest to fetch water from a hilltop \\
well. As they climbed, singing joyfully, misfortune \\
struck—Jack tripped on a stone and tumbled \\
down the hill, with Jill following suit. \\
Though slightly battered, the pair returned home to \\
comforting embraces. Despite the mishap, \\
their adventurous spirits remained undimmed, and they \\
continued exploring with delight.
"""
# example 1
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple \\
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \\
keys: french_summary, num_names.

Separate your answers with line breaks.

Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
  • Ask for output in a specified format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
prompt_2 = f"""
Your task is to perform the following actions:
1 - Summarize the following text delimited by
<> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the
following keys: french_summary, num_names.

Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in summary>
Output JSON: <json with summary and num_names>

Text: <{text}>
"""
response = get_completion(prompt_2)
print("\\nCompletion for prompt 2:")
print(response)
  • Instructing the model to work out its own solution first
1
2
3
4
5
6
7
8
9
10
11
12
prompt = f"""
Your task is to determine if the student's solution \\
is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem including the final total.
- Then compare your solution to the student's solution \\
and evaluate if the student's solution is correct or not.
Don't decide if the student's solution is correct until
you have done the problem yourself.

Use the following format:
Question:

question here

1
Student's solution:

student's solution here

1
Actual solution:

steps to work out the solution and your solution here

1
2
Is the student's solution the same as actual solution \\
just calculated:

yes or no

1
Student grade:

correct or incorrect

1
Question:

I'm building a solar power installation and I need help  working out the financials.

  • Land costs $100 / square foot
  • I can buy solar panels for $250 / square foot
  • I negotiated a contract for maintenance that will cost  me a flat $100k per year, and an additional $10 / square  foot What is the total cost for the first year of operations  as a function of the number of square feet.
1
Student's solution:

Let x be the size of the installation in square feet. Costs:

  1. Land cost: 100x
  2. Solar panel cost: 250x
  3. Maintenance cost: 100,000 + 100x Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
1
2
3
4
Actual solution:
"""
response = get_completion(prompt)
print(response)
  • Iterative
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# original
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.

Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

# The text is too long
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.

Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

Use at most 50 words.

Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

# Text focuses on the wrong details
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.

Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.

At the end of the description, include every 7-character
Product ID in the technical specification.

Use at most 50 words.

Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

# Description needs a table of dimensions
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.

Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.

At the end of the description, include every 7-character
Product ID in the technical specification.

After the description, include a table that gives the
product's dimensions. The table should have two columns.
In the first column include the name of the dimension.
In the second column include the measurements in inches only.

Give the table the title 'Product Dimensions'.

Format everything as HTML that can be used in a website.
Place the description in a <div> element.

Technical specifications: ```{fact_sheet_chair}```
"""

response = get_completion(prompt)
print(response)

# To view HTML
from IPython.display import display, HTML
display(HTML(response))
  • Summarizing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Summarize with a focus on price and value
prompt = f"""
Your task is to generate a short summary of a product \\
review from an ecommerce site to give feedback to the \\
pricing deparmtment, responsible for determining the \\
price of the product.

Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \\
that are relevant to the price and perceived value.

Review: ```{prod_review}```
"""

response = get_completion(prompt)
print(response)

# Summarize multiple product reviews
for i in range(len(reviews)):
prompt = f"""
Your task is to generate a short summary of a product \\
review from an ecommerce site.

Summarize the review below, delimited by triple \\
backticks in at most 20 words.

Review: ```{reviews[i]}```
"""

response = get_completion(prompt)
print(i, response, "\\n")
  • Inferring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Sentiment (positive/negative)
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?

Give your answer as a single word, either "positive" \\
or "negative".

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

# Identify types of emotions
prompt = f"""
Identify a list of emotions that the writer of the \\
following review is expressing. Include no more than \\
five items in the list. Format your answer as a list of \\
lower-case words separated by commas.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

# Identify anger
prompt = f"""
Is the writer of the following review expressing anger?\\
The review is delimited with triple backticks. \\
Give your answer as either yes or no.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

# Doing multiple tasks at once
prompt = f"""
Identify the following items from the review text:
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \\
Format your response as a JSON object with \\
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \\
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

# Infer 5 topics
prompt = f"""
Determine five topics that are being discussed in the \\
following text, which is delimited by triple backticks.

Make each item one or two words long.

Format your response as a list of items separated by commas.

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)
  • Transforming
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Tone Transformation
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)

# Format Conversion
data_json = { "resturant employees" :[
{"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
{"name":"Bob", "email":"bob32@gmail.com"},
{"name":"Jai", "email":"jai87@gmail.com"}
]}

prompt = f"""
Translate the following python dictionary from JSON to an HTML \\
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print(response)

from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))

# Spellcheck/Grammar check
text = [
"The girl with the black and white puppies have a ball.", # The girl has a ball.
"Yolanda has her notebook.", # ok
"Its going to be a long day. Does the car need it’s oil changed?", # Homonyms
"Their goes my freedom. There going to bring they’re suitcases.", # Homonyms
"Your going to need you’re notebook.", # Homonyms
"That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
"This phrase is to cherck chatGPT for speling abilitty" # spelling
]
for t in text:
prompt = f"""Proofread and correct the following text
and rewrite the corrected version. If you don't find
and errors, just say "No errors found". Don't use
any punctuation around the text:
```{t}```"""
response = get_completion(prompt)
print(response)

from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))

prompt = f"""
proofread and correct this review. Make it more compelling.
Ensure it follows APA style guide and targets an advanced reader.
Output in markdown format.
Text: ```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))
  • Expanding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \\
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \\
their review.
If the sentiment is negative, apologize and suggest that \\
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print(response)
  • Chatbot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]

messages = [
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \\
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'} ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

# OrderBot
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))

return pn.Column(*panels)

import panel as pn # GUI
pn.extension()

panels = [] # collect display

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \\
You first greet the customer, then collects the order, \\
and then asks if it's a pickup or delivery. \\
You wait to collect the entire order, then summarize it and check for a final \\
time if the customer wants to add anything else. \\
If it's a delivery, you ask for an address. \\
Finally you collect the payment.\\
Make sure to clarify all options, extras and sizes to uniquely \\
identify the item from the menu.\\
You respond in a short, very conversational friendly style. \\
The menu includes \\
pepperoni pizza 12.95, 10.00, 7.00 \\
cheese pizza 10.95, 9.25, 6.50 \\
eggplant pizza 11.95, 9.75, 6.75 \\
fries 4.50, 3.50 \\
greek salad 7.25 \\
Toppings: \\
extra cheese 2.00, \\
mushrooms 1.50 \\
sausage 3.00 \\
canadian bacon 3.50 \\
AI sauce 1.50 \\
peppers 1.00 \\
Drinks: \\
coke 3.00, 2.00, 1.00 \\
sprite 3.00, 2.00, 1.00 \\
bottled water 5.00 \\
"""} ] # accumulate messages

inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard
messages = context.copy()
messages.append(
{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\\
The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size 4) list of sides include size 5)total price '},
)
#The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price 4) list of sides include size include price, 5)total price '},

response = get_completion_from_messages(messages, temperature=0)
print(response)