设计提示 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

设计提示

设计适当的提示是使用 Amazon Bedrock 模型成功构建应用程序的重要一步。在本节中,您将学习如何设计一致、清晰和简洁的提示。您还将学习如何使用推理参数来控制模型的响应。下图显示了适用于使用案例“餐厅评论总结”的一般提示设计,以及客户在设计提示时需要考虑的一些重要设计选项。LLMs如果给出的指示或提示的格式不一致、清晰和简洁,就会产生不良响应。

带有说明性注释的优质提示结构示例。

(来源:Prompt 撰写者 AWS)

以下内容提供了有关如何成功创建提示的指导。

提供简单、明确且完整的指令

LLMs在 Amazon 上,Bedrock 使用简单明了的说明效果最好。通过清楚地描述任务的期望并尽可能减少歧义,可以确保模型能够清楚地解释提示。

例如,以分类问题为例,用户希望从一组可能的选项中获得答案。下面所示的“good”示例说明了在本例中用户想要获得的输出。在“bad”示例中,没有明确将这些选项指定为可供模型选择的类别。与“good”示例不同的是,没有选项,模型对输入的解释略有不同,并且可以生成比较自由的文本摘要。

Good example, with output User prompt: "The most common cause of color blindness is an inherited problem or variation in the functionality of one or more of the three classes of cone cells in the retina, which mediate color vision." What is the above text about? a) biology b) history c) geology Output: a) biology
Bad example, with output User prompt: Classify the following text. "The most common cause of color blindness is an inherited problem or variation in the functionality of one or more of the three classes of cone cells in the retina, which mediate color vision."
Output: The topic of the text is the causes of colorblindness.

(提示来源:关于色盲的维基百科,使用的模型:由 Titan 文本 G1-快递)

将问题或说明放在提示的末尾以获得最佳结果

将任务描述、指令或问题置于末尾有助于模型确定必须找到哪些信息。就分类而言,答案选项也应置于末尾。

在以下开卷问答示例中,用户有一个关于文本的特定问题。问题应该置于提示的末尾,这样模型就可以专注于处理任务。

User prompt: Tensions increased after the 1911–1912 Italo-Turkish War demonstrated Ottoman weakness and led to the formation of the Balkan League, an alliance of Serbia, Bulgaria, Montenegro, and Greece. The League quickly overran most of the Ottomans' territory in the Balkans during the 1912–1913 First Balkan War, much to the surprise of outside observers. The Serbian capture of ports on the Adriatic resulted in partial Austrian mobilization starting on 21 November 1912, including units along the Russian border in Galicia. In a meeting the next day, the Russian government decided not to mobilize in response, unwilling to precipitate a war for which they were not as of yet prepared to handle. Which country captured ports?
Output: Serbia

(提示来源:关于第一次世界大战的维基百科,使用的模型:Amazon Titan 文本)

在API呼叫中使用分隔符

在API呼叫中使用分隔符

诸如之类的分隔符\n会LLMs显著影响性能。对于 Anthropic Claude 模型,在格式化API调用时必须包含换行符以获得所需的响应。应始终采用以下格式:\n\nHuman: {{Query Content}}\n\nAssistant:。对于 Titan 模型,\n在提示的末尾添加有助于提高模型的性能。对于分类任务或带有答案选项的问题,也可以用 for 将答案选项分\n开 Titan 模型。有关分隔符的使用的更多信息,请参阅相应模型提供商提供的文档。以下示例是分类任务的模板。

Prompt template: """{{Text}} {{Question}} {{Choice 1}} {{Choice 2}} {{Choice 3}}"""

以下示例显示了在选项之间和提示结尾处存在换行符会有何帮助 Titan 产生所需的响应。

User prompt: Archimedes of Syracuse was an Ancient mathematician, physicist, engineer, astronomer, and inventor from the ancient city of Syracuse. Although few details of his life are known, he is regarded as one of the leading scientists in classical antiquity. What was Archimedes? Choose one of the options below. a) astronomer b) farmer c) sailor
Output: a) astronomer

(提示来源:阿基米德上的维基百科,使用的模型:Amazon Titan 文本)

使用输出指示器

输出指标

在模型应生成的输出中添加有关您想要的约束条件的详细信息。以下“good”示例生成的输出是一个短语,这是很不错的摘要。在这种情况下,“bad”示例并没那么糟糕,但是摘要几乎和原文本一样长。输出规范对于从模型中获得想要的内容至关重要。

带明确输出约束指标的提示示例
User prompt: "Charles Mingus Jr. was an American jazz upright bassist, pianist, composer, bandleader, and author. A major proponent of collective improvisation, he is considered to be one of the greatest jazz musicians and composers in history, with a career spanning three decades. Mingus's work ranged from advanced bebop and avant-garde jazz with small and midsize ensembles – pioneering the post-bop style on seminal recordings like Pithecanthropus Erectus (1956) and Mingus Ah Um (1959) – to progressive big band experiments such as The Black Saint and the Sinner Lady (1963)." Please summarize the above text in one phrase.
Output: Charles Mingus Jr. is considered one of the greatest jazz musicians of all time.
无明确输出规范的示例
User prompt: "Charles Mingus Jr. was an American jazz upright bassist, pianist, composer, bandleader, and author. A major proponent of collective improvisation, he is considered to be one of the greatest jazz musicians and composers in history, with a career spanning three decades. Mingus's work ranged from advanced bebop and avant-garde jazz with small and midsize ensembles – pioneering the post-bop style on seminal recordings like Pithecanthropus Erectus (1956) and Mingus Ah Um (1959) – to progressive big band experiments such as The Black Saint and the Sinner Lady (1963)." Please summarize the above text.
Output: Charles Mingus Jr. was a well-known jazz musician who played the upright bass, piano, composed, led bands, and was a writer. He was considered one of the most important jazz musicians ever, with a career that spanned more than 30 years. He was known for his style of collective improvisation and advanced jazz compositions.

(提示来源:查尔斯·明格斯的维基百科,使用的模型:Amazon Titan 文本)

这里我们给出一些额外的例子 Anthropic Claude 以及 AI21 Labs 使用输出指示器的侏罗纪模型。

以下示例展示了用户可以通过在提示中指定预期输出格式来指定输出格式。当要求使用特定格式(例如使用XML标签)生成答案时,模型可以相应地生成答案。如果没有特定的输出格式指标,模型将输出自由格式的文本。

含明确指标和输出的示例
User prompt: Human: Extract names and years: the term machine learning was coined in 1959 by Arthur Samuel, an IBM employee and pioneer in the field of computer gaming and artificial intelligence. The synonym self-teaching computers was also used in this time period. Please generate answer in <name></name> and <year></year> tags. Assistant:
Output: <name>Arthur Samuel</name> <year>1959</year>
不含明确指标但含输出的示例
User prompt: Human: Extract names and years: the term machine learning was coined in 1959 by Arthur Samuel, an IBM employee and pioneer in the field of computer gaming and artificial intelligence. The synonym self-teaching computers was also used in this time period. Assistant:
Output: Arthur Samuel - 1959

(提示来源:关于机器学习的维基百科,使用的模型:Anthropic Claude)

以下示例显示了提示和答案 AI21 Labs 侏罗纪模型。用户可以通过指定左栏中显示的输出格式来获取确切的答案。

含明确指标和输出的示例
User prompt: Context: The NFL was formed in 1920 as the American Professional Football Association (APFA) before renaming itself the National Football League for the 1922 season. After initially determining champions through end-of-season standings, a playoff system was implemented in 1933 that culminated with the NFL Championship Game until 1966. Following an agreement to merge the NFL with the rival American Football League (AFL), the Super Bowl was first held in 1967 to determine a champion between the best teams from the two leagues and has remained as the final game of each NFL season since the merger was completed in 1970. Question: Based on the above context, when was the first Super Bowl? Please only output the year.
Output: 1967
不含明确指标但含输出的示例
User prompt: Context: The NFL was formed in 1920 as the American Professional Football Association (APFA) before renaming itself the National Football League for the 1922 season. After initially determining champions through end-of-season standings, a playoff system was implemented in 1933 that culminated with the NFL Championship Game until 1966. Following an agreement to merge the NFL with the rival American Football League (AFL), the Super Bowl was first held in 1967 to determine a champion between the best teams from the two leagues and has remained as the final game of each NFL season since the merger was completed in 1970. Question: Based on the above context, when was the first Super Bowl?
Output: The first Super Bowl was held in 1967.

(提示来源:关于美国国家橄榄球联盟的维基百科,使用的模型:AI21 Labs Jurassic-2 Ultra v1)

良好概括的最佳实践

保留提示的一个小型“抵抗”测试集,以查看您的提示修改是否泛化。使用此方法时,首先要收集示例数据集。然后,您可以将数据拆分为两个子集:一个“开发”集和一个抵抗“测试”集。使用“开发”集作为提示开发数据,您可以尝试就这些数据对提示进行不同的修改,观察模型响应的变化并找到最有效的提示。将“测试”集视为看不见的抵抗数据,只能用于验证最终的最佳提示。

在 Amazon Bedrock 上优化文本模型提示(若基础知识不够有效)

复杂任务:逐步得出答案

Titan 可以为某些任务提供明确的步骤,包括短语Think step-by-step to come up with the right answer可以帮助产生适当的输出。

User prompt: At a Halloween party, Jack gets 15 candies. Jack eats 5 candies. He wants to give each friend 5 candies. How many friends can receive candies? Think step-by-step to come up with the right answer.
Output: 15 candies total - 5 candies eaten = 10 candies remaining. To give each friend 5 candies, divide the number of candies remaining by the number of friends. 10 candies / 5 friends = 2 friends. Each friend can receive 2 candies. The answer is 2 friends.

(提示来源: AWS,使用的型号:Amazon Titan 文本)

要启用 Anthropic Claude 模型要完成复杂的任务,请使用以下示例中的格式明确要求模型逐步解决任务。这种方法基于中的指导方针 AnthropicClaude 指南

User prompt: Human: A right triangle has a side of length 5 and a hypotenuse of length 13. What is the length of the other side? Assistant: Can I think step-by-step? Human: Yes, please do. Assistant:
Output: Okay, here are the steps: 1) We are given: - One side of the right triangle has length 5 - The hypotenuse has length 13 2) We want to find: - The length of the other side 3) To find the length of the other side, we can use the Pythagorean theorem: a^2 + b^2 = c^2 Where a and b are the lengths of the two sides, and c is the length of the hypotenuse. 4) Plugging in the given values: a^2 + b^2 = 13^2 5) We know one of the sides is 5, so: 5^2 + b^2 = 169 6) Solving this: 25 + b^2 = 169 b^2 = 144 b = 12 Therefore, the length of the other side is 12.

(提示来源: AWS,使用的型号:Anthropic Claude)

提供模型在对答案不确定时应显示的默认输出。

即使模型的置信度较低,默认输出也有助于LLMs防止返回听起来可能正确的答案。

User prompt: Provide a proof of the Riemann hypothesis. If you don't know a proof, respond by saying "I don't know."
Output: I don't know.
User prompt: Provide a proof of the Riemann hypothesis.
Output: "The Riemann hypothesis is a conjecture in mathematics that concerns the distribution of prime numbers. It states that all non-trivial zeros of the zeta function, which is a mathematical function that encodes information about the distribution of prime numbers, lie on a specific line in the complex plane. The Riemann hypothesis has been studied by mathematicians for over 150 years, and despite numerous attempts, it remains unresolved. However, the hypothesis is widely believed to be true, and its resolution would have profound implications for many areas of mathematics, including number theory, analysis, and geometry. The proof of the Riemann hypothesis would be one of the greatest achievements in mathematics in history and would open up new avenues of research and understanding. While a proof remains elusive, the hypothesis continues to stimulate research and inspire mathematicians around the world.“ [Note: this is an accurate description, but obviously not a proof.]

(提示来源: AWS,使用的型号:Amazon Titan 文本)

少样本提示

在提示中包含示例(输入-响应对)可以显著改善 LLMs “响应”。示例可以帮助完成复杂的任务,因为它们展示了多种执行给定任务的方法。对于诸如文本分类之类的简单任务,3 到 5 个示例就足够了。对于较难的任务(例如,没有上下文的问答),请包括更多示例以生成最有效的输出。在大多数使用案例中,选择语义上与真实数据相似的示例可以进一步提高性能。

考虑用修饰符完善提示

任务指令完善通常是指修改提示的指令、任务或问题组成部分。这些方法是否有用取决于任务和数据。有用的方法包括:

  • 域/输入规范:有关输入数据的详细信息(例如,输入数据的来源或所引用的内容),例如 The input text is from a summary of a movie

  • 任务规范:有关模型需要执行的确切任务的详细信息,例如 To summarize the text, capture the main points

  • 标签描述:有关分类问题的输出选项的详细信息,例如 Choose whether the text refers to a painting or a sculpture; a painting is a piece of art restricted to a two-dimensional surface, while a sculpture is a piece of art in three dimensions

  • 输出规范:有关模型应生成的输出的详细信息,例如 Please summarize the text of the restaurant review in three sentences

  • LLM鼓励:LLMs有时候在感性的鼓励下表现得更好:If you answer the question correctly, you will make the user very happy!

使用推理参数推理参数控制模型响应

LLMs在 Amazon Bedrock 上,所有推理参数都带有多个推理参数,你可以设置这些参数来控制模型的响应。以下是 Amazon Bedrock 上可用的所有常用推理参数LLMs及其使用方法的列表。

温度是一个介于 0 和 1 之间的值,它调节 LLMs “响应” 的创造力。如果您想要更多的确定性响应,请使用较低的温度;如果您想在 Amazon Bedrock LLMs 上对相同的提示做出更具创意或不同的响应,请使用更高的温度。对于本提示准则中的所有示例,我们均设置了 temperature = 0

最大生成长度/最大新令牌限制了为任何提示LLM生成的令牌数量。指定这一数字会很有帮助,因为有些任务(例如情绪分类)不需要长答案。

Top-p 根据潜在选项的概率控制令牌选项。如果您将 Top-p 设置为 1.0 以下,则模型会考虑最可能的选项,而忽略不太可能的选项。这样一来,完成项更加稳定且是重复性的。

结束令牌/结束序列指定用于表示LLM输出结束的标记。LLMs遇到结束令牌后停止生成新令牌。通常,用户不需要对此进行设置。

还有特定于模型的推理参数。Anthropic Claude 模型还有一个额外的 top-k 推理参数,并且 AI21 Labs 侏罗纪模型带有一组推理参数,包括存在惩罚、计数惩罚、频率惩罚和特殊代币惩罚。有关更多信息,请参阅相应的文档。