提示詞工程:撰寫能讓 AI 產出更好結果的提示詞
為什麼你的 AI 提示詞沒有效
你大概試過請 AI 幫忙——寫一封 email、除錯程式碼、摘要一篇文章——結果得到的回應卻不如預期。可能太模糊、太冗長,或是完全離題。問題通常不在 AI,而在提示詞本身。
提示詞工程(prompt engineering)是一門設計輸入內容、以從大型語言模型(LLM)獲得更好輸出結果的實務。它靠的不是什麼神奇咒語,而是清楚溝通。本指南提供實用技巧,幫助你寫出能穩定產出有用結果的提示詞。
1. 具體且明確
LLM 不會讀心術。如果你只說「寫一些關於狗的東西」,你只會得到一篇泛泛而談的文章。相反地,請明確指定主題、格式、長度和目標讀者。
不好的: Write about dogs.
好的: Write a 300-word article for pet owners about the top 3 dog breeds for apartments. Include exercise needs and temperament.
你提供的限制條件越多,輸出就越聚焦。把它想像成在給自由工作者下指令:你講得越清楚,成果就越好。
2. 提供背景脈絡與範例
LLM 會從你給予的脈絡中學習。如果你想要特定的風格或格式,就示範一個範例。
範例: Summarize the following customer review in one sentence, focusing on the main complaint. Example: "The product arrived late and was damaged." Review: "I ordered this on the 5th, it shipped on the 10th, and when it finally arrived, the box was crushed and the item was scratched."
這種技巧稱為「少樣本提示」(few-shot prompting),能幫助模型理解你想要的模式。
3. 運用角色扮演
為 AI 指派一個角色,能提升回應的品質與語氣。例如「扮演一位資深軟體工程師」或「你是一位親切的客服人員」。這能引導模型採用特定的觀點和專業程度。
範例: Act as a cybersecurity expert. Explain the risks of SQL injection to a non-technical manager in under 200 words.
4. 將複雜任務拆解成步驟
如果你的要求很複雜,就把它拆成更小、依序進行的提示詞。這能降低模型不堪負荷或遺漏細節的機率。
不要這樣: Create a marketing plan for a new app.
試試這樣: Step 1: Identify the target audience for a productivity app aimed at remote workers. Step 2: List three key features that would appeal to them. Step 3: Suggest two marketing channels to reach them.
你也可以請模型「一步一步思考」(think step by step),以鼓勵它進行推理。
5. 指定輸出格式
如果你需要特定格式的輸出——JSON、Markdown、表格——請明確說出來。當你要把 AI 整合進自動化工作流程時,這點尤其有用。
範例: Extract the product name, price, and rating from the review below. Return the result as a JSON object with keys: name, price, rating.
對開發者來說,像 JSON Formatter 這類工具可以幫助驗證並格式化 AI 的輸出。
6. 反覆迭代與精煉
第一個提示詞很少會一次到位。把它當成一場對話:檢視輸出、找出缺少什麼,然後據此調整你的提示詞。
範例: 如果 AI 的摘要太長,就接著說:Make it shorter—under 50 words. 如果太專業難懂,就說:Explain it as if I'm a beginner.
7. 避免模稜兩可與否定句
LLM 有時難以處理否定句。與其說「不要用行話」,不如說「使用簡單的語言」。與其說「不要含糊」,不如說「要具體並附上範例」。
8. 嘗試調整 temperature 與各項參數
如果你使用 API,可以調整像 temperature(創造力)和 max_tokens(長度)這類參數。較低的 temperature(例如 0.2)會產生更確定、更聚焦的輸出;較高(例如 0.8)則鼓勵創造力。處理需要事實的任務時,請使用低 temperature。
比較:弱提示詞 vs. 強提示詞
| 弱提示詞 | 強提示詞 |
|---|---|
| Write a blog post. | Write a 500-word blog post for small business owners about the benefits of cloud accounting software. Include 3 key advantages and a call to action. |
| Fix this code. | Debug this Python function that calculates Fibonacci numbers. Explain the error and provide a corrected version with comments. |
| Summarize this. | Summarize the following article in 3 bullet points, each under 20 words, focusing on the main argument. |
常見問題
什麼是提示詞工程?
提示詞工程是設計並精煉輸入內容(提示詞)的過程,用以引導大型語言模型產出想要的輸出。它涉及明確性、脈絡和反覆迭代。
寫出好的提示詞需要會寫程式嗎?
不需要。雖然像 API 參數調整這類技巧需要寫程式,但大多數提示詞工程談的是清楚溝通,任何人使用聊天介面都能做到。
我該如何快速改善我的提示詞?
從具體開始:說明格式、長度和目標讀者。情況允許就提供範例。接著反覆迭代——如果輸出不對,就告訴 AI 要改什麼。
準備好把這些技巧付諸實踐了嗎?如果你在處理 AI 生成的 JSON,可以使用我們的 JSON Formatter 來驗證並美化輸出,方便應用到你的程式裡。