AI 的使用场景非常多,基础篇仅介绍最最最常用的两种。后续会介绍更多使用场景和方法。
这个场景应该是使用 AI 产品最常见的方法。以 ChatGPT 为例,一般就是你提一个问题,ChatGPT 会给你答案,比如像这样:
在这个场景下,prompt 只要满足前面提到的基本原则,基本上就没有什么问题。但需要注意,不同的 AI 模型擅长的东西都不太一样,prompt 可能需要针对该模型进行微调。另外,目前的 AI 产品,也不是无所不能,有些问题你再怎么优化 prompt 它也没法回答你。以 ChatGPT 为例:
求助
这种直接提问的 prompt ,我们称之为 Zero-shot prompt。模型基于一些通用的先验知识或模型在先前的训练中学习到的模式,对新的任务或领域进行推理和预测。你会在高级篇看到相关的介绍,以及更多有意思的使用方法。
另外,正如我在前面基础用法一章中提到的那样,问答场景里还有一个很重要的玩法,就是多轮聊天,你可以针对某个问题,进行多轮的提问。
注意
我介绍的技巧其实在各个场景都可以使用,我将其放在某个场景下解释,只是因为我觉得它更有可能在这个场景用到。你也会更容易记住这个用法。并不意味着这个技巧仅能在此场景使用。并且多技巧混用也是个不错的用法。
在问答场景里,为了让 AI 回答更加准确,一般会在问题里加条件。比如让 AI 推荐一部电影给你 Recommend a movie to me 。但这个 prompt 太空泛了,AI 无法直接回答,接着它会问你想要什么类型的电影,但这样你就需要跟 AI 聊很多轮,效率比较低。
所以,为了提高效率,一般会在 prompt 里看到类似这样的话(意思是不要询问我对什么感兴趣,或者问我的个人信息):
DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.
如果你在 ChatGPT 里这样提问,或者使用 ChatGPT 最新的 API ,它就不会问你问题,而是直接推荐一部电影给你,它的 Output 是这样的:
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 很可能是这样的,它还会问你的兴趣爱好:
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 ,但如果你想要的是明确的答案,加入更多限定词,告知模型能干什么,回答的效率会更高,且预期会更明确。还是电影推荐这个案例,你可以加入一个限定词:
Recommend a movie from the top global trending movies to me.
当然并不是 Not Todo 就不能用,如果:
以下是一些场景案例,我整理了两个 Less Effective(不太有效的) 和 Better(更好的) prompt,你可以自己尝试下这些案例:
在某些场景下,我们能比较简单地向 AI 描述出什么能做,什么不能做。但有些场景,有些需求很难通过文字指令传递给 AI,即使描述出来了,AI 也不能很好地理解。
比如给宠物起英文名,里面会夹杂着一些所谓的名字风格。此时你就可以在 prompt 里增加一些例子,我们看看这个例子。
这个是没有任何示例的 Prompt:
Suggest three names for a horse that is a superhero.
Output 如下所示。第一个感觉还行,第二个 Captain 有 hero 的感觉,但 Canter 就像是说这匹马跑得很慢,感觉不太合适,而且三个都比较一般,不够酷。
Thunder Hooves, Captain Canter, Mighty Gallop
如果你无法用文字准确解释问题或指示,你可以在 prompt 里增加一些案例:
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 的结果就更酷一些,或者说是接近我想要的那种风格的名字。
Gallop Guardian, Equine Avenger, The Mighty Stallion
以下是一些场景案例,我整理了两个 Less Effective(不太有效的)和 Better(更好的)prompt,你可以自己尝试下这些案例:
在问答这个大场景下,还有一个子场景是推理,这个场景非常有意思,而且是个非常值得深挖的场景,prompt 在此场景里发挥的作用非常大。
如果你想用 ChatGPT API 做点什么小应用,我建议可以从这个场景入手,相对来说没有其他场景那么红海。
举个比较简单的例子,我将美国工商管理硕士的入学考试 GMAT 的一道数学题输入进了 ChatGPT:
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.
这道题目的中文意思是这样的(你不妨也思考下这个题目?):
如果一个房地产经纪人的佣金是某个房子的售价的6%,那么这个房子的售价是多少? (1)售价减去房地产经纪人的佣金为84,600美元。 (2)购买价是36,000美元,售价是购买价的250%。 (A)仅陈述(1)足以回答问题,但仅陈述(2)不能回答问题。 (B)仅陈述(2)足以回答问题,但仅陈述(1)不能回答问题。 (C)两个陈述合起来足以回答问题,但没有一个陈述单独足以回答问题。 (D)每个陈述单独足以回答问题。 (E)陈述(1)和(2)合起来不能回答问题。
它的 Output 是这样的,它会根据题目的要求去推理和运算:
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.
翻译成中文是这样的:
假设房子的售价为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 里试试。
除了回答问题外,另一个重要的场景是让 AI 根据你的要求完成一些内容生成任务,根据输出的结果差异,我将其概括为以下几个主要场景:
本章,我们先来聊聊「无中生有」场景。顾名思义,就是让 AI 给你生成一些内容。你只需要向 AI 描述你想写的内容,AI 就会按你的要求写出该内容。比如:
像撰写招聘信息等,你只需要明确目标,将目标定得比较明确即可生成能让你较为满意的答案。本章我想聊下写代码。下面是一个让 ChatGPT 写代码的案例。
Prompt:
Create a MySQL query for all students in the Computer Science Department: Table departments, columns = [DepartmentId, DepartmentName] Table students, columns = [DepartmentId, StudentId, StudentName]
Output:
SELECT students.* FROM students INNER JOIN departments ON students.DepartmentId = departments.DepartmentId WHERE departments.DepartmentName = 'Computer Science';
在代码生成场景里,有一个小技巧,上面提到的案例,其 prompt 还可以继续优化,在 prompt 最后,增加一个代码的引导,告知 AI 我已经将条件描述完了,你可以写代码了。
在 prompt 的最后增加 SELECT 可以很好地提示 AI 可以写 SQL 代码了。Better prompt:
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 要生成何种语言的代码。
在吴恩达的 ChatGPT Prompt Engineering 课程中,也提到这个技巧,只是在课程中,引导词并不是放在最后,而是在 prompt 里直接说明,让 AI 生成一个 JSON 格式的内容。课程中的例子是这样的(注意这个是 python 代码):
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. """
我简单解释下,其关键是在 prompt 里跟 AI 说明,需要 AI 按照 JSON 格式输出内容
除了从 0 生成内容外,你也可以先给 AI 一段已经写好的内容,然后让 AI 对其进行修改,让 AI 帮你将内容改得更好。实际的场景有如:
本章重点介绍下润色场景,下面这是个润色的例子。
Prompt(内容来自维基百科):
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 是这样的:
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.
前面提到的改写例子,我在 prompt 里加了 Role 让其更易于阅读,这个优化方法是 OK 的。也很常用,比如你想将你写的 Email 改得更商务,则只需要加 business 相关的词即可。
我再介绍一个更有效的技巧,就是在 prompt 里增加一些 role(角色)相关的内容,让 AI 生成的内容更符合你的需求。
比如还是上面那个 rewrite 的例子,我在例子前加入这样的一段话,我让 AI 假设自己是一个小学老师,并且很擅长将复杂的内容转变成 7、8 岁小朋友也能听懂的话,然后再改写这段话:
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 就变成了下面这样。不管是用词,还是语法,看起来都更容易懂了。
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 的风格改写上述那段话:
Imagine you are the famous writer David Foster Wallace, please rewrite this sentences.
Output 是这样的:
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.
锦上添花下的第二大的场景是信息解释。它跟改写内容有点像,但又不太一样,信息解释有可能提供超过原文内容的信息。
举几个信息解释的例子,大家应该就能理解了:
以解释代码为例,它的 prompt 是这样的(此案例来自 OpenAI 官方案例):
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,它阐述了这段代码是拿来干什么的,同时还阐述了各个参数的含义:
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.