Openai+Coursera: ChatGPT Prompt Engineering(一)

I want to share with you the recently learned Coursera and openai jointly created the ChatGPT Prompt Engineering online course. The following is the main code to access ChatGPT through the API:

import openai

openai.api_key  ='XXXXXXXXX'

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, 
    )
    return response.choices[0].message["content"]

Prompting Principles

  • Principle 1: Write clear and specific instructions

  • Principle 2: Give the model time to "think" ( Give the model time to "think" )

Strategy

Strategy 1 : Use delimiters to clearly indicate the different parts of the input
Delimiters can be: ```, """, < >, <tag> </tag>, :

Explanation for Strategy 1:

Strategy 1 means: if we need chatgpt to complete a specific task, then we need to clearly tell ChatGPT what needs to be done and the location of the required material in the prompt. For example, if we want chatGPT to make a summary or abstract of a piece of text content, then we need to indicate in the prompt: 1. What is the task that needs to be completed by gpt, 2. Where is the material required for the task.

The following code demonstrates that gpt makes a summary of a specific text content. When defining the prompt, we need to use a specific delimiter to separate the text content that needs to be summarized. The purpose of this is to let gpt know where the text content is located.

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)

 Strategy 2: You can ask GPT to give a resultant result

  • JSON, HTML
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)

 Strategy 3: Ask the model to check if the condition is met

Here we ask gpt to check whether the content of a piece of text meets a specific condition (step). If it is satisfied, it will output the qualified content according to the condition, and if it does not meet the condition, it will output: does not meet the condition.

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)

 The content of the first prompt in the above two prompts meets the specific conditions, so the result is output according to the conditions, and the second prompt does not meet the conditions, so the output: No steps provided.

Tactic 4: "Few-shot" cue words

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)

The above prompt requires ChatGPT to answer questions in a consistent style. In the prompt, the content of a round of dialogue between child and grandparent is given as an example. The purpose of this is to let ChatGPT understand what the so-called "consistent style" is all about. At the end of the prompt is a question from the child, but there is no answer from the grandparent. This is to let ChatGPT understand what to do: act as a grandparent to answer the child's last question, and the style should be consistent with the style of the previous grandparent.

Here are the results returned by ChatGPT:

 Principle 2: Give the model time to "think"

Strategy 1: Specify the steps required to complete the task

Here we will give the model a piece of text, and then ask the model to complete a task according to the specified steps:

Do the following:
1 - Summarize the following triple backtick delimited text in 1 sentence.
2 - Translate the abstract into French.
3 - List each name in the French summary.
4 - Outputs a json object with the following keys: french_summary, num_names.

Separate your answers with newlines.

ext = 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)

 Request output in a specified format

Here we ask ChatGPT to output the content according to the specified format. The content in the prompt is divided into 2 parts. The first half tells ChatGPT what things need to be done, and the output format of the second half:

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 Italian summary>
Output JSON: <json with summary and num_names>

Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)

 Strategy 2: Instruct the model to formulate its own solution before jumping to conclusions

Here, ChatGPT is given a simple math problem in the prompt, and I hope ChatGPT can judge whether the student's answer is correct. For the answer given by ChatGPT, we found that it did not carefully go to do this question in person, and the result gave a wrong answer. From this, we found that when the logic of the questions in the prompt is more complicated, when we directly ask ChatGPT to give an answer, ChatGPT will often make mistakes like humans. Let's translate the prompt first:

prompt = f"""
Determine if the student's solution is correct or not.

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.

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
"""
response = get_completion(prompt)
print(response)

 

Note that the student's answer is actually incorrect.

We can judge whether the student's answer is correct by instructing ChatGPT to calculate its own answer first. So we need to rewrite the prompt, we need to tell ChatGPT the detailed steps to solve this application problem:

The specific format for implementing these steps is given below:

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. 
- 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
```
Student's solution:
```
student's solution here
```
Actual solution:
```
steps to work out the solution and your solution here
```
Is the student's solution the same as actual solution \
just calculated:
```
yes or no
```
Student grade:
```
correct or incorrect
```

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.
``` 
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
```
Actual solution:
"""
response = get_completion(prompt)
print(response)

 Here we can find that the actual output of ChatGPT starts from the "Actual solution:" of "Use the following format:", and does not output the two parts of "Question:" and "Student's solution:". This shows that ChatGPT can understand human intentions, because the "Question:" and "Student's solution:" parts have already appeared in the title, so there is no need to repeat the output, just output the content after the "Actual solution:" part. Finally, ChatGPT gave the correct answer by comparing its own answer with the student's answer.

Model Limitations: Hallucinations

When ChatGPT does not know the answer to the question, ChatGPT will often not admit that it does not know, so GPT will often give a seemingly correct wrong answer when it does not know the correct answer.

Below we ask ChatGPT to give relevant information about the electric toothbrush of Boie company in the prompt. Boie here is a real company, and the product name is not real.

prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)

 Here we find that ChatGPT says a lot about a non-existent product of Boie Company, as if the product actually exists.

Guess you like

Origin blog.csdn.net/weixin_42608414/article/details/130604478