本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
提示工程概念
提示工程是指最佳化大型語言模型 (LLM) 的文字輸入,以獲得所需回應的做法。提示有助於 LLM 執行各種任務,包括分類、問題回答、產生程式碼、創意寫作等。您提供給 LLM 的提示品質可能會影響模型回應的品質。本節為您提供所有必要的資訊,以便開始使用提示工程。其中也涵蓋可幫助您在 Amazon Bedrock 上使用 LLM 時為您的使用案例找到最佳的提示格式的工具。
注意
本文件中的所有範例都是透過 API 呼叫取得。由於 LLM 產生過程的隨機性質,回應可能會有所不同。如果沒有其他指定,提示詞則由 AWS的員工撰寫。
注意
若要減少幻覺,您可以使用提示最佳化技術來精簡提示;使用檢索增強生成 (RAG) 等技術來提供模型對更相關資料的存取權;或使用可能產生改善結果的不同模型。
Amazon Bedrock 包含來自各種供應商的模型。以下是這些模型的清單提示工程指導方針。
-
Amazon Nova Micro、Lite 和 Pro 提示指南:Amazon Nova 理解模型的提示最佳實務
-
Amazon Nova Canvas 提示指南:使用 Amazon Nova 產生影像
-
Amazon Nova Reel 提示指南:使用 Amazon Nova 產生影片
-
Anthropic Claude 模型提示指南:https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview
-
Cohere 提示指南:https://txt.cohere.com/how-to-train-your-pet-llm-prompt-engineering
-
AI21 Labs Jurassic 模型提示指南:https://docs.ai21.com/docs/prompt-engineering
-
Meta Llama 2 提示指南:https://ai.meta.com/llama/get-started/#prompting
-
Stability AI 提示指南:https://platform.stability.ai/docs/getting-started
-
Mistral AI 提示指南:https://docs.mistral.ai/guides/prompting_capabilities/
免責聲明:本文件中的範例使用 Amazon Bedrock 中目前提供的文字模型。此外,本文件適用於一般提示指導方針。如需模型專用指南,請參閱 Amazon Bedrock 上各自的文件。本文件提供起點。雖然下列範例回應是使用 Amazon Bedrock 上的特定模型產生的,但您也可以使用 Amazon Bedrock 中的其他模型來取得結果。不同模型的結果可能會有所不同,因為每個模型都有自己的效能特性。您使用 AI 服務產生的輸出就是您的內容。由於機器學習的性質,客戶的輸出可能不是唯一的,而且服務可能會在客戶間產生相同或類似的結果。
什麼是提示詞?
提示詞是您 (使用者) 提供的一組特定輸入內容,可引導 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,使用的模型:AnthropicClaude)
提示範本
提示範本會指定提示詞的格式,其中包含可交換的內容。提示範本是將 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 Text 和 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,使用的模型:AnthropicClaude)
請注意提示詞的主要內容是如何像這樣包裝的:\n\nHuman:
{{Main Content}}\n\nAssistant:。對於 Anthropic Claude 模型,透過 API 傳送的提示必須包含\n\n人類:和
\n\nAssistant:。
若要在 Titan 上使用對話模式,您可以在提示模型時使用 User: {{}}
\n Bot: 的格式。