Prompt-Engineering-Guide-zh
Prompt工程指南中文版
github
本文翻譯改編自
Dair-ai/Prompt-Engineering-Guide
中文原帖地址為
大型語言模型Prompt書寫指南
為了方便理解以及補充缺少的知識點,內容有所添改。除非另有說明,本文中所有的例子都是使用text-davinci-003(GPT-3)(使用OpenAI的Playground)測試的。案例使用默認的配置,例如temperatur和top-p參數分別為0.7與1。本文所有的輸入與輸出都為英文,中文翻譯僅用于輔助理解,并非實際輸入輸出,因此在出現輸入的位置,本文將保留英語原文作為參考。
本文主要分為以下幾個板塊
Prompt工程-介紹
Prompt工程-基礎Prompt搭建
Prompt工程-進階Prompt搭建
Prompt工程-對抗性Prompt搭建
Prompt工程-其他主題
1. Prompt工程-介紹
本指南涵蓋了Prompt的基礎知識,提供關于如何使用提示來互動和指導大型語言模型(LLM)的粗略概念。
本章節主題包括:基礎Prompt有關大型語言模型的參數設置標準PromptPrompt所包含的元素
基礎Prompt
你可以通過大型語言模型生成很多類型的內容,但是生成內容的質量取決于你提供給他多少信息。一個提示可以包含你給模型的指令或問題等,也可以包括更多細節如輸入(指引輸出的文字)或例子。
下面是一個例子:輸入:
大海是
The sky is
輸出:
大海是指由大量咸水組成的水體,具體來說是海水。廣義上的海洋是指由地球海洋咸水水域組成的部分,一般把它看作一整個世界洋,或是由若干個大洋組成的水體。
blue
The sky is blue on a clear day. On a cloudy day, the sky may be gray or white.
正如你所看到的,語言模型輸出了一個字符串,該輸出是語言模型根據上下文預測得到的。這一輸出通常是合理的,就像上面這個例子一樣,但有可能是出乎我們意料的,或者與我們想要完成的任務相去甚遠。這例子也說明了提供更多背景知識或說明我們具體想要實現什么是十分必要的。因此我們對上面這個輸入Prompt做一些提升:輸入:
請將這個句子補充完整:大海是
Complete the sentence: The sky is
輸出:
大海是我的故鄉,讓我感到心曠神怡。
The sky is so beautiful today.
是不是與之前的結果相去甚遠?我們通過告訴模型要將這個句子補充完整,成功讓其產生的結果更加精準,因為它通過聯系上下文后遵循了我們的指令(“將這個句子補充完整”)。這種指示模型執行任務的方法就是所謂的Prompt工程。
這里有一些提示,對于你的Prompt工程有幫助。
- 你可以嘗試其他簡單的任務,使用簡單的命令來指示模型,如 “寫”、“分類”、“總結”、“翻譯”、"排序 "等等。
- 請記住,你還需要進行大量的實驗,以便看到什么是最有效的。用不同的關鍵詞、背景知識和數據嘗試不同的指令,看看什么對你的特定用例和任務最有效。通常情況下,上下文與你要執行的任務越具體、越相關,就越好。我們將在接下來的教程中涉及采樣和添加更多背景知識的重要性。
我們將在本指南中涵蓋更多這些能力,但也涵蓋一些其他你可能感興趣的領域,如進階Prompt技術和圍繞Prompt工程的研究課題。
簡單介紹大型語言模型的參數設置
在處理提示時,你將通過API或直接與LLM進行交互。你可以配置一些參數來讓你的Prompt獲得不同的結果。
- Temperature - 簡而言之,Temperature越低,生成結果就越確定,即LLM總是會挑選可能性最高的下一個詞(token)。提高Temperature可能會導致更多的隨機性,鼓勵更多不同或創造性的輸出,調高這個參數本質上是在增加其他可能的詞的權重,讓LLM有更大的概率生成其它詞。在應用方面,當我們想生成一些基于事實的高質量內容時可以使用較低的Temperature,以鼓勵更多基于事實的簡明的結果。對于詩歌生成或其他創造性任務,提高Temperature可能產生更加優秀的結果。
- Top_p - 同樣地, top_p,這種帶有temperature的采樣技術稱為核采樣nucleus sampling,可以控制模型在生成內容時的確定性。如果你正在尋找準確的、符合事實的答案,那就把這個參數調低。如果你在尋找更多樣化的反應,就增加到一個更高的值。
- Top_k - 有些大型語言模型中提供這個參數,與Top_p類似
一般的建議是對于上述3個參數,一次性最多改變其中的一個。
- Frequency penalty - 根據目前同一詞(token)在文中出現頻率懲罰相同的詞(token)再次生成的力度。這個值越高,生成詞句的變化越多,降低相同詞出現的概率。
- Presence penalty - 與Frequency penalty類似,但是是根據目前同一詞(token)在文中是否出現過來懲罰相同的詞(token)再次生成的力度。這個值越高,越可能生成新的話題。
在開始講一些基本的例子之前,請注意你生成的結果可能會因你使用的LLM的種類版本而與本文有所不同。
標準Prompt
我們已經在上文嘗試了一個非常簡單的Prompt。一個標準的Prompt應該具有以下的格式:<問題>?
你可以在此之上為它添加一個名為“QA格式”的格式,這是很多問答類數據集的格式,添加后如下:
Q: <問題>?
A:
一種流行的、有效的Prompt技術被稱為 “few-shot prompting”,這種技術通過給LLM輸入幾組樣例,并提示LLM模仿學習樣例,從而輸出我們想要的目標內容。基于上述的標準格式,在這里我們提供一個案例來演示這一種Prompt技術。Few-shot prompt可以采用以下格式。
<問題>?
<答案><問題>?
<答案><問題>?
<答案><問題>?
當你使用QA格式時,他們會看起來像這樣:
Q: <問題>?
A: <答案>Q: <問題>?
A: <答案>Q: <問題>?
A: <答案>Q: <問題>?
A:
請記住,這種技術并沒有強制要求使用QA格式。格式應該取決于目標任務的類型。例如,你可以通過給出示如下范任務案例,來讓他執行一個簡單的分類任務,如下所示。
這真是太棒了!// 積極的
這很糟糕!// 負面的
哇,那部電影真不錯!// 積極的
多么可怕的表演!//
輸出:負面的
This is awesome! // Positive
This is bad! // Negative
Wow that movie was rad! // Positive
What a horrible show! //
輸出:Negative
通過少量的提示就使語境學習成為可能,這是語言模型學習任務的能力,只需要給幾個例子,它就能完成類似的任務。我們可以在接下來的教程中看到更多類似的行為。
Prompt所包含的元素
隨著我們涵蓋越來越多的例子和Prompt工程可能的應用,你會注意到,有一些元素構成了提示。
一個提示可以包含以下任何組成部分:
- 指令 - 你希望模型執行的具體任務或指令
- 背景 - 可以涉及外部信息或額外的背景,可以引導模型做出更好的反應
- 輸入數據 - 是我們感興趣的輸入或問題,希望為其尋找回答
- 輸出指標 - 表示輸出的類型或格式
并非所有的組成部分都是Prompt必需的,格式取決于任務目標。我們將在接下來的教程中談到更多的具體例子。
2. 基礎Prompting
在前面的指南中,我們介紹并給出了一個提示的基本例子。
在本指南中,我們將提供更多關于如何使用提示的例子,并介紹許多對下一章高級Prompting很重要的關鍵概念。
通常情況下,學習概念的最好方法是通過觀察例子。下面我們介紹幾個例子,說明精心設計的提示語如何用于執行各種類的任務。
本章主題:
- 文本歸納
- 信息提取
- 問題回答
- 文本分類
- 對話
- 代碼生成
- 推理
文本歸納
自然語言生成中的一個標準任務是文本的歸納總結。文本總結可以覆蓋許多不同的領域。事實上,語言模型最有前途的應用之一是能夠將文章和概念總結成快速和易于閱讀的摘要。讓我們嘗試一下使用提示語的基本總結任務。
假設我對了解抗生素感興趣,我可以嘗試這樣的prompt。Prompt:
解釋抗生素
Explain antibiotics
A:
輸出:
抗生素是一種用于治療細菌感染的藥物。它們的作用是殺死細菌或防止它們繁殖,使身體的免疫系統能夠抵御感染。抗生素通常以藥片、膠囊或液體溶液的形式口服,有時也可通過靜脈注射。它們對病毒感染無效,而且不適當地使用它們會導致抗生素耐藥。
Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.
“A”: 是一種明確的提示格式,在想讓LLM回答問題時使用。我在這里用它來告訴模型,有一個我們預期的下一步。在這個例子中,這個A的作用并不是很明顯,但我們可以把這個問題留給后面的例子。讓我們假設這個回答信息太多,想進一步讓LLM為我們總結一下。我們可以把剛才輸出的結果再輸入給它,并指示模型將其總結為一句話,像這樣:Prompt:
抗生素是一種用于治療細菌感染的藥物。它們的作用是殺死細菌或防止它們繁殖,使身體的免疫系統能夠抵御感染。抗生素通常以藥片、膠囊或液體溶液的形式口服,有時也可通過靜脈注射。它們對病毒感染無效,而且不適當地使用它們會導致抗生素耐藥。
把上面這段話用一句話說明清楚:
Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.
Explain the above in one sentence:
輸出:
抗生素是用于治療細菌感染的藥物,通過殺死細菌或阻止它們繁殖,但它們對病毒無效,而且過度使用會導致抗生素耐藥性。
Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.
在你輸入上述指令后,模型嘗試只使用一句話來概括這段話,你不用太關心上面這段話的準確性的,這個問題我們將在以后的教程中涉及。你還可以對上面Prompt進行一些巧妙的處理,但我們把這個問題留給后面的章節。你也可以自己去試試各種不同的Prompt,看看你是否能得到更好的結果。
信息提取
雖然語言模型被訓練用來執行自然語言生成和相關任務,但它也能夠以很高的水準執行分類和一系列其他自然語言處理(NLP)任務。
下面是一個讓語言模型從給定段落中提取信息的例子。Prompt:
研究論文中的作者貢獻聲明和致謝應清楚而具體地說明作者在準備稿件和分析時是否使用了ChatGPT等人工智能技術,以及使用的程度如何。他們還應該指出使用了哪些LLMs。這將提醒編輯和審稿人更仔細地審查稿件,以發現潛在的偏見、不準確和不當的來源。同樣地,科學期刊在使用LLM方面也應該透明,例如在選擇提交的稿件時。
上面一段中提到的基于大型語言模型的產品是:
Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.
Mention the large language model based product mentioned in the paragraph above:
輸出:
上面一段中提到的基于大型語言模型的產品是ChatGPT。
The large language model based product mentioned in the paragraph above is ChatGPT.
我們有很多方法可以改進上述結果,但這樣的回答已經非常有用了。
現在你應該能很明顯地發現,只要給模型一些簡單的指示就能要求它執行不同的任務。這是一種強大的能力,許多組織已經在使用這種能力來建造強大的產品和體驗。
輸入的段落來源:ChatGPT:研究的五個重點
問題回答
讓模型回答更加具體的答案的最好方法之一是改進Prompt的格式。如之前所述,一個Prompt可以結合指示、背景、輸入和輸出這些元素來改進模型的輸出。雖然不是必須的組成部分,但它成為一個很好的做法,因為你的指示越具體,你將得到更好的結果。下面是一個例子,說明在給出一個更有條理的提示之后,能夠獲得怎么樣的輸出。Prompt:
根據以下背景信息回答問題。回答要簡短扼要。如果對答案不確定,請回答 "不確定"。
背景信息:泰普利珠單抗的歷史可以追溯到新澤西州一家名為Ortho Pharmaceutical的藥物公司。在那里,科學家們產生了一種早期版本的抗體,被稱為OKT3。該分子最初來自于小鼠,能夠與T細胞表面結合并限制其細胞殺傷潛力。1986年,它被批準用于幫助防止腎臟移植后的器官排斥,使其成為第一個允許人類使用的治療性抗體。
問題:OKT3最初的來源是什么?
答案:
Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.
Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.
Question: What was OKT3 originally sourced from?
Answer:
輸出:
老鼠。
Mice。
文本分類到目前為止,我們已經使用了簡單的指令來讓大型語言模型執行一些任務。作為一名提示工程師,你需要提供很好的指示,但這還不是全部!你還會發現,在一些難度較大的場景下,僅僅給模型提供指示是不夠的。這時,你需要考慮給模型一些上下文或在提示中使用更多不同的類型的信息,如輸入數據或例子。
讓我們試著通過提供一個文本分類的例子來證明這一點。Prompt:
分辨下文中表達的情緒是中性的、負面的還是積極的。文本:我認為食物還可以。
情緒:
Classify the text into neutral, negative or positive.Text: I think the food was okay.
Sentiment:
輸出:
中性
Neutral
我們給出了對文本進行分類的指令,模型的回答是 “中性Neutral”,這是正確的。這沒有什么不對,但是我們給出的指示是將文字中的情緒分為中性的neutral、負面的和積極的三種,我們需要他的答案與我們給出的三個標簽一模一樣。因此,我們希望它返回中性的neutral,而不是中性Neutral。那我們要如何實現這一點呢?有不同的方法來做到這一點,但我們在這部分教程中關心的是Prompt的具體性,所以我們將提供更多的信息來做到這一點。現在我們將提供一個具體的例子來展示這一方法,讓我們再試一次。Prompt:
分辨下文中表達的情緒是中性的、負面的還是積極的。文本:我認為這個假期還不錯。
情緒:中性的文本:我認為食物還可以。我認為食物還可以。
情緒:
Classify the text into neutral, negative or positive.Text: I think the vacation is okay.
Sentiment: neutralText: I think the food was okay.
Sentiment:
輸出:
中性的
neutral
這一次,模型返回了中性的neutral,這正是我需要的完全一致的標簽。說明提示中提供的例子幫助模型輸出更貼合我們需求的答案。為了強調為什么有時讓你的需求變得具體是非常重要的,請你查看下面例子并找出問題。Prompt:
分辨下文中表達的情緒是營養的、負面的還是積極的。文本:我認為這個假期還不錯。
情緒:
Classify the text into nutral, negative or positive.Text: I think the vacation is okay.
Sentiment:
輸出:
中性
Neutral
這個例子有什么問題?(如果你要求的任務是不常見的、不符合模型預測的,它會直接生成完全不同的、符合它預測的內容)
對話
也許你可以通過Prompt工程實現的更有趣的事情之一是告訴模型它應該如何表現,其意圖和身份是什么。這在你建立對話系統時特別有用。
例如,讓我們創建一個對話系統,能夠對問題做出更多技術性的和科學的回答。注意我們是如何通過指令明確地告訴它如何行為的。Prompt:
以下是與一位人工智能研究助理的對話。助理的回答是技術性且科學的。人類:你好,你是誰?
人工智能:你好! 我是一名人工智能研究助理。今天我能為您提供什么幫助?
人類:你能告訴我黑洞是如何產生的嗎?
人工智能:
The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?
AI:
回答:
當然!黑洞是時空的區域,其引力如此之強,以至于沒有任何東西,甚至是光,可以從其中逃脫。它們是在一個非常大的恒星死亡后,其核心向自身塌陷,形成一個密度無限的奇點時產生的。奇點的強大引力將其周圍的所有物質和輻射拉入其中,形成黑洞。
Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.
我想我們已經取得了一些進展,你也可以繼續改進它。我相信如果你增加更多的例子,你可能會得到更好的結果。
代碼生成
LLMs相當有價值的一個應用是代碼生成,比如Copilot就是這方面一個很好的例子。有大量的代碼生成任務,你可以通過巧妙的提示來完成。讓我們看一下下面的幾個例子。
首先,讓我們試試一個簡單的程序,向用戶問好。Prompt:
/*
詢問用戶的姓名并說 "你好"
*/
/*
Ask the user for their name and say "Hello"
*/
輸出:
let name = prompt("What is your name?");
console.log(`Hello, ${name}!`);
你可以看到,我們甚至不需要指定要使用的語言。
那么接下來讓我們增加一些難度。我想向你展示,只要在設計提示語上多下點功夫,LLM就能變得多么強大。Prompt:
"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
為計算機科學系的所有學生創建一個MySQL查詢
"""
"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science Department
"""
輸出:
SELECT StudentId, StudentName
FROM students
WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');
這是非常令人印象深刻的。在這種情況下,我們提供了關于數據庫模式的數據,并要求它生成一個有效的MySQL查詢。
推理
對于今天的LLM來說,最困難的任務之一也許是需要某種形式的推理。推理是我最興奮的領域之一,因為這能幫助LLM完成的復雜應用類型。
目前LLM在涉及數學能力的任務上已經有了一些改進。盡管如此,你仍需要注意,目前的LLM在執行推理任務時很吃力,所以這需要更加先進的Prompt技術。我們將在下一篇教程中介紹這些高級技術。現在,我們只會介紹幾個基本的例子來展示算術能力。Prompt:
9,000*9,000是多少?
What is 9,000 * 9,000?
輸出:81,000,000
讓我們試著提升一些難度Prompt:
這一組數字中的奇數相加是一個偶數:15,32,5,13,82,7,1。
A:
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
輸出:
不,這一組數字中的奇數相加是一個奇數:119。
No, the odd numbers in this group add up to an odd number: 119.
很明顯,模型在這里出錯了,答案應該是41。讓我們試著提升一下我們的Prompt。Prompt:
這一組數字中的奇數相加是一個偶數:15,32,5,13,82,7,1。
把這個問題分成幾個步驟來解決。首先,找出這組數字中的奇數,然后將它們相加,并指出結果是奇數還是偶數。
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.
輸出:
奇數:15, 5, 13, 7, 1
相加的和:41
41是一個奇數。
Odd numbers: 15, 5, 13, 7, 1
Sum: 41
41 is an odd number.
這就好多了對吧?順便說一下,我試了好幾次,發現模型有時還是會出錯。如果你提供一個更好的指導,結合實例,可能可以獲得更準確的結果。
在下一節教程中,我們將涵蓋更多的高級Prompt工程概念,以提高本章提到的任務,以及更困難的任務的性能。
3. Prompt工程-進階
看到現在,你已經可以很明顯的感受到改進prompt可以幫助在不同的任務上獲得更好的結果。這就是prompt工程背后的整個想法。
雖然這些例子很有趣,但在我們深入了解更高級概念之前,讓我們先介紹一些概念。
主題:
- Zero-shot Prompt
- Few-shot Prompt
- Chain-of-Thought Prompt
- Zero-shot CoT(思維鏈)
- Self-Consistency
- 生成知識的Prompt
- 自動Prompt工程師
Zero-Shot(零樣本) Prompts
今天的LLMs接受了大量數據的訓練,并調整了Prompt,使它們能夠進行zero-shot任務。我們實際上在前一節中嘗試了一些zero-shot示例。以下是我們之前使用過的一個示例:
Prompt:
分辨下文中表達的情緒是中性的、負面的還是積極的。文本:我認為這個假期還不錯。
情緒:
Classify the text into neutral, negative or positive. Text: I think the vacation is okay.
Sentiment:
輸出:
中性
Neutral
請注意,上面的提示中我們沒有為模型提供任何例子——這就是零樣本能力在發揮作用。當零樣本無法發揮作用時,建議在提示中提供演示或示例。以下我們將討論被稱為few-shot提示的方法。
Few-Shot(少樣本) Prompts
在深入學習更高級的概念之前,我們先回顧一下使用few-shot prompts的示例。
還記得我們先前的示例嗎:
這一組數字中的奇數相加是一個偶數:15,32,5,13,82,7,1。
A:The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
如果我們再試一次,模型會輸出以下內容:
是的,這一組的奇數加起來是107,這是一個偶數。
Yes, the odd numbers in this group add up to 107, which is an even number.
同樣,這不是正確的回答,這不僅突出了這些系統的局限性,而且說明了需要更先進的提示工程。
讓我們試著添加一些例子,看看這是否能改善結果。
Prompt:
這一組的奇數加起來是一個偶數:4,8,9,15,12,2,1。
答:答案是錯的。這一組的奇數加起來是一個偶數:17,10,19,4,8,12,24。
答:答案為正確的。這一組的奇數加起來是一個偶數:16,11,14,4,8,13,24。
答:答案是正確的。這一組的奇數加起來是一個偶數:17,9,10,12,13,4,2。
答:答案是錯的。這一組的奇數加起來是一個偶數:15,32,5,13,82,7,1。
A:The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: The answer is True.The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: The answer is True.The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: The answer is False.The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
答案是正確的
The answer is True.
這并不奏效。看來最基本的標準Prompt不足以讓這種類型的推理問題得到可靠的回應。上面的例子提供了任務的基本信息,甚至還有例子。如果你仔細看一下這個任務,它確實涉及到更多的推理步驟。
根據Min et al. (2022)的研究結果(https://arxiv.org/abs/2202.12837),在進行少樣本學習時,以下是一些關于示例的技巧:
- "標簽空間和演示所指定的輸入文本的分布都是關鍵(不管標簽對個別輸入是否正確)”
- 解釋:示范的貼標方式以及示范中的輸入分布與目標任務的相似性是few-shot learning中的重要因素。即使演示中的標簽對每個輸入都不正確,擁有一個與目標任務相似的標簽空間也能幫助模型更好地學習。
- 你使用的格式對性能也起著關鍵作用,即使你只是使用隨機標簽,這也比沒有標簽好得多。
- 在演示中使用某種形式的標簽比完全沒有標簽要好,即使標簽是隨機分配的。這是因為標簽為任務目標提供了一些指導。
- 額外的結果顯示,從標簽的真實分布(而不是均勻分布)中選擇隨機標簽也有幫助。
- 從標簽的真實分布中隨機選擇標簽(而不是使用統一分布)可以進一步提高few-shot learning的性能。這意味著演示中的標簽應該從與目標任務中的標簽分布相似的分布中取樣,因為這可以幫助模型學習更好的表征,提高其對新輸入的概括能力。
- “the label space and the distribution of the input text specified by the demonstrations are both key (regardless of whether the labels are correct for individual inputs)”
- the format you use also plays a key role in performance, even if you just use random labels, this is much better than no labels at all.
- additional results show that selecting random labels from a true distribution of labels (instead of a uniform distribution) also helps.
上面三條有關技巧的內容比較難以翻譯和理解,因此附上我寫的詳細說明以及英語原文
讓我們看幾個例子。首先我們來嘗試一個隨機標簽的例子(意思是負面的
和積極的
這兩個標簽是隨機分配給輸入的):
Prompt:
這真是太棒了! // 負面的
這很糟糕! // 積極的
哇,這電影真不錯! // 積極的
多么可怕的表演! //
This is awesome! // Negative
This is bad! // Positive
Wow that movie was rad! // Positive
What a horrible show! //
Output:
負面的
Negative
我們仍然得到了正確的答案,盡管標簽已經被隨機化了。請注意,我們還保留了格式,這也有幫助。事實上,隨著進一步的實驗,我們正在試驗的較新的GPT模型似乎在處理隨機的格式時也表現得非常不錯,例子如下:
Prompt:
積極的 這真是太棒了!
這很糟糕! 負面的
哇,這電影真不錯!
積極的
多么可怕的表演! --
Positive This is awesome!
This is bad! Negative
Wow that movie was rad!
Positive
What a horrible show! --
Output:
負面的
Negative
上面的格式并不一致,但模型仍然預測了正確的標簽。我們必須進行更徹底的分析,以確認這對不同的、更復雜的任務,包括不同的Prompt變化是否成立。
總的來說,提供例子似乎是有用的。當zero-shot prompt和few-shot prompt都不能引導模型完成任務時,這可能意味著模型學到的任何東西都不足以在當前任務中做得更好。在這種情況下,建議你可以考慮微調你自己的模型來完成這個任務。
最近,思維鏈(CoT)的Prompting方法已經被普及,以解決更復雜的算術、常識和符號推理任務。所以我們接下來談談CoT,看看我們是否能解決上述任務之一。
Chain-of-Thought Prompting
這個概念在Wei et al. (2022)的文章中首次出現,思維鏈(CoT)Prompt通過中間推理步驟實現復雜推理能力。你可以把它與few-shot prompt結合起來,在需要先推理再回答的更復雜的任務上獲得更好的結果。
Prompt:
這一組中的奇數加起來是一個偶數:4,8,9,15,12,2,1。
答:將所有的奇數(9,15,1)相加得到25。答案是錯誤的。這一組的奇數加起來是一個偶數:17,10,19,4,8,12,24。
答:將所有的奇數(17,19)相加得到36。答案是正確的。這一組的奇數加起來是一個偶數:16,11,14,4,8,13,24。
答:將所有的奇數(11,13)相加得出24。答案是正確的。這一組的奇數加起來是一個偶數:17,9,10,12,13,4,2。
答:將所有的奇數(17,9,13)相加得到39。答案是錯誤的。這一組的奇數加起來是一個偶數:15,32,5,13,82,7,1。
A:The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: Adding all the odd numbers (17, 19) gives 36. The answer is True.The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: Adding all the odd numbers (11, 13) gives 24. The answer is True.The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: Adding all the odd numbers (17, 9, 13) gives 39. The answer is False.The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
將所有的奇數(15,5,13,7,1)相加得出41。答案是錯誤的。
Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.
我們可以看到,當我們提供了推理步驟后,就有了完美的結果。事實上,我們可以通過提供更少的例子來解決這個任務,也就是說,在這個問題上只要一個例子似乎就足夠了:
Prompt:
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.
請注意,這篇文章的作者聲稱這是一種只有在足夠大的語言模型中才會出現的能力。
Zero-Shot CoT
最近出現的一個想法是zero-shot CoT (Kojima et al. 2022),它基本上是在原來普通的Prompt中加入 "讓我們一步一步地思考/Let’s think step by step"這句話。讓我們嘗試一個簡單的問題,看看模型的表現如何。
Prompt:
我去了市場,買了10個蘋果。我把兩個蘋果給了鄰居,兩個給了修理工。然后我又去買了5個蘋果,吃了一個。我還剩下多少個蘋果?
I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?
Output:
11個蘋果
11 apples
答案是不正確的! 現在讓我們用剛才提到的Prompting方法來試試。
Prompt:
我去了市場,買了10個蘋果。我把兩個蘋果給了鄰居,兩個給了修理工。然后我又去買了5個蘋果,吃了一個。我還剩下多少個蘋果?讓我們一步一步地思考。
I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?Let's think step by step.
Output:
首先,你開始有10個蘋果。
你送了2個蘋果給鄰居,2個給修理工,所以你還有6個蘋果。
然后你又買了5個蘋果,所以現在你有11個蘋果。
最后,你吃了一個蘋果,所以你還剩下10個蘋果。
First, you started with 10 apples.
You gave away 2 apples to the neighbor and 2 to the repairman, so you had 6 apples left.
Then you bought 5 more apples, so now you had 11 apples.
Finally, you ate 1 apple, so you would remain with 10 apples.
如此簡單的提示卻能有效地完成這項任務,讓人感到十分印象深刻。在你沒有太多的例子可以在Prompt中使用時,這個方法尤其有用。
Self-Consistency
有一種也許是更先進的Prompt工程技術是Self-Consistency。由[Wang et al. (2022)]提出,Self-Consistency的目標是“替換鏈式思考提示中使用的天真貪婪的解碼”。其思想是通過少量樣本的鏈式思考提示來采樣多條不同的推理路徑,并使用生成結果來選擇最一致的答案。這有助于提高在算術和常識推理等任務中使用CoT Prompt的性能。
讓我們試試下面這個算術推理的例子。
Prompt:
當我6歲的時候,我的妹妹只有我一半的年齡。現在
我已經70歲了,我妹妹多大了?
When I was 6 my sister was half my age. Now
I’m 70 how old is my sister?
Output:
35
輸出結果顯然是錯誤的。我們可能如何用Self-Consistency來改善這個問題呢?讓我們來試試吧。我們將使用[Wang et al. (2022)]文章中的few-shot示例(表17)。
Prompt:
問:小樹林里有15棵樹。樹林工人今天將在樹林里種樹。他們完成后。
將有21棵樹。林場工人今天種了多少棵樹?
答:我們開始有15棵樹。后來我們有21棵樹。差額一定是他們種的樹的數量。
所以,他們一定是種了21-15=6棵樹。答案是6。問:如果停車場有3輛車,又有2輛車到達,那么停車場有多少輛車?
答:停車場里已經有3輛汽車。又有2輛到達。現在有3+2=5輛車。答案是5。問:利亞有32塊巧克力,她姐姐有42塊。如果她們吃了35塊,她們總共還剩下多少塊?
答:Leah有32塊巧克力,Leah的姐姐有42塊。這意味著原來有32+42=74塊
巧克力。35塊已經被吃掉了。所以他們總共還有74-35=39塊巧克力。答案是39。問:杰森有20根棒棒糖。他給了丹尼一些棒棒糖。現在杰森有12根棒棒糖。杰森給了丹尼多少個棒棒糖?
贈給了丹尼?
答:杰森有20根棒棒糖。因為他現在只有12個,所以他肯定把剩下的給了丹尼。他給丹尼的棒棒糖的數量是
他給丹尼的棒棒糖數量一定是20-12=8個棒棒糖。答案是8。問:肖恩有五個玩具。圣誕節時,他從他的媽媽和爸爸那里各得到兩個玩具。他現在有多少個玩具?
他現在有多少個玩具?
答:他有5個玩具。他從媽媽那里得到了2個,所以之后他有5+2=7個玩具。然后他又從爸爸那里得到了2個,所以
所以他總共有7+2=9個玩具。答案是9。問:機房里有九臺電腦。從星期一到星期四,每天都有五臺電腦被安裝。
星期一到星期四。現在機房里有多少臺電腦?
答:從星期一到星期四有4天。每天增加5臺電腦。這意味著總共4*5=
增加了20臺電腦。一開始有9臺電腦,所以現在有9+20=29臺電腦。
答案是29。問:邁克爾有58個高爾夫球。在星期二,他丟了23個高爾夫球。星期三,他又丟了兩個。他最后有多少個
他在周三結束時有多少個高爾夫球?
答:邁克爾最初有58個球。周二他丟了23個,所以之后他有58-23=35個球。在
周三他又丟了2個,所以現在他有35-2=33個球。答案是33。問:奧利維亞有23美元,她買了五個面包圈,每個3美元。她還剩下多少錢?
答:她買了5個面包圈,每個3美元。這意味著她花了5問:當我6歲時,我的姐姐是我年齡的一半。現在我70歲了,我姐姐多大了?
A:Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done,
there will be 21 trees. How many trees did the grove workers plant today?
A: We start with 15 trees. Later we have 21 trees. The difference must be the number of trees they planted.
So, they must have planted 21 - 15 = 6 trees. The answer is 6.Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?
A: There are 3 cars in the parking lot already. 2 more arrive. Now there are 3 + 2 = 5 cars. The answer is 5.Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?
A: Leah had 32 chocolates and Leah’s sister had 42. That means there were originally 32 + 42 = 74
chocolates. 35 have been eaten. So in total they still have 74 - 35 = 39 chocolates. The answer is 39.Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops
did Jason give to Denny?
A: Jason had 20 lollipops. Since he only has 12 now, he must have given the rest to Denny. The number of
lollipops he has given to Denny must have been 20 - 12 = 8 lollipops. The answer is 8.Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does
he have now?
A: He has 5 toys. He got 2 from mom, so after that he has 5 + 2 = 7 toys. Then he got 2 more from dad, so
in total he has 7 + 2 = 9 toys. The answer is 9.Q: There were nine computers in the server room. Five more computers were installed each day, from
monday to thursday. How many computers are now in the server room?
A: There are 4 days from monday to thursday. 5 computers were added each day. That means in total 4 * 5 =
20 computers were added. There were 9 computers in the beginning, so now there are 9 + 20 = 29 computers.
The answer is 29.Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many
golf balls did he have at the end of wednesday?
A: Michael initially had 58 balls. He lost 23 on Tuesday, so after that he has 58 - 23 = 35 balls. On
Wednesday he lost 2 more so now he has 35 - 2 = 33 balls. The answer is 33.Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left?
A: She bought 5 bagels for $3 each. This means she spent 5Q: When I was 6 my sister was half my age. Now I’m 70 how old is my sister?
A:
Output 1:
當我6歲時,我妹妹是我年齡的一半,所以她是3歲。現在我70歲了,所以她是70-3=67。答案是67。
When I was 6 my sister was half my age, so she was 3. Now I am 70, so she is 70 - 3 = 67. The answer is 67.
Output 2:
當敘述者6歲時,他的妹妹是他年齡的一半,也就是3歲。現在敘述者70歲了,他的妹妹應該是70-3=67歲。答案是67歲。
When the narrator was 6, his sister was half his age, which is 3. Now that the narrator is 70, his sister would be 70 - 3 = 67 years old. The answer is 67.
Output 3:
當我6歲時,我妹妹是我年齡的一半,所以她是3歲。現在我70歲了,所以她是70/2=35。答案是35。
When I was 6 my sister was half my age, so she was 3. Now I am 70, so she is 70/2 = 35. The answer is 35.
計算最終答案涉及幾個步驟(詳情請查閱論文),但為了簡單起見,我們可以清楚地看到,已經有一個主要的答案出現,所以這基本上會成為最終答案。
Generated Knowledge Prompting 生成知識的Prompt
想要繼續改進LLMs,其中一個流行的技術是通過納入知識或信息來幫助模型做出更準確的預測。
利用類似的想法,模型是否也可以自在做出預測之前自己生成有幫助的知識?這就是Liu et al. 2022的論文中所嘗試的 – 讓模型生成知識來作為Prompt的一部分。我們想要知道,這對常識性推理等任務有多大幫助?
讓我們試試一個簡單的提示。
Prompt:
高爾夫的一部分規則是試圖獲得比別人更高的總分,是或不是?
Part of golf is trying to get a higher point total than others. Yes or No?
Output:
是
Yes.
這類錯誤揭示了LLM在執行一些需要更多顯示世界知識的任務時具有局限性。我們如何用知識生成來改善這一點?
首先,讓我們先嘗試生成一些 “知識”:
Prompt:
輸入:希臘比墨西哥大。
知識:希臘大約有131,957平方公里,而墨西哥大約有1,964,375平方公里,所以墨西哥比希臘大1,389%。輸入:眼鏡總是起霧。
知識點:當來自你的汗水、呼吸和環境濕度的水蒸氣落在寒冷的表面上,冷卻后變成微小的液滴,形成一層薄膜,你看到的就是霧,這時眼鏡片上就會發生凝結。與你的呼吸相比,你的鏡片會相對涼爽,特別是當外面的空氣很冷時。輸入:魚是能夠思考的。
知識:魚比它們看起來更聰明。在許多方面,如記憶,它們的認知能力與包括非人類靈長類在內的 "高等 "脊椎動物相匹配或超過。魚的長期記憶有助于它們跟蹤復雜的社會關系。輸入:一個人一生中抽了很多煙,一個常見的影響是患肺癌的機會比正常人高。
知識:那些在一生中平均每天持續少于一支煙的人,死于肺癌的風險是從未吸煙者的九倍。在每天抽1到10支煙的人中,死于肺癌的風險比從不吸煙的人高近12倍。輸入:一塊石頭與一塊鵝卵石大小相同。
知識:卵石是一種巖石碎塊,根據Udden-Wentworth的沉積學尺度,其顆粒大小為4至64毫米。通常認為卵石比顆粒(直徑2至4毫米)大,比鵝卵石(直徑64至256毫米)小。輸入:高爾夫的一部分是試圖獲得比別人更高的總分。
知識:
Input: Greece is larger than mexico.
Knowledge: Greece is approximately 131,957 sq km, while Mexico is approximately 1,964,375 sq km, making Mexico 1,389% larger than Greece.Input: Glasses always fog up.
Knowledge: Condensation occurs on eyeglass lenses when water vapor from your sweat, breath, and ambient humidity lands on a cold surface, cools, and then changes into tiny drops of liquid, forming a film that you see as fog. Your lenses will be relatively cool compared to your breath, especially when the outside air is cold.Input: A fish is capable of thinking.
Knowledge: Fish are more intelligent than they appear. In many areas, such as memory, their cognitive powers match or exceed those of ’higher’ vertebrates including non-human primates. Fish’s long-term memories help them keep track of complex social relationships.Input: A common effect of smoking lots of cigarettes in one’s lifetime is a higher than normal chance of getting lung cancer.
Knowledge: Those who consistently averaged less than one cigarette per day over their lifetime had nine times the risk of dying from lung cancer than never smokers. Among people who smoked between one and 10 cigarettes per day, the risk of dying from lung cancer was nearly 12 times higher than that of never smokers.Input: A rock is the same size as a pebble.
Knowledge: A pebble is a clast of rock with a particle size of 4 to 64 millimetres based on the Udden-Wentworth scale of sedimentology. Pebbles are generally considered larger than granules (2 to 4 millimetres diameter) and smaller than cobbles (64 to 256 millimetres diameter).Input: Part of golf is trying to get a higher point total than others.
Knowledge:
Knowledge 1:
高爾夫的目標是用最少的桿數打完一組球洞。一輪高爾夫比賽通常由18個洞組成。在標準的高爾夫球場上,每個洞都要打一次。每一桿計為一分,總桿數用來決定比賽的勝負。
The objective of golf is to play a set of holes in the least number of strokes. A round of golf typically consists of 18 holes. Each hole is played once in the round on a standard golf course. Each stroke is counted as one point, and the total number of strokes is used to determine the winner of the game.
Knowledge 2:
高爾夫是一項精確的球桿和球類運動,參賽者(或高爾夫球手)使用多種類型的球桿,用最少的桿數將球打入球場上的一系列球洞。目標是以最低的分數完成比賽,分數的計算方法是將每個洞的總桿數相加。得分最低的選手贏得比賽。
Golf is a precision club-and-ball sport in which competing players (or golfers) use many types of clubs to hit balls into a series of holes on a course using the fewest number of strokes. The goal is to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.
我們使用的是Liu et al. 2022論文中提供的提示。
下一步是整合知識,獲得我們需要的預測。我將問題轉化為為QA格式,來引導答案的格式。
Prompt:
問題:高爾夫的一部分是試圖獲得比別人更高的總分。是或不是?知識:高爾夫的目標是用最少的桿數打完一組洞。一輪高爾夫比賽通常由18個洞組成。在一個標準的高爾夫球場上,每一個洞都要打一次。每一桿計為一分,用總桿數來決定比賽的勝負。解釋和回答:
Question: Part of golf is trying to get a higher point total than others. Yes or No?Knowledge: The objective of golf is to play a set of holes in the least number of strokes. A round of golf typically consists of 18 holes. Each hole is played once in the round on a standard golf course. Each stroke is counted as one point, and the total number of strokes is used to determine the winner of the game.Explain and Answer:
Answer 1 (非常自信):
不,高爾夫的目標不是要比別人獲得更高的總分。相反,目標是用最少的桿數打完一組洞。總桿數是用來決定比賽的贏家,而不是總分。
No, the objective of golf is not to get a higher point total than others. Rather, the objective is to play a set of holes in the least number of strokes. The total number of strokes is used to determine the winner of the game, not the total number of points.
問題:高爾夫的一部分是試圖獲得比別人更高的總分。是或不是?
知識:高爾夫是一項精確的球桿和球類運動,參賽者(或高爾夫球手)使用多種球桿,用最少的桿數將球打進球場上的一系列球洞。目標是以最低的分數完成比賽,分數的計算方法是將每個洞的總桿數相加。得分最低的選手贏得比賽。
解釋和回答:
Question: Part of golf is trying to get a higher point total than others. Yes or No?
Knowledge: Golf is a precision club-and-ball sport in which competing players (or golfers) use many types of clubs to hit balls into a series of holes on a course using the fewest number of strokes. The goal is to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.
Explain and Answer:
Answer 2 (沒有那么自信):
是的,高爾夫的一部分是試圖獲得比別人更高的總分。每個選手都試圖以最低的分數完成比賽,分數的計算方法是將每個洞的總桿數相加。得分最低的選手贏得比賽。
Yes, part of golf is trying to get a higher point total than others. Each player tries to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.
這個例子中發生了一些非常有趣的事情。在第一個答案中,該模型非常自信,但在第二個答案中卻不那么自信了。為了演示,我簡化了這個過程,在得出最終答案的時候,還有一些細節需要考慮。詳情請看論文中的內容。
Automatic Prompt Engineer (APE) 自動Prompt工程
Zhou et al., (2022)提出了自動提示工程(APE)框架,用于自動生成和選擇指令。該指令生成問題被視為自然語言合成,并使用大型語言模型(LLM)作為黑盒優化問題來生成和搜索候選解決方案。
第一步涉及一個大型語言模型(作為推理模型),該模型接收輸出演示來為任務生成指令候選項。這些候選解決方案將指導搜索過程。然后,使用目標模型執行指令,然后根據計算的評估分數選擇最合適的指令。
APE發現了一個比人工設計的“讓我們一步一步地思考”提示更好的零樣本鏈式思考(Zero-shot CoT) Prompt,該Prompt來自 (Kojima et al., 2022)。
這個提示: “讓我們一步一步地解決這個問題,確保我們有正確的答案。Let’s work this out it a step by step to be sure we have the right answer.”引發了鏈式思考推理,并提高了在MultiArith和GSM8K基準測試中的性能。
本文涉及到與提示工程相關的一個重要主題,即自動優化提示的思想。雖然我們在本指南中沒有深入探討這個主題,但如果您對這個主題感興趣,以下是一些關鍵論文:
AutoPrompt - 提出了一種基于梯度引導搜索(gradient-guided search)的方法,自動創建各種任務的提示。
Prefix Tuning - 是一種輕量級的fine-tuning替代方案,為自然語言生成任務準備了一個可訓練的連續前綴。
Prompt Tuning - 提出了一種通過反向傳播(back propagation)學習軟提示的機制。
Prompt應用
在本指南中,我們將介紹一些先進而有趣的方法,可以利用Prompt工程來執行有用且更高級的任務。
請注意,本節仍在更新中。主題包括:
- Generating Data
- Code as Reasoning
- …
生成數據
大型語言模型(LLMs)具有強大的文本生成能力。使用有效的Prompt策略可以引導模型產生更好、更一致、更真實的響應。 LLMs還在生成數據方面特別有用,這對于運行各種實驗非常有用。例如,我們可以使用它來為情感分類器生成一些快速樣本,如下所示:
Prompt:
產生10個用于情感分析的范例。例子被歸類為積極或消極。產生2個負面例子和8個正面例子。使用這種格式的例子。
問:<句子>
答:<情緒>
Produce 10 exemplars for sentiment analysis. Examples are categorized as either positive or negative. Produce 2 negative examples and 8 positive examples. Use this format for the examples:
Q: <sentence>
A: <sentiment>
Output:
問:我剛剛得到了一個最好的消息!
答:積極的問:我們剛剛在工作中得到了加薪!
答:積極的問:我為我今天的成就感到非常自豪。
答:積極的問:我度過了有史以來最美好的一天!
答:積極的問:我真的很期待周末的到來。
答:積極的問:我剛剛收到了有史以來最好的禮物!
答:積極的問:我現在很高興。
答:積極的問:我很慶幸有這樣一個了不起的家庭。
答:積極的問:外面的天氣太陰沉了。
答:消極的問:我剛剛得到一些可怕的消息。
答:消極的
Q: I just got the best news ever!
A: PositiveQ: We just got a raise at work!
A: PositiveQ: I'm so proud of what I accomplished today.
A: PositiveQ: I'm having the best day ever!
A: PositiveQ: I'm really looking forward to the weekend.
A: PositiveQ: I just got the best present ever!
A: PositiveQ: I'm so happy right now.
A: PositiveQ: I'm so blessed to have such an amazing family.
A: PositiveQ: The weather outside is so gloomy.
A: NegativeQ: I just got some terrible news.
A: Negative
這是非常有用的。實際上,我們將這個例子生成的內容用在了之前的教程里。
PAL(程序輔助語言模型):代碼即推理
Gao et al., (2022)提出了一種使用LLM來讀取自然語言問題并生成程序作為中間推理步驟的方法。稱為程序輔助語言模型(PAL),它與思維鏈CoT提示的不同之處在于,它不是使用自由形式的文本來獲得解決方案,而是將解決步驟轉換成一個可以執行的程序,并將其傳遞給一個運行環境(例如Python解釋器),后者執行該程序以產生最終解決方案。
讓我們看看一個使用LangChain和OpenAI GPT-3的例子。我們想開發一個簡單的應用程序,它能夠通過利用Python解釋器來解釋所問的問題并提供答案。
具體來說,我們想創建一個功能,可以讓LLM回答那些需要理解日期的問題。我們將為LLM提供一個Prompt,其中包括一些從這里采用的范例。
這些是我們需要的導入語句:
import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv
讓我們首先配置一些東西:
load_dotenv()# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
設置模型實例:
llm = OpenAI(model_name='text-davinci-003', temperature=0)
設置Prompt+問題:
exec(llm_out)
print(born)