# 【學習筆記】ChatGPT Prompt Engineering 提示工程 課程: DeepLearning.AI 的 [ChatGPT Prompt Engineering for Developers](https://www.coursera.org/projects/chatgpt-prompt-engineering-for-developers-project) ## Intro LLM 可分為兩種類型: - Base LLM:根據訓練資料預測下一個下一個詞彙 - Intruction tuned LLM:微調於特定指令,可以很好地遵循指令 提示工程可以讓 Intruction tuned LLM 產生更符合我們需求的結果。 ## Guidelines for Prompting ### 提示原則 #### 原則 1:清楚的指令 清楚不代表「短」,有時候足夠長的指令更能夠明確表達需求。 技巧 1:使用分隔符,例如`""`, `<>`, ` ``` ` 等,將特定文字區分開來,讓模型知道是分開的段落,可以避免 prompt injections。 > prompt injection:text 當中有和指令衝突的文字,使模型受影響 ``` Summarize the text delimited by triple backticks \ into a single sentence. ```{text}``` ``` 技巧 2:要求結構化的輸出,例如 HTML、JSON ``` 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. ``` 技巧 3:讓模型確認特定條件是否成立 ``` 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}\"\"\" ``` 技巧 4:Few-shot prompting,給模型成功完成任務的例子,再讓模型執行任務 ``` Your task is to answer in a consistent style. <child>: Teach me about patience. <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. <child>: Teach me about resilience. ``` #### 原則 2:給模型「思考」時間 要求模型思考久一些,不要直接跳到結論 技巧 1:指定完成任務需要的步驟 ``` 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}``` ``` 技巧 2:要求模型在得到結論前先自行計算答案 假設讓模型判斷某個學生數學題目是否計算正確,應該先要求模型自行計算一次,再來判斷學生的答案,而不是直接得到學生答案是否正確的結論。 ### Hallucinations 幻覺 模型可能會產生煞有其事但事實上不正確的敘述。為了避免這種問題,我們可以在提示時先行提供一些相關資訊,再請模型根據提供的資訊進行回答。 ## Iterative Prompt Development ![截圖 2025-06-06 晚上10.05.07](https://hackmd.io/_uploads/S1CqH_lmxx.png =500x) Prompting 的週期就好比機器學習週期,最一開始我們可能會有一些想法,寫一些 prompt 讓模型執行,如果發現結果不符合需求,我們可以分析原因,然後再使用修正後的 prompt 執行。 迭代過程 - 嘗試 - 分析哪裡導致結果不如預期 - 讓指示更清楚,給模型更多時間思考 ## Tasks using Prompt ### Summarizing LLM 可用來進行文章**摘要**,並且我們也可以利用加上額外條件的方式,讓摘要符合我們的需求。 例子: ``` prod_review = """ Got this panda plush toy for my daughter's birthday, \ who loves it and takes it everywhere. It's soft and \ super cute, and its face has a friendly look. It's \ a bit small for what I paid 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 her. """ ``` 限制字數: ``` 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}``` ``` :::success Soft, cute panda plush toy loved by daughter, but smaller than expected for the price. Arrived early, friendly face. ::: 要求專注在特定重點: ``` 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}``` ``` :::success The panda plush toy is loved for its softness and cuteness, but some customers feel it's a bit small for the price. ::: ### Inferring 原先我們想進行情緒分析、提取文章特定詞彙等等任務時,必須要訓練一個模型,再用來使用在需要的文章上,並且不同的任務,必須訓練不同的模型。現在我們可以使用 LLM 來進行 inferring,代替原先需要訓練模型才能完成的任務。 ``` 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}''' ``` ### Transforming LLM 擅長將文字**轉換**成另一種形式,例如將文字翻譯成另一個語言、糾正拼字錯誤、語氣改寫等等。 ``` Translate the following from slang to a business letter: 'Dude, This is Joe, check out this spec on this standing lamp.' ``` ### Expanding 我們可以利用 LLM 進行**擴寫**,給模型一段短文章,要求 LLM 根據內容寫成一段長文章。 ### Chat Workflow LLM 也可以用來建立 Chatbot。與先前輸入單一一個訊息,得到對應的回應,我們可以改成輸入一系列的訊息,這些訊息包含各種不同角色,讓模型進行角色扮演。 ``` {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'} ``` :::success Hello Isa! It's nice to meet you. How can I help you today? :::