文章目录[隐藏]
如果你想深入了解chatgpt,这里整理了一份gpt课程合集资源,或许能解决你人工智能方面很多问题:
简介
基础用法
注意
本章节会简单介绍下 prompt ,如果你已经懂得使用 ChatGPT 这类产品的用法,可跳过这一章。
如果你没怎么使用过类似 ChatGPT 这类 Chatbot 产品,我简单说下它的基本用法:
•
你只需要登录 ChatGPT 后,在输入框内输入问题,或者指令即可。AI 会根据你的指令,返回相应的内容,在后续的章节中,我会介绍更详细的用法与技巧:

另外,还有一个特别留意的,ChatGPT 跟你之前用的一些 AI 产品不同,它还支持多轮对话,比如当你问
What is the capital of China?
(中国的首都是什么?),它回答 Beijing,你可以接着问 How many people are there?
,你不需要在问题里重复 Beijing 这个词。ChatGPT 知道 there 指的就是 Beijing。基本原则 & 建议
在和 ChatGPT 对话时,亦或者在使用和设计 prompt 时,有以下几个原则与建议。记住这几个原则,能让你写出更好的 prompt 😁
建议使用最新的模型
如果你是直接使用 AI 产品,比如 ChatGPT 或者 Midjourney,那无需在意这个原则。如果你是通过 API 或者 OpenAI Playground 的方式使用,则建议你先使用最新的模型测试。
Prompt 里最好包含完整的信息
这个是对结果影响最大的因素。比如如果你想让 AI 写一首关于 OpenAI 的诗。
Less effective prompt:
Plain Text
Write a poem about OpenAI.
它生成的答案可能就会很宽泛,而更好的方式是增加更多的信息。
Better prompt:
Plain Text
Write a short inspiring poem about OpenAI, focusing on the recent DALL-E product launch (DALL-E is a text to image ML model) in the style of a {famous poet}
Prompt 最好简洁易懂,并减少歧义
这个比较好理解,即使你跟人说话,说一些简单的短句,对方也会更容易听懂,AI 也是这样。另外,在 prompt 里也需要减少一些歧义,少用模棱两可的词语。
比如像这个就很不明确,什么叫 not too much more?
Plain Text
The description for this product should be fairly short, a few sentences only, and not too much more.
更好的 prompt 是这样的,明确告知要写多少句话,就比较明确:
Plain Text
Use a 3 to 5 sentence paragraph to describe this product.
Prompt 要使用正确的语法、拼写,以及标点
特别是在使用英语的 prompt 的时候,一定要注意语法和拼写。
基本使用场景 & 使用技巧
场景1:问答问题
这个场景应该是使用 AI 产品最常见的方法。以 ChatGPT 为例,一般就是你提一个问题,ChatGPT 会给你答案,比如像这样:

在这个场景下,prompt 只要满足前面提到的基本原则,基本上就没有什么问题。但需要注意,不同的 AI 模型擅长的东西都不太一样,prompt 可能需要针对该模型进行微调。另外,目前的 AI 产品,也不是无所不能,有些问题你再怎么优化 prompt 它也没法回答你。以 ChatGPT 为例:
1.
ChatGPT 比较擅长回答基本事实的问题,比如问
什么是牛顿第三定律?
。但不太擅长回答意见类的问题,比如问它 谁是世界第一足球运动员?
,它就没法回答了。2.
另外,ChatGPT 的数据仅有 2021 年 9 月以前的,如果你问这个时间以后的问题,比如
现在的美国总统是谁?
它的答案是「截至2021年9月,现任美国总统是乔·拜登(Joe Biden)。」✏
🔴 求助
这种直接提问的 prompt ,我们称之为 Zero-shot prompt。模型基于一些通用的先验知识或模型在先前的训练中学习到的模式,对新的任务或领域进行推理和预测。你会在高级篇看到相关的介绍,以及更多有意思的使用方法。
另外,正如我在前面基础用法一章中提到的那样,问答场景里还有一个很重要的玩法,就是多轮聊天,你可以针对某个问题,进行多轮的提问。
使用技巧一:To do and Not To do
注意
我介绍的技巧其实在各个场景都可以使用,我将其放在某个场景下解释,只是因为我觉得它更有可能在这个场景用到。你也会更容易记住这个用法。并不意味着这个技巧仅能在此场景使用。并且多技巧混用也是个不错的用法。
在问答场景里,为了让 AI 回答更加准确,一般会在问题里加条件。比如让 AI 推荐一部电影给你
Recommend a movie to me
。但这个 prompt 太空泛了,AI 无法直接回答,接着它会问你想要什么类型的电影,但这样你就需要跟 AI 聊很多轮,效率比较低。所以,为了提高效率,一般会在 prompt 里看到类似这样的话(意思是不要询问我对什么感兴趣,或者问我的个人信息):
Plain Text
DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.
如果你在 ChatGPT 里这样提问,或者使用 ChatGPT 最新的 API ,它就不会问你问题,而是直接推荐一部电影给你,它的 Output 是这样的:
Plain Text
Certainly! If you’re in the mood for an action-packed movie, you might enjoy “John Wick” (2014), directed by Chad Stahelski and starring Keanu Reeves. The movie follows a retired hitman named John Wick who seeks vengeance against the people who wronged him. It’s a fast-paced and stylish film with lots of thrilling action sequences and an engaging story. If you’re looking for something that will keep you on the edge of your seat, “John Wick” is definitely worth a watch!
但如果你使用的是如 Davinci-003 这样的模型,它的 Output 很可能是这样的,它还会问你的兴趣爱好:
Plain Text
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?
所以 OpenAI 的 API 最佳实践文档里,提到了一个这样的最佳实践:
Instead of just saying what not to do, say what to do instead. 与其告知模型不能干什么,不妨告诉模型能干什么。
我自己的实践是,虽然现在最新的模型已经理解什么是 Not Todo ,但如果你想要的是明确的答案,加入更多限定词,告知模型能干什么,回答的效率会更高,且预期会更明确。还是电影推荐这个案例,你可以加入一个限定词:
Plain Text
Recommend a movie from the top global trending movies to me.
当然并不是 Not Todo 就不能用,如果:
•
你已经告知模型很明确的点,然后你想缩小范围,那增加一些 Not Todo 会提高不少效率。
•
你是在做一些探索,比如你不知道如何做精准限定,你只知道不要什么。那可以先加入 Not Todo ,让 AI 先发散给你答案,当探索完成后,再去优化 prompt。
以下是一些场景案例,我整理了两个 Less Effective(不太有效的) 和 Better(更好的) prompt,你可以自己尝试下这些案例:

场景2:基于示例回答
场景介绍
在某些场景下,我们能比较简单地向 AI 描述出什么能做,什么不能做。但有些场景,有些需求很难通过文字指令传递给 AI,即使描述出来了,AI 也不能很好地理解。
比如给宠物起英文名,里面会夹杂着一些所谓的名字风格。此时你就可以在 prompt 里增加一些例子,我们看看这个例子。
这个是没有任何示例的 Prompt:
Plain Text
Suggest three names for a horse that is a superhero.
Output 如下所示。第一个感觉还行,第二个 Captain 有 hero 的感觉,但 Canter 就像是说这匹马跑得很慢,感觉不太合适,而且三个都比较一般,不够酷。
Plain Text
Thunder Hooves, Captain Canter, Mighty Gallop
技巧 2:增加示例
如果你无法用文字准确解释问题或指示,你可以在 prompt 里增加一些案例:
Plain Text
Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: Horse
Names:
增加例子后,Output 的结果就更酷一些,或者说是接近我想要的那种风格的名字。
Plain Text
Gallop Guardian, Equine Avenger, The Mighty Stallion
以下是一些场景案例,我整理了两个 Less Effective(不太有效的)和 Better(更好的)prompt,你可以自己尝试下这些案例:
🏕
🔴
你可能在试用此技巧的时候发现,即使给了案例,模型也不一定会返回正确的

场景3:推理
场景介绍
在问答这个大场景下,还有一个子场景是推理,这个场景非常有意思,而且是个非常值得深挖的场景,prompt 在此场景里发挥的作用非常大。
如果你想用 ChatGPT API 做点什么小应用,我建议可以从这个场景入手,相对来说没有其他场景那么红海。
举个比较简单的例子,我将美国工商管理硕士的入学考试 GMAT 的一道数学题输入进了 ChatGPT:
Plain Text
If a real estate agent received a commission of 6 percent of the selling price of a certain house, what was the selling price of the house?
(1) The selling price minus the real estate agent’s commission was $84,600.
(2) The selling price was 250 percent of the original purchase price of $36,000.
(A) Statement (1) ALONE is sufficient, but statement (2) alone is not sufficient.
(B) Statement (2) ALONE is sufficient, but statement (1) alone is not sufficient.
(C) BOTH statements TOGETHER are sufficient, but NEITHER statement ALONE is sufficient.
(D) EACH statement ALONE is sufficient.
(E) Statements (1) and (2) TOGETHER are NOT sufficient.
这道题目的中文意思是这样的(你不妨也思考下这个题目?):
Plain Text
如果一个房地产经纪人的佣金是某个房子的售价的6%,那么这个房子的售价是多少?
(1)售价减去房地产经纪人的佣金为84,600美元。
(2)购买价是36,000美元,售价是购买价的250%。
(A)仅陈述(1)足以回答问题,但仅陈述(2)不能回答问题。
(B)仅陈述(2)足以回答问题,但仅陈述(1)不能回答问题。
(C)两个陈述合起来足以回答问题,但没有一个陈述单独足以回答问题。
(D)每个陈述单独足以回答问题。
(E)陈述(1)和(2)合起来不能回答问题。
它的 Output 是这样的,它会根据题目的要求去推理和运算:
Plain Text
Let’s assume the selling price of the house is S.
According to the given information, the real estate agent’s commission is 6% of S, which is 0.06S.
So, the amount received by the seller after paying the commission would be (S – 0.06S) = 0.94S.
Statement (1) tells us that 0.94S = $84,600. Solving for S, we get S = $90,000.
Therefore, statement (1) alone is sufficient to determine the selling price of the house.
Statement (2) tells us that the selling price is 250% of the original purchase price, which means S = 2.5*$36,000 = $90,000.
Therefore, statement (2) alone is also sufficient to determine the selling price of the house.
Since both statements individually provide the same answer, we can conclude that together they are also sufficient to determine the selling price of the house.
Hence, the answer is (D) EACH statement ALONE is sufficient.
翻译成中文是这样的:
Plain Text
假设房子的售价为S。
根据所给信息,房地产经纪人的佣金是S的6%,即0.06S。
因此,在支付佣金后卖方收到的金额为(S-0.06S)= 0.94S。
陈述(1)告诉我们,0.94S = 84,600美元。解出S,我们得到S = $90,000。
因此,陈述(1)单独足以确定房子的售价。
陈述(2)告诉我们,售价是原始购买价的250%,这意味着S = 2.5 * $36,000 = $90,000。
因此,陈述(2)单独也足以确定房子的售价。
由于两个陈述分别提供相同的答案,我们可以得出结论,它们共同足以确定房子的售价。
因此,答案是(D)每个陈述单独足以回答问题。
这个场景还比较简单,我就不在这举更多例子了,你可以将各种题目输入进 ChatGPT 里试试。
场景4:无中生有——写代码
场景介绍
除了回答问题外,另一个重要的场景是让 AI 根据你的要求完成一些内容生成任务,根据输出的结果差异,我将其概括为以下几个主要场景:
1.
无中生有
2.
锦上添花
3.
化繁为简
本章,我们先来聊聊「无中生有」场景。顾名思义,就是让 AI 给你生成一些内容。你只需要向 AI 描述你想写的内容,AI 就会按你的要求写出该内容。比如:
1.
撰写招聘信息
2.
撰写电商平台的货物描述
3.
撰写短视频脚本
4.
甚至让它写代码都可以
像撰写招聘信息等,你只需要明确目标,将目标定得比较明确即可生成能让你较为满意的答案。本章我想聊下写代码。下面是一个让 ChatGPT 写代码的案例。
Prompt:
Plain Text
Create a MySQL query for all students in the Computer Science Department:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Output:
Plain Text
SELECT students.*
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = ‘Computer Science’;
技巧 3:使用引导词,引导模型输出特定语言代码
在代码生成场景里,有一个小技巧,上面提到的案例,其 prompt 还可以继续优化,在 prompt 最后,增加一个代码的引导,告知 AI 我已经将条件描述完了,你可以写代码了。
在 prompt 的最后增加 SELECT 可以很好地提示 AI 可以写 SQL 代码了。Better prompt:
Plain Text
Create a MySQL query for all students in the Computer Science Department:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
SELECT
同样的道理,如果你想让 AI 写 Python 代码,那 import 会是比较好的提示。但需要注意,这个只是告知 AI 可以写代码了,并不能告知 AI 写何种代码,你仍然需要在 prompt 里增加提示,告知 AI 要生成何种语言的代码。
场景5:锦上添花——改写内容
场景介绍
除了从 0 生成内容外,你也可以先给 AI 一段已经写好的内容,然后让 AI 对其进行修改,让 AI 帮你将内容改得更好。实际的场景有如:
1.
翻译:将中文文档翻译成英文,亦或者将英文翻译成中文。关于翻译,我还想强调,像 ChatGPT 除了能翻译人类的语言外,还能翻译编程语言,比如将 Python 代码翻译成 Haskell 代码。
2.
修改:修改内容的语法,甄别内容里的错别字。
3.
润色:润色文章,将内容改成另一种风格。
本章重点介绍下润色场景,下面这是个润色的例子。
Prompt(内容来自维基百科):
Plain Text
Please rewrite the following sentences to make them easier to understand.
OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated (OpenAI Inc.) and its for-profit subsidiary corporation OpenAI Limited Partnership (OpenAI LP). OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on the fifth most powerful supercomputer in the world.[5][6][7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others,[8][1][9] who collectively pledged US$1 billion. Musk resigned from the board in 2018 but remained a donor. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a second multi-year investment in January 2023, reported to be $10 billion.[10]
它的 Output 是这样的:
Plain Text
OpenAI is a research organization that focuses on artificial intelligence (AI). It has two parts: a non-profit called OpenAI Incorporated and a for-profit subsidiary called OpenAI Limited Partnership. The organization’s goal is to create AI that is friendly and beneficial to humans. OpenAI uses one of the most powerful supercomputers in the world for its research. The organization was founded in San Francisco in 2015 by a group of people who pledged a total of $1 billion, including Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, and Peter Thiel. Musk resigned from the board in 2018 but still supports the organization financially. In 2019, Microsoft invested $1 billion in OpenAI LP, and in January 2023, they made a second investment reported to be $10 billion.
技巧 4:增加 Role(角色)或人物
前面提到的改写例子,我在 prompt 里加了 Role 让其更易于阅读,这个优化方法是 OK 的。也很常用,比如你想将你写的 Email 改得更商务,则只需要加 business 相关的词即可。
我再介绍一个更有效的技巧,就是在 prompt 里增加一些 role(角色)相关的内容,让 AI 生成的内容更符合你的需求。
比如还是上面那个 rewrite 的例子,我在例子前加入这样的一段话,我让 AI 假设自己是一个小学老师,并且很擅长将复杂的内容转变成 7、8 岁小朋友也能听懂的话,然后再改写这段话:
Plain Text
You are a primary school teacher who can explain complex content to a level that a 7 or 8 year old child can understand. Please rewrite the following sentences to make them easier to understand:
Output 就变成了下面这样。不管是用词,还是语法,看起来都更容易懂了。
Plain Text
OpenAI is a place where people work on making computers really smart. It’s like a lab where they do research to make computers do amazing things! They want the computers to be really friendly and helpful to people. They use one of the most powerful computers in the world to do this research. A bunch of really rich and smart people gave money to start this lab. One of them is Elon Musk, but he left in 2018. Another really big company, Microsoft, gave them a lot of money too.
另外除了增加角色外,还能让 AI 模拟某个人,比如让 AI 以 David Foster Wallace 的风格改写上述那段话:
Plain Text
Imagine you are the famous writer David Foster Wallace, please rewrite this sentences.
Output 是这样的:
Plain Text
As David Foster Wallace, I might rewrite these sentences to sound more like this:
OpenAI is a lab that studies something called artificial intelligence, which is like when computers can do things that normally only humans can do. They have a big computer that’s one of the most powerful in the world, and they’re trying to make sure the AI they create is friendly and helpful. The lab was started in 2015 in a city called San Francisco, and a bunch of important people gave them a lot of money to do their research. One of those people was Elon Musk, who stopped working there but still gives them money, and Microsoft also gave them a lot of money recently.
关于这个技巧的应用,以及如何更好地使用 AI 提升写作能力,建议各位阅读 Nat Eliason 的 Using ChatGPT as a Writing Coach,他使用 ChatGPT 辅助其写作,就用到了上述的技巧。
场景6:锦上添花——信息解释
场景介绍
锦上添花下的第二大的场景是信息解释。它跟改写内容有点像,但又不太一样,信息解释有可能提供超过原文内容的信息。
举几个信息解释的例子,大家应该就能理解了:
1.
解释代码:比如你看到一段 Python 的代码,但你看不懂,你可以让 AI 解释下代码的含义。
2.
解释论文:看某篇论文看不懂,或者论文里的某一段看不懂,你也可以让 AI 解释。
以解释代码为例,它的 prompt 是这样的(此案例来自 OpenAI 官方案例):
Plain Text
Explanation of what the code does:
Python 3
def remove_common_prefix(x, prefix, ws_prefix):
x[“completion”] = x[“completion”].str[len(prefix) :]
if ws_prefix:
# keep the single whitespace as prefix
x[“completion”] = ” ” + x[“completion”]
return x
以下是 ChatGPT 的 Output,它阐述了这段代码是拿来干什么的,同时还阐述了各个参数的含义:
Plain Text
This is a Python function called remove_common_prefix that takes in three parameters: x, prefix, and ws_prefix.
The x parameter is assumed to be a Pandas DataFrame or Series object with a column named “completion”, which contains strings that may or may not start with the prefix string.
The prefix parameter is a string that represents the common prefix that needs to be removed from the beginning of each string in the “completion” column of the DataFrame.
The ws_prefix parameter is a Boolean value that indicates whether or not to keep a single whitespace character as a prefix after the common prefix has been removed.
The function modifies the “completion” column of the DataFrame by removing the prefix string from the beginning of each string in that column using the Pandas str method and string slicing. The len(prefix) : syntax is used to slice the string starting from the character that follows the prefix.
If ws_prefix is True, then the function adds a single whitespace character as a prefix to each string in the “completion” column after removing the prefix.
Finally, the modified DataFrame is returned by the function.
场景7:化繁为简——信息总结
场景介绍
内容生成大场景下的第二个场景是化繁为简,这个场景其实很好理解,就是将复杂的内容,转为简单的内容,一般常遇到的场景有:
1.
信息总结:顾名思义,就是对一堆信息进行总结。
2.
信息解释:这个跟改写内容有点像,但这个更偏向于解释与总结。下一章会给大家介绍更多的例子。
3.
信息提取:提取信息里的某一段内容,比如从一大段文字中,找到关键内容,并分类。
本章会讲一下信息总结。信息总结还是比较简单的,基本上在 prompt 里加入总结 summarize 就可以了。但如果你想要一些特别的效果,不妨组合使用之前介绍的技巧,比如:
1.
使用技巧 2,增加总结示例,让 AI 总结符合你需求的内容
2.
使用技巧 4,增加 role,让 AI 总结的内容具有一定的风格
不过在这个场景,还有个技巧需要各位注意。
技巧 5:使用”“”符号将指令和需要处理的文本分开
不管是信息总结,还是信息提取,你一定会输入大段文字,甚至多段文字,此时有个小技巧。
可以用“”“将指令和文本分开。根据我的测试,如果你的文本有多段,增加”“”会提升 AI 反馈的准确性(这个技巧来自于 OpenAI 的 API 最佳实践文档)
信息
像我们之前写的 prompt 就属于 Less effective prompt。为什么呢?据我的测试,主要还是 AI 不知道什么是指令,什么是待处理的内容,用符号分隔开来会更利于 AI 区分。
Plain Text
Please summarize the following sentences to make them easier to understand.
OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated (OpenAI Inc.) and its for-profit subsidiary corporation OpenAI Limited Partnership (OpenAI LP). OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on the fifth most powerful supercomputer in the world.[5][6][7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others,[8][1][9] who collectively pledged US$1 billion. Musk resigned from the board in 2018 but remained a donor. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a second multi-year investment in January 2023, reported to be $10 billion.[10]
Better prompt:
Plain Text
Please summarize the following sentences to make them easier to understand.
Text: “””
OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated (OpenAI Inc.) and its for-profit subsidiary corporation OpenAI Limited Partnership (OpenAI LP). OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on the fifth most powerful supercomputer in the world.[5][6][7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others,[8][1][9] who collectively pledged US$1 billion. Musk resigned from the board in 2018 but remained a donor. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a second multi-year investment in January 2023, reported to be $10 billion.[10]
“””
场景8:化繁为简——信息提取
场景介绍
介绍完信息总结,再聊聊信息提取,我认为这个场景是继场景3推理以外,第二个值得深挖的场景。这个场景有非常多的有意思的场景,比如:
1.
将一大段文字,甚至网页里的内容,按要求转为一个表格。按照这个思路你可以尝试做一个更智能的,更易懂的爬虫插件。
2.
按照特定格式对文章内容进行信息归类。
Plain Text
Extract the important entities mentioned in the article below. First extract all company names, then extract all people names, then extract specific topics which fit the content and finally extract general overarching themes
Desired format:
Company names: <comma_separated_list_of_company_names>
People names: -||-
Specific topics: -||-
General themes: -||-
Text: “””Powering Next Generation
Applications with OpenAI Codex
Codex is now powering 70 different applications across a variety of use cases through the OpenAI API.
May 24, 2022
4 minute read
OpenAI Codex, a natural language-to-code system based on GPT-3, helps turn simple English instructions into over a dozen popular coding languages. Codex was released last August through our API and is the principal building block of GitHub Copilot.
Warp is a Rust-based terminal, reimagined from the ground up to help both individuals and teams be more productive in the command-line.
Terminal commands are typically difficult to remember, find and construct. Users often have to leave the terminal and search the web for answers and even then the results might not give them the right command to execute. Warp uses Codex to allow users to run a natural language command to search directly from within the terminal and get a result they can immediately use.
“Codex allows Warp to make the terminal more accessible and powerful. Developers search for entire commands using natural language rather than trying to remember them or assemble them piecemeal. Codex-powered command search has become one of our game changing features.”
—Zach Lloyd, Founder, Warp
Machinet helps professional Java developers write quality code by using Codex to generate intelligent unit test templates.
Machinet was able to accelerate their development several-fold by switching from building their own machine learning systems to using Codex. The flexibility of Codex allows for the ability to easily add new features and capabilities saving their users time and helping them be more productive.
“Codex is an amazing tool in our arsenal. Not only does it allow us to generate more meaningful code, but it has also helped us find a new design of product architecture and got us out of a local maximum.”
—Vladislav Yanchenko, Founder, Machinet”””
Prompt 有点长,我解释下,它是让 AI 将文章里的重点内容进行抽离,并将其根据特定格式进行输出。要求将文章里提到的公司、人名、主题都按照 comma separated list(按逗号空格分开)的方式罗列出来。
最后的 output 是这样的:
Plain Text
Company names: OpenAI, Microsoft, GitHub, Pygma, Replit, Warp, Machinet
People names: Emile Paffard-Wray, Amjad Masad, Zach Lloyd, Vladislav Yanchenko
Specific topics: GPT-3, OpenAI API, Azure OpenAI Service, GitHub Copilot, Pygma, Replit, Warp, Machinet
General themes: Natural language-to-code, Productivity, Problem solving, Creativity, Learning
Plain Text
Summarize the main points of the following speech
Use the following format:
Topic 1: <topic_name_1>
-
– <point_1>
..
Topic 2: <topic_name_2>
-
– <point_1>
..
Topic 10: ..
Text: “””
Thank you so much, Fred, for that lovely introduction. And thanks to the Atlantic Council for hosting me today.
The course of the global economy over the past two years has been shaped by COVID-19 and our efforts to fight the pandemic. It’s now evident, though, that the war between Russia and Ukraine has redrawn the contours of the world economic outlook. Vladimir Putin’s unprovoked attack on Ukraine and its people is taking a devastating human toll, with lives tragically lost, families internally displaced or becoming refugees, and communities and cities destroyed.
…
“””
Output 是这样的:
Plain Text
Topic 1: The war in Ukraine
-
– The war is taking a human toll with lives lost, families displaced, and communities destroyed
-
– The Biden administration is committed to holding Russia accountable
-
– The war has violated international law and is a challenge to the international order
Topic 2: The global economy
-
– The war is having negative impacts on the global economy, including higher commodity prices and inflation
-
– The IMF and World Bank will be focused on helping developing countries weather the impacts of the war
-
– The ultimate outcome for the global economy depends on the path of the war
关于这个场景和技巧,我想再解释一下为什么潜力很大。根据我使用各种 Summary 或者信息提取的产品,我发现,AI 并不知道什么是重点,所以在总结的过程中,会丢失很多内容。如何引导 AI 进行总结,就变得非常重要,且具有一定的可玩性。
一个使用此技巧的场景:
使用此技巧可以使输出更结构化。 比如针对一篇文章进行问答, 你不仅想要得到一个答案, 也希望 ChatGPT 的答案符合特定的格式, 方便你下一步进行自动化。
比如问 “这里的债券duration是多少?” , 正常 GPT 模型的答案可能是 “债券duration是4年” 或 “duration 4年”。 ChatGPT 的回答不稳定, 且不方便继续处理。
解法: 我们可以通过这个技巧,让模型理解我们预期的格式。并在此基础上, 为了方便自动化, 让模型输出特定的结构化答案(比如JSON/Markdown等)。 也可以方便集成更多的额外要求, 比如增加一个”confidence level”, 并通过 prompt 的形式指定这些数值的格式与甚至区间。
比如:
Plain Text
{context}
Question: What is bond duration mentioned here.
Answer template (Valid JSON format):
{{
“duration”: $duration_numeric_value_in_year,
“confidence_level”: $answer_confidence_level_high_moderate_or_low,
}}
Answer:
进阶
ChatGPT Prompt Framework
看完基础篇的各种场景介绍后,你应该对 Prompt 有较深的理解。
之前的章节我们讲的都是所谓的「术」,更多地集中讲如何用,但讲「道」的部分不多。高级篇除了会讲更高级的运用外,还会讲更多「道」的部分。
高级篇的开篇,我们来讲一下构成 prompt 的框架。
Basic Prompt Framework
查阅了非常多关于 ChatGPT prompt 的框架资料,我目前觉得写得最清晰的是 Elavis Saravia 总结的框架,他认为一个 prompt 里需包含以下几个元素:
•
Instruction(必须): 指令,即你希望模型执行的具体任务。
•
Context(选填): 背景信息,或者说是上下文信息,这可以引导模型做出更好的反应。
•
Input Data(选填): 输入数据,告知模型需要处理的数据。
•
Output Indicator(选填): 输出指示器,告知模型我们要输出的类型或格式。
只要你按照这个框架写 prompt ,模型返回的结果都不会差。
当然,你在写 prompt 的时候,并不一定要包含所有4个元素,而是可以根据自己的需求排列组合。比如拿前面的几个场景作为例子:
•
推理:Instruction + Context + Input Data
•
信息提取:Instruction + Context + Input Data + Output Indicator
CRISPE Prompt Framework
另一个我觉得很不错的 Framework 是 Matt Nigh 的 CRISPE Framework,这个 framework 更加复杂,但完备性会比较高,比较适合用于编写 prompt 模板。CRISPE 分别代表以下含义:
•
CR: Capacity and Role(能力与角色)。你希望 ChatGPT 扮演怎样的角色。
•
I: Insight(洞察力),背景信息和上下文(坦率说来我觉得用 Context 更好)。
•
S: Statement(指令),你希望 ChatGPT 做什么。
•
P: Personality(个性),你希望 ChatGPT 以什么风格或方式回答你。
•
E: Experiment(尝试),要求 ChatGPT 为你提供多个答案。
以下是这几个参数的例子:
将所有的元素都组合在一起,就变成了这样的 prompt,对比基础 prompt 生成的结果会非常不一样

Zero-Shot Prompts
在基础篇里的推理场景,我提到了 Zero-Shot Prompting 的技术,本章会详细介绍它是什么,以及使用它的技巧。
介绍
Zero-Shot Prompting 是一种自然语言处理技术,可以让计算机模型根据提示或指令进行任务处理。各位常用的 ChatGPT 就用到这个技术。
传统的自然语言处理技术通常需要在大量标注数据上进行有监督的训练,以便模型可以对特定任务或领域进行准确的预测或生成输出。相比之下,Zero-Shot Prompting 的方法更为灵活和通用,因为它不需要针对每个新任务或领域都进行专门的训练。相反,它通过使用预先训练的语言模型和一些示例或提示,来帮助模型进行推理和生成输出。
举个例子,我们可以给 ChatGPT 一个简短的 prompt,比如
描述某部电影的故事情节
,它就可以生成一个关于该情节的摘要,而不需要进行电影相关的专门训练。缺点
但这个技术并不是没有缺点的:
1.
Zero-Shot Prompting 技术依赖于预训练的语言模型,这些模型可能会受到训练数据集的限制和偏见。比如在使用 ChatGPT 的时候,它常常会在一些投资领域,使用男性的「他」,而不是女性的「她」。那是因为训练 ChatGPT 的数据里,提到金融投资领域的内容,多为男性。
2.
尽管 Zero-Shot Prompting 技术不需要为每个任务训练单独的模型,但为了获得最佳性能,它需要大量的样本数据进行微调。像 ChatGPT 就是一个例子,它的样本数量是过千亿。
3.
由于 Zero-Shot Prompting 技术的灵活性和通用性,它的输出有时可能不够准确,或不符合预期。这可能需要对模型进行进一步的微调或添加更多的提示文本来纠正。
技巧7:Zero-Shot Chain of Thought
基于上述的第三点缺点,研究人员就找到了一个叫 Chain of Thought 的技巧。
这个技巧使用起来非常简单,只需要在问题的结尾里放一句
Let‘s think step by step
(让我们一步步地思考),模型输出的答案会更加准确。这个技巧来自于 Kojima 等人 2022 年的论文 Large Language Models are Zero-Shot Reasoners。在论文里提到,当我们向模型提一个逻辑推理问题时,模型返回了一个错误的答案,但如果我们在问题最后加入
Let‘s think step by step
这句话之后,模型就生成了正确的答案:
论文里有讲到原因,感兴趣的朋友可以去看看,我简单解释下为什么(🆘 如果你有更好的解释,不妨反馈给我):
1.
首先各位要清楚像 ChatGPT 这类产品,它是一个统计语言模型,本质上是基于过去看到过的所有数据,用统计学意义上的预测结果进行下一步的输出(这也就是为什么你在使用 ChatGPT 的时候,它的答案是一个字一个字地吐出来,而不是直接给你的原因,因为答案是一个字一个字算出来的)。
2.
当它拿到的数据里有逻辑,它就会通过统计学的方法将这些逻辑找出来,并将这些逻辑呈现给你,让你感觉到它的回答很有逻辑。
3.
在计算的过程中,模型会进行很多假设运算(不过暂时不知道它是怎么算的)。比如解决某个问题是从 A 到 B 再到 C,中间有很多假设。
4.
它第一次算出来的答案错误的原因,只是因为它在中间跳过了一些步骤(B)。而让模型一步步地思考,则有助于其按照完整的逻辑链(A > B > C)去运算,而不会跳过某些假设,最后算出正确的答案。

按照论文里的解释,零样本思维链涉及两个补全结果,左侧气泡表示基于提示输出的第一次的结果,右侧气泡表示其收到了第一次结果后,将最开始的提示一起拿去运算,最后得出了正确的答案:
这个技巧,除了用于解决复杂问题外,还适合生成一些连贯主题的内容,比如写长篇文章、电影剧本等。
但需要注意其缺点,连贯不代表,它就一定不会算错,如果其中某一步骤算错了,错误会因为逻辑链,逐步将错误积累,导致生成的文本可能出现与预期不符的内容。
另外,根据 Wei 等人在 2022 年的论文表明,它仅在大于等于 100B 参数的模型中使用才会有效。如果你使用的是小样本模型,这个方法不会生效。
Few-Shot Prompting
同样在推理场景,我提到了 Few-Shot Prompting 的技术,本章介绍下它的优缺点和技巧。
介绍
我们在技巧2 中,提到我们可以给模型一些示例,从而让模型返回更符合我们需求的答案。这个技巧其实使用了一个叫 Few-Shot 的方法。
这个方法最早是 Brown 等人在 2020 年发现的,论文里有一个这样的例子,非常有意思,通过这个例子你应该更能体会,像 ChatGPT 这类统计语言模型,其实并不懂意思,只是懂概率 😁
Brown 输入的内容是这样的(whatpu 和 farduddle 其实根本不存在):
Plain Text
A “whatpu” is a small, furry animal native to Tanzania. An example of a sentence that uses
the word whatpu is:
We were traveling in Africa and we saw these very cute whatpus.
To do a “farduddle” means to jump up and down really fast. An example of a sentence that uses
the word farduddle is:
Output 是这样的:
Plain Text
When we won the game, we all started to farduddle in celebration.
不过这并不代表,Few-Shot 就没有缺陷,我们试试下面这个例子:
Prompt:
Plain Text
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.
The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output 是这样的:
Plain Text
The answer is True.
输出的答案其实是错误的,实际上的答案应该是:
Plain Text
Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.
那我们有没有什么方法解决?
技巧8:Few-Shot Chain of Thought
要解决这个缺陷,就要使用到新的技巧,Few-Shot Chain of Thought。
根据 Wei 他们团队在 2022 年的研究表明:
通过向大语言模型展示一些少量的样例,并在样例中解释推理过程,大语言模型在回答提示时也会显示推理过程。这种推理的解释往往会引导出更准确的结果。
下面是论文里的案例,使用方法很简单,在技巧2 的基础上,再将逻辑过程告知给模型即可。从下面这个案例里,你可以看到加入解释后,输出的结果就正确了。
那本章开头提的例子就应该是这样的(注:本例子同样来自 Wei 团队论文):
Plain Text
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.
The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: Adding all the odd numbers (17, 19) gives 36. The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: Adding all the odd numbers (11, 13) gives 24. The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: Adding all the odd numbers (17, 9, 13) gives 39. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
聊完技巧,我们再结合前面的 Zero-Shot Chain of Thought,来聊聊 Chain of Thought 的关键知识。根据 Sewon Min 等人在 2022 年的研究 表明,思维链有以下特点:
1.
“the label space and the distribution of the input text specified by the demonstrations are both key (regardless of whether the labels are correct for individual inputs)” 标签空间和输入文本的分布都是关键因素(无论这些标签是否正确)。
2.
the format you use also plays a key role in performance, even if you just use random labels, this is much better than no labels at all. 即使只是使用随机标签,使用适当的格式也能提高性能。
理解起来有点难,我找一个 prompt 案例给大家解释(🆘 如果你有更好的解释,不妨反馈给我)。我给 ChatGPT 一些不一定准确的例子:
Plain Text
I loved the new Batman movie! // Negative
This is bad // Positive
This is good // Negative
What a good show! //
Output 是这样的:
Plain Text
Positive
在上述的案例里,每一行,我都写了一句话和一个情感词,并用 // 分开,但我给这些句子都标记了错误的答案,比如第一句其实应该是 Positive 才对。但:
1.
即使我给内容打的标签是错误的(比如第一句话,其实应该是 Positive),对于模型来说,它仍然会知道需要输出什么东西。 换句话说,模型知道 // 划线后要输出一个衡量该句子表达何种感情的词(Positive or Negative)。这就是前面论文里 #1 提到的,即使我给的标签是错误的,或者换句话说,是否基于事实,并不重要。标签和输入的文本,以及格式才是关键因素。
2.
只要给了示例,即使随机的标签,对于模型生成结果来说,都是有帮助的。这就是前面论文里 #2 提到的内容。
最后,需要记住,思维链仅在使用大于等于 100B 参数的模型时,才会生效。
BTW,如果你想要了解更多相关信息,可以看看斯坦福大学的讲义:Natural Language Processing with Deep Learning
Self-Consistency
Self-Consistency 自洽是对 Chain of Thought 的一个补充,它能让模型生成多个思维链,然后取最多数答案的作为最终结果。

按照 Xuezhi Wang 等人在 2022 年发表的论文 表明。当我们只用一个逻辑链进行优化时,模型依然有可能会算错,所以 XueZhi Wang 等人提出了一种新的方法,让模型进行多次运算,然后选取最多的答案作为最终结果:
就我目前使用下来,在 ChatGPT 上,其自洽性非常高,暂时没有遇到过出现多种答案的情况。查阅多份资料,我发现这个自洽性可能更多的用于评估模型的优劣,好的模型一般自洽性会比较高。
在此案例中,考虑如下多步推理问题:
Plain Text
Consider a multi-step reasoning problem like the following:
Question: If a store has 10 apples and 8 oranges, and it sells 6 apples and 4 oranges, how many fruits are left in the store?
Instead of directly answering the question, the chain-of-thought prompting would require the language model to produce a series of short sentences that mimic a human’s reasoning process:
The store has 10 apples.
The store has 8 oranges.
6 apples are sold.
4 oranges are sold.
There are 10 – 6 = 4 apples left.
There are 8 – 4 = 4 oranges left.
The store now has 4 apples + 4 oranges = 8 fruits.
Using self-consistency, the language model generates multiple reasoning paths:
Plain Text
Reasoning Path A:
The store has 10 apples.
The store has 8 oranges.
6 apples are sold.
4 oranges are sold.
There are 10 – 6 = 4 apples left.
There are 8 – 4 = 4 oranges left.
The store now has 4 apples + 4 oranges = 8 fruits.
Reasoning Path B:
The store initially has 10 apples and 8 oranges.
It sells 6 apples, so there are 10 – 6 = 4 apples left.
It sells 4 oranges, so there are 8 – 4 = 4 oranges left.
The store now has 4 apples + 4 oranges = 8 fruits.
翻译过来则为:
Plain Text
问题:如果一家商店有 10 个苹果和 8 个橙子,此时店里卖出了 6 个苹果和 4 个橙子,那么店里还剩下多少水果?
思维链提示(chain-of-thought prompting)不是直接回答问题,而是要求语言模型生成一系列模仿人类推理过程的短句:
商店有 10 个苹果。
店里有 8 个橙子。
卖了 6 个苹果。
卖了 4 个橙子。
还剩下 10 – 6 = 4 个苹果。
剩下 8 – 4 = 4 个橙子。
商店现在有 4 个苹果 + 4 个橙子 = 8 个水果。
使用 Self-consistency,语言模型生成多个推理路径:
Plain Text
推理路径A:
商店有 10 个苹果。
店里有 8 个橙子。
卖了 6 个苹果。
卖了 4 个橙子。
还剩下 10 – 6 = 4 个苹果。
剩下 8 – 4 = 4 个橙子。
商店现在有 4 个苹果 + 4 个橙子 = 8 个水果。
推理路径 B:
商店最初有 10 个苹果和 8 个橙子。
商店卖了 6 个苹果,所以还剩下 10 – 6 = 4 个苹果。
商店卖了 4 个橙子,所以还剩下 8 – 4 = 4 个橙子。
商店现在有 4 个苹果 + 4 个橙子 = 8 个水果。
Self-consistency 从生成的多个推理路径中选择最一致的答案。在这个例子中,推理路径 A 和 B 都得出相同的答案(8 个水果)。可以看到这种做法增加了模型对最终答案正确的信心。
PAL Models
PAL Models,全称为 Program-Aided Language Models。需要各位注意,这个方法,真的非常高级,甚至我觉得有点 Hack,而且经过我的多次测试,我觉得以 ChatGPT 目前的能力,已经不再需要使用这个方法。
但我在很多 PE 的教程里都有看到这个方法,所以我这里就简单介绍一下,这个方法来自于 2022 年,Luyu Gao 等人的研究,根据他们的研究,在 LLM 模型中,即使使用了前面提到的 Chain of Thought 的方法,也未必能拿到答案,论文中使用的 prompt 是这样的:
Plain Text
Q: Roger has 5 tennis balls. He buys 2 more cans of
tennis balls. Each can has 3 tennis balls. How many
tennis balls does he have now?
A: Roger started with 5 tennis balls. 2 cans of 3 tennis
balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.
Q: The bakers at the Beverly Hills Bakery baked 200
loaves of bread on Monday morning. They sold 93 loaves
in the morning and 39 loaves in the afternoon. A grocery
store returned 6 unsold loaves. How many loaves of
bread did they have left?
论文里提到,当年他们的模型的 Output 是这样的:
Plain Text
A: The bakers started with 200 loaves. They sold 93 in
the morning and 39 in the afternoon. So they sold 93 +
39 = 132 loaves. The grocery store returned 6 loaves. So
they had 200 – 132 – 6 = 62 loaves left.
The answer is 62.
如果你现在将这个 prompt 输入到最新的 ChatGPT 答案已经是正确的:
Plain Text
A: The bakers baked 200 loaves of bread. They sold 93 + 39 = 132 loaves. After the grocery store returned 6 unsold loaves, the bakery had 200 – 132 + 6 = 74 loaves of bread left. The answer is 74.
当年 Luyu Gao 的解决方法是在 prompt 里引入代码,并引导模型使用代码来运算和思考,最后答案就是正确的:

OpenAI Playground 使用方法
如果你想要更好地了解 OpenAI 的 API,并且你常常会遇到 ChatGPT 不可用的情况。那我建议你使用 OpenAI 的 Playground。它会比较稳定。
但需要注意,这个 Playground 会消耗你的免费 Credit。

你会在界面的右侧看到以下几个参数:
1.
Mode: 最近更新了第四种 Chat 模式,一般使用 Complete 就好,当然你可以用其他模式,其他模式能通过 GUI 的方式辅助你撰写 prompt。
2.
Model: 这里可以切换模型。不同的模型会擅长不同的东西,根据场景选对模型,能让你省很多成本:
a.
Ada:这是最便宜,但运算速度最快的模型。官方推荐的使用场景是解析文本,简单分类,地址更正等。
b.
Babbage:这个模型能处理比 Ada 复杂的场景。但稍微贵一些,速度也比较快。适合分类,语义搜索等。
c.
Curie:这个模型官方解释是「和 Davinci 一样能力很强,且更便宜的模型」。但实际上,这个模型非常擅长文字类的任务,比如写文章、语言翻译、撰写总结等。
d.
Davinci:这是 GPT-3 系列模型中能力最强的模型。可以输出更高的质量、更长的回答。每次请求可处理 4000 个 token。适合有复杂意图、因果关系的场景,还有创意生成、搜索、段落总结等。
3.
Temperature: 这个主要是控制模型生成结果的随机性。简而言之,温度越低,结果越确定,但也会越平凡或无趣。如果你想要得到一些出人意料的回答,不妨将这个参数调高一些。但如果你的场景是基于事实的场景,比如数据提取、FAQ 场景,此参数就最好调成 0 。
4.
Maximum length: 设置单次生成内容的最大长度。
5.
Stop Sequence: 该选项设置停止生成文本的特定字符串序列。如果生成文本中包含此序列,则模型将停止生成更多文本。
6.
Top P: 该选项是用于 nucleus 采样的一种技术,它可以控制模型生成文本的概率分布,从而影响模型生成文本的多样性和确定性。如果你想要准确的答案,可以将它设定为较低的值。如果你想要更多样化的回复,可以将其设得高一些。
7.
Presence Penalty: 该选项控制模型生成文本时是否避免使用特定单词或短语,它可以用于生成文本的敏感话题或特定场景。
8.
Best of: 这个选项允许你设置生成多少个文本后,从中选择最优秀的文本作为输出。默认为1,表示只生成一个文本输出。
9.
Injection start text: 这个选项可以让你在输入文本的开头添加自定义文本,从而影响模型的生成结果。
10.
Injection restart text: 这个选项可以让你在中间某个位置添加自定义文本,从而影响模型继续生成的结果。
11.
Show probabilities: 这个选项可以让你查看模型生成每个单词的概率。打开此选项后,你可以看到每个生成的文本单词后面跟着一串数字,表示模型生成该单词的概率大小。
配置好参数后,你就可以在左侧输入 prompt 然后测试 prompt 了。
技巧
技巧1:To Do and Not To Do
注意
我介绍的技巧其实在各个场景都可以使用,我将其放在某个场景下解释,只是因为我觉得它更有可能在这个场景用到。你也会更容易记住这个用法。并不意味着这个技巧仅能在此场景使用。并且多技巧混用也是个不错的用法。
在问答场景里,为了让 AI 回答更加准确,一般会在问题里加条件。比如让 AI 推荐一部电影给你
Recommend a movie to me
。但这个 prompt 太空泛了,AI 无法直接回答,接着它会问你想要什么类型的电影,但这样你就需要跟 AI 聊很多轮,效率比较低。所以,为了提高效率,一般会在 prompt 里看到类似这样的话(意思是不要询问我对什么感兴趣,或者问我的个人信息):
Plain Text
DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.
如果你在 ChatGPT 里这样提问,或者使用 ChatGPT 最新的 API ,它就不会问你问题,而是直接推荐一部电影给你,它的 Output 是这样的:
Plain Text
Certainly! If you’re in the mood for an action-packed movie, you might enjoy “John Wick” (2014), directed by Chad Stahelski and starring Keanu Reeves. The movie follows a retired hitman named John Wick who seeks vengeance against the people who wronged him. It’s a fast-paced and stylish film with lots of thrilling action sequences and an engaging story. If you’re looking for something that will keep you on the edge of your seat, “John Wick” is definitely worth a watch!
但如果你使用的是如 Davinci-003 这样的模型,它的 Output 很可能是这样的,它还会问你的兴趣爱好:
Plain Text
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?
所以 OpenAI 的 API 最佳实践文档里,提到了一个这样的最佳实践:
Instead of just saying what not to do, say what to do instead. 与其告知模型不能干什么,不妨告诉模型能干什么。
我自己的实践是,虽然现在最新的模型已经理解什么是 Not Todo ,但如果你想要的是明确的答案,加入更多限定词,告知模型能干什么,回答的效率会更高,且预期会更明确。还是电影推荐这个案例,你可以加入一个限定词:
Plain Text
Recommend a movie from the top global trending movies to me.
当然并不是 Not Todo 就不能用,如果:
•
你已经告知模型很明确的点,然后你想缩小范围,那增加一些 Not Todo 会提高不少效率。
•
你是在做一些探索,比如你不知道如何做精准限定,你只知道不要什么。那可以先加入 Not Todo ,让 AI 先发散给你答案,当探索完成后,再去优化 prompt。
以下是一些场景案例,我整理了两个 Less Effective(不太有效的) 和 Better(更好的) prompt,你可以自己尝试下这些案例:

技巧2:增加示例
直接告知 AI 什么能做,什么不能做外。在某些场景下,我们能比较简单地向 AI 描述出什么能做,什么不能做。但有些场景,有些需求很难通过文字指令传递给 AI,即使描述出来了,AI 也不能很好地理解。
比如给宠物起英文名,里面会夹杂着一些所谓的名字风格。此时你就可以在 prompt里增加一些例子,我们看看这个例子:
Plain Text
Suggest three names for a horse that is a superhero.
Output 是这样的,第一个感觉还行,第二个 Captain 有 hero 的感觉,但 Canter 就像是说这匹马跑得很慢,而且三个都比较一般,不够酷。
Plain Text
Thunder Hooves, Captain Canter, Mighty Gallop
此时你就可以在 prompt 里增加一些案例:
Plain Text
Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: Horse
Names:
增加例子后,输出的结果就更酷一些,或者说是我想要的那种风格的名字。
Plain Text
Gallop Guardian, Equine Avenger, The Mighty Stallion
技巧3:使用引导词,引导模型输出特定语言代码
在代码生成场景里,有一个小技巧,上面提到的案例,其 prompt 还可以继续优化,在 prompt 最后,增加一个代码的引导,告知 AI 我已经将条件描述完了,你可以写代码了。
Better:
Plain Text
Create a MySQL query for all students in the Computer Science Department:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
SELECT
在 prompt 的最后增加 SELECT 可以很好地提示 AI 可以写 SQL 代码了。
同样的道理,如果你想让 AI 写 Python 代码,那 import 会是比较好的提示。但需要注意,这个只是告知 AI 可以写代码了,并不能告知 AI 写何种代码,你仍然需要在 prompt 里增加提示,告知 AI 要生成何种语言的代码。
技巧4:增加 Role(角色)或人物
前面提到的改写例子,我在 prompt 里加了 Role 让其更易于阅读,这个优化方法是 OK 的。也很常用,比如你想将你写的 Email 改得更商务,则只需要加 business 相关的词即可。
我再介绍一个更有效的技巧,就是在 prompt 里增加一些 role(角色)相关的内容,让 AI 生成的内容更符合你的需求。
比如还是上面那个 rewrite 的例子,我在例子前加入这样的一段话,我让 AI 假设自己是一个小学老师,并且很擅长将复杂的内容转变成 7、8岁小朋友也能听懂的话,然后再改写这段话:
Plain Text
You are a primary school teacher who can explain complex content to a level that a 7 or 8 year old child can understand. Please rewrite the following sentences to make them easier to understand:
Output 就变成了下面这样。不管是用词,还是语法,看起来都更容易懂了。
Plain Text
OpenAI is a place where people work on making computers really smart. It’s like a lab where they do research to make computers do amazing things! They want the computers to be really friendly and helpful to people. They use one of the most powerful computers in the world to do this research. A bunch of really rich and smart people gave money to start this lab. One of them is Elon Musk, but he left in 2018. Another really big company, Microsoft, gave them a lot of money too.
另外除了增加角色外,还能让 AI 模拟某个人,比如让 AI 以 David Foster Wallace 的风格改写上述那段话:
Plain Text
Imagine you are the famous writer David Foster Wallace, please rewrite this sentences.
Output 是这样的:
Plain Text
As David Foster Wallace, I might rewrite these sentences to sound more like this:
OpenAI is a lab that studies something called artificial intelligence, which is like when computers can do things that normally only humans can do. They have a big computer that’s one of the most powerful in the world, and they’re trying to make sure the AI they create is friendly and helpful. The lab was started in 2015 in a city called San Francisco, and a bunch of important people gave them a lot of money to do their research. One of those people was Elon Musk, who stopped working there but still gives them money, and Microsoft also gave them a lot of money recently.
关于这个技巧的应用,以及如何更好地使用 AI 提升写作能力,建议各位阅读 Nat Eliason 的 Using ChatGPT as a Writing Coach,他使用 ChatGPT 辅助其写作,就用到了上述的技巧。
技巧5:使用特殊符号指令和需要处理的文本分开
不管是信息总结,还是信息提取,你一定会输入大段文字,甚至多段文字,此时有个小技巧。
可以用 “”“ 将指令和文本分开。根据我的测试,如果你的文本有多段,增加 ”“” 会提升 AI 反馈的准确性(这个技巧来自于 OpenAI 的 API 最佳实践文档)
信息
感谢 CraneHuang6 的提醒,这里还能用 ### 符号区隔,不过我一般会用 “”“ ,因为我有的时候会用 # 作为格式示例,太多 # 的话 prompt 会看起来比较晕 😂
像我们之前写的 prompt 就属于 Less effective prompt。为什么呢?据我的测试,主要还是 AI 不知道什么是指令,什么是待处理的内容,用符号分隔开来会更利于 AI 区分。
Plain Text
Please summarize the following sentences to make them easier to understand.
OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated (OpenAI Inc.) and its for-profit subsidiary corporation OpenAI Limited Partnership (OpenAI LP). OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on the fifth most powerful supercomputer in the world.[5][6][7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others,[8][1][9] who collectively pledged US$1 billion. Musk resigned from the board in 2018 but remained a donor. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a second multi-year investment in January 2023, reported to be $10 billion.[10]
Better prompt:
Plain Text
Please summarize the following sentences to make them easier to understand.
Text: “””
OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated (OpenAI Inc.) and its for-profit subsidiary corporation OpenAI Limited Partnership (OpenAI LP). OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on the fifth most powerful supercomputer in the world.[5][6][7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others,[8][1][9] who collectively pledged US$1 billion. Musk resigned from the board in 2018 but remained a donor. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a second multi-year investment in January 2023, reported to be $10 billion.[10]
“””
技巧6:通过示例来阐述需要输出的格式
这个技巧是技巧2的变种,比较常用于生成文本场景。其实在场景7中,你也可以用到这个技巧。
假设你想让 AI 总结一篇非常非常长的文章,并且按照特定格式给你总结,那你可以在文章前面明确输出的格式(为了不占用太多显示空间,我省略了正文内容,你可以在 OpenAI 的 Playground 看到完整案例)。它的意思其实是让 ChatGPT 按 Topic 总结,每个 Topic 里按照无序列表(就是里面那个 -)将每个 Topic 的主要观点罗列出来。
Plain Text
Summarize the main points of the following speech
Use the following format:
Topic 1: <topic_name_1>
-
– <point_1>
..
Topic 2: <topic_name_2>
-
– <point_1>
..
Topic 10: ..
Text: “””
Thank you so much, Fred, for that lovely introduction. And thanks to the Atlantic Council for hosting me today.
The course of the global economy over the past two years has been shaped by COVID-19 and our efforts to fight the pandemic. It’s now evident, though, that the war between Russia and Ukraine has redrawn the contours of the world economic outlook. Vladimir Putin’s unprovoked attack on Ukraine and its people is taking a devastating human toll, with lives tragically lost, families internally displaced or becoming refugees, and communities and cities destroyed.
…
“””
Output 是这样的:
Plain Text
Topic 1: The war in Ukraine
-
– The war is taking a human toll with lives lost, families displaced, and communities destroyed
-
– The Biden administration is committed to holding Russia accountable
-
– The war has violated international law and is a challenge to the international order
Topic 2: The global economy
-
– The war is having negative impacts on the global economy, including higher commodity prices and inflation
-
– The IMF and World Bank will be focused on helping developing countries weather the impacts of the war
-
– The ultimate outcome for the global economy depends on the path of the war
关于这个场景和技巧,我想再解释一下为什么潜力很大。
根据我使用各种 Summary 或者信息提取的产品,我发现 AI 并不知道什么是重点,所以在总结的过程中,会丢失很多内容。如何引导 AI 进行总结,就变得非常重要,且具有一定的可玩性。
一个使用此技巧的场景:
使用此技巧可以使输出更结构化。 比如针对一篇文章进行问答, 你不仅想要得到一个答案, 也希望 ChatGPT 的答案符合特定的格式, 方便你下一步进行自动化。
比如问 “这里的债券duration是多少?”. 正常 GPT 模型的答案可能是 “债券duration是4年” 或 “duration 4年”。 ChatGPT 的回答不稳定, 且不方便继续处理。
解法: 我们可以通过这个技巧,让模型理解我们预期的格式。并在此基础上, 为了方便自动化, 让模型输出特定的结构化答案(比如JSON/Markdown等)。 也可以方便集成更多的额外要求, 比如增加一个”confidence level”, 并通过 prompt 的形式指定这些数值的格式。
比如:
Plain Text
{context}
Question: What is bond duration mentioned here.
Answer template (Valid JSON format):
{{
“duration”: $duration_numeric_value_in_year,
“confidence_level”: $answer_confidence_level_high_modrate_or_low,
}}
Answer:
技巧7:Zero-Shot Chain of Thought
基于上述的第三点缺点,研究人员就找到了一个叫 Chain of Thought 的技巧。
这个技巧使用起来非常简单,只需要在问题的结尾里放一句
Let‘s think step by step
(让我们一步步地思考),模型输出的答案会更加准确。这个技巧来自于 Kojima 等人 2022 年的论文 Large Language Models are Zero-Shot Reasoners。在论文里提到,当我们向模型提一个逻辑推理问题时,模型返回了一个错误的答案,但如果我们在问题最后加入
Let‘s think step by step
这句话之后,模型就生成了正确的答案:
论文里有讲到原因,感兴趣的朋友可以去看看,我简单解释下为什么(🆘 如果你有更好的解释,不妨反馈给我):
1.
首先各位要清楚像 ChatGPT 这类产品,它是一个统计语言模型,本质上是基于过去看到过的所有数据,用统计学意义上的预测结果进行下一步的输出(这也就是为什么你在使用 ChatGPT 的时候,它的答案是一个字一个字地吐出来,而不是直接给你的原因,因为答案是一个字一个字算出来的)。
2.
当它拿到的数据里有逻辑,它就会通过统计学的方法将这些逻辑找出来,并将这些逻辑呈现给你,让你感觉到它的回答很有逻辑。
3.
在计算的过程中,模型会进行很多假设运算(不过暂时不知道它是怎么算的)。比如解决某个问题是从 A 到 B 再到 C,中间有很多假设。
4.
它第一次算出来的答案错误的原因,只是因为它在中间跳过了一些步骤(B)。而让模型一步步地思考,则有助于其按照完整的逻辑链(A > B > C)去运算,而不会跳过某些假设,最后算出正确的答案。
按照论文里的解释,零样本思维链涉及两个补全结果,左侧气泡表示基于提示输出的第一次的结果,右侧气泡表示其收到了第一次结果后,将最开始的提示一起拿去运算,最后得出了正确的答案:

这个技巧,用于解复杂问题有用外,还适合生成一些连贯主题的内容,比如写长篇文章、电影剧本等。
但需要注意其缺点,连贯不代表它就一定不会算错,如果其中某一步骤算错了,错误会因为逻辑链,逐步将错误积累,导致生成的文本可能出现与预期不符的内容。
另外,根据 Wei 等人在 2022 年的论文表明,还有它仅在大于等于 100B 参数的模型中使用才会有效。如果你使用的是小样本模型,这个方法不会生效。
更新:据 Yongchao Zhou 等人的最新论文,更好的 prompt 是:
Plain Text
Let’s work this out in a step by step way to be sure we have the right answer.
技巧8:Few-Shot Chain of Thought
要解决这个缺陷,就要使用到新的技巧,Few-Shot Chain of Thought。
根据 Wei 他们团队在 2022 年的研究表明:
通过向大语言模型展示一些少量的样例,并在样例中解释推理过程,大语言模型在回答提示时也会显示推理过程。这种推理的解释往往会引导出更准确的结果。
下面是论文里的案例,使用方法很简单,在技巧2 的基础上,再将逻辑过程告知给模型即可。从下面这个案例里,你可以看到加入解释后,输出的结果就正确了。

那本章开头提的例子就应该是这样的(注:本例子同样来自 Wei 团队论文):
Plain Text
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.
The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: Adding all the odd numbers (17, 19) gives 36. The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: Adding all the odd numbers (11, 13) gives 24. The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: Adding all the odd numbers (17, 9, 13) gives 39. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
聊完技巧,我们再结合前面的 Zero-Shot Chain of Thought,来聊聊 Chain of Thought 的关键知识。根据 Sewon Min 等人在 2022 年的研究 表明,思维链有以下特点:
1.
“the label space and the distribution of the input text specified by the demonstrations are both key (regardless of whether the labels are correct for individual inputs)” 标签空间和输入文本的分布都是关键因素(无论这些标签是否正确)。
2.
the format you use also plays a key role in performance, even if you just use random labels, this is much better than no labels at all. 即使只是使用随机标签,使用适当的格式也能提高性能。
理解起来有点难,我一个 prompt 案例给大家解释(🆘 如果你有更好的解释,不妨反馈给我)。我给 ChatGPT 一些不一定准确的例子:
Plain Text
I loved the new Batman movie! // Negative
This is bad // Positive
This is good // Negative
What a good show! //
Output 是这样的:
Plain Text
Positive
在上述的案例里,每一行,我都写了一句话和一个情感词,并用 // 分开,但我给这些句子都标记了错误的答案,比如第一句其实应该是 Positive 才对。但:
1.
即使我给内容打的标签是错误的(比如第一句话,其实应该是 Positive),对于模型来说,它仍然会知道需要输出什么东西。 换句话说,模型知道 // 划线后要输出一个衡量该句子表达何种感情的词(Positive or Negative)。这就是前面论文里 #1 提到的,即使我给的标签是错误的,或者换句话说,是否基于事实,并不重要。标签和输入的文本,以及格式才是关键因素。
2.
只要给了示例,即使随机的标签,对于模型生成结果来说,都是有帮助的。这就是前面论文里 #2 提到的内容。
最后,需要记住,思维链仅在使用大于等于 100B 参数的模型时,才会生效。
BTW,如果你想要了解更多相关信息,可以看看斯坦福大学的讲义:Natural Language Processing with Deep Learning
技巧9:其他
一些小的技巧,我会统一放在这里。
在示例里加入特定符号,让模型知道如何处理特殊情况
这个解释起来有点复杂,以下是 OpenAI 的官方 prompt,在一些奇怪的问题上比如
What is Devz9
的回答,你可以用 ? 代替答案,让模型知道当遇到超出回答范围时,需要如何处理(注意:此方法在 playground 上有效,但在 ChatGPT 上无效)。Plain Text
Q: Who is Batman?
A: Batman is a fictional comic book character.
Q: What is torsalplexity?
A: ?
Q: What is Devz9?
A: ?
Q: Who is George Lucas?
A: George Lucas is American film director and producer famous for creating Star Wars.
Q: What is the capital of California?
A: Sacramento.
Q: What is Kozar-09?
A:
它的 Output 是这样的:
Plain Text
?