我的两分钱 2023-04-03 323 0 0 0 0
ChatGPT使用技巧,Prompt,ChatGPT提示词,本文将和大家一起从简单到复杂,梳理一下目前常见的Prompt技巧,他山之石可以攻玉,相信这些经验一定能给大家带来帮助(下文中引用格式的内容为Prompt)。

用过一段时间ChatGPT的同学都会发现,Prompt的好坏对于能否充分发挥ChatGPT的潜力有着决定性的作用,那么我们如何能写出好的Prompt呢?除了自己摸索尝试,借鉴别人的经验也会大有裨益。

本文将和大家一起从简单到复杂,梳理一下目前常见的Prompt技巧,他山之石可以攻玉,相信这些经验一定能给大家带来帮助(下文中引用格式的内容为Prompt)。

Prompt技巧完整指南

一些通用原则

从简单到复杂:在写一个Prompt的时候,虽然你可能会想到很多能让Prompt更好的点,但最好还是先写一个简单的Prompt,看看效果,不满意再逐步修改Prompt增加更多的提示,有很多时候我们会发现简单的Prompt就可以得到不错的效果。

Prompt要足够具体:一般Prompt越具体,描述性越强,其效果越好,这里更重要的是Prompt的格式和描述,不要试图找到一两个魔法词,一下就能提高效果。同时也要注意平衡Prompt的长度,毕竟ChatGPT有Context大小限制,太多不必要的细节可能还会起到反作用,简单但具体的Prompt是最好的。

避免使用模糊的词:比如下面这个Prompt:

"Explain the concept of prompt engineering. Keep the explanation short, only a few sentences, and don't be too descriptive."

"short","a few",”too"这几个词就比较模糊,而改成下面这个Prompt就准确了很多:

"Use 2-3 sentences to explain the concept of prompt engineering to a high school student."

说你要做什么而不是不做什么:有时候你即使在Prompt中强调不要干什么,但ChatGPT可能还是会忽略你的否定性的要求,比如这个Prompt:

The following is an agent that recommends movies to a customer. DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION. 

Customer: Please recommend a movie based on my interests. 

Agent:

其得到的输出是:

Sure, I can recommend a movie based on your interests. What kind of movie would you like to watch? Do you prefer action, comedy, romance, or something else?

而改成下面这样

The following is an agent that recommends movies to a customer. The agent is responsible to recommend a movie from the top global trending movies. It should refrain from asking users for their preferences and avoid asking for personal information. .". 

Customer: Please recommend a movie based on my interests. 

Agent:

得到的输出是:

Sorry, I don't have any information about your interests. However, here's a list of the top global trending movies right now: [list of movies]. I hope you find something you like!

基础Prompt技巧

Zero-shot(零提示):这是Prompt最基础的形式,我们直接向ChatGPT提出问题即可,比如下面这个情感分类问题:

Text: i'll bet the video game is a lot more fun than the film.

Sentiment:

Few-shot(少量提示):这个技巧就是先给ChatGPT几个例子,然后再让ChatGPT回答自己的问题,还是上面的情感分类问题:

Text: (lawrence bounces) all over the stage, dancing, running, sweating, mopping his face and generally displaying the wacky talent that brought him fame in the first place.

Sentiment: positive

Text: despite all evidence to the contrary, this clunker has somehow managed to pose as an actual feature movie, the kind that charges full admission and gets hyped on tv and purports to amuse small children and ostensible adults.

Sentiment: negative

Text: for the first time in years, de niro digs deep emotionally, perhaps because he's been stirred by the powerful work of his co-stars.

Sentiment: positive

Text: i'll bet the video game is a lot more fun than the film.

Sentiment:

通过给ChatGPT提供几个例子,能显著提升其回答的准确性,这里有几个需要注意的点:

    • 例子和最后的问题越相似,效果越好

    • 例子的多样性越多,效果越好

    • 例子的排序最好是随机的,以上面的分类问题为例,先全部都是positive,然后全部都是negative就容易误导ChatGPT

    • 如果你发现ChatGPT给出的答案总是一定的倾向性,可以试着打乱例子的排序看看效果

Instruction (说明意图):上面few-shot这个技巧,本质可以理解为通过例子向ChatGPT说明自己的意图,所以我们也可以直接说明我们的意图,然后再提出问题:

Please label the sentiment towards the movie of the given movie review. The sentiment label should be "positive" or "negative". 

Text: i'll bet the video game is a lot more fun than the film. 

Sentiment:

指出我们的目标听众是另一种说明意图的方法:

Describe what is quantum physics to a 6-year-old kid.

Few-shot Instruction:我们也可以把few-shot和Instruction组合起来进一步说明意图:

Definition: Determine the speaker of the dialogue, "agent" or "customer". Input: I have successfully booked your tickets. Ouput: agent Definition: Determine which category the question asks for, "Quantity" or "Location". Input: What's the oldest building in US? Ouput: Location 
Definition: Classify the sentiment of the given movie review, "positive" or "negative". Input: i'll bet the video game is a lot more fun than the film. Output:

Chain of Thought(CoT,思维链):就是告诉ChatGPT要先给出思考过程,最后再给出最终答案,有点像我们在学校做数学题,需要先给出中间的推导步骤,最后才是最终答案。下面是一个Zero-shot 的CoT:

Question: Marty has 100 centimeters of ribbon that he must cut into 4 equal parts. Each of the cut parts must be divided into 5 equal parts. How long will each final cut be?

Answer: Let's think step by step:

也可以在CoT中加入几个例子,下面是一个Few-shot的CoT:

Question: Tom and Elizabeth compete to climb a hill. Elizabeth takes 30 minutes. Tom takes four times as long as Elizabeth does. How many hours does it take Tom to climb up the hill?

Answer: It takes Tom 30*4 = <<30*4=120>>120 minutes to climb the hill.

It takes Tom 120/60 = <<120/60=2>>2 hours to climb the hill.

So the answer is 2.

===

Question: Jack needs to buy two pairs of socks and a pair of soccer shoes. Each pair of socks cost $9.50, and the shoes cost $92. Jack has $40. How much more money does Jack need?

Answer: The cost of socks is $9.50 x 2 = $<<9.5*2=19>>19. cost of the socks and shoes is $19 + $92 = $<<19+92=111>>111.

Jack need $111 - $40 = $<<111-40=71>>71 more.

So the answer is 71.

===

Question: Marty has 100 centimeters of ribbon that he must cut into 4 equal parts. Each of the cut parts must be divided into 5 equal parts. How long will each final cut be?

Answer:

思维链虽然在我们看起来很简单,甚至Zero-shot的CoT只是简单增加了“let's think step by step"这句话,但其推理能力却出现了巨大的提升,为什么会这样?目前还没有明确的研究结果。

使用CoT也有两个需要注意的细节:

    • 一般给出的例子中涉及的推导步骤越多,效果越好,但如果问题很简单,但例子太过复杂,效果也不好

    • 如果例子涉及多个步骤,用换行符"\n",要比用文字"第n步",或者"."、";"等字符分隔多个步骤效果更好

基于CoT还有一个衍生的技术叫Self-Ask(自我提问):其基本思路就是让ChatGPT不断地问自己问题(问题的答案则可以来自外部API),从而形成一个思维链,下面是一个Few-shot self-ask的例子:

Prompt技巧完整指南

LangChain也有一个类似的实现,我在《大白话说AI:探索 MRKL 架构,将LLM与外部知识和推理模块相融合》一文的LangChain部分有详细介绍,有兴趣的同学可以参考这个介绍和LangChain的源码来详细了解这个技术。

分而治之:对于复杂的问题,CoT是让ChatGPT自己把思考过程写下来,我们也可以人为地为ChatGPT设定思考过程,或者说是工作流程,以下是OpenAI自己给出的例子:

Use the following clues to answer the following multiple-choice question.

Clues:
1. Miss Scarlett was the only person in the lounge.
2. The person with the pipe was in the kitchen.
3. Colonel Mustard was the only person in the observatory.
4. Professor Plum was not in the library nor the billiard room.
5. The person with the candlestick was in the observatory.
Question: Was Colonel Mustard in the observatory with the candlestick?
(a) Yes; Colonel Mustard was in the observatory with the candlestick
(b) No; Colonel Mustard was not in the observatory with the candlestick
(c) Unknown; No enough information to determine whether Colonel Mustard was in the observatory with the candlestick

Solution:

得到的答案是:

Unknown; there is not enough information to determine whether Colonel Mustard was in the observatory with the candlestick

现在让我们换个Prompt为ChatGPT设定工作思路:

Use the following clues to answer multiple-choice questions with this procedure:(1) First, go through the clues one by one and consider whether the clue is potentially relevant (2) Second, combine the relevant clues to reason out the answer to the question (3) Third, map the answer to one of the multiple choice answers: either (a), (b), or (c)

Clues: 

1. Miss Scarlett was the only person in the lounge. 

2. The person with the pipe was in the kitchen. 

3. Colonel Mustard was the only person in the observatory. 

4. Professor Plum was not in the library nor the billiard room. 

5. The person with the candlestick was in the observatory.

Question: Was Colonel Mustard in the observatory with the candlestick? 

(a) Yes; Colonel Mustard was in the observatory with the candlestick 

(b) No; Colonel Mustard was not in the observatory with the candlestick 

(c) Unknown; there is not enough information to determine whether Colonel Mustard was in the observatory with the candlestick 

Solution: 

(1) First, go through the clues one by one and consider whether the clue is potentially relevant:

我们得到的输出是:

Clue 1: Miss Scarlett was the only person in the lounge. This clue is not relevant.

Clue 2: The person with the pipe was in the kitchen. This clue is not relevant.

Clue 3: Colonel Mustard was the only person in the observatory. This clue is relevant.

Clue 4: Professor Plum was not in the library nor the billiard room. This clue is not relevant.

Clue 5: The person with the candlestick was in the observatory. This clue is relevant.

(2) Second, combine the relevant clues to reason out the answer to the question:

Clue 3 and Clue 5 both say that the person with the candlestick was in the observatory. Colonel Mustard was the only person in the observatory, so Colonel Mustard was the person with the candle

(3) Third, write the final answer:

(a) Yes; Colonel Mustard was in the observatory with the candlestick

这回我们得到了正确答案。

总结一下,无论使用什么Prompt技巧,我们都应该让我们的Prompt尽可能简单、具体、准确,有话直说,在此基础上,我们有Zero-shot, Few-shot, Instruction, CoT以及分而治之等技巧,在Few-shot中,例子的多样性和排序对结果有较大的影响,在CoT中,例子的复杂度和描述方式对结果也有很大影响。这些技巧还可以组合起来使用如Few-Shot Instruction, Zero-shot CoT, Few-Shot CoT等,另外CoT还有一个衍生技术叫做Self-Ask,让ChatGPT自己提问,我们通过外部API来给它答案,帮助它逐步推导出问题的答案。

除了本文描述的基础技巧外,还有一些更复杂的Prompt技术,我也将在未来文章中继续介绍。


Tag: ChatGPT使用技巧 Prompt ChatGPT提示词
歡迎評論
未登錄,
請先 [ 註冊 ] or [ 登錄 ]
(一分鍾即可完成註冊!)
返回首頁     ·   返回[ChatGPT]   ·   返回頂部