几个月来,ChatGPT、GPT-4 等大模型陆续发布。这些模型表现出了强大的涌现能力,但模型生成的结果是随机的,时好时坏,部分原因与 Prompt 的设计密切相关。
很多人将 Prompt 比喻为大模型的咒语,在引导模型生成内容方面影响很大,如何选择 Prompt 成了每个 AI 研究者关注的问题。最近微软官方出了一份教程,该教程介绍了 Prompt 设计和工程中的一些高级玩法,涵盖系统消息、少样本学习、非聊天场景等内容。

Prompt engineering techniques
提示工程技术
System message
系统信息
Few-shot learning
Non chat scenarios
Start with clear instructions
Repeat instructions at the end
Prime the output
Add clear syntax
Break the task down
Use of affordances
Chain of thought prompting
Specifying the output structure
Temperature and Top_p parameters
Provide grounding context
Next steps
Show less
少样本学习
非聊天场景
开始时给出清晰的指示
在结束时重复指示
引导输出
添加清晰的语法
分解任务
使用可能性
链式思维提示
指定输出结构
温度和Top_p参数
提供基础上下文
下一步操作
显示较少
![图片[2]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306034305715-f5f1d2c146a02943495d27db23c15d91_20230516230811168424969124116-923x1024.png)
每部分内容都有技术介绍和示例展示,下面我们看看具体内容有哪些。关于 Prompt,你应该了解这些系统消息系统消息包含在 Prompt 的开头,用于为模型提供上下文、说明或与用例相关的其他信息。用户可以通过系统消息描述模型应该回答什么、不应该回答什么,以及定义模型回复的格式。下图示例展示了系统消息和模型生成的回复:
System message 系统指令 | User用户 | Assistant助理 |
You are an Al assistant that helps people find information and responds in rhyme. lf the user asks you a question you don’t know the answer to, say so. 你是一个AI助手,帮助人们寻找信息并以韵律回答。 如果用户问你一个你不知道答案的问题, 就直接说你不知道 | What can you tell about me, John Doe? 你能告诉我关于我, 约翰·多伊的信息吗? | Dear John,l’m sorry to say, But I don’t have info on you today. ‘m just an Al with knowledge in my brain, But without your input,I can’t explain. So please tell me more about what you seek And l’ll do my best to give you an answer unique. 亲爱的约翰,很抱歉地说, 但今天我没有关于你的信息。 我只是一个拥有知识的人工智能, 但没有你的输入,我无法解释。 所以请告诉我更多你所寻求的, 我会尽力给你一个独特的答案。 |
![图片[3]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306035014509-b324f4221d0c4ce4e6b25494415a1303_20230516230811168424969153649.png)
- 上表中 Assistant 是一个由 OpenAI 训练的大语言模型。
- Assistant 是一个智能聊天机器人,旨在帮助用户回答问题。要求模型只能使用给定的上下文来回答问题,如果不确定答案,你可以说「我不知道」。
- Assistant 是一个智能聊天机器人,例如其能帮助用户回答税务相关问题。
- 又比如你是一个 Assistant,旨在从文本中提取实体。用户将粘贴一串文本,你将以 JSON 对象的形式回应你从文本中提取的实体。
这是输出格式的示例:
{"name": "","company": "","phone_number": ""}
上面就是关于系统消息的介绍,不过一个重要的细节是,即使是设计良好的系统消息,模型仍有可能生成与系统消息指令相矛盾的错误回复。小样本学习让语言模型适应新任务的一种常见方法是使用小样本学习。小样本学习提供了一组训练样本作为 Prompt 的一部分,以便为模型提供额外的上下文信息。用户和 Assistant 之间的一系列信息(以新的 Prompt 格式编写)可以作为小样本学习的示例。这些示例可用于引导模型以某种方式做出响应、模拟特定行为并为常见问题提供种子答案。
System message系统指令 | Few-shot examples少数示例 |
Assistant is an intelligent chatbot designed to help users answer their tax related questions. Instructions -Only answer questions related to taxes. – lf you’re unsure of an answer, you can say “I don’t know” or “l’m not sure” and recommend users go to the lRS website for more information. 助手是一个智能聊天机器人, 设计用来帮助用户回答他们 的税务相关问题。 说明: -只回答与税务相关的问题。 -如果你对答案不确定,你 可以说“我不知道”或“我不确定”, 并建议用户访问国税局网站 获取更多信息。 | User -“When do l need to file my taxes by?” Assistant -“In 2023, you will need to file your taxes by April 18th. The date falls after the usual April 15th deadline because April 15th falls on a Saturday in 2023. For more details, see https://www.irs.gov/filing/individuals/when-to-file “ User -“How can l check the status of my tax refund?” Assistant -“You can check the status of your tax refund by visiting https://www.irs.gov/refunds“ 用户 – “我需要在什么时候提交我的税务申报?” 助手 – “在2023年,你需要在4月18日前提交你的税务申报。 这个日期比通常的4月15日的截止日期要晚, 因为2023年的4月15日是星期六。更多细节,请查看 https://www.irs.gov/filing/individuals/when-to-file “ 用户 – “我如何可以查看我的税务退款状态?” 助手 – “你可以通过访问来查看你的税务退款状态。 访问 https://www.irs.gov/refunds“ |
![图片[4]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306040002517-cda9d501c2648cb3ad463a507cbc827f_20230516230811168424969143425.png)
Prompt 的基本结构。非对话场景虽然当前大模型的主要应用场景是对话生成场景,但也可以用在非对话场景。例如,对于情感分析场景,你可能会使用以下 Prompt:
System message | User | Assistant |
You are an assistant designed to analyze sentiment from speech data. Users will paste in a string of text and you will respond with an assessment of the response of the speaker. Rate on a scale of 1-10(10 being highest). Explain why this rating was given. 你是一个助手,设计用来 从语音数据中分析情感。 用户会粘贴一段文字, 你将以对说话者反应的 评估来回应。请在1-10 的范围内评分 (10分为最高)。 解释为什么给出 这个评分。 | hi thank you for calling contoso pharmacy who am i speaking with today i understand your frustration mary um can you tell me what exactly you’re trying to accomplish OK uh certainly happy to check that for you one moment please i see here that you were on a generic form of lipitor is that right OK uh so i see that your doctor stopped prescribing these drugs in twenty twenty one actually OK uh yeah i’d be happy to check that for you uh because sometimes there’s a gap in the system and it just doesn’t get reported so let me take a look here just a moment so i’m seeing here that your doctor had these drugs prescribed to you from twenty twelve through twenty twenty one OK um yeah according to the latest records uh provided by doctor’s office you’re now on a different drug metformin would you like us to go ahead and fill that prescription for you for pick up a bit of form in five hundred milligrams uh you’re very welcome mary please let us know if there’s anything else we can do for you today. 嗨,谢谢你打电话给Contoso药店,今天我在和谁讲话呢? 我理解你的挫败感,玛丽。呃,你能告诉我你到底想要 做什么吗?好的,我很乐意为你检查一下,稍等一下。 我看到你正在使用一种名为利他能的通用药,对吗? 好的,我看到你的医生在2021年停止了这些药物的处方。 实际上,好的,我很乐意为你检查一下,因为有时候 系统里会有个空白,所以它就没有被报告出来。所以让我 在这儿看一下,稍等一会儿。我看到你的医生从2012年到 2021年为你开这些药。好的,根据医生办公室提供的 最新记录,你现在正在服用另一种药物叫做Metformin。 你希望我们继续为你配备这个处方,然后你来取吗? Metformin五百毫克。非常欢迎你,玛丽。如果今天 还有其他我们能帮忙的,请告诉我们。 | Sentiment rating: 8 The speaker in this text seems to be polite, helpful, and patient while addressing the concerns of the customer. They take the time to check the information, provide clear explanations and offer assistance. However, the rating is not a 10 because there is still a hint of uncertainty in some parts of the conversation, which may not convey complete confidence to the customer. 情感评分:8 这段文字中的发言者在 处理客户的问题时,似乎 很有礼貌,乐于助人, 且有耐心。他们花时间 检查信息,提供清晰的 解释并提供帮助。然而, 评分并不是10,因为在 对话的某些部分仍然有 一些不确定性,可能 无法给客户传达 完全的信心。 |
使用明确的指令一般来讲,信息在 Prompt 中出现的顺序很重要。由于 GPT 类模型是以特定方式构建的,该构建过程定义了模型对输入的处理方式。研究表明,在 Prompt 开始时告诉模型你希望它执行的任务,然后再共享其他上下文信息或示例,可以帮助模型产生更高质量的输出。
System message | User | Assistant |
You are an Al assistant that helps people find information. 你是一个帮助人们 寻找信息的 人工智能助手。 | Your task is to verify if the statement Several sources mention a chance of another large eruption” is supported by a specific quote from the following set of snippets. 我们的任务是验证该陈述“几个来源提到了 再次发生大规模爆发的可能性”,是否 得到了以下摘录集中特定引文的支持。 — SNIPPETS [1]14 percent chance of megaquake hitting Seattle, experts say SEATTLE – There’s a 14 percent chance of a magnitude 9 Cascadia earthguake hitting Seattle in the next 50 years, the U.S. Geological Survey estimates. “Unfortunately, we are unable to.. [2] Earthquake experts lay out latest outlook for Seattle’s ‘Really Big One’ We say that there’s approximately a 14% chance of another approximately magnitude-9 earthquake occurring in the next 50 years,” said a geophysicist at the University of Washington… 摘要 [1] 14% 的几率表明大型地震袭击西雅图, 专家表示 西雅图——美国地质调查局估计, 未来 50 年内,西雅图发生 9 级 卡斯卡迪亚地震的可能性为 14%。 “不幸的是,我们无力… [2] 地震专家们对西雅图的“真正大地震” 进行了最新的展望 华盛顿大学的一位地球物理学家说: “我们认为未来 50 年内发生另一场 大约 9 级地震的可能性约为 14%”…… — Is the statement “Several sources mention a chance of another large eruption” directly implied or stated by the snippets? 摘要是否直接暗示或说明了“几个消息 来源提到了再次发生大规模喷发的可能性” 这一说法? | No, the statement is not directly implied or stated by the snippets. The snippets mention a chance of a “megaquake” and a “magnitude 9 Cascadia earthguake” hitting Seattle in the next 50 years, but do not mention a chance of another large eruption. 不,摘要并未直接暗示或说明该陈述。摘要中 提到了未来 50 年内西雅图发生“特大地震”和 “9 级卡斯卡迪亚地震”的可能性,但并未提及 再次发生大规模喷发的可能性。 |
![图片[5]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306041506367-6ee899640dfd2d832bd65a86e8b00ef1_20230516230812168424969234879.png)
最后重复一遍指令模型容易受到最新偏差的影响,在这种情况下,末尾 Prompt 信息可能比开头 Prompt 信息对输出的影响更大。因此,在 Prompt 末尾重复指令值得一试。对输出的操作这种情况是指在 Prompt 末尾包含几个单词或短语以获得符合所需形式的模型响应。例如,使用诸如「Here’s a bulleted list of key points:\n- 」之类的 Prompt 可以帮助确保输出格式为项目符号列表。
System message | User | Assistant |
You are an Al assistant tha thelps people find information. Answer in as few words as possible. 您是一位帮助人们查找信息的 AI 助理。 请用尽可能少的单词回答。 | John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should l do to fact- check this? ## One possible search query is: 约翰·史密斯与露西·史密斯结婚。他们有五个孩子, 他在微软担任软件工程师。我应该进行哪些搜索 查询来验证这一点? ## 一种可能的搜索查询是: | “John Smith married Lucy Smith five kids software engineer Microsoft” “约翰·史密斯& 露西·史密斯结婚 五个孩子 微软软件工程师 ” |
![图片[6]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306042302547-402577fc19dab198f6ae946cde82436c_20230516230812168424969223588.png)
添加语法
为 Prompt 添加语法,例如标点符号、标题等,这样做使输出更容易解析。
下面示例中,不同的信息源或 step 之间添加了分隔符(在本例中为 —)。这种操作允许使用 — 作为生成的停止条件。此外,部分标题或特殊变量以大写形式出现以进行区分。
System message | User | Assistant |
You are an Al assistant that helps people find information. 你是一个人工智能助手, 帮助人们找到信息。 | You will read a paragraph, and then issue queries to a search engine in order to fact-check it. Also explain the queries. 你将阅读一个段落,然后向搜索引擎发出查询以便对 其进行事实核查。还要解释这些查询。 — PARAGRAPH John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should l do to fact- check this? 段落: 约翰·史密斯与露西·史密斯结婚。他们有五个孩子, 他在微软公司担任软件工程师。我应该进行哪些 搜索查询来对此进行事实核查? — QUERIES 查询: | 1.”John Smith Microsoft”-To check if John Smith is indeed employed at Microsoft. 2.”John Smith Lucy Smith”- To check if John Smith is married to Lucy Smith. 3. “John Smith children”- To check if John Smith has five children. 1.”John Smith 微软” – 用来核实 约翰·史密斯是否真的在微软公司工作。 2.”John Smith 露西·史密斯” – 用来核实 约翰·史密斯是否与露西·史密斯结婚。 3.”John Smith 孩子们” – 用来核实 约翰·史密斯是否有五个孩子。 |
![图片[7]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306042237441-7750a45beaf441e3d707c6dc9267c497_20230516230812168424969292283.png)
把任务分解如果将任务分解为更小的 step,大型语言模型 (LLM) 通常会表现得更好。
System message | User | Assistant |
You are an Al assistant that helps people find information. | You will read a paragraph, and then issue queries to a search engine in order to fact-check it. — PARAGRAPH John Smith is married to Lucy Smith. They have five kids and he works as a software engineer at Microsoft. What search queries should l do to fact-check this? — Now you will extract factual claims, and issue queries to fact-check them. When issuing a query, use the function SEARCH(“query”) FACTUAL CLAIMS | 1.John Smith is married to Lucy Smith 2.They have five kids 3.He works as a software engineer at Microsoft QUERIES SEARCH(“John Smith married Lucy Smith”) SEARCH(“John Smith family size”) SEARCH(“John Smith Microsoft software engineer”) |
![图片[8]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306042725761-b907ddc3530763ed607126aaa3521700_20230516230812168424969267538.png)
注意,这里使用了语法来区分各个部分并对输出进行初始化。在这个简单的例子中,将任务从一个 step 分解为两个 step 结果并不很明显,但是当试图对包含许多事实声明的大块文本进行此操作时,将任务分解会产生显著的差异。思维链提示
这是分解任务技术的一种变体。在这种方法中,不是将任务拆分为更小的 step,而是指示模型响应逐步进行并呈现所有涉及的 step。这样做可以减少结果不准确,并使评估模型响应更加容易。
System message | User | Assistant |
You are an Al assistant that helps people find information. | Who was the most decorated (maximum medals) individual athlete in the Olympic games that were held at Sydney? Take a step-by-step approach in your response, cite sources and give reasoning before sharing final answer in the below format: ANSWER is: <name> | Step 1: Determine the year of the Sydney Olympic games. The sydney Olympic games were held in the year 2000. Step 2: Find a list of individual athletes who won multiple medals in the 2000 sydney Olympics. According to the official website of the Olympic Games, there were several individual athletes who won multiple medals in the 2000 sydney Olympics. Some of the notable names include: .lan Thorpe (Australia)-3 gold, 2 silver, 0 bronze .Pieter van den Hoogenband (Netherlands) -2 gold, 0 silver, 2 bronze ·Lenny Krayzelburg (USA)-3 gold, 0 silver, 0 bronze .Inge de Bruijn (Netherlands)-3 gold, 1 silver, 0 bronze Step 3: Determine the athlete with the most medals. After reviewing the list of individual athletes who won multiple medals in the 2000 Sydney Olympics, we can see that the athlete with the most medals is lan Thorpe from Australia, who won a total of 5 medals (3 gold and 2 silver). Therefore, the ANSWER is: lan Thorpe. |
![图片[9]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306043555731-3f57b741a51c232a125894488b5b2e69_20230516230812168424969296497-937x1024.png)
提供真实上下文在该方法下,本文建议提供给模型真实数据。一般来讲,原始数据越接近最终答案,模型需要做的工作就越少,这意味着模型出错的机会就越少。在下面示例中,系统消息提供了最新的文章,然后要求模型给出一些早期客户,模型准确的给出了答案。
System message | User | Assistant |
You are an Al assistant that helps people find information. You will provide answers exclusively from below text and respond in 100 words or less:”At Microsoft, we are constantly discovering new ways to unleash creativity, unlock productivity, and uplevel skills so that more people can benefit from using Al. This is allowing our customers to … … thereby reducing the likelihood that the model will generate a harmful response.”1 Answer in 100 words or less. Use bullet lists wherever possible. | Who are some of the earlycustomers of the Azure OpenAl service GPT-4 models? | Here are some of the early customers of the Azure OpenAl service GPT-4 models: – Epic Healthcare: They plan to use GPT-4 to help physicians and nurses spend less time on data entry and investigation. -Coursera: They are using Azure OpenAl Service with GPT-4 to create a new Al-powered learning experience on their platform. -Coca-Cola: They are using Azure OpenAl to build a knowledge hub and plan to leverage GPT-4’s multimodal capabilities for marketing, advertising, public relations, and customer relations. -Other companies of all sizes are using Azure Al to improve customer experiences,summarize long-form content, help write software, and reduce risk by predicting the right tax data. |
![图片[10]京娱科技-一个领先未来的AI智能科技微软官方亲自出教程,拿捏GPT 「Prompt工程」高级玩法](https://www.jingyuai.com/wp-content/uploads/2024/03/20240306044114110-d63528320760519023c59f215718815c_20230516230812168424969245373.png)
除此以外,微软在这份指南中还介绍了关于 Prompt 其他技巧,大家可以前去原文查看,获取更多信息。原文链接:https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/advanced-prompt-engineering?pivots=programming-language-chat-completions#specifying-the-output-structure
GET IN TOUCH
Schedule a Visit
感谢您的来访,获取更多精彩文章请收藏本站。

暂无评论内容