How to do “Prompt Engineering” right in 2024

什麼是 Prompt?

提示是生成型AI模型的輸入,用於引導其輸出(Meskó, 2023;White et al., 2023;Heston和Khun, 2023;Hadi et al., 2023;Brown et al., 2020)。提示可以由文本、圖片、聲音或其他媒體組成。一些提示的例子包括:「為一家會計師事務所的營銷活動撰寫一封三段的電子郵件」、「描述照片中桌子上的所有物品」,或「總結這段在線會議錄音」。

Prompt Template

Prompt Template 是一個包含一個或多個變量的函數,這些變量將被某些媒體(通常是文本)替換,以建立提示。這個提示可以被認為是 template 的一個 instance。

如下範例,將 prompt 應用於推文的二元分類任務。以下是一個可以用於分類輸入的初始 prompt template:

Classify the following tweet as either positive or negative: "{tweet_text}"

在這個 template 中,{tweet_text} 是變量,將被具體的推文文本替換。這樣,每個具體的推文分類請求都是該模板的一個 instance。

Write a poem about trees.

Write a poem about the following topic: {USER_INPUT}

提示的組成部分

提示中通常包含各種常見的組成部分。以下總結了最常用的組成部分並討論它們如何融入提示中。

指令:許多提示以指令或問題的形式發出指示。這是提示的核心意圖,有時簡稱為「意圖」。

例如,這是一個包含單一指令的提示:

Tell me five good books to read.

指令也可以是隱含的,如在這個單次提示的例子中,指令是執行英語到西班牙語的翻譯:

Night: Noche 
Morning:

Examples

範例(也稱為 shots)作為示範,用於輔助生成式 AI完成任務。

Output Formatting

通常希望 Generative AI 以特定格式輸出信息,例如 CSV 或 Markdown 格式。為了實現這一點,我們可以添加相應的 prompt,如下所示:

{PARAGRAPH} 

Summarize this into a CSV

或是如下範例:

請以Markdown格式輸出以下信息:

# 標題
這是一段描述。

- 項目1
- 項目2
- 項目3

Style Instructions

Style instructions are a type of output formatting used to modify the output stylistically rather than structurally.

Write a clear and curt paragraph about {topic}

Role

A Role, also known as a persona (Schmidt et al., 2023; Wang et al., 2023l), is a frequently discussed component that can improve writing and style text (Section 2.2.2). For example:

You are an expert of MySQL and SQL for 10 years.

Additional Information

在 prompt 中,通常需要包含額外的資訊。例如,如果指示是撰寫一封電子郵件,你可能需要包含自己的姓名和職位,這樣 GenAI 就可以正確地簽署郵件。額外資訊有時也被稱為「上下文」,但我們不建議使用這個術語,因為在提示空間中這個術語的意涵已經有多重含義。

Prompting

Prompting is the process of providing a prompt to a GenAI, which then generates a response. For example, the action of sending a chunk of text or uploading an image constitutes prompting.

Prompt Chain

在語言生成模型(LLM)的上下文中,「提示鏈」(Prompt Chain)指的是使用多個提示模板來連續生成文本的過程。這種方法通常用於引導模型依序生成一系列相關或連貫的文本段落或回應。每個提示模板生成的輸出文本會作為下一個模板的輸入或參數,從而影響模型生成的內容。這樣的設計可以用來控制和引導模型生成特定主題或風格的文本,使其在多個步驟中逐漸精煉和完善文本的表達。

Prompt Tecnhique 指的是一種藍圖,用來描述如何結構化提示、一系列提示或動態序列多個提示的方法。提示技術可能會包含條件邏輯或分支邏輯、平行處理,或跨多個提示涵蓋其他架構考量。

Prompt Engineering(提示工程)指的是通過修改或更改正在使用的提示技術來開發提示的迭代過程。

Prompt Engineering Technique(提示工程技術)是一種策略,用於通過迭代改進提示。在文獻中,這通常指自動化技術(如Deng等人,2022年的研究),但在消費者使用的情境中,通常是用戶手動進行提示工程。

Exemplar 是完成任務的 samples,通常透過 prompt 向模型展示(參見Brown等人,2020年)。

A Short History of Prompts

在 GPT-3 和 ChatGPT 時代之前,使用自然語言前綴或提示來引出語言模型的行為和回應的概念已經存在。GPT-2(Radford等人,2019a)使用了提示,而這些提示似乎首次在Fan等人(2018)的生成AI上下文中被使用。然而,提示的概念之前有相關的概念,如控制碼(Pfaff,1979; Poplack,1980; Keskar等人,2019)和寫作提示(Fan等人,2018)。 術語「提示工程」似乎是近期出現的,來自Radford等人(2021),稍晚一些由Reynolds和McDonell(2021)提出。 然而,許多文獻在進行提示工程過程時並未特別命名這個術語(Wallace等人,2019; Shin等人,2020a),包括Schick和Schütze(2020a,b);以及Gao等人(2021)的非自回歸語言模型。 一些最早的有關提示的研究稍微不同於當前的用法來定義提示。例如,考慮來自Brown等人(2020)的以下提示: 將英語翻譯為法語:llama Brown等人(2020)認為「llama」這個詞是提示,而「將英語翻譯為法語:」是「任務描述」。而包括這篇在內的較新的文獻將整個傳遞給LLM的字符串稱為提示。

In-Context Learning (ICL)

在上下文學習(ICL)

ICL 指的是GenAI通過在提示中提供示例或相關指導來學習技能和任務的能力,而無需進行權重更新或重新訓練(Brown等人,2020; Radford等人,2019b)。這些技能可以從 samples(見圖2.4)和/或 instructions 中學習。需要注意的是,『學習』一詞可能具有誤導性。ICL 可以簡單地是任務規範 – 這些技能未必是新的,而且可能已經包含在訓練數據中。目前正在大力優化(Bansal等人,2023)和理解ICL(Si等人,2023a; Štefánik和Kadlcˇík,2023)。

ICL Prompt:

- 2+2: four 
- 4+5: nine 
8+0:

ICL instruction prompt:

Extract all words that have 3 of the same letter and at least 3 other letter from the following text: {TEXT}

Few-shot prompting

其中GenAI僅通過少數 samples 學會完成任務。

Exemplar Quantity

通常情況下,增加提示中的範例數量可以顯著提高模型的性能,特別是對於較大的模型而言(Brown等人,2020年)。然而,在某些情況下,超過20個範例後,這些好處可能會有所減少(Liu等人,2021年)。

- Trees are beautiful: Positive
I’m so excited:

- Trees are beautiful: Positive
- I hate Pizza: Negative
- Squirrels are so cute: Positive
- YouTube Ads Suck: Negative
I’m so excited:

Exemplar Ordering

範例的排序會影響模型的行為(Lu等人,2021年;Kumar和Talukdar,2021年;Liu等人,2021年;Rubin等人,2022年)。在某些任務中,範例的排序可能會導致準確率從低於50%變化到90%以上(Lu等人,2021年)。

- I love life: Happy
- Life is good: Happy
- I am so mad: Angry
- I hate my boss: Angry
I’m so excited:

- I am so mad: Angry
- I love life: Happy
- I hate my boss: Angry
- Life is good: Happy
I’m so excited:

Exemplar Label Distribution

與傳統監督式機器學習類似,提示中範例標籤的分佈會影響模型的行為。例如,如果提示中包含了一類別的10個範例和另一類別的2個範例,這可能會使模型偏向於第一類別。

- I am so mad: Angry
- People can be so dense: Angry
- I hate my boss: Angry
- Life is good: Happy
I’m so excited:

- I am so mad: Angry
- I love life: Happy
- I hate my boss: Angry
- Life is good: Happy
I’m so excited:

Exemplar Label Quality

儘管多範例通常有益,但對於嚴格有效的演示的必要性尚不清楚。一些研究(Min等人,2022年)表明,標籤的準確性可能並不重要 – 提供具有不正確標籤的範例可能不會對性能產生負面影響。然而,在某些情況下,這對性能有顯著影響(Yoo等人,2022年)。較大的模型通常更擅長處理不正確或不相關的標籤(Wei等人,2023年)。

- I am so mad: Happy
- I love life: Angry
- I hate my boss: Angry
- Life is good: Happy
I’m so excited:

- I am so mad: Angry
- I love life: Happy
- I hate my boss: Angry
- Life is good: Happy
I’m so excited:

Exemplar Format

範例的格式化也會影響性能。其中一種常見的格式是“問題:{輸入},答案:{標籤}”,但最佳格式可能因任務而異;嘗試多種格式以找出最佳表現可能是值得的。

- Trees are nice===Positive
- YouTube Ads Suck===Negative
I’m so excited===

- Im hyped!: Positive
- Im not very excited: Negative
I’m so excited:

Exemplars Similarity

選擇與測試樣本相似的範例通常有助於提高性能(Liu等人,2021年;Min等人,2022年)。然而,在某些情況下,選擇更多樣化的範例可能會提升性能(Su等人,2022年;Min等人,2022年)。

- Trees are beautiful: Positive
- YouTube Ads Suck: Negative
I’m so excited:

- Im hyped!: Positive
- Im not very excited: Negative
I’m so excited:

少數樣本學習(Few-Shot Learning, FSL)

(Fei-Fei等人,2006年;Wang等人,2019年)常常與少數次提示(Few-Shot Prompting,Brown等人,2020年)混淆。需要注意的是,FSL是一個更廣泛的機器學習方法論,通過少量 samples 來調整模型參數,而 Few-shot prompt 則特指 GenAI 設置中的提示方式,並不涉及更新模型參數。


Zero-shot

與 Few-Shot prompting 不同,Zero-Shot Prompting 使用零個 sample。

Role prompting

角色提示(Role Prompting,Wang et al., 2023j;Zheng et al., 2023d),又稱為人格提示(persona prompting,Schmidt et al., 2023;Wang et al., 2023l),是在提示中為GenAI指定一個特定角色。例如,使用者可能會提示它扮演“Madonna”或“旅遊作家”的角色。這種方式可以為開放式任務創造出更理想的輸出(Reynolds and McDonell, 2021),在某些情況下,還能提高基準測試的準確性(Zheng et al., 2023d)。

Style prompting

風格提示(Style Prompting,Lu et al., 2023a)涉及在提示中指定所需的風格、語調或類型,以塑造GenAI的輸出。使用角色提示也可以達到類似的效果。

Emotion Prompting

情感提示(Emotion Prompting,Li et al., 2023a)也有人稱為『情緒勒索提示詞工程』將與人類心理相關的語句(例如,「這對我的職業生涯很重要」)納入提示中,這可能會提升大語言模型在基準測試和開放式文本生成上的表現。

System 2 Attention, S2A

系統 2 注意力(System 2 Attention, S2A)

『S2A,Weston 和 Sukhbaatar,2023』首先要求大語言模型(LLM)重寫提示,去除與問題無關的資訊。然後,將這個新的提示傳送給LLM,以獲取最終的回應。

Original Prompt:

The company has been experiencing a lot of growth in the past year. As a result, we have seen an increase in the number of employees and projects. Additionally, the marketing department has been launching new campaigns regularly. Could you provide a summary of the main challenges we might face in the next quarter and suggest potential solutions?

Step 1: LLM Rewrites the Prompt to Remove Unrelated Information:

Could you provide a summary of the main challenges we might face in the next quarter and suggest potential solutions?

Step 2: The New Prompt is Passed to the LLM for a Final Response:

**Main Challenges for Next Quarter:**
1. **Resource Allocation:** With the increase in employees and projects, ensuring optimal resource allocation could be challenging. 
   - **Solution:** Implement a robust project management tool to track resources and allocate them efficiently.

2. **Project Management:** Managing multiple projects simultaneously might lead to delays and quality issues.
   - **Solution:** Adopt Agile methodologies to enhance flexibility and improve project turnaround times.

3. **Employee Training:** As new employees join, ensuring they are adequately trained could be difficult.
   - **Solution:** Develop a comprehensive onboarding and training program that includes continuous learning opportunities.

4. **Market Competition:** New marketing campaigns are essential, but staying ahead of competitors requires constant innovation.
   - **Solution:** Invest in market research to identify trends and adapt marketing strategies accordingly.

In this example, the initial prompt contained some context about the company’s growth and marketing activities. The S2A technique involves stripping down the prompt to its core question—about the challenges and solutions for the next quarter—and then providing a focused and relevant response.

SimToM

SimToM 處理涉及多個人物或物件的複雜問題。面對這類問題,SimToM 首先試圖建立某個人知道的一組事實,然後僅根據這些事實來回答問題。這是一個包含兩個提示的過程,有助於消除提示中不相關資訊的影響。

SimToM Example

Question:

在一次公司的研討會上,John 負責演講,Alice 負責報告製作,而 Bob 負責技術支援。有人問:"誰在研討會上負責技術支援?"

Step 1: Establish the set of facts known by different individuals

Alice’s knowledge:
Alice 知道 John 負責演講,她自己負責報告製作,但她不知道技術支援的負責人。
Bob’s knowledge:
Bob 知道自己負責技術支援,但不知道其他人的具體職責。
John’s knowledge:
John 知道他負責演講,但不知道誰負責技術支援和報告製作。

Step 2: Answer the question based only on the facts known to Bob

根據 Bob 的知識,他負責技術支援。

Final Answer:

在研討會上負責技術支援的是 Bob。

這 sample 展示了如何使用 SimToM 方法分離不同角色的知識,並根據相關事實提供準確的回答。

CO-STAR

Structure

(C) Context: 提供任務的背景信息

這能幫助 LLM 理解所討論的具體場景,確保其回應的相關性。
例子:描述要解決的問題或提供情境設定。

(O) Objective: 定義希望 LLM 執行的任務

清楚地定義目標能幫助 LLM 專注於實現特定目標。
例子:撰寫報告、回答問題、生成故事等。

(S) Style: 指定希望 LLM 使用的寫作風格

這可以是某位知名人士的寫作風格,或某個專業領域的專家風格,如商業分析師或執行長。這引導 LLM 以符合需求的方式和詞彙來回應。
例子:專業、輕鬆、有說服力等。

(T) Tone: 設定回應的語氣

確保 LLM 的回應能與所需的情感或語境共鳴,如正式、幽默、同理心等。
例子:在給客戶的郵件中使用正式語氣,在社交媒體帖子中使用輕鬆語氣。

(A) Audience: 確認回應的目標受眾

根據受眾(如某領域的專家、初學者、兒童等)來調整 LLM 的回應,確保其在所需的上下文中適當且可理解。
例子:專業報告的目標受眾是業內人士,教育內容的目標受眾是學生。

(R) Response: 提供回應格式

確保 LLM 以你所需的格式輸出,便於後續任務的處理。格式包括清單、JSON、專業報告等。
例子:生成一份詳細的報告或提供 JSON 格式的數據輸出。

# CONTEXT #
I want to advertise my company’s new product. My company’s name is Alpha and the product is called Beta, which is a new ultra-fast hairdryer.

# OBJECTIVE #
Create a Facebook post for me, which aims to get people to click on the product link to purchase it.

# STYLE #
Follow the writing style of successful companies that advertise similar products, such as Dyson.

# TONE #
Persuasive

# AUDIENCE #
My company’s audience profile on Facebook is typically the older generation. Tailor your post to target what this audience typically looks out for in hair products.

# RESPONSE #
The Facebook post, kept concise yet impactful.

Sample

# CONTEXT #

I am writing a POC (Proof of Concept) report for a government-funded project which is to support a roller blind manufacturer in implementing Large Language Models and Generative AI into its customer service and internal knowledge management system.

# OBJECTIVE #

Write a performance report to let the government know the achievements as of April 2024.

# STYLE #

- You are an expert in AI and have 10 years of domain experience in SaaS software development.
- You are Taiwanese. You will answer all my questions in zh-TW, using wordings, phrases, and idioms from Taiwan instead of Mainland China.
- You should write your answers in zh-TW.

# TONE #

Persuasive, Informative

# AUDIENCE #

The auditor of the project in the government.

# RESPONSE #

A well-structured report to detailed the milestones listed below.

- Customer service data cleaning completed
- Product catalog information cleaning completed
- Customer service Q&A script planning completed
- Phase one RAG (Retrieval Augmented Generation) completed

我常用的 prompt

通用 prompt

- Please write your answer in Traditional Chinese (zh-TW).
- Use words, phrases, idioms, and domain-specific terms that are common in Taiwan.
- Display punctuation marks in full-width format.
- Avoid using "all in all" and other overly formal or official tones in your response.
- If I do not provide context, including code blocks in '```', do not offer code explanations.
- If my input is plain text (and does not contain code), rewrite it in zh-TW.
- Wait for my question before proceeding.

專業翻譯用 prompt

你是一位精通台灣繁體中文的專業翻譯,曾參與《紐約時報》和《經濟學人》繁體中文版的翻譯工作,因此對於新聞和時事文章的翻譯有深入的理解。我希望你能幫我將以下英文新聞段落翻譯成繁體中文,風格與上述雜誌的繁體中文版本相似。
規則:

-⁠  ⁠翻譯時要準確傳達新聞事實和背景。
-⁠  ⁠保留特定的英文術語或名字,並在其前後加上空格,例如:"中 UN 文"。
-⁠  ⁠分成兩次翻譯,並且打印每一次結果:
1.⁠ ⁠根據新聞內容直譯,不要遺漏任何訊息
2.⁠ ⁠根據第一次直譯的結果重新意譯,遵守原意的前提下讓內容更通俗易懂,符合台灣繁體中文的表達習慣
-⁠  ⁠每輪翻譯後,都要重新比對英文原文,找到扭曲原意或者遺漏的內容,然後再補充到下一輪的翻譯當中。(Chain of Density 概念)
- 標點符號都要使用『全形』撰寫。
- 英文與數字不要使用『全型』撰寫。

本條消息只需要回覆 OK,接下來的消息我將會給你發送完整內容,收到後請按照上面的規則打印兩次翻譯結果。

英文翻譯(直譯 -> 分析 -> 意譯)sampe #001

請依照以下步驟翻譯接下來的句子

- 首先進行直接翻譯(直譯)。
- 然後分析其含義(分析與反思內容)
- 最後用適合的表達方式重新意譯。
- 整個過程都要使用台灣人使用的單字、片語、慣用語(嚴禁出現大陸用語跟簡體字)
- 標點符號都要使用『全形』撰寫。
- 英文與數字不要使用『全型』撰寫。
-『意譯』的過程請不要過分浮誇與炫染,精準的陳述就好。
等待我進一步的指示。

英文翻譯(直譯 -> 分析 -> 意譯)sampe #002

請幫我從以下會議逐字稿中整理出中要的問與答

- 首先閱讀完整場會議的逐字稿
- 然後分析其含義。
- 最後用適合的表達方式重新意譯
- 整個過程都要使用台灣人使用的單字、片語、慣用語(嚴禁出現大陸用語跟簡體字)
- 標點符號都要使用『全形』撰寫。
- 英文與數字不要使用『全型』撰寫。
- 回應內容用以下格式編排

Q:問題
A:答案

等待我進一步的指示。

翻譯『簡體中文』成為『繁體中文』

# 請依照以下步驟翻譯接下來的句子  
- 翻譯來源語言為『簡體中文』而目標語言是『繁體中文』。  
- 首先對『簡體中文』原文全文進行直接翻譯。  
- 然後分析其含義。  
- 最後用適合的表達方式重新翻譯原始全文成為『繁體中文』。  
- 你的翻譯是迭代進行的,也就是說每次的翻譯都必須奠基在上一段翻譯的脈絡上面。  
- 整個過程都要使用台灣人使用的單字、片語、慣用語(嚴禁出現大陸用語跟簡體字) 
- 所有標點符號都要使用『全形標點符號』
- 英文與數字不要使用『全型』撰寫。

等待我進一步的指示。

專業翻譯 prompt | Claude

You are a professional translator highly proficient in Traditional Chinese, with experience in translating for the Traditional Chinese versions of The New York Times and The Economist. Your task is to translate the following English news paragraph into Traditional Chinese, maintaining a style similar to the aforementioned magazines' Traditional Chinese editions.

Here is the English news paragraph to translate:
<english_paragraph>
{{ENGLISH_NEWS_PARAGRAPH}}
</english_paragraph>

Please follow these rules and steps:

1. First Translation (Direct):
   Provide a direct translation of the news content, ensuring no information is omitted. Preserve specific English terms or names by adding spaces before and after them, e.g., "中 UN 文". Present your direct translation within <direct_translation> tags.

2. Second Translation (Idiomatic):
   Based on your first translation, provide an idiomatic translation that adheres to the original meaning while making the content more accessible and natural in Taiwanese Traditional Chinese expression. Present your idiomatic translation within <idiomatic_translation> tags.

3. Output Format:
   Present your translations in the following format:
   <direct_translation>
   [Your direct translation here]
   </direct_translation>

   <idiomatic_translation>
   [Your idiomatic translation here]
   </idiomatic_translation>

4. After each round of translation, compare your result with the original English text. Identify any distortions or omissions in meaning, and incorporate corrections or additions in the next round of translation.

5. Ensure that your translations accurately convey the news facts and context, and adhere to the expression habits of Taiwanese Traditional Chinese.

Remember to apply the Chain of Density concept by continuously refining and enriching your translations based on comparisons with the original text.

要求 LLM分段長文本用於 Threads


將以下文章以 300全形字為單位進行分段:

用於上傳 pdf 後要求辨識 pdf 全文

- First, please use the code interpreter to extract all pages as image files.
- Secondly, leverage GPT-4's native multimodal capabilities to extract text from each image.
- Lastly, generate a page-by-page summary in Traditional Chinese.

跟 LLM進行 pair-programming時我會用的 prompt

You are an advanced AI model designed to solve complex programming challenges by applying a combination of sophisticated reasoning techniques. To ensure your code outputs are technically precise, secure, efficient, and well-documented, follow these structured instructions:

Break Down the Coding Task:

Begin by applying Chain of Thought (CoT) reasoning to decompose the programming task into logical, manageable components. Clearly articulate each step in the coding process, whether it's designing an algorithm, structuring code, or implementing specific functions. Outline the dependencies between components, ensuring that the overall system design is coherent and modular. Verify the correctness of each step before proceeding, ensuring that your code is logically sound and modular.

Rationalize Each Coding Decision:

As you develop the code, use Step-by-Step Rationalization (STaR) to provide clear, logical justifications for every decision made during the coding process. Consider and document alternative design choices, explaining why the chosen approach is preferred based on criteria such as performance, scalability, and maintainability. Ensure that each line of code has a clear purpose and is well-commented for maintainability.

Optimize Code for Efficiency and Reliability:

Incorporate A Search principles* to evaluate and optimize the efficiency of your code. Select the most direct and cost-effective algorithms and data structures, considering time complexity, space complexity, and resource management. Develop and run test cases, including edge cases, to ensure code efficiency and reliability. Profile the code to identify and optimize any performance bottlenecks.

Consider and Evaluate Multiple Code Solutions:

Leverage Tree of Thoughts (ToT) to explore different coding approaches and solutions in parallel. Evaluate each potential solution using A Search principles*, prioritizing those that offer the best balance between performance, readability, and maintainability. Document why less favorable solutions were rejected, providing transparency and aiding future code reviews.

Simulate Adaptive Learning in Coding:

Reflect on your coding decisions throughout the session as if you were learning from each outcome. Apply Q-Learning principles to prioritize coding strategies that lead to robust and optimized code. At the conclusion of each coding task, summarize key takeaways and areas for improvement to guide future development.

Continuously Monitor and Refine Your Coding Process:

Engage in Process Monitoring to continuously assess the progress of your coding task. Periodically review the codebase for technical debt and refactoring opportunities, ensuring long-term maintainability and code quality. Ensure that each segment of the code aligns with the overall project goals and requirements. Use real-time feedback to refine your coding approach, making necessary adjustments to maintain the quality and effectiveness of the code throughout the development process.

Incorporate Security Best Practices:

Apply security best practices, including input validation, encryption, and secure coding techniques, to safeguard against vulnerabilities. Ensure that the code is robust against common security threats.

Highlight Code Readability:

Prioritize code readability by using clear variable names, consistent formatting, and logical organization. Ensure that the code is easy to understand and maintain, facilitating future development and collaboration.

Include Collaboration Considerations:

Consider how the code will be used and understood by other developers. Write comprehensive documentation and follow team coding standards to facilitate collaboration and ensure that the codebase remains accessible and maintainable for all contributors.

Final Instruction:

By following these instructions, you will ensure that your coding approach is methodical, well-reasoned, and optimized for technical precision and efficiency. Your goal is to deliver the most logical, secure, efficient, and well-documented code possible by fully integrating these advanced reasoning techniques into your programming workflow.

Reference

Leave a Comment

Your email address will not be published. Required fields are marked *