近几个月,AI界的明星们,如ChatGPT、GPT-4等大模型纷纷亮相,它们展现出了令人惊叹的创新能力。然而,这些模型的表现就像是一场未知的抽奖,有时你会得到惊喜,有时却可能让你失望,这在很大程度上取决于Prompt的设计。
有趣的是,很多人将Prompt比喻为对大模型施加的魔法咒语,它在引导模型生成内容方面起着决定性的作用。因此,如何巧妙地选择Prompt,就成了每个AI研究者的心头之事。
最近,微软官方就像是开启了一扇神秘的大门,他们发布了一份教程,详细介绍了Prompt设计和工程中的一些高级技巧。这份教程就像是一本魔法书,涵盖了系统消息、少样本学习、非聊天场景等内容,让我们一起探索这个神秘的AI世界吧!

每部分内容都有技术介绍和示例展示,下面我们看看具体内容有哪些。关于 Prompt,你应该了解这些。
系统消息
系统消息包含在提示的开头,用于为模型提供上下文、说明或与用例相关的其他信息。 可以使用系统消息来描述助手的个性,定义模型应回答和不应回答的内容,以及定义模型响应的格式。
下面的示例显示了示例系统消息和生成的模型响应:
System message | User | Assistant |
You are an AI assistant that helps people find information and responds in rhyme. If the user asks you a question you don't know the answer to, say so. | What can you tell about me, John Doe? | Dear John, I'm sorry to say,But I don't have info on you today.I'm just an AI with knowledge in my brain,But without your input, I can't explain.So please tell me more about what you seek,And I'll do my best to give you an answer unique. |
系统消息的其他一些示例包括:
- “助手是由 OpenAI 训练的大型语言模型。”
- “助手是一种智能聊天机器人,旨在帮助用户回答有关 Azure OpenAI 服务的技术问题。 仅使用以下上下文回答问题,如果不确定答案,可以说“我不知道”。
- “助手是一种智能聊天机器人,旨在帮助用户回答其税务相关问题。”
- “你是一名助手,旨在从文本中提取实体。 用户将粘贴文本字符串,你将使用从文本中提取的实体作为 JSON 对象进行响应。 下面是输出格式的示例:
JSON复制
{ "name": "", "company": "", "phone_number": "" }
需要了解的一个重要细节是,即使你在系统消息中指示模型在不确定答案时回答“我不知道”,这并不能保证此请求得到履行。 设计良好的系统消息可以增加产生特定结果的可能性,但仍可能会生成不正确的响应,可能会与系统消息中的指令的意图相矛盾。
少样本学习
使语言模型适应新任务的一个常见方法是使用少样本学习。 在少样本学习中,需要在提示中提供一组训练示例,以便为模型提供额外的上下文。
使用聊天补全 API 时,用户和助手之间的一系列消息(以新的提示格式编写)可以作为进行少样本学习的示例。 这些例子可以用来引导模型以某种方式相应,模仿特定的行为,并为常见的问题提供种子答案。
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.- If you're unsure of an answer, you can say "I don't know" or "I'm not sure" and recommend users go to the IRS website for more information. | User - "When do I 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 I check the status of my tax refund?"
Assistant - "You can check the status of your tax refund by visiting https://www.irs.gov/refunds " |
上表介绍了基本的提示结构,但有关确切提示格式的详细说明,需要参考聊天补全指南。
非聊天场景
虽然聊天补全 API 已优化为处理多回合对话,但它也可用于非聊天场景。 例如,对于情绪分析场景,可以使用以下提示:
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. | 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. | Sentiment rating: 8The 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. |
从明确的说明开始
提示中显示信息的顺序很重要。 这是因为 GPT 风格的模型是以某种方式构建的,这定义了它们处理输入的方式。 我们的研究表明,在共享其他上下文信息或示例之前,在提示开始时告诉模型你希望它执行的任务有助于生成更高质量的输出。
备注
尽管通常仍建议遵循此方法,但与之前的模型版本(GPT-3 和更早)相比,我们的测试表明,无论是否使用该技术,ChatGPT 和 GPT-4 模型的模型响应都是相同的。 在下面的示例中,我们看到添加了语句“几个消息来源... 爆发”到提示的开头或末尾后,不会导致最终模型响应发生任何变化。
System message | User | Assistant |
You are an AI 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 saySEATTLE - There's a 14 percent chance of a magnitude 9 Cascadia earthquake 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...
---
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 earthquake" hitting Seattle in the next 50 years, but do not mention a chance of another large eruption. |
在末尾重复指令
模型可能容易受到近因偏差的影响,在此上下文中,这意味着提示结束时的信息对输出的影响可能比提示开头的信息更大。 因此,值得尝试的是,在提示结束时重复指令,并评估对生成的响应的影响。
引导输出
这是指在提示的末尾包含几个字词或短语,以获取遵循所需形式的模型响应。 例如,使用
“Here’s a bulleted list of key points:\n- ”
等提示有助于确保输出的格式为项目符号列表。System message | User | Assistant |
You are an AI assistant that helps people find information. Answer in as few words as possible. | John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should I do to fact-check this? ## One possible search query is: | "John Smith married Lucy Smith five kids software engineer Microsoft" |
在上述提示中,文本“一个可能的搜索查询是:”引导模型生成单个输出。 如果没有此提示,模型将生成多个搜索查询作为输出。
添加明确的语法
对提示使用明确的语法(包括标点符号、标题和节标记)有助于传达意向,并且通常使输出更易于分析。
在下面的示例中,分隔符(本例中为
---
)已添加到不同的信息源或步骤之间。 这允许使用 ---
作为生成的停止条件。 此外,节标题或特殊变量以大写形式显示,用于区分。System message | User | Assistant |
You are an AI 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 I 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. |
如果不确定要使用哪种语法,请考虑使用 Markdown 或 XML。 这些模型已通过 XML 和 Markdown 的大量 Web 内容进行了训练,这可能会提供更好的结果。
分解任务
如果任务分解为较小的步骤,大型语言模型(LLM)的性能通常会更好。 例如,在前面引用的搜索查询提示中,可以调整提示的结构,以便首先指示模型提取相关事实,然后指示生成可用于验证这些事实的搜索查询。
System message | User | Assistant |
You are an AI 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 I 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") |
请注意,应使用清晰的语法来区分不同部分并引导输出。 在此简单示例中,将任务从一步分解为两步并不十分引人注目,但当为一篇有许多事实主张的大文本做这件事时,将任务分解就会产生很大的不同。
思维链提示
这是分解任务技术的变体。 在这种方法中,不是将一项任务分割成较小的步骤,而是指示模型响应逐步进行,并提出所有涉及的步骤。 这样做可以减少结果不准确的可能性,并使评估模型响应更容易。
System message | User | Assistant |
You are an AI 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:
• Ian 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 Ian Thorpe from Australia, who won a total of 5 medals (3 gold and 2 silver).
Therefore, the ANSWER is: Ian Thorpe. |
指定输出结构
使用提示指定输出结构时,可能会对结果的性质和质量产生重大影响。 有时,系统消息输入“仅写出真实事实”或“不捏造信息”可能不足以缓解问题。 相反,要求模型响应同时包含引文有助于减少错误响应的概率。
如果你指示模型在编写语句时引用源材料,则这些语句更有可能有根据。 请求引文会使模型在每次生成响应时都犯两个错误:第一个错误是捏造的响应,第二个错误是错误的引文。 请注意,引文越接近它支持的文本,模型预测引文所需的距离就越短,这表明内联引文比内容末尾的引文更适合缓解虚假内容的生成。
同样,如果要求模型从段落中提取事实陈述,它可能会提取复合语句,例如“X 正在执行 Y 和 Z”(这可能更难验证)。 可以通过指定输出结构来避免这种情况,如(实体 1、关系、实体 2)。
以下示例演示了引文的使用,并指导模型响应适应定义的结构。
System message | User | Assistant |
You are an AI assistant that helps people find information | List down the different behaviors exhibited by the Customer Support agent in the following conversation.
---
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.
---
How was the response of the Customer Support agent?Classify and list down their various behaviors and cite the reason behind the classification, using the format BEHAVIOR("reason") | 1. Polite Greeting BEHAVIOR("Agent starts the conversation with a polite greeting and asks for the caller's name")
2. Empathetic BEHAVIOR("Agent acknowledges Mary's frustration and tries to understand her concern")
3. Attentive BEHAVIOR("Agent listens to the customer's problem and asks for more details")
4. Proactive BEHAVIOR("Agent offers to check the issue for the customer")
5. Informative BEHAVIOR("Agent provides the relevant information about the customer's prescription history")
6. Thorough BEHAVIOR("Agent checks for any possible gaps in the system and provides a complete response")
7. Confirmatory BEHAVIOR("Agent confirms the current medication with the customer")
8. Solution-oriented BEHAVIOR("Agent offers to fill the new prescription for the customer")
9. Courteous BEHAVIOR("Agent thanks the customer and offers further assistance if needed") |
System message | User | Assistant |
You are an AI assistant that helps people find information. | Cluster the following news headlines into topic categories based on patterns seen within the text. Also mention reasoning behind how these categories were defined.
Output format:
{
"TOPIC_NAME": "",
"HEADLINES": [],
"REASONING": ""
}
Input news headlines:
1. "From books to presentations in 10s with AR + ML"
2. "Demo from 1993 of 32-year-old Yann LeCun showing off the World's first Convolutional Network for Text Recognition"
3. "First Order Motion Model applied to animate paintings"
4. "Robinhood and other brokers literally blocking purchase of $GME, $NOK, $BB, $AMC; allow sells"
5. "United Airlines stock down over 5% premarket trading"
Output: | {
"ARTIFICIAL_INTELLIGENCE": {
"HEADLINES": [
"From books to presentations in 10s with AR + ML",
"Demo from 1993 of 32-year-old Yann LeCun showing off the World's first Convolutional Network for Text Recognition",
"First Order Motion Model applied to animate paintings"
],
"REASONING": "These headlines are related to artificial intelligence, machine learning, and their applications in various fields."
},
"FINANCIAL_MARKETS": {
"HEADLINES": [
"Robinhood and other brokers literally blocking purchase of $GME, $NOK, $BB, $AMC; allow sells",
"United Airlines stock down over 5% premarket trading",
"Bitcoin was nearly $20,000 a year ago today"
],
"REASONING": "These headlines are related to financial markets, stocks, cryptocurrencies, and trading platforms."
}
} |
温度和 Top_p 参数
改变温度参数会改变模型的输出。 温度参数可以设置为 0 到 2。 较高的值(例如 0.7)将使输出更随机,并产生更多发散的响应,而较小的值(例如 0.2)将使输出更加集中和具体。 虚构的故事可以使用更高的温度生成。 而要生成法律文件的话,建议使用低得多的温度。 Top_probability 是另一个参数,与温度类似,它也控制模型响应的随机性,但它的控制方式有所不同。 一般建议一次只更改这两个参数其中之一,而不是同时更改它们。
提供基础上下文
提供可靠答案的最有效方法之一是为模型提供数据,让它从基础数据得出响应。 如果你的用例依赖于最新、可靠的信息,且不是纯粹的创意场景,我们强烈建议提供基础数据。 通常,源材料越接近所需答案的最终形式,模型需要完成的工作就越少,这意味着出错的可能性就越小。 下面的示例向系统提供了“描述 GPT-4 在 Azure OpenAI 服务中推出的最新博客”,并要求其命名一些早期客户。
System message | User | Assistant |
You are an AI 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 AI. 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 early customers of the Azure OpenAI service GPT-4 models? | Here are some of the early customers of the Azure OpenAI 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 OpenAI Service with GPT-4 to create a new AI-powered learning experience on their platform.
- Coca-Cola: They are using Azure OpenAI 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 AI to improve customer experiences, summarize long-form content, help write software, and reduce risk by predicting the right tax data. |
原文地址: