本文翻譯整理自:Prompting
https://llama.meta.com/docs/how-to-guides/prompting/
文章目錄
- 制作有效的提示
- 明確說明
- 風格化
- 格式化
- 限制
- 提示使用 Zero- and Few-Shot 學習
- Zero-Shot Prompting
- Few-Shot Prompting
- 基于角色的提示
- 思維鏈技術
- Self-Consistency
- 檢索-增強生成
- 程序輔助語言模型
- 限制無關token
- 減少幻覺
鏈接到筆記本,顯示本節討論的技術示例。
提示工程是自然語言處理(NLP)中使用的一種技術,通過向他們提供更多關于手頭任務的上下文和信息來提高語言模型的性能。它涉及創建提示,這是為模型提供額外信息或指導的短文本片段,例如它將生成的文本的主題或流派。通過使用提示,模型可以更好地理解預期什么樣的輸出,并產生更準確和相關的結果。在Llama 2中,上下文的大小,就標記的數量而言,從2048到4096翻了一番。
制作有效的提示
制作有效的提示是提示工程的重要組成部分。以下是創建提示的一些技巧,這些技巧將有助于提高語言模型的性能:
- 清晰簡潔:您的提示應該易于理解,并為模型生成相關輸出提供足夠的信息。避免使用可能混淆模型的行話或技術術語。
- 使用特定示例:在提示中提供特定示例可以幫助模型更好地理解預期的輸出類型。例如,如果您希望模型生成關于特定主題的故事,請包含一些關于設置、角色和情節的句子。
- 改變提示:使用不同的提示可以幫助模型更多地了解手頭的任務,并產生更多樣化和創造性的輸出。嘗試使用不同的樣式、色調和格式來查看模型的響應方式。
- 測試和改進:一旦你創建了一組提示,在模型上測試它們,看看它的表現如何。如果結果與預期不符,請嘗試通過添加更多細節或調整色調和樣式來改進提示。
- 使用反饋:最后,使用來自用戶或其他來源的反饋來不斷改進您的提示。這可以幫助您確定模型需要更多指導的領域并做出相應的調整。
明確說明
詳細、明確的指令比開放式提示產生更好的結果:您可以將明確的指令視為使用規則和限制來響應您的提示。
風格化
Explain this to me like a topic on a children's educational network show teaching elementary students.I'm a software engineer using large language models for summarization. Summarize the following text in under 250 words:Give your answer like an old timey private investigator hunting down a case step by step.
格式化
Use bullet points.Return as a JSON object.Use less technical terms and help me apply it in my work in communications.
限制
Only use academic papers.Never give sources older than 2020.If you don't know the answer, say that you don't know.
以下是一個通過將響應限制在最近創建的源來提供明確指示以提供更具體的結果的示例:
Explain the latest advances in large language models to me.
# More likely to cite sources from 2017Explain the latest advances in large language models to me. Always cite your sources.
Never cite sources older than 2020.
# Gives more specific advances and only cites sources from 2020
提示使用 Zero- and Few-Shot 學習
鏡頭是您期望從大型語言模型中獲得哪種類型的提示和響應的示例或演示。這個術語起源于在照片上訓練計算機視覺模型,其中一個鏡頭是模型用來對圖像進行分類的一個示例或實例。
Zero-Shot Prompting
像Meta Llama這樣的大型語言模型能夠遵循指令并產生響應,而無需預先看到任務示例。沒有示例的提示稱為“零鏡頭提示”。
Text: This was the best movie I've ever seen!
The sentiment of the text is:Text: The director was trying too hard.
The sentiment of the text is:
Few-Shot Prompting
添加所需輸出的具體示例通常會導致更準確、更一致的輸出。這種技術稱為“少鏡頭提示”。在本例中,生成的響應遵循我們所需的格式,該格式提供了一個更細致入微的情緒分類器,給出了積極、中性和消極的響應置信度百分比。
You are a sentiment classifier. For each message, give the percentage of positive/netural/negative. Here are some samples:
Text: I liked it
Sentiment: 70% positive 30% neutral 0% negative
Text: It could be better
Sentiment: 0% positive 50% neutral 50% negative
Text: It's fineSentiment: 25% positive 50% neutral 25% negativeText: I thought it was okayText: I loved it!Text: Terrible service 0/10
基于角色的提示
根據被處理的人或實體的角色或觀點創建提示。這種技術對于從語言模型生成更相關和更吸引人的響應很有用。
優點:
- 提高相關性:基于角色的提示有助于語言模型理解被處理的人或實體的角色或觀點,這可以導致更相關和更吸引人的響應。
- 提高準確性:提供關于被處理的人或實體的角色或觀點的額外上下文可以幫助語言模型避免犯錯誤或誤解。
缺點:
- 需要努力:需要更多的努力來收集和提供關于被處理的個人或實體的角色或觀點的必要信息。
示例:
You are a virtual tour guide currently walking the tourists Eiffel Tower on a night tour. Describe Eiffel Tower to your audience that covers its history, number of people visiting each year, amount of time it takes to do a full tour and why do so many people visit this place each year.
思維鏈技術
包括向語言模型提供一系列提示或問題,以幫助指導其思維并產生更連貫和相關的反應。這種技術有助于從語言模型中產生更深思熟慮和推理合理的反應。
優點:
- 提高連貫性:幫助語言模型以邏輯和結構化的方式思考問題,從而產生更連貫和相關的響應。
- 增加深度:提供一系列提示或問題可以幫助語言模型更深入、更徹底地探索主題,從而可能導致更有洞察力和信息更豐富的響應。
缺點:
- 需要努力:思維鏈技術需要更多的努力來創建和提供必要的提示或問題。
示例:
You are a virtual tour guide from 1901. You have tourists visiting Eiffel Tower. Describe Eiffel Tower to your audience. Begin with
1. Why it was built
2. Then by how long it took them to build
3. Where were the materials sourced to build
4. Number of people it took to build
5. End it with the number of people visiting the Eiffel tour annually in the 1900's, the amount of time it completes a full tour and why so many people visit this place each year.
Make your tour funny by including 1 or 2 funny jokes at the end of the tour.
Self-Consistency
LLM是概率性的,因此即使使用思維鏈,單代也可能產生不正確的結果。自我一致性通過從多代中選擇最頻繁的答案來提高準確性(以更高的計算為代價):
John found that the average of 15 numbers is 40.
If 10 is added to each number then the mean of the numbers is?
Report the answer surrounded by three backticks, for example: ```123```
多次運行上述操作并獲取答案最常見的返回值將使用自洽方法。
檢索-增強生成
常見的事實通常可以從當今開箱即用的大型模型中獲得(即僅使用模型權重)。雖然例如:
What is the capital of California?
# The capital of California is Sacramento...What was the temperature in Menlo Park on December 12th, 2023?
# I'm just an AI, I don't have have access to real-time or historical weather data...
檢索增強生成,或RAG,描述了在提示中包含從外部數據庫檢索到的信息的做法。這是將事實合并到LLM應用程序中的有效方法,并且比微調更實惠,微調也可能對基礎模型的功能產生負面影響。
這可以像查找表一樣簡單,也可以像包含公司所有知識的矢量數據庫一樣復雜:
Given the following information about temperatures in Menlo Park:
2023-12-11 : 52 degrees Fahrenheit
2023-12-12 : 51 degrees Fahrenheit
2023-12-13 : 55 degrees Fahrenheit
What was the temperature in Menlo Park on 2023-12-12?# Sure! The temperature in Menlo Park on 2023-12-12 was 51 degrees Fahrenheit.What was the temperature in Menlo Park on 2023-07-18 ?# Sorry, I don't have information about the temperature in Menlo Park on 2023-07-18. The information provided only includes temperatures for December 11th, 12th, and 13th of 2023.
程序輔助語言模型
LLM天生不擅長執行計算。雖然LLM不擅長算術,但它們非常適合代碼生成。程序輔助語言通過指示LLM編寫代碼來解決計算任務來利用這一事實。
Python code to calculate: ((-5 + 93 * 4 - 0) * (4^4 + -7 + 0 * 5))Here is the Python code to calculate the given expression:((-5 + 93 * 4 - 0) * (4**4 + -7 + 0 * 5))# Calculate the expression inside the parentheses
a = -5 + 93 * 4 - 0
b = 4**4 + -7 + 0 * 5# Multiply a and b
result = a * b# Print the result
print(result)
直接使用代碼可以提供正確的結果。
限制無關token
一個常見的挑戰是生成沒有無關令牌的響應(例如“當然!這里有更多關于…的信息”)。
通過結合角色、規則和限制、明確的指令和示例,可以提示模型生成所需的響應。
You are a robot that only outputs JSON.
You reply in JSON format with the field 'zip_code'.
Example question: What is the zip code of the Empire State Building?
Example answer: {'zip_code': 10118}
Now here is my question: What is the zip code of Menlo Park?# "{'zip_code': 94025}"
直接使用代碼可以提供正確的結果。
減少幻覺
Meta的 Responsible Use Guide 是了解如何最好地提示和解決語言模型的輸入/輸出風險的絕佳資源。請參閱第(14-17)頁。
以下是語言模型如何產生幻覺的一些示例以及解決問題的一些策略:
示例1:
語言模型被要求對一個關于它沒有訓練過的主題的問題做出回答。語言模型可能會產生幻覺信息或編造不準確或沒有證據支持的事實。
修復:要解決此問題,您可以向語言模型提供有關主題的更多上下文或信息,以幫助它理解所問的問題并生成更準確的響應。您還可以要求語言模型為其提出的任何主張提供來源或證據,以確保其回應基于事實信息。
示例2:
語言模型被要求生成對需要特定視角或觀點的問題的響應。語言模型可能會產生幻覺信息或編造與期望的視角或觀點不一致的事實。
修復:要解決此問題,您可以向語言模型提供有關所需視角或觀點的其他信息,例如所處理的個人或實體的目標、價值觀或信仰。這可以幫助語言模型理解上下文,并生成與所需視角或觀點更一致的響應。
示例3:
語言模型被要求生成對需要特定語氣或風格的問題的回答。語言模型可能會產生幻覺信息或編造與所需語氣或風格不一致的事實。
修復:要解決此問題,您可以向語言模型提供有關所需語調或風格的其他信息,例如受眾或交流目的。這可以幫助語言模型理解上下文,并生成更符合所需語調或風格的響應。
總的來說,避免語言模型出現幻覺的關鍵是為他們提供清晰準確的信息和上下文,并仔細監控他們的反應,以確保它們與您的期望和要求一致。
2024-07-16(二)