什么是提示?
提示是您(用户)提供的一组特定输入,可指导 Amazon Bedrock 上的 LLM 为给定任务或指令生成适当的响应或输出。
User Prompt: Who invented the airplane?
当收到此提示时,Titan 会提供以下输出:
Output: The Wright brothers, Orville and Wilbur Wright are widely credited with inventing and manufacturing the world's first successful airplane.
(提示来源:AWS,使用的模型:Amazon Titan Text)
提示的组成部分
一条提示包括多个组成部分,例如,您希望 LLM 执行的任务或指令、任务的上下文(例如,相关域的描述)、演示示例以及您希望 Amazon Bedrock 上的 LLM 在响应中使用的输入文本。根据您的使用案例、数据的可用性和任务,您的提示应包含这些组成部分中的一个或多个。
例如,此提示要求 Titan 总结一篇评论:
User Prompt: The following is text from a restaurant review: “I finally got to check out Alessandro’s Brilliant Pizza and it is now one of my favorite restaurants in Seattle. The dining room has a beautiful view over the Puget Sound but it was surprisingly not crowded. I ordered the fried castelvetrano olives, a spicy Neapolitan-style pizza and a gnocchi dish. The olives were absolutely decadent, and the pizza came with a smoked mozzarella, which was delicious. The gnocchi was fresh and wonderful. The waitstaff were attentive, and overall the experience was lovely. I hope to return soon." Summarize the above restaurant review in one sentence.
(提示来源:AWS)
根据这一提示,Titan 用一行内容言简意赅地总结了餐厅评论。该评论提到了一些关键事实,并根据需要传达了一些要点。
Output: Alessandro's Brilliant Pizza is a fantastic restaurant in Seattle with a beautiful view over Puget Sound, decadent and delicious food, and excellent service.
(使用的模型:Amazon Titan Text)
指令 Summarize the above restaurant review in one
sentence
和评论文本 I finally got to check out
...
都是此类输出需要包括的。缺少其中任何一个,模型就没有足够的信息,无法做出合理的总结。指令会告诉 LLM 需要做什么,而文本是 LLM 针对其执行操作的输入。上下文 (The following is text
from a restaurant review
) 提供其他信息和关键字,以指导模型使用输入来创建输出。
在下面的示例中,文本 Context: Climate change threatens people with
increased flooding ...
是 LLM 可用于执行“回答问题(即,Question: What
organization calls climate change the greatest threat to global health in the
21st century?”
)”任务的输入。
User prompt: Context: Climate change threatens people with increased flooding, extreme heat, increased food and water scarcity, more disease, and economic loss. Human migration and conflict can also be a result. The World Health Organization (WHO) calls climate change the greatest threat to global health in the 21st century. Adapting to climate change through efforts like flood control measures or drought-resistant crops partially reduces climate change risks, although some limits to adaptation have already been reached. Poorer communities are responsible for a small share of global emissions, yet have the least ability to adapt and are most vulnerable to climate change. The expense, time required, and limits of adaptation mean its success hinge on limiting global warming. Question: What organization calls climate change the greatest threat to global health in the 21st century?
(提示来源:https://en.wikipedia.org/wiki/Climate_change)
AI21 Labs Jurassic 根据提示中提供的上下文,使用正确的组织名称进行响应。
Output: The World Health Organization (WHO) calls climate change the greatest threat to global health in the 21st century.
(使用的模型:AI21 Labs Jurassic-2 Ultra v1)
少样本与零样本提示
有时候,比较有用的做法是举几个例子来帮助 LLM 更好地校准输出以满足您的期望,这也称为少样本提示或上下文内学习,其中样本与成对出现的示例输入和所需输出相对应。为了说明起见,在这里首先来看一个零样本情绪分类提示的示例,其中提示文本中没有提供示例输入-输出对:
User prompt: Tell me the sentiment of the following headline and categorize it as either positive, negative or neutral: New airline between Seattle and San Francisco offers a great opportunity for both passengers and investors.
(提示来源:AWS)
Output: Positive
(使用的模型:Amazon Titan Text)
以下是少样本版的情绪分类提示:
User prompt: Tell me the sentiment of the following headline and categorize it as either positive, negative or neutral. Here are some examples: Research firm fends off allegations of impropriety over new technology. Answer: Negative Offshore windfarms continue to thrive as vocal minority in opposition dwindles. Answer: Positive Manufacturing plant is the latest target in investigation by state officials. Answer:
(提示来源:AWS)
Output: Negative
(使用的模型:Amazon Titan Text)
以下示例使用 Anthropic Claude 模型。使用 Anthropic Claude 模型时,最好将演示示例包含在 <example></example> 标签内。我们还建议在示例中使用不同的分隔符(例如 H:
和 A:
)以避免与整个提示的分隔符(Human:
和 Assistant:
)混淆。请注意,在最后一个少样本示例中,最后的 A:
被替换成了 Assistant:
,提示 Anthropic Claude 改为生成答案。
User prompt: Human: Please classify the given email as "Personal" or "Commercial" related emails. Here are some examples. <example> H: Hi Tom, it's been long time since we met last time. We plan to have a party at my house this weekend. Will you be able to come over? A: Personal </example> <example> H: Hi Tom, we have a special offer for you. For a limited time, our customers can save up to 35% of their total expense when you make reservations within two days. Book now and save money! A: Commercial </example> H: Hi Tom, Have you heard that we have launched all-new set of products. Order now, you will save $100 for the new products. Please check our website. Assistant: Output: Commercial
(提示来源:AWS,使用的模型:Anthropic Claude)
提示模板
提示模板指定含可交换内容的提示的格式。提示模板是将 LLM 用于不同使用案例(例如分类、总结、问答等)的“秘诀”。提示模板可能包括指令、少样本示例,以及适合给定使用案例的特定上下文和问题。以下示例是一个模板,您可以借助该模板使用 Amazon Bedrock 文本模型进行少样本情绪分类:
Prompt template: """Tell me the sentiment of the following {{Text Type, e.g., “restaurant review”}} and categorize it as either {{Sentiment A}} or {{Sentiment B}}. Here are some examples: Text: {{Example Input 1}} Answer: {{Sentiment A}} Text: {{Example Input 2}} Answer: {{Sentiment B}} Text: {{Input}} Answer:"""
使用 Amazon Bedrock 上的 LLM 时,用户可以复制粘贴此模板,并填写自己的文本和少样本示例来完成提示。本节还提供了其他模板。
注意
双大括号 {{ and }}
标记模板中用于输入数据特定信息的位置,不应包含在提示文本中。
维护对 Amazon Bedrock 推理请求的召回率
如果您要通过 API 调用访问 Amazon Bedrock 模型,请注意,除非当前提示中包含之前的交互,否则它们不会重新调用之前的提示和之前的请求。在请求中包含之前的提示,您可以进行对话式交互或提出后续请求。
如果您要使用 API 调用而不是 Amazon Bedrock 文本操场来访问 Anthropic Claude 模型,则可以将提示封装成对话式以获得所需响应(对于 Titan 和 AI21 Labs Jurassic 模型系列,则不需要这样做)。以下示例展示了对话式提示:
User prompt: Human: Describe where sea otters live in two sentences. Assistant: Output: Here is a two sentence description of where sea otters live: Sea otters live in the northern Pacific Ocean along the coasts of Russia, Alaska, Canada, and the continental United States. They prefer coastal habitats like rocky shores, kelp forests, and estuaries where they have access to shallow, nearshore waters.
(提示来源:AWS,使用的模型:Anthropic Claude)
请注意,提示主要内容的封装方式如下:\n\nHuman:
{{Main Content}}\n\nAssistant:
。对于 Anthropic Claude 模型,通过 API 发送的提示必须包含 \n\nHuman: 和
\n\nAssistant:
。
要在 Titan 上使用对话模式,您可以使用 User: {{}}
\n Bot:
格式来提示模型。