設計提示 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

設計提示

設計適當的提示詞是使用 Amazon Bedrock 模型組建成功應用程式的重要一步。在本節中,您將學習如何設計一致、清晰且簡潔的提示。您也會瞭解如何使用推論參數來控制模型的回應。下圖顯示使用案例餐廳評論摘要的一般提示詞設計,以及客戶在設計提示詞時需要考慮的一些重要設計選擇。LLMs如果提供的指示或提示格式不一致、清晰且簡潔,則會產生不需要的回應。

具有描述性註解的良好提示詞建構模組範例。

(來源:提示編寫 AWS)

以下內容提供如何建立成功提示的指引。

提供簡單、明確及完整的說明

LLMs在 Amazon 基岩上通過簡單直接的說明工作得最好。透過清楚描述對任務的期望,並儘可能減少模糊性,可以確保模型能清楚地解譯提示詞涵意。

例如,思考一個分類問題,其中使用者想要從一組可能的選項中獲得答案。以下顯示的「良好」範例說明了使用者在此案例中所需的輸出。在「不良」範例中,這些選擇沒有明確命名為可供模型選擇的類別。。模型會在沒有選擇的情況下稍微不同地解譯輸入,並產生與良好範例相反之風格更自由的文字摘要。

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.

(提示來源:維基百科關於色盲,使用的模型:by 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 model,有必要在格式化API調用時包含換行符以獲得所需的響應。格式應始終遵循:\n\nHuman: {{Query Content}}\n\nAssistant:。用於 Titan 模型,\n在提示結束時加入有助於改善模型的效能。對於分類任務或具有答案選項的問題,您也可以將答案選項分隔\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 文字)

使用輸出指示器

輸出指示器

在模型應產生之輸出上加入想要的限制的詳細資訊。下列的良好範例會產生輸出,該輸出是一個簡短的片語,一個出色的摘要。在這種情況下,不好的範例並沒有那麼糟糕,但摘要幾乎與原始文字一樣長。輸出規格對於從模型取得所需內容極為重要。

具有明確輸出限制指示器的範例提示
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 model 來完成複雜的任務,使用下面實例中的格式明確要求模型逐步解決任務。這種方法基於中的準則 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 基岩上,全部都帶有幾個推論參數,您可以設置這些參數來控制來自模型的響應。以下是 Amazon 基岩上可用的所有常見推論參數以LLMs及如何使用它們的清單。

溫度是 0 和 1 之間的值,它調節 LLMs '響應的創造力。如果您想要更多確定性回應,請使用較低的溫度;如果您希望 Amazon Bedrock LLMs 上的相同提示獲得更多創意或不同的回應,請使用較高的溫度。對於此提示指引中的所有範例,我們設定了temperature = 0

最大產生長度/最大新記號會限制針對任何提示LLM產生的記號數目。指定此數字會很有幫助,因為某些任務 (例如情緒分類) 不需要很長的答案。

Top-p 根據潛在選擇的機率控制記號選擇。如果您將 Top-P 設定為 1.0 以下,該模型會考慮最有可行的選項,並忽略較不可行的選項。結果是能更穩定和重複性地完成。

結束標記/結束序列指定了LLM用於指示輸出結束的令牌。LLMs遇到結束令牌後停止生成新令牌。通常不需由使用者設定。

也有模型特定推論參數。Anthropic Claude 模型還有一個額外的 Top-K 推論參數,並且 AI21 Labs 侏羅紀模型帶有一組推論參數,包括存在懲罰,計數罰款,頻率懲罰和特殊令牌懲罰。如需詳細資訊,請參閱各自的文件。