AI – 指令工程 Prompt Engineering

 

Al Prompt Engineer (指令工程師)
指令工程師是發揮AI的伯樂,指令工程技術大约有數十種,其中有幾種可以作為大家參考:
1 零樣本指令(Zero -Shot): 不需要提供範本,直接下達指令。
2少量樣本指令(Few-Shot):指令本身是由具有關係的上下文所组成。
3 思考鏈指令(Chain of thought prompting): 將複什任務拆分成不同的子任務,然後鏈接起来。
4 自我一致性(Self Consistency): 由於複雜的推理問題,應具有多種不同的思考路徑,也就是有很多不同的思考鏈,可以由每一條思考鏈來解題去產生答案。
5 自動推理和調用工具(Automatic Reasoning &Tool Use, ART): 將任務分解成不同的步驟,並且在每個步驟調用不同的外部工具。
6 生成知識指令(Generated Knowledge Prompting): 這是比較抽象及創新形式,利用Al生成知識指令的方式,要求大語言模型(LLM)先產生背景資料,再组合成為輸出內容,十分有效。
還有,指令工程可以搭配「搜尋增强生成」或是「微調」,讓大語言模型有更多的應用功能,進而生成更高品質的輸出內容。
  1. [鏈式思維提示]是一種在提示工程中用來增強大型語言模型(LLMs)推理和解決問題能力的技術。這種方法由Wei等人在2022年提出,涉及將用戶輸入結構化,使模型在得出最終答案之前生成逐步解釋或推理過程。

這種方法對於需要中間推理步驟的複雜任務特別有用。通過將問題分解成較小的、可管理的部分,鏈式思維提示使模型能夠提供更準確和可靠的答案。例如,在算術問題中,模型可以顯示每一步的計算過程,從而導致更透明和易於理解的解決方案。

鏈式思維提示在實踐中有許多應用,特別是在需要複雜推理和解決問題的任務中。以下是一些具體的應用範例:

數學問題解決:鏈式思維提示可以幫助AI模型逐步解決數學問題。例如,當解決一個複雜的算術問題時,模型會展示每一步的計算過程,從而提供更透明和準確的答案.

問題: 您的倉庫有5個托盤的貨物。您購買了2次貨物,每次運送包含3個托盤。現在您有多少個托盤的貨物?

答案:

  1. 起初,您的倉庫有5個托盤的貨物。
  2. 您購買了2次貨物,每次運送包含3個托盤。
  3. 因此,新購貨物的托盤總數為 2次運送×3每次運送的托盤數=6

現在,將新購托盤數加到起初的托盤數上:

5托盤+6托盤=11托盤

所以,您現在倉庫總共有11個托盤的貨物。

  1. Chain – of -thought

 Chain-of-thought (CoT) prompting is a technique used in prompt engineering to enhance the reasoning and problem-solving abilities of large language models (LLMs). Introduced by Wei et al. in 2022, CoT prompting involves structuring the user input in a way that makes the model generate a step-by-step explanation or reasoning process before arriving at the final answer

This method is particularly useful for complex tasks that require intermediate reasoning steps. By breaking down the problem into smaller, manageable parts, CoT prompting enables the model to provide more accurate and reliable answers. For example, in arithmetic problems, the model can show each step of the calculation, leading to a more transparent and understandable solution

.Q Your warehouse has 5 pallets of wights. You purchase 2 more shipments of widghts. Each shipment contains 3 pallets. How many pallets of widgets do you have now ?

Ans “Cot” Let’s break it down step by step:

  1. Initially, your warehouse has 5 pallets of widgets.
  2. You purchase 2 shipments, and each shipment contains 3 pallets.
  3. Therefore, the total number of pallets from the new shipments is \(2 \text{ shipments} \times 3 \text{ pallets per shipment} = 6 \text{ pallets}\).

Now, add the new pallets to the initial number of pallets:

\[ 5 \text{ pallets} + 6 \text{ pallets} = 11 \text{ pallets} \]

So, you have a total of **11 pallets of widgets** in your warehouse now. 📦📦📦

2. 自我一致性提示是一種在提示工程中提高AI模型準確性和可靠性的先進技術,特別是在涉及算術和常識推理的任務中。這種方法由Wang等人在2022年提出,取代了鏈式思維(CoT)提示中使用的簡單貪婪解碼。自我一致性提示不是依賴單一的推理路徑,而是通過少量示例的鏈式思維提示來抽取多個多樣的推理路徑,然後選擇其中最一致的答案。

這種方法通過對同一提示的多個回應進行平均來提高AI的準確性,從而導致更可靠和基於共識的結果。它已被證明能夠提升鏈式思維提示在複雜推理任務中的性能。

自我一致性提示在實踐中有多種應用,特別是在需要高準確性和可靠性的任務中。以下是一些具體的應用範例:

  1. 數學問題解決:自我一致性提示可以通過生成多個輸出來解決數學問題,從而找到最一致的解決方案,這樣可以提高答案的準確性。例如,解決一個數學問題時,AI會生成多個可能的答案,然後選擇最一致的那個[1]
  2. 醫學影像分析:在醫學影像分析中,自我一致性提示可以幫助醫生更準確地發現疾病。例如,對X光片、CT掃描和MRI進行分析,通過多次生成和比較結果來提高診斷的準確性[2]
  3. 計算藥物發現:科學家們可以使用自我一致性提示來發現新藥物。這種方法可以通過多次生成和比較結果來提高藥物發現的效率和準確性[2]
  4. 量子計算中的錯誤校正:在量子計算中,自我一致性提示可以用來校正錯誤,從而提高計算的準確性和可靠性[2]
  5. 法律技術中的自動合同分析:在法律技術中,自我一致性提示可以用來自動分析合同,從而提高合同分析的準確性和效率[2]
  6. 工業物聯網中的預測性維護:在工業物聯網中,自我一致性提示可以用來進行預測性維護,通過多次生成和比較結果來預測設備的故障,從而提高維護的準確性和效率[2]

當然可以!以下是自一致性提示技術的實際範例翻譯成繁體中文:

### 問題:

*你有100顆蘋果,送出了37顆蘋果。你還剩下多少顆蘋果?*

自一致性提示過程:

  1. **生成多個回答:**

回答1:你有 ( 100 – 37 = 63 ) 顆蘋果剩下。

回答2:給出了37顆蘋果後,從100顆蘋果中剩下 ( 100 – 37 = 63 ) 顆。

回答3:如果你有100顆蘋果並送出了37顆,你將剩下 ( 100 – 37 = 63 ) 顆蘋果。

回答4:送出37顆蘋果後,你還有 63 顆蘋果剩下。

回答5:給出37顆蘋果後,你的100顆蘋果中剩下 6 顆。

  1. **識別一致的回答: 大多數回答指示正確答案是63顆蘋果。
  2. **選擇最一致的回答: 基於大多數一致的回答,選擇的答案是:你剩下63顆蘋果。**

在實際操作中,模型會內部生成多個回答,並選擇共識最高的回答。這確保了選擇的答案更可靠且可能正確。

 2. Self-consistency prompting

Self-consistency prompting is an advanced technique in prompt engineering that aims to improve the accuracy and reliability of AI models, particularly in tasks involving arithmetic and commonsense reasoning. Proposed by Wang et al. in 2022, this method replaces the naive greedy decoding used in chain-of-thought (CoT) prompting. Instead of relying on a single reasoning path, self-consistency prompting samples multiple, diverse reasoning paths through few-shot CoT and then selects the most consistent answer among them[1].

This approach enhances AI accuracy by averaging multiple responses to the same prompt, leading to more reliable and consensus-based results[2]. It has shown to boost the performance of CoT prompting on complex reasoning tasks[3].

3. 自動推理和工具使用(Automatic Reasoning and Tool-use, ART)是一個創新的框架,旨在通過使大型語言模型(LLMs)能夠執行多步推理和有效利用外部工具來增強其能力。這種方法由Bhargavi Paranjape及其同事於2023年提出。

ART的工作原理是使用凍結的LLMs自動生成作為程序的中間推理步驟。當面臨新任務時,ART會從任務庫中選擇多步推理和工具使用的示範。在推理過程中,ART可以在需要外部工具時無縫暫停生成,整合其輸出,然後繼續生成。這種方法使模型能夠分解複雜問題,在適當的地方使用工具,並提供詳細、準確的解決方案。

ART在各種基準測試(如BigBench和MMLU)上顯示出顯著的改進,超越了傳統的少量示例提示和自動鏈式思維(CoT)提示。在許多任務上,它的表現也與手工製作的CoT提示相匹配。此外,ART具有可擴展性,允許人類通過糾正任務特定程序中的錯誤或引入新工具來提高性能。

自動推理和工具使用(ART)在實踐中有許多應用,特別是在需要多步推理和外部工具的任務中。以下是一些具體的應用範例:

  1. 數學問題解決:ART可以幫助AI模型逐步解決數學問題,並在需要時使用計算器等外部工具來提高答案的準確性。例如,當解決一個複雜的算術問題時,模型會展示每一步的計算過程,並在需要時使用計算器來驗證結果[1]
  2. 醫學診斷:在醫學領域,ART可以幫助醫生進行診斷。通過逐步分析病人的症狀和檢查結果,AI模型可以提供更準確的診斷建議,並在需要時使用醫學數據庫來查找相關信息[1]
  3. 法律分析:在法律技術中,ART可以用來分析法律案件。模型可以逐步解釋每一個法律條款和相關案例,並在需要時使用法律數據庫來查找相關判例[1]
  4. 編程輔助:ART可以幫助程序員解決編程問題。模型可以逐步解釋每一行代碼的功能和作用,並在需要時使用代碼庫來查找相關代碼片段[1]
  5. 教育輔助:在教育領域,ART可以用來輔助教學。模型可以逐步解釋每一個知識點,並在需要時使用教育資源來提供更多的學習材料[1]

當然!自動推理和工具使用(ART)是一種利用人工智慧自動推理與多種工具協同工作的技術。以下是一個實際範例:

### 範例問題:

你有一個 Excel 檔案包含銷售數據,你需要計算每月的總銷售額、找到最高銷售額的產品,並生成該產品的詳細報告。*

自動推理和工具使用過程:

  1. **讀取並處理數據:**

首先,利用一個工具來讀取 Excel 檔案。

利用推理過程從 Excel 檔案中提取銷售數據,並將數據載入至數據分析工具。

  1. **計算每月的總銷售額:**

使用數據分析工具來計算每月的總銷售額,並生成一個包含每月總銷售額的表格。

  1. **找到最高銷售額的產品:**

利用數據分析工具,識別出銷售額最高的產品。

  1. **生成詳細報告:**

使用報告生成工具,生成包含最高銷售額產品詳細信息的報告,例如產品名稱、每月銷售額、以及銷售趨勢分析等。

### 自動推理和工具使用的示例步驟:

  1. **讀取並處理數據:**

ExcelTool.read(file_path=”sales_data.xlsx”)

  1. **計算每月的總銷售額:**

total_sales_per_month = DataAnalysisTool.calculate_total_sales(data)

  1. **找到最高銷售額的產品:**

top_selling_product = DataAnalysisTool.find_top_selling_product(data)

  1. **生成詳細報告:**

ReportGenerator.generate_report(product=top_selling_product

透過這樣的自動推理和工具使用過程,可以大大提高數據處理和分析的效率,並自動生成所需的報告。

3 . Automatic reasoning and tool use

Automatic Reasoning and Tool-use (ART) is an innovative framework designed to enhance the capabilities of large language models (LLMs) by enabling them to perform multi-step reasoning and utilize external tools effectively. This approach was introduced by Bhargavi Paranjape and colleagues in 2023[1].

ART works by using frozen LLMs to automatically generate intermediate reasoning steps as a program. When faced with a new task, ART selects demonstrations of multi-step reasoning and tool use from a task library. During the reasoning process, ART can seamlessly pause generation whenever external tools are needed, integrate their output, and then resume generation. This method allows the model to decompose complex problems, use tools in appropriate places, and provide detailed, accurate solutions[1][2].

ART has shown significant improvements over traditional few-shot prompting and automatic chain-of-thought (CoT) prompting on various benchmarks, such as BigBench and MMLU. It also matches the performance of hand-crafted CoT prompts on many tasks. Additionally, ART is extensible, allowing humans to improve performance by correcting errors in task-specific programs or incorporating new tools[1][2].

4. 生成知識提示是一種在提示工程中使用的技術,涉及在做出預測之前從語言模型生成相關知識。這種方法旨在提高大型模型的性能,特別是在需要常識推理的任務中。

其工作原理是首先生成與輸入提示相關的知識片段。這些生成的知識片段然後用作模型進行更準確預測的額外上下文。例如,如果輸入提示是「希臘比墨西哥大」,生成的知識可能包括關於希臘和墨西哥大小的具體信息,這有助於模型做出更明智的決策。

這種技術已被證明能夠在各種基準測試中提高模型的性能,而不需要任務特定的監督或結構化知識庫。

當前,生成知識提示在實踐中的應用可以通過以下範例來說明:

假設我們有一個輸入提示:「希臘比墨西哥大」。直接回答這個問題可能會導致錯誤的結論。為了提高準確性,我們可以使用生成知識提示來生成相關的知識片段,然後再做出最終的預測。

  1. 輸入提示:希臘比墨西哥大。
  2. 生成的知識
    • 希臘的面積約為131,957平方公里,而墨西哥的面積約為1,964,375平方公里,這使得墨西哥比希臘大1,389%。
  3. 最終預測:根據生成的知識,墨西哥比希臘大。

這種方法通過生成相關的知識片段來提供額外的上下文,從而幫助模型做出更準確的預測。這樣可以提高模型在常識推理任務中的性能。

### 範例情境:

*你是一名內容創作者,想要撰寫一篇關於喝綠茶的好處的部落格文章。你需要收集有關其健康益處、製備方法和文化意義的資訊。*

### 生成知識提示過程:

  1. **識別主要主題:**

– 綠茶的健康益處

– 製備方法

– 文化意義

  1. **生成每個主題的知識:**

**健康益處:**

– 綠茶富含抗氧化劑,有助於減少炎症。

– 綠茶的定期飲用與改善腦功能有關。

– 它可以通過提高新陳代謝來幫助體重管理。

– 綠茶與降低某些類型癌症風險有關。

 

**製備方法:**

– 綠茶通常通過將茶葉浸泡在熱水中(但不是沸水中)製備,以保留其細膩的味道。

– 標準比例為每杯水約一茶匙的綠茶葉。

– 浸泡時間可以根據個人喜好在2到3分鐘之間變化。

– 綠茶也可以冷飲,通過像往常一樣沖泡然後冷卻。

**文化意義:**

– 在日本,茶道(茶道,sadō)是一種受禪宗影響的傳統儀式,涉及抹茶(綠茶粉)的儀式性製備和飲用。

– 在中國,綠茶已被飲用數千年,是中國文化和醫學的重要組成部分。

– 在其他亞洲國家,如韓國,綠茶也很受歡迎,無論是正式場合還是非正式場合都經常飲用。

– 在西方,綠茶因其健康益處而受到歡迎,常被作為養生產品來推廣。

## 部落格文章範例:

**標題:綠色靈藥:揭開綠茶的神奇**

**引言:**

綠茶,通常被譽為“生命之靈藥”,幾個世紀以來在各種文化中受到珍愛。從其細膩的香氣到多種健康益處,綠茶已成為世界各地受歡迎的飲品。在這篇文章中,我們將探索飲用綠茶的諸多好處、其製備藝術以及豐富的文化意義。

**健康益處:**

綠茶是抗氧化劑的寶庫,這在減少炎症和保護細胞免受損害方面起著重要作用。定期飲用綠茶因其含有的咖啡因和L-茶氨酸而與改善腦功能有關。此外,綠茶還可能通過提高新陳代謝來幫助體重管理,使其成為追求健康生活方式者的受歡迎選擇。研究還表明,飲用綠茶可以降低某些類型癌症的風險,增加其令人印象深刻的健康益處列表。

**製備方法:**

綠茶的細膩味道最好通過將茶葉浸泡在熱水中(但不是沸水中)來保留。理想的比例是每杯水約一茶匙的綠茶葉。根據個人口味,浸泡時間可以在2到3分鐘之間變化。對於喜歡清涼飲品的人來說,可以像往常一樣沖泡綠茶,然後冷卻後享用。

**文化意義:**

在日本,茶道(茶道,sadō)是一種深受禪宗影響的傳統儀式。這個儀式涉及抹茶(綠茶粉)的仔細製備和飲用。在中國,綠茶已成為文化的一部分數千年,並在中國醫學中占有重要地位。綠茶在其他亞洲國家如韓國也很受歡迎,無論是正式場合還是非正式場合都經常飲用。近年來,綠茶因其諸多健康益處在西方受到歡迎,常作為養生產品來推廣。

 

**結論:**

無論您是品味在傳統儀式中的熱綠茶,還是在陽光明媚的日子享用冷飲版本,這種神奇的飲品提供了諸多健康益處和豐富的文化遺產。擁抱這綠色靈藥,探索它能給您的生活帶來的奇妙。

在這個例子中,人工智慧利用生成的知識,基於識別的主要主題,創造出一篇完善的部落格文章。這展示了如何使用生成知識提示技術來產生新的、連貫且有價值的內容。

 

4 Generated Knowledge Prompting

Generated knowledge prompting is a technique in prompt engineering that involves generating relevant knowledge from a language model before making a prediction. This method aims to improve the performance of large-scale models, particularly in tasks that require commonsense reasoning.

The process works by first generating pieces of knowledge related to the input prompt. These generated knowledge pieces are then used as additional context for the model to make more accurate predictions. For example, if the input prompt is “Greece is larger than Mexico,” the generated knowledge might include specific information about the sizes of Greece and Mexico, which helps the model make a more informed decision[1].

This technique has been shown to improve the performance of models on various benchmarks without requiring task-specific supervision or a structured knowledge base[2].

Generated Knowledge Prompting (GKP) is a technique where the AI generates new information based on the knowledge it has, even if it hasn’t been directly trained on that specific question. This involves creating new, accurate, and coherent content from existing knowledge.

### Practical Example:

### Scenario:

*You are a content creator who wants to write a blog post about the benefits of drinking green tea. You need to gather information about its health benefits, preparation methods, and cultural significance.*

### Generated Knowledge Prompting Process:

  1. **Identify Key Topics:**

– Health benefits of green tea.

– Preparation methods.

– Cultural significance.

  1. **Generate Knowledge on Each Topic:**

**Health Benefits:**

Green tea is rich in antioxidants, which can help reduce inflammation.

Regular consumption of green tea has been linked to improved brain function.

It may help in weight management by boosting metabolism.

Green tea has been associated with a lower risk of certain types of cancer.

**Preparation Methods:**

– Green tea is typically prepared by steeping the leaves in hot water, but not boiling water, to preserve its delicate flavor.

– The standard ratio is about one teaspoon of green tea leaves per cup of water.

Steeping time can vary from 2 to 3 minutes, depending on personal preference.

Green tea can also be enjoyed cold by brewing it as usual and then chilling it.

**Cultural Significance:**

In Japan, the tea ceremony (茶道, sadō) is a traditional ritual influenced by Zen Buddhism and involves the ceremonial preparation and consumption of matcha, powdered green tea.

– In China, green tea has been consumed for thousands of years and is an integral part of Chinese culture and medicine.

– Green tea is also popular in other Asian countries such as Korea, where it is enjoyed in both formal and informal settings.

– In the West, green tea has gained popularity for its health benefits and is often marketed as a wellness product.

Blog Post Example:

**Title: The Green Elixir: Unveiling the Wonders of Green Tea**

**Introduction:**

Green tea, often hailed as the “elixir of life,” has been cherished for centuries across various cultures. From its subtle aroma to its myriad health benefits, green tea has become a beloved beverage worldwide. In this post, we will explore the numerous advantages of drinking green tea, the art of its preparation, and its rich cultural significance.

**Health Benefits:**

Green tea is a powerhouse of antioxidants, which play a crucial role in reducing inflammation and protecting cells from damage. Regular consumption of green tea has been linked to enhanced brain function, thanks to its caffeine and L-theanine content. Additionally, green tea may assist in weight management by boosting metabolism, making it a popular choice for those seeking a healthy lifestyle. Studies have also suggested that green tea consumption can lower the risk of certain types of cancer, adding to its impressive list of health benefits.

 

**Preparation Methods:**

The delicate flavor of green tea is best preserved by steeping the leaves in hot, but not boiling water. The ideal ratio is about one teaspoon of green tea leaves per cup of water. Steeping time can vary between 2 to 3 minutes, depending on your taste preference. For those who enjoy a refreshing beverage, green tea can be brewed as usual and then chilled for a cold treat.

**Cultural Significance:**

In Japan, the tea ceremony (茶道, sadō) is a traditional ritual deeply influenced by Zen Buddhism. This ceremony involves the meticulous preparation and consumption of matcha, a powdered form of green tea. In China, green tea has been a part of the culture for thousands of years and holds a significant place in Chinese medicine. Green tea is also enjoyed in other Asian countries such as Korea, where it is consumed in both formal and informal settings. In recent years, green tea has gained popularity in the West, often marketed as a wellness product for its numerous health benefits.

**Conclusion:**

Whether you are savoring a hot cup of green tea in a traditional ceremony or enjoying a chilled version on a sunny day, this remarkable beverage offers a plethora of health benefits and a rich cultural heritage. Embrace the green elixir and discover the wonders it can bring to your life.

In this example, the AI used generated knowledge to create a well-rounded blog post based on the key topics identified. This demonstrates how GKP can be used to produce new, coherent, and valuable content from existing knowledge.