最佳化語言模型提示

已完成

提示 (Prompt) 是您提供給大型語言模型 (LLM) 的對話指引,它會根據您的查詢或指示來形成回應。 例如,您可以提示 LLM 將句子從英文轉換成法文,或產生文字的摘要。

在上一個單元中,您已建立提示作為輸入字串:

string input = @"I'm a vegan in search of new recipes. I love spicy food! 
Can you give me a list of breakfast recipes that are vegan friendly?";
input = """I'm a vegan in search of new recipes. I love spicy food! 
Can you give me a list of breakfast recipes that are vegan friendly?"""

在此提示中,您會提供內容給語言模型以及指示。 內容可協助模型產生與使用者更相關的結果。

提示涉及到製作清楚且內容豐富的指示,以引導模型產生所需的回應。 為了製作有效的提示,精確度和清晰性是關鍵。 您可能需要實驗並調整提示,以取得精確的結果。

使用範例來引導模型

您可以在提示中包含範例,以協助引導回應。 這些範例可以在指示之前或之後出現。 提示範例會根據它們是否包含逐字完成,被分類為零樣本學習或少樣本學習。 逐字完成是提示中包含的回應的特定、預先定義的範例。 它精確地示範 AI 預期的答案類型,逐字呈現。 這些範例可讓 AI 更輕鬆地模擬所提供完成的結構、樣式或語氣。

零樣本學習

透過零樣本學習,您可以包含指令但排除逐字完成。 零樣本提示會依賴模型現有的知識來產生回應。 當您需要一般答案或工作相當簡單,不需要太多指引時,Zero-Shot提示會非常有用。 零樣本提示也較不需要大量的資源,因為它會依賴現有的知識。

以下是一個零樣本提示的範例,它告訴模型評估使用者輸入、確定使用者的意圖,並在輸出前面加上「意圖:」。

string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent: 
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent: 
"""

Few shot learning

透過少樣本學習,您可以在提示中包含逐字完成,以協助引導模型的回應。 通常包含一到五個範例。 這些範例示範您想要的結構、樣式或回應類型。 少樣本學習會產生更多的詞元,同時也會導致模型更新其知識。 少量提示在減少模棱兩可和對齊期望結果方面特別有用。

以下是數次提示的範例,告知模型評估使用者輸入、判斷使用者的意圖,並在輸出前面加上「意圖:」。

string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.

User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage

User Input: Can you send the full update to the marketing team?
Intent: SendEmail

User Input: {request}
Intent:
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.

User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage

User Input: Can you send the full update to the marketing team?
Intent: SendEmail

User Input: {request}
Intent:
"""

在提示中使用角色

在提示中指派角色是用來引導模型在產生回應時採用特定觀點、語氣或專業知識的技術。 角色可讓您量身打造輸出,以更符合任務的情境或受眾。 當您需要回應以模擬職業或反映語氣時,這個角色很有用。 若要指派角色,您應該在提示中清楚描述角色定義。

以下是指派角色的提示範例:

string prompt = $"""
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
""";
prompt = """
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
"""

思考鏈提示

使用連鎖思維提示時,您會引導模型逐步執行任務,並依次在輸出中呈現每個步驟及其結果。 鏈結提示可以藉由將一些執行計劃交由模型處理,來簡化提示設計。 鏈結提示可讓您更輕鬆地將任何問題隔離至特定步驟,讓您知道進一步專注在何處。 您可以指示模型包含其思想鏈結,也可以使用範例來示範模型如何細分工作。

以下是指示模型描述逐步推理的範例:

string prompt = $"""
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
""";
prompt = """
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
"""

以下範例說明完成模型的步驟:

prompt = $"""
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?

First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1. 

Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder: 
1.

"Finally, the farmer will eat the remaining apples:
1.
""";
prompt = """
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?

First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1. 

Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder: 
1.

Finally, the farmer will eat the remaining apples:
1.
"""

此提示輸出應該類似下列輸出:

Divide 150 by 12 to find the number of full baskets the farmer can make: 150 / 12 = 12.5 full baskets
The farmer can make 12 full baskets with 12 apples each.
Multiply 12 full baskets by 12 apples per basket to find the number of apples used in the baskets: 12 * 12 = 144 apples
Subtract the number of apples used in the baskets from the total number of apples: 150 - 144 = 6 apples
The farmer will eat 6 remaining apples.

製作提示的秘訣

  • 特定的輸入會產生特定的輸出:LLM 會根據其收到的輸入來做出回應。 製作清楚且具體的提示對於取得所需的輸出至關重要。

  • 實驗是關鍵:您可能需要反覆運算並實驗不同的提示,以瞭解模型如何解譯和產生回應。 微小的調整可能會導致結果發生重大的變化。

  • 內容的重要性:LLM 會考慮提示中所提供的內容。 您應確保內容已妥善定義且相關以取得準確且一致的回應。

  • 處理模棱兩可:請記住,LLM 可能會難以處理模棱兩可的查詢。 請提供內容或結構以避免模糊或非預期的結果。

  • 提示的長度:雖然 LLM 可以處理短提示和長提示,但您應該考慮簡潔性與清晰度之間的權衡取捨。 以提示長度來進行實驗可協助您找出最佳的平衡點。

製作有效的提示需要清楚、精確且深思熟慮的設計。 諸如零投籃和少射學習、角色指派和思維鏈條提示等技術可以提升響應的質量和相關性。 您可以視需要提供清楚的指示、妥善定義的內容和範例,引導模型產生微調的相關回應。 若要達到最佳結果,請記得實驗並精簡您的提示。