logo
Loading...

ChatGPT Prompt Engineering for Developers - 生成式AI從No-Code到Low-Code開發與應用四部曲 - Cupoy

ChatGPT Prompt Engineering for Developers Settings 設置 Key 的主要方式 設置和大模型通話的函數 Guidelines for Promp...

ChatGPT Prompt Engineering for Developers Settings 設置 Key 的主要方式 設置和大模型通話的函數 Guidelines for Prompting (Prompt 指南心法) 寫出優質 Prompt 的原則 原則1: 明確你的指令 (Write clear and specific instructions) 原則2: 給模型更多的推導機會 (Give the model time to “think”) Iterative Prompt Development (不斷調整我們的 Prompt) Prompt 版本 0 Prompt 版本 1 Prompt 版本 2 Prompt 版本 3 Prompt 迭代小結 Summarizing (LLM 協助摘要) Summarizing 版本 0 Summarizing 方式 Extracting 方式 Iterative Summarizing/ Extracting 方式 LLM 協助摘要小結 Inferring (LLM 推理) 正負面的情緒推理 情感識別列舉推理 情感識別推理 訊息提取的推理 綜合且連續的推理 主題關鍵字推理 主題關鍵字識別推理 LLM 推理小結 Transforming (LLM 輸出轉換) 翻譯任務 語言識別任務 文法檢查任務 文本語調轉換 LLM 輸出轉換小結 Expanding (LLM 擴寫) Chatbots (LLM 聊天機器人應用) LLM 有沒有記憶功能的實驗 OrderBot 的例子 總結部分 Settings ( 剛剛已經講過了XD ) 設置 Key 的主要方式 方法一 - 直接設置方法二 - 載入環境文件 設置和大模型通話的函數 設置 key import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.getenv('OPENAI_API_KEY') 設置調用回覆函數 def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) return response.choices[0].message["content"] Guidelines for Prompting 寫出優質 Prompt 的原則 原則1: 明確你的指令 (Write clear and specific instructions) Tactic 1: 使用合適的分隔符輔助 (Use delimiters) Tactic 2: 要求其返回我們要的格式輸出 (Ask for structured output) Tactic 3: 確認指令能否正常執行 (Check whether conditions are satisfied) Tactic 4: 舉個例子 (Few-shot prompting) 原則2: 給模型更多的推導機會 (Give the model time to “think”) Tactic 1: 給模型提供具體的步驟 (Specify the steps required to complete a task) Tactic 2: 請自己解題過後確認正確與否 (Instruct the model to work out its own solution before rushing to a conclusion) 原則1: 明確你的指令 (Write clear and specific instructions) 注意 : 清楚不等於短 Tactic 1: 使用合適的分隔符輔助 (Use delimiters) 用來包裝指令的手段,使得後續我們可以對包起來的這些內容做控制、分類、方便地下決策指令 主要有這幾種常見的包裝形式 Triple quotes: """ Triple backticks: ``` Triple dashes: --- Angle brackets: < > XML tags: 吳教授課程中的例子是,使用 get_completion (見 設置調用回覆函數 ) 做調用 Explain text = f""" You should express what you want a model to do by \ providing instructions that are as clear and \ specific as you can possibly make them. \ This will guide the model towards the desired output, \ and reduce the chances of receiving irrelevant \ or incorrect responses. Don't confuse writing a \ clear prompt with writing a short prompt. \ In many cases, longer prompts provide more clarity \ and context for the model, which can lead to \ more detailed and relevant outputs. """ prompt = f""" Summarize the text delimited by triple backticks \ into a single sentence. ```{text}``` """ # 調用 response = get_completion(prompt) print(response) 模板化 這邊有一個很重要的想法: 模板化 包裝能讓 Prompt 更豐富,能讓 LLM 接收到的指令更精確、豐富,能課制化調整風格。 通過疊加嵌套,能提高指令的覆用、不用重新設計 模板化結構 Tactic 2: 要求其返回我們要的格式輸出 (Ask for structured output) HTML, JSON 可以方便我們在下游程序調用時,方便使用或轉換;在不同的寫作場景,能夠比較無縫的切換。 這是一種讓 LLM 連接不同工具或場景的技巧。 prompt = f""" Generate a list of three made-up book titles along \ with their authors and genres. Provide them in JSON format with the following keys: book_id, title, author, genre. """ response = get_completion(prompt) print(response) 結構化輸出 結構化輸出是一個讓模型掌握工具的重要橋梁, 參考例子取自 Toolformer 中,研究人員嘗試訓練模型使用 API Call 的方式樣例。 (有興趣的人可以參考連結中的 paper) 例子: 讓 LLM 使用計算機的樣子 Tactic 3: 確認指令能否正常執行 (Check whether conditions are satisfied) Check assumptions required to do the task 有時候,我們要求模型推理的任務可能會有 不能推理的狀況 產生,這時候如果模型硬要去推理,硬得出一個結果的話,這往往是不對的、奇怪的。 不能推理的狀況 是什麼? 「輸入的條件不滿足」執行指令的假設邊緣情況 執行指令的假設是什麼? 比如說,我希望模型幫我將文本中的 英文 翻成 中文,但是我給它的文本輸入只有 德文,這,就是很明顯與執行指令前提不符合的例子 如果 我們對於輸入文本不嚴格規範 寫 Prompt 時,我們寫出奇怪的問題或要求時 ,就很容易讓模型陷入 與執行指令前提不符合的推論 ,而容易出現奇怪的推理輸出結果。 因此,我們可以 對模型要求 對自己要求 因此,關鍵在這段 prompt 中 (我請你列出文本中的關鍵步驟,但如果文本中沒有步驟訊息可言,不要輸出任何信息) If the text does not contain a sequence of instructions, \ then simply write \"No steps provided.\" 我們的 prompt 某種意義上,起到這樣的作用 if (check(Question) == True) and (check(Conditions) == True): # 依序執行 Prompt 中提到的指令,將結果加入到返回結果 returns 中 for instruction in instructions: returns.append(do_instruction(instruction)) else: print("Warning...\n" "I can\'t help you because either the Question or the Condition has an issue. \n" "Unpredictable responses may occur." ) 例一 (text_1 描述一段有明顯操作步驟的過程) text_1 = f""" Making a cup of tea is easy! First, you need to get some \ water boiling. While that's happening, \ grab a cup and put a tea bag in it. Once the water is \ hot enough, just pour it over the tea bag. \ Let it sit for a bit so the tea can steep. After a \ few minutes, take out the tea bag. If you \ like, you can add some sugar or milk to taste. \ And that's it! You've got yourself a delicious \ cup of tea to enjoy. """ prompt = f""" You will be provided with text delimited by triple quotes. If it contains a sequence of instructions, \ re-write those instructions in the following format: Step 1 - ... Step 2 - … … Step N - … If the text does not contain a sequence of instructions, \ then simply write \"No steps provided.\" \"\"\"{text_1}\"\"\" """ response = get_completion(prompt) print("Completion for Text 1:") print(response) 例二 (text_2 描述一段景色,但是似乎沒有明顯、能夠表示操作步驟的過程,那還要寫出步驟嗎?) Explain # Tactic 3-2 text_2 = f""" The sun is shining brightly today, and the birds are \ singing. It's a beautiful day to go for a \ walk in the park. The flowers are blooming, and the \ trees are swaying gently in the breeze. People \ are out and about, enjoying the lovely weather. \ Some are having picnics, while others are playing \ games or simply relaxing on the grass. It's a \ perfect day to spend time outdoors and appreciate the \ beauty of nature. """ prompt = f""" You will be provided with text delimited by triple quotes. If it contains a sequence of instructions, \ re-write those instructions in the following format: Step 1 - ... Step 2 - … … Step N - … If the text does not contain a sequence of instructions, \ then simply write \"No steps provided.\" \"\"\"{text_2}\"\"\" """ response = get_completion(prompt) print("Completion for Text 2:") print(response) Tactic 4: 舉個例子 (Few-shot prompting) Give successful examples of completing tasks, then ask model to perform the task 給模型一些具體例子的 prompting ,也蠻常會使用到 Explain prompt = f""" Your task is to answer in a consistent style. ###draft_code_symbol_lessthen###child>: Teach me about patience. ###draft_code_symbol_lessthen###grandparent>: The river that carves the deepest \ valley flows from a modest spring; the \ grandest symphony originates from a single note; \ the most intricate tapestry begins with a solitary thread. ###draft_code_symbol_lessthen###child>: Teach me about resilience. """ response = get_completion(prompt) print(response) 原則2: 給模型更多的推導機會 (Give the model time to “think”) Tactic 1: 給模型提供具體的步驟 (Specify the steps required to complete a task) 如果對於一項較為複雜的任務,我們已經有一套合理的處理流程了,那這時候,我們可以給出每一步的步驟,使模型能順著我們的方法做處理、推理。 關鍵是這部分的 prompt,明確指出按照這些步驟去生成相應的結果 Perform the following actions: 1 - Summarize the following text delimited by triple \ backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following \ keys: french_summary, num_names. text = f""" In a charming village, siblings Jack and Jill set out on \ a quest to fetch water from a hilltop \ well. As they climbed, singing joyfully, misfortune \ struck—Jack tripped on a stone and tumbled \ down the hill, with Jill following suit. \ Though slightly battered, the pair returned home to \ comforting embraces. Despite the mishap, \ their adventurous spirits remained undimmed, and they \ continued exploring with delight. """ # example 1 prompt_1 = f""" Perform the following actions: 1 - Summarize the following text delimited by triple \ backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following \ keys: french_summary, num_names. Separate your answers with line breaks. Text: ```{text}``` """ response = get_completion(prompt_1) print("Completion for prompt 1:") print(response) 這邊,我們一樣可以根據Tactic 2: 要求其返回我們要的格式輸出的原則,加上Use the following format這類的 prompt 去導出更規整的輸出結果。 prompt_2 = f""" Your task is to perform the following actions: 1 - Summarize the following text delimited by ###draft_code_symbol_lessthen###> with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following keys: french_summary, num_names. Use the following format: Text: ###draft_code_symbol_lessthen###text to summarize> Summary: ###draft_code_symbol_lessthen###summary> Translation: ###draft_code_symbol_lessthen###summary translation> Names: ###draft_code_symbol_lessthen###list of names in summary> Output JSON: ###draft_code_symbol_lessthen###json with summary and num_names> Text: ###draft_code_symbol_lessthen###{text}> """ response = get_completion(prompt_2) print("Completion for prompt 2:") print(response) Tactic 2: 請自己解題過後確認正確與否 (Instruct the model to work out its own solution before rushing to a conclusion) 我們要怎麼知道模型在推論的過程中,有沒有直接根據結論去判斷對錯,有沒有明顯的誤區/理解錯誤? 可以使用什麼方法去確認這些事呢? 請模型自己列出思考過程,正是一個好方法。 另一方面,列出思考過程後,模型對自己已經輸出的東西、論點也能更加清晰,因此也比較不容易出錯。 我們可以測試一下,這是直接給答案的結果, prompt = f""" Determine if the student's solution is correct or not. Question: I'm building a solar power installation and I need \ help working out the financials. - Land costs $100 / square foot - I can buy solar panels for $250 / square foot - I negotiated a contract for maintenance that will cost \ me a flat $100k per year, and an additional $10 / square \ foot What is the total cost for the first year of operations as a function of the number of square feet. Student's Solution: Let x be the size of the installation in square feet. Costs: 1. Land cost: 100x 2. Solar panel cost: 250x 3. Maintenance cost: 100,000 + 100x Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000 """ response = get_completion(prompt) print(response) 注意,這個回覆的答案很可能是 不正確的 不過,我們可以用 prompt 讓他仔細一步步想想看,列出思考過程後,再讓模型和這些輸出的訊息做比較、推論。 主要重點是這幾行, Explain To solve the problem do the following: - First, work out your own solution to the problem including the final total. - Then compare your solution to the student's solution \ and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. 看看有沒有辦法去避免這樣的狀況,並且老師也根據 Tactic 2: 要求其返回我們要的格式輸出 的原則,讓模型的思考過程更清楚明確 … prompt = f""" Your task is to determine if the student's solution \ is correct or not. To solve the problem do the following: - First, work out your own solution to the problem including the final total. - Then compare your solution to the student's solution \ and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. ​ Use the following format: Question: ``` question here ``` Student's solution: ``` student's solution here ``` Actual solution: ``` steps to work out the solution and your solution here ``` Is the student's solution the same as actual solution \ just calculated: ``` yes or no ``` Student grade: ``` correct or incorrect ``` ​ Question: ``` I'm building a solar power installation and I need help \ working out the financials. - Land costs $100 / square foot - I can buy solar panels for $250 / square foot - I negotiated a contract for maintenance that will cost \ me a flat $100k per year, and an additional $10 / square \ foot What is the total cost for the first year of operations \ as a function of the number of square feet. ``` Student's solution: ``` Let x be the size of the installation in square feet. Costs: 1. Land cost: 100x 2. Solar panel cost: 250x 3. Maintenance cost: 100,000 + 100x Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000 ``` Actual solution: """ response = get_completion(prompt) print(response) Iterative Prompt Development (不斷調整我們的 Prompt) 本節主要介紹的是如何一步步調整 prompt 的實用技巧。 當我們一開始下的 prompt 雖然還可以,而模型可能回答的也很詳盡,但就是在一些細節上,模型的回答難以滿足我們的輸出要求。 此時,要怎麼對 prompt 做調整呢? 這正是本節要來探討的方向 (並且一步步點出如何迭代的方法) 一開始,這是我們的原始版本 Prompt 版本 0 fact sheet chair 的相關資訊在這邊 fact_sheet_chair prompt 在這邊 Explain prompt = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Technical specifications: ```{fact_sheet_chair}``` """ response = get_completion(prompt) print(response) 但是輸出完我們發現,他答得太長了~ Prompt 版本 1 Issue 1: The text is too long 問題: 模型回答的太長了,怎麼辦? 可以加入一些 prompt 將你(模型)的回答限縮在 多少字以內、幾句話、幾個段落 因此可以, 變化一 Explain prompt = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Use at most 50 words. Technical specifications: ```{fact_sheet_chair}``` """ response = get_completion(prompt) print(response) 請以三句話總結你(模型)的回答 變化二 Explain prompt = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Use at most 3 sentences. Technical specifications: ```{fact_sheet_chair}``` """ response = get_completion(prompt) print(response) Prompt 版本 2 Issue 2: Text focuses on the wrong details 問題: 我們想讓模型關注的地方可能有點不清楚,怎麼辦? 我們的 prompt 可能沒有強調某些點的輸出,因此被模型所忽略。因此,可以再加上一些 prompt 使模型聚焦在某一方面,去對流程做調整 老師用的這個例子,比如說,他想讓模型關注技術細節多一些,所以又加入了這幾行的 prompt: The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. 因此變成… 接著,老師希望模型可以在描述的尾部,能都加上技術規範中規定的 7 個字元產品ID。 因此,老師又加了這幾行 prompt: At the end of the description, include every 7-character Product ID in the technical specification. 因此迭代成 … Explain prompt = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. Use at most 3 sentences. Technical specifications: ```{fact_sheet_chair}``` """ response = get_completion(prompt) print(response) Explain prompt = f""" Your task is to help a marketing team create adescription for a retail website of a product basedon a technical fact sheet.Write a product description based on the informationprovided in the technical specifications delimited bytriple backticks.The description is intended for furniture retailers,so should be technical in nature and focus on thematerials the product is constructed from.At the end of the description, include every 7-characterProduct ID in the technical specification.Use at most 3 sentences.Technical specifications: ```{fact_sheet_chair}```""" response = get_completion(prompt)print(response) Prompt 版本 3 Description needs a table of dimensions 問題: 如果我們想要求模型將這段輸出轉成 某一種格式 的數據,可以嗎? 答案是可以的,這一步,老師就示範了如何將內容調整好的文字部分,轉換(翻譯)成其他結構化的格式,比如說 HTML 格式、json 格式也可以。 在這裡老師希望模型將這些資料以 HTML 的標籤方式輸出,並且再加入一個表格元素去描述 產品名 和 產品尺寸 的關係,訂定相關標題。 因此,老師加入了這幾行 prompt, After the description, include a table that gives the product's dimensions. The table should have two columns. In the first column include the name of the dimension. In the second column include the measurements in inches only. Give the table the title 'Product Dimensions'. Format everything as HTML that can be used in a website. Place the description in a ###draft_code_symbol_lessthen###div> element. 因此可以迭代成 prompt = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. At the end of the description, include every 7-character Product ID in the technical specification. After the description, include a table that gives the product's dimensions. The table should have two columns. In the first column include the name of the dimension. In the second column include the measurements in inches only. Give the table the title 'Product Dimensions'. Format everything as HTML that can be used in a website. Place the description in a ###draft_code_symbol_lessthen###div> element. Technical specifications: ```{fact_sheet_chair}``` """ response = get_completion(prompt) print(response) 最後,我們可以使用HTML 展示套件,看看生成的效果! from IPython.display import display, HTML display(HTML(response)) Prompt 迭代小結 迭代! 可以控制模型回答的長短 可以讓模型關注我們想關注的地方 可以嘗試要求模型將這段輸出轉成 某一種格式 的數據 Summarizing (LLM 協助摘要) 讓 LLM 幫我們做摘要 (Summarizing) 是一個很常見、需求量大的任務。 對於某些較長的文本內容,我們往往只想知道它的結論、重點,可以節省一些閱讀時間。 那麼這一小節,就是來說明,如何讓模型更好地幫我們摘要文本內容,有什麼技巧或方法呢? Summarizing 版本 0 這部分我們顯示下,沒有經過任何修飾的總結版本。 這部份是我們的資訊回顧 review prod_review 我們一開始的 prompt 在這邊 prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site. Summarize the review below, delimited by triple backticks, in at most 30 words. Review: ```{prod_review}``` """ response = get_completion(prompt) print(response) Summarizing 方式 這邊,我們可以重點關注某個類型的訊息,對他們做總結 這邊的例子,吳恩達希望模型把重點放在 shipping 和 delivery 並對這部分做總結,於是加入這樣的 prompt, ... to give feedback to the \ Shipping deparmtment. ..., and focusing on any aspects \ that mention shipping and delivery of the product.  於是,變成了這樣, prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ Shipping deparmtment. ​ Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that mention shipping and delivery of the product. ​ Review: ```{prod_review}``` """ ​ response = get_completion(prompt) print(response) 換成其他項的關注也是一樣,可以這樣, prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ pricing deparmtment, responsible for determining the \ price of the product. Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that are relevant to the price and perceived value. Review: ```{prod_review}``` """ response = get_completion(prompt) 16 print(response) Extracting 方式 不過,Summarizing 的方式可能會生成出其他我們不是很想關心的訊息,如果要做到 簡潔、精準 的回覆,我們可以試試 提取重點 的方式。 Try “extract” instead of “summarize”. ... extract the information relevant to shipping and \ delivery. Limit to 30 words. 因此 prompt 可以寫成這樣, prompt = f""" Your task is to extract relevant information from \ a product review from an ecommerce site to give \ feedback to the Shipping department. ​ From the review below, delimited by triple quotes \ extract the information relevant to shipping and \ delivery. Limit to 30 words. ​ Review: ```{prod_review}``` """ ​ response = get_completion(prompt) print(response) Iterative Summarizing/ Extracting 方式 如果我們有很多上下文需要去總結或提取,可以用 loop 串起來,分別做總結。 只要控制好輸出的風格、總結/提取、字數/段落/文本限制、格式,基本上都可以一步步得到想要的結果。 這邊有很多篇需要 總結 的文本, review_2、review_3、review_4 然後,我們就將這些文本統合起來,用 for 循環依序執行, LLM 協助摘要小結 我們用到的摘要方法,主要有兩大類型 Summarizing 方式 check 問全文總結 問某些細節部分的總結 Extracting 方式 check 問某些細節、內容的提取 如果我們需要 總結/提取 很多部分的文本訊息,我們可以用循環的方式,得到每一段的摘要結果。 Iterative Summarizing/ Extracting 方式 check Inferring (LLM 推理) 經過大量預訓練任務 + 微調任務的 LLM ,其實已經具有應對 NLP 各項任務的能力。 LLM 可以利用它 Sequence to Sequence 的架構特性,去應對各項下游的 NLP 具體任務。 什麼具體任務呢?如何透過 Prompt 的形式,讓模型執行某類具體任務? 這一小節,主要就是要回答這兩個問題,並體驗下 LLM 的巨大泛用能力。 lamp 的資訊回顧 這部份是我們的資訊回顧 review lamp_review 正負面的情緒推理 Sentiment (positive/negative) 這邊,我們使用 lamp_review 的資訊內容,讓 LLM 幫我們做 正面 or 負面的情感分類 (2 分類問題)。 (check) prompt 在這邊。 prompt = f""" What is the sentiment of the following product review, which is delimited with triple backticks? ​ Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) 模型回覆一段話,不夠簡潔,我們也可以直接讓它向我們輸出 positive 或 negative 即可。 prompt = f""" What is the sentiment of the following product review, which is delimited with triple backticks? Give your answer as a single word, either "positive" \ or "negative". Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) Question 情感識別列舉推理 Identify types of emotions 這邊,我們使用 lamp_review 的資訊內容,讓 LLM 幫我們做情感識別,並列舉這些情感詞彙。 (check) (類似多二分類問題) prompt = f""" Identify a list of emotions that the writer of the \ following review is expressing. Include no more than \ five items in the list. Format your answer as a list of \ lower-case words separated by commas. Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) 情感識別推理 Identify anger 這邊,我們使用 lamp_review 的資訊內容,讓 LLM 幫我們識別作者是否有憤怒的情緒,並且做是否的回答。 (check) (類似二分類問題) prompt = f""" Is the writer of the following review expressing anger?\ The review is delimited with triple backticks. \ Give your answer as either yes or no. ​ Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) 訊息提取的推理 Extract product and company name from customer reviews 這邊是在說明 LLM 的訊息提取任務使用,更多的可以看上一節的內容 (見 Summarizing (LLM 協助摘要)、LLM 協助摘要小結 ) 這邊,我們使用 lamp_review 的資訊內容,讓 LLM 幫我們提取文本中,關於產品和公司的訊息,並做盡可能短的回覆。 (check) prompt = f""" Identify the following items from the review text: - Item purchased by reviewer - Company that made the item ​ The review is delimited with triple backticks. \ Format your response as a JSON object with \ "Item" and "Brand" as the keys. If the information isn't present, use "unknown" \ as the value. Make your response as short as possible. Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) 綜合且連續的推理 Doing multiple tasks at once 這邊,我們使用 lamp_review 的資訊內容,讓 LLM 幫我們對這節提過的所有任務,依次完成推理。 (check) prompt = f""" Identify the following items from the review text: - Sentiment (positive or negative) - Is the reviewer expressing anger? (true or false) - Item purchased by reviewer - Company that made the item ​ The review is delimited with triple backticks. \ Format your response as a JSON object with \ "Sentiment", "Anger", "Item" and "Brand" as the keys. If the information isn't present, use "unknown" \ as the value. Make your response as short as possible. Format the Anger value as a boolean. ​ Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response) 主題關鍵字推理 Inferring topics 故事文本資訊 這是我們要讓模型推論的故事文本 story 我們可以請模型列出文中提到的 5 個主題關鍵字,並用某個格式輸出出來 prompt = f""" Determine five topics that are being discussed in the \ following text, which is delimited by triple backticks. ​ Make each item one or two words long. ​ Format your response as a list of items separated by commas. ​ Text sample: '''{story}''' """ response = get_completion(prompt) print(response) response.split(sep=',') 主題關鍵字識別推理 這邊,我們使用 story 的資訊內容。 (check) 我們可以自己列出我們感興趣的 5 個主題關鍵字字表,請模型判斷文章中有沒有包含這些關鍵字。 如果有,請模型幫我們標記為 1(True) ;如果沒有,則標記為 0(False)。 topic_list = [ "nasa", "local government", "engineering", "employee satisfaction", "federal government" ] ​ prompt = f""" Determine whether each item in the following list of \ topics is a topic in the text below, which is delimited with triple backticks. ​ Give your answer as list with 0 or 1 for each topic.\ ​ List of topics: {", ".join(topic_list)} ​ Text sample: '''{story}''' """ response = get_completion(prompt) print(response) 最後,我們可以透過這種方式,讓擷取新訊息的動作,變得自動化起來 topic_dict = { i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n') } if topic_dict['nasa'] == 1: print("ALERT: New NASA story!") def getText(title: str) -> str: # 依照標題,讀取相應的檔案文本,並返回文本 return text ​ topic_list = [ ..., ..., ... ] ​ def topicCheck(topic_list: list[str], specific_topic: str) -> list[str]: ​ selected_titles = [] ​ for title in titles: text: str = getText(title) topics_check: dict[str, bool] = getOutput(text, topic_list) ​ if topics_check[specific_topic] == True: # do something selected_titles.append(title) else: # pass or do something ​ return selected_titles LLM 推理小結 Transforming (LLM 輸出轉換) 這一小節,我們主要探討 LLM 語言屬性的相關任務,主要有幾個類別: 文本翻譯任務(Translation)、文法錯誤檢查(spelling and grammar checking)、語調轉換任務(tone adjustment)、格式轉換任務(format conversion) 因為影片中的 chatGPT 使用許多語言的文本數據去訓練,因此能夠識別多種語言,並能相互翻譯轉換。不過這一步要看我們做出來的 LLM 有沒有支持到這些語料的轉換和訓練,不過像這樣的大模型,對於普遍使用到的語言,翻譯能力還是很不錯的。 翻譯任務 實驗1: 將某個語言的文本轉成另一個語言的文本 Explain prompt = f""" Translate the following English text to Spanish: \ ```Hi, I would like to order a blender``` """ response = get_completion(prompt) print(response) 輸出為: Hola, me gustaría pedir una licuadora 實驗6: 數據結構間的轉換 可以和之前的格式化輸出心法是一樣的,可以見 check 這個例子將 json 格式轉換成 HTML 標籤的格式 Explain data_json = { "resturant employees" :[ {"name":"Shyam", "email":"shyamjaiswal@gmail.com"}, {"name":"Bob", "email":"bob32@gmail.com"}, {"name":"Jai", "email":"jai87@gmail.com"}]} prompt = f"""Translate the following python dictionary from JSON to an HTML \table with column headers and title: {data_json}""" response = get_completion(prompt)print(response)# 顯示 HTML 格式的數據輸出from IPython.display import display, Markdown, Latex, HTML, JSONdisplay(HTML(response)) 語言識別任務 實驗2: 請模型告訴我們某個文本是什麼語言 Explain prompt = f"""Tell me which language this is:```Combien coûte le lampadaire?```""" response = get_completion(prompt)print(response) 輸出為: The language of the text is French. 文法檢查任務 實驗7: 請模型檢查我們提供的文本,是否出現文法錯誤 Explain text = [ "The girl with the black and white puppies have a ball.", # The girl has a ball. "Yolanda has her notebook.", # ok "Its going to be a long day. Does the car need it’s oil changed?", # Homonyms "Their goes my freedom. There going to bring they’re suitcases.", # Homonyms "Your going to need you’re notebook.", # Homonyms "That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms "This phrase is to cherck chatGPT for speling abilitty" # spelling]for t in text: prompt = f"""Proofread and correct the following text and rewrite the corrected version. If you don't find and errors, just say "No errors found". Don't use any punctuation around the text: ``` {t}```""" response = get_completion(prompt) print(response) 實驗8: 校對一大段文字 Explain text = f"""Got this for my daughter for her birthday cuz she keeps taking \mine from my room. Yes, adults also like pandas too. She takes \it everywhere with her, and it's super soft and cute. One of the \ears is a bit lower than the other, and I don't think that was \designed to be asymmetrical. It's a bit small for what I paid for it \though. I think there might be other options that are bigger for \the same price. It arrived a day earlier than expected, so I got \to play with it myself before I gave it to my daughter.""" prompt = f"proofread and correct this review: ```{text}```" response = get_completion(prompt)print(response) 如果我們想看看到底哪裡有錯誤,我們可以用這個 Markdown 的方式顯示被模型修改前後的狀態,不一樣的地方在哪裡, from redlines import Redlines diff = Redlines(text, response) display(Markdown(diff.output_markdown)) 實驗9: 校對 + 風格規範轉換 Explain prompt = f"""proofread and correct this review. Make it more compelling.Ensure it follows APA style guide and targets an advanced reader.Output in markdown format.Text: ```{text}```""" response = get_completion(prompt)display(Markdown(response)) 文本語調轉換 實驗3: 請模型將我們輸入的文本轉成不同語言/語調的內容 角色語調的轉換 Explain prompt = f"""Translate the following text to French and Spanishand English pirate: \```I want to order a basketball```""" response = get_completion(prompt)print(response) 輸出為: The translations of the text are: French: Je veux commander un ballon de basket Spanish: Quiero pedir un balón de baloncesto English pirate: I be needin' t' order a basketball Question 實驗4: 讓輸入的文字變得正式/不正式 正式/不正式的語調轉換 Explain prompt = f"""Translate the following text to Spanish in both the \formal and informal forms:'Would you like to order a pillow?'""" response = get_completion(prompt)print(response) 輸出為: The Spanish translations of the text in both the formal and informal forms are: Formal: ¿Le gustaría pedir una almohada? Informal: ¿Te gustaría pedir una almohada? 實驗5: 語境和格式層面的轉換 Explain prompt = f"""Translate the following from slang to a business letter:'Dude, This is Joe, check out this spec on this standing lamp.'""" response = get_completion(prompt)print(response) 輸出為: Explain Dear Sir or Madam,This is Joe, writing to inform you about the specifications of this standing lamp. Please find the attached document that details the features, dimensions, and price of this product. I hope you will find it suitable for your needs and preferences.Thank you for your time and attention.Sincerely, Joe LLM 輸出轉換小結 翻譯任務 語言識別任務 文法檢查任務 文本語調轉換 Expanding (LLM 擴寫) 這一小節,我們主要探討 LLM 的擴寫功能,通常當我們想要讓模型… 根據已知訊息,生成下個部分的文本 腦力激盪,讓模型生成出多個版本的結果 由少數條件設定開始,直接生成某段的內容的時候,我們會用到 擴寫功能 這邊,老師也提到了 temperature 在 LLM 當中起到的作用 (模型輸出的隨機性設定)。 temperature 越接近 0 ,隨機性越小(總是輸出當前模型認定最好的答案); 反之亦然。 review 部分的文本 review 那我們根據 review 中的 sentiment 和內容(用戶信件),生成對用戶的、專業的、有禮貌的回信。 Explain prompt = f"""You are a customer service AI assistant.Your task is to send an email reply to a valued customer.Given the customer email delimited by ```, \Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for \their review.If the sentiment is negative, apologize and suggest that \they can reach out to customer service.Make sure to use specific details from the review.Write in a concise and professional tone. Sign the email as `AI customer agent`. Customer review: ```{review}``` Review sentiment: {sentiment}""" response = get_completion(prompt,temperature=0.7 # 這邊,我們可以對模型回覆的隨機性做調整)print(response) 最後,我們需要將這段 AI 回覆的電子郵件做 AI 屬名,表明此為 AI 生成的文本,因此加上這段 prompt Sign the email as `AI customer agent`. Chatbots (LLM 聊天機器人應用) 這一小節,我們主要探討如何使用 LLM 做為我們客製化的聊天機器人,作為 prompt 的課程例子。 在這一節中,需要強調的是,LLM 接收到的 message 有這樣的角色設定: “user” 角色: 這一段記錄著 用戶輸入的訊息 “assistant” 角色: 這一段記錄著 模型輸出的訊息 “system” 角色: 這一段記錄著 系統設定的模型指令訊息,這部分會在用戶和模型交互的同時被不斷地調用。 對話訊息會怎麼被儲存? 在我們與 LLM 對話的過程中,這些輸入過的訊息是不會被模型記下的,而是會在每一次輸入時被 message 一條條加入儲存,以供應下游問題解答使用。 message 的保存方式 因此,模型便能用這一部分的歷史紀錄,來給出相應的答案,讓我們覺得模型似乎有記憶能力的錯覺,事實上並沒有。 LLM 有沒有記憶功能的實驗 不給之前的輸入 直接輸入當前的 message 而已 messages =  [   {'role':'system', 'content':'You are friendly chatbot.'},     {'role':'user', 'content':'Hi, my name is Isa'}  ] response = get_completion_from_messages(messages, temperature=1) print(response) 接著,再輸入, messages =  [   {'role':'system', 'content':'You are friendly chatbot.'},     {'role':'user', 'content':'Yes,  can you remind me, What is my name?'}  ] response = get_completion_from_messages(messages, temperature=1) print(response) 和之前的敘述一起輸入 輸入當前 + 以前的 message 對話訊息 Explain messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'}, {'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \Is there anything I can help you with today?"}, {'role':'user', 'content':'Yes, you can remind me, What is my name?'} ] response = get_completion_from_messages(messages, temperature=1)print(response) OrderBot 的例子 設置 key 和 回覆函數 Explain # API Key 設置 import openai import os from dotenv import load_dotenv, find_dotenv_ = load_dotenv(find_dotenv()) # read local .env fileopenai.api_key = os.getenv('OPENAI_API_KEY') # 回覆函數 def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) return response.choices[0].message["content"] def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, # this is the degree of randomness of the model's output )# print(str(response.choices[0].message)) return response.choices[0].message["content"] 這部分是用來專門管理 message 狀態的函數 Explain def collect_messages(_): prompt = inp.value_input inp.value = '' context.append({'role':'user', 'content':f"{prompt}"}) response = get_completion_from_messages(context) context.append({'role':'assistant', 'content':f"{response}"}) panels.append( pn.Row('User:', pn.pane.Markdown(prompt, width=600))) panels.append( pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'}))) return pn.Column(*panels) 這邊是菜單訊息 context Explain import panel as pn # GUIpn.extension()panels = [] # collect displayinp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…') button_conversation = pn.widgets.Button(name="Chat!") interactive_conversation = pn.bind(collect_messages, button_conversation)dashboard = pn.Column( inp, pn.Row(button_conversation), pn.panel(interactive_conversation, loading_indicator=True, height=300),)dashboard 使用這個 dashboard 點餐和對話,最後結束點餐和結算 總結部分 這一系列的課程,我們學習到 : Principles (Prompt 心法): Write clear and specific instructions Give the model time to “think” Iterative prompt development (迭代調整 Prompt 的方法) Capabilities (LLM 能完成的任務) Summarizing Inferring Transforming Expanding Building a Chatbot (搭建一個簡易聊天機器人)