【譯】Prompt Engineering (提示詞工程) 簡介

在這里,我們討論了幾個撰寫提示(輸入模型的內(nèi)容)的原則和技巧,這些提示將幫助您為您的任務(wù)獲得最佳的生成結(jié)果。選擇正確的溫度也會對生成質(zhì)量產(chǎn)生很大的影響,我們會在這里單獨討論溫度。


主要的原則

我們發(fā)現(xiàn),在設(shè)計我們的模型的提示時需要牢記兩個主要的想法。

1. 提示將引導(dǎo)模型生成有用的輸出。

例如,如果您需要一篇文章的摘要,足夠訓(xùn)練的大型語言模型可以在您的引導(dǎo)下生成摘要:

這個提示有兩個部分:您想要進行摘要的文本以及任務(wù)描述。

2. 嘗試多種提示表述以獲得最佳的生成結(jié)果

在使用generate時,嘗試不同的提示對于解決問題非常有用。即使看起來類似的不同提示表述也可能導(dǎo)致生成結(jié)果截然不同。這可能是因為我們的模型已經(jīng)學習到這些不同的提示表述在不同的上下文和用途中使用,因此會產(chǎn)生不同的結(jié)果。下面我們將給出一些我們發(fā)現(xiàn)在不同任務(wù)中特別有效的示例。
以摘要為例,如果使用“In summary”無法得到好的生成結(jié)果,我們可能需要嘗試使用“To summarize in plain language”或“The main point to take from this article is that”。

此外,您還可以在playground中使用可能性特征來查看模型是否存在特定的單詞、短語或結(jié)構(gòu)理解困難的問題。但是,請記住,序列開始時的標記平均可能性總是很高的。模型可能會將首次引入新概念或名稱的可能性降低,但一旦它看到它一次,就可以輕松地在生成中使用它。您還可以使用可能性功能查看是否有任何拼寫或標點符號會導(dǎo)致記號化問題。

3.描述任務(wù)和一般情境

通常在任務(wù)描述中包含額外的組成部分會很有用,自然而然地,這些組成部分往往會在我們要處理的輸入文本之后。

為模型提供足夠的上下文。例如,在文章之前更詳細地描述摘要任務(wù)。

示例:以自然語言塑造我們需要模型完成的任務(wù),可以使用輸入文本之前和之后的文本。

讓我們通過另一個示例來考慮這個問題的更多方面。假設(shè)您想要使用我們的模型通過自動生成合理的回復(fù)來協(xié)助您的客戶滿意度部門(注意:生成結(jié)果不會發(fā)送給客戶,這只是一種模擬)。

一個客戶向您的公司提出了以下問題:

Hi, I'd like a refund for the coffee maker I ordered. Would that be possible?

如何設(shè)計一個提示語以便與顧客進行交互時得到有用的生成呢?首先,我們需要讓我們的模型知道這個問題的一般背景,并且明確指出我們將在提示語中包含什么內(nèi)容:

This is a conversation between a customer and a polite, helpful customer service agent.
Question of the customer: Hi, I'd like a refund for the coffee maker I ordered. Would that be possible?

好的,我們已經(jīng)告訴模型可以期望什么,并清楚地表明我們的查詢是針對顧客提出的問題。接下來,讓我們展示模型我們想要給顧客的回答的開頭部分。

Response by the customer service agent: Hello, thank you for reaching out to us. Yes,

請注意,我們已經(jīng)明確表示下一個句子是對問題的回答,它來自客戶服務(wù)代理,并且我們希望給出一個積極的答案。將這一切結(jié)合在一起,我們得到以下提示:

This is a conversation between a customer and a polite, helpful customer service agent.
Question of the customer: Hi, I'd like a refund for the coffee maker I ordered. Would that be possible?
Response by the customer service agent: Hello, thank you for reaching out to us. Yes,
在提示語中包含特定的組件(例如輸入和輸出指示器)有助于向模型描述所需的任務(wù),特別是在提示語中包含多個示例時(如下圖所示)。

將其多次輸入到我們的中型模型中,我們得到以下的完成結(jié)果:

  • 是的,如果產(chǎn)品未使用并且未打開,我們可以接受退貨。
  • 是的,我們很樂意退還您的購買款項。但是,我們需要您將物品退回到我們的商店以獲得全額退款。
  • 是的,我們可以這樣做。請給我們發(fā)送一條消息,包括您的姓名、電話號碼和退款原因。我們將盡快回復(fù)您。
  • 請注意,即使這只是一個簡化的示例,我們也可以從基線模型中獲得可信的完成結(jié)果,而且只使用了很少的客戶服務(wù)交互!通過對特定問題和請求的處理示例進行微調(diào),這可以進一步改進。

4. 展示給模型您希望看到的內(nèi)容

將示例添加到提示中是實現(xiàn)良好生成的關(guān)鍵方式之一。示例向模型展示了我們所針對的輸出類型。

請?zhí)峁┠胍囊恍┥深愋偷氖纠?。這被稱為少樣本學習。我們來看一個例子。假設(shè)您想要使用我們的模型來分類電影評論是積極的、消極的還是中性的。想象一下,您將以下提示輸入到我們的模型中:

Review: "I really enjoyed this movie!"
This sentiment of this review is

根據(jù)此提示,我們的 Medium 模型實際生成了以下內(nèi)容:

This sentiment of this review is apt, considering the movie's plot,

顯然,我們的模型看到可能是我們不希望得到的生成類型。

在提示中,示例應(yīng)包括要模擬的示例輸入和輸出。

將所有這些內(nèi)容結(jié)合在一起,將其饋入Medium Generation模型,我們可靠地獲得積極的生成。

This is a movie review sentiment classifier.
Review: "I loved this movie!"
This review is positive.
Review: "I don't know, it was ok I guess.."
This review is neutral.
Review: "What a waste of time, would not recommend this movie."
This review is negative.
Review: "I really enjoyed this movie!"
This review is

這個提示的一個更簡單的版本可以這樣展示:


一個將提示的各個組成部分結(jié)合起來的示例。我們還可以在每個示例中重復(fù)任務(wù)描述,以強調(diào)對模型的指示。

少樣本生成通常會在我們更大的模型中更好地工作。您可以使用可能性終點來查看模型對示例中給出的正確答案的不確定性。

如果命令格式不起作用,請嘗試將其結(jié)構(gòu)化為散文。與生成模型進行直觀互動的一種方式是向模型發(fā)出有關(guān)所需生成類型的命令,例如:列舉藝術(shù)職業(yè)的清單。但是,由于我們的模型看到的大部分文本都是互聯(lián)網(wǎng)文章,有時會誤解這種寫作方式。嘗試將命令重新表述為散文的方式,以使模型產(chǎn)生所需的輸出:

The table lists the following professions as artistic careers:
1. Painter 2.

總的來說,您可能希望嘗試不同的寫作風格,直到找到適合您的東西。例如,寫新聞文章、博客文章或?qū)υ挼娘L格。

案例

在這里,我們將展示如何應(yīng)用上述原則來完成兩項具體任務(wù):根據(jù)給定的文章生成關(guān)鍵詞和根據(jù)少數(shù)已有示例生成更多示例。

生成關(guān)鍵詞:假設(shè)我們有一些文本段落,希望自動標記其中最相關(guān)的概念。

通過結(jié)合上述技術(shù),我們可以實現(xiàn)這一目標!首先,在提示語的開頭說明此提示的設(shè)定。然后,我們展示模型兩個要完成的示例:用標簽“約翰·馮·諾依曼”標記來自約翰·馮·諾依曼維基百科頁面的段落,用標簽“女權(quán)主義”標記來自女權(quán)主義維基百科頁面的段落。最后,我們向模型提供來自 Python 維基百科頁面的一段。

This is a bot that automatically finds the most important keyword for a given text passage.

Text: "John von Neumann (/v?n ?n??m?n/; Hungarian: Neumann János Lajos, pronounced [?n?jm?n ?ja?no? ?l?jo?]; December 28, 1903 – February 8, 1957) was a Hungarian-American mathematician, physicist, computer scientist, engineer and polymath. Von Neumann was generally regarded as the foremost mathematician of his time[2] and said to be "the last representative of the great mathematicians".[3] He integrated pure and applied sciences."

Most important key word: "John von Neumann"

Text: "Some scholars consider feminist campaigns to be a main force behind major historical societal changes for women's rights, particularly in the West, where they are near-universally credited with achieving women's suffrage, gender-neutral language, reproductive rights for women (including access to contraceptives and abortion), and the right to enter into contracts and own property.[9] Although feminist advocacy is, and has been, mainly focused on women's rights, some feminists argue for the inclusion of men's liberation within its aims, because they believe that men are also harmed by traditional gender roles.[10] Feminist theory, which emerged from feminist movements, aims to understand the nature of gender inequality by examining women's social roles and lived experience; feminist theorists have developed theories in a variety of disciplines in order to respond to issues concerning gender."

Most important key word:  "Feminism"

Text: "Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python 0.9.0.[31] Python 2.0 was released in 2000 and introduced new features, such as list comprehensions and a garbage collection system using reference counting and was discontinued with version 2.7.18 in 2020.[32] Python 3.0 was released in 2008 and was a major revision of the language that is not completely backward-compatible and much Python 2 code does not run unmodified on Python 3."

Most important key word:

這個提示可靠地生成“Python”作為答案,有時還會返回另一個可信選項“Guido van Rossum”。

示例生成:常見的任務(wù)是讓模型根據(jù)某些描述生成示例。以以下格式列出提示通常效果良好。

This is a list of ideas for blog posts for tourists visiting Toronto:

1. The best sights to see in Toronto
2. My favourite walks in Toronto

這將給我們帶來類似以下的生成結(jié)果:

3. An overview of Toronto
4. Toronto events
5. Restaurants in Toronto
6. Shopping in Toronto
7. Travel tips for Toronto
8. Sightseeing in Toronto
9. What to do in Toronto
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容