ChatGPT提示词原则

Posted by Chase Shen on 2023-05-15
Estimated Reading Time 8 Minutes
Words 1.7k In Total
Viewed Times

1. 编写明确而具体的指示

明确 ≠ 短少

策略1. 使用分隔符

三个双引号 """
反引号 ```
三个破折号 ---
尖括号 < >
XML标签 <tag> </tag>

1
2
3
4
5
6
7
8
9
10
11
12
#以下案例,使用```将特定文本部分与提示的其余部分分隔开
text = f"""
You should express what you want a model to do by providing instructions that are\ as clear and 1specific as you can possibly make them.This will quide 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)

使用分隔符还可以避免提示词冲突。提示冲突是指如果允许用户向提示中添加一些输入,则他们可能会给出与想要的任务不符的指令,导致模型遵循用户的指令而不是预先设定好的指令。

策略2. 要求结构化输出

为了使解析模型输出结构更容易,可以请求使用HTML或JSON等结构化输出

1
2
3
4
5
6
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)

策略3. 要求模型检查是否满足条件

如果任务存在假设未必满足,那么我们可以告诉模型首先检查这些假设;如果不满足,则指示并停止尝试完成任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 1hot 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
then simply write \"No steps provided.\"

\"\"\"{text 1}\"\"\"
"""
response = get_completion(prompt)
print("completion for Text 1:")
print(response)

策略4. 少量训练提示

要求模型执行任务之前,提供成功执行任务的示例

1
2
3
4
5
6
7
8
9
10
11
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)

2. 给模型足够的时间来思考

如果模型通过急于做出错误的结论而出现推理错误,应该尝试重新构建查询请求相关推理的链或序列;另一种思考方式是,如果你给模型一个太复杂的任务,在短时间内或少数词中完成它,它可能会猜测结果,这可能是不正确的,同样的问题也可能会发生在人类身上。因此,你可以指示模型在问题上花更多的时间思考,这意味着它在任务上会花费更多的计算力。

策略1. 指定完成任务所需的步骤

1
2
3
4
Step 1: ...
Step 2: ...
...
Step N: ...
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
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)

#example 2, asking for output in a specified format
prompt_2 = f"""
Your task is to perform the following actions:
1 - Summarize the following text delimited by <> with 1 sentence.
2 - Translate the surmary into French.
3 - List each name in the prench 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)

策略2. 指示模型在匆忙做出结论之前思考解决方案

在让模型说出答案是否正确之前,为模型提供足够时间先自己去实际思考问题,就像一个人一样,然后再用自己的结论去对比。

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
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:
<tag>
question here
</tag>

Student's solution:
<tag>
student's solution here
</tag>

Actual solution:
<tag>
steps to work out the solution and your solution here
</tag>

Is the student's solution the same as actual solution just calculated:
<tag>
yes or no
</tag>

Student grade:
<tag>
correct or incorrect
</tag>

Question:
<tag>
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.
</tag>

Student's solution:
<tag>
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
</tag>

Actual solution:
"""
response = get_completion(prompt)
print(response)

3.语言模型的温度

温度参数允许我们改变模型响应的多样性,可以将温度视为模型的探索程度或随机性。一般来说,如果想构建一个可靠和可预测的系统,建议使用温度为0;如果想以更有创意的方式使用模型,获得更广泛的不同输出,可能需要使用更高的温度。总之,在较高的温度下,模型的输出更随机,同时也更容易分散注意力,但可能更有创造力。

4.模型的局限性

随着使用生成式AI应用程序的大型语言模型(LLM)日趋复杂,人们愈发担心这些模型可能会产生不准确或具误导性的输出。该问题称之为“幻觉”,即AI模型会产生完全捏造的信息,既不准确也不真实。


如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !