Prompt engineering ถือว่ามีความสำคัญอย่างยิ่งในกระบวนการทำงานกับโมเดลภาษาขนาดใหญ่ (Large Language Models, LLMs) เช่น GPT, BERT, หรือ LLaMA เป็นต้น การเขียน prompt ที่ดีสามารถเพิ่มประสิทธิภาพและความแม่นยำของผลลัพธ์ที่ได้จากโมเดล ซึ่งช่วยให้การตอบสนองของระบบตรงกับความต้องการของผู้ใช้งานมากยิ่งขึ้น
ความสำคัญของ prompt engineering อยู่ในหลายด้าน เช่น:
การควบคุมคุณภาพของผลลัพธ์:
การเขียน prompt ที่ชัดเจนและตรงจุดสามารถช่วยให้โมเดลสร้างคำตอบที่มีคุณภาพสูงและสอดคล้องกับโจทย์ที่ผู้ใช้ตั้งไว้
การลดข้อผิดพลาด:
การใช้ prompt ที่ชัดเจนและเหมาะสมช่วยลดข้อผิดพลาดของโมเดล ทำให้โมเดลตอบคำถามได้ถูกต้องมากขึ้น
การเพิ่มประสิทธิภาพในการแก้ปัญหา:
Prompt ที่ดีสามารถช่วยให้โมเดลเข้าใจบริบทและแนวทางการแก้ปัญหาได้ดีขึ้น เช่น ในกรณีของการสร้างบทสนทนา การวิเคราะห์ข้อมูล หรือการตอบคำถามเชิงลึก
การปรับแต่งการใช้งานเฉพาะทาง:
ในการนำโมเดลมาใช้งานในบริบทเฉพาะ การออกแบบ prompt อย่างรอบคอบสามารถปรับแต่งให้โมเดลตอบสนองต่อความต้องการที่เจาะจง เช่น การวิเคราะห์ข้อมูลทางการแพทย์หรือการวิเคราะห์ข้อความในภาษาถิ่น
ดังนั้น prompt engineering จึงเป็นทักษะที่สำคัญในการใช้งาน LLMs ให้เกิดประโยชน์สูงสุด และสามารถช่วยเพิ่มคุณค่าในหลายๆ อุตสาหกรรม
มี advanced prompt engineering ซึ่งเป็นการพัฒนาทักษะการเขียน prompt ไปอีกขั้นหนึ่ง โดยใช้เทคนิคที่ซับซ้อนขึ้นเพื่อให้ได้ผลลัพธ์ที่เฉพาะเจาะจงและมีประสิทธิภาพมากกว่า prompt แบบพื้นฐาน เทคนิคเหล่านี้สามารถใช้ได้ในกรณีที่ต้องการเพิ่มความแม่นยำในการประมวลผล หรือเพื่อจัดการกับสถานการณ์ที่ต้องการคำตอบที่ซับซ้อนจากโมเดล LLMs
ตัวอย่างของเทคนิคใน advanced prompt engineering ได้แก่:
Few-shot Learning:
การใช้ few-shot learning: คือการใส่ตัวอย่างบางส่วนลงใน prompt เพื่อให้โมเดลเข้าใจแนวทางในการตอบคำถาม การให้ตัวอย่าง 2-3 ข้อจะช่วยให้โมเดลเรียนรู้และสร้างคำตอบที่สอดคล้องกับบริบทหรือรูปแบบที่กำหนด
Conditional Prompting:
การสร้าง prompt ที่มีเงื่อนไขหลายๆ อย่าง (Conditional Prompting): การระบุเงื่อนไขที่ชัดเจนภายใน prompt ทำให้โมเดลสามารถตอบได้อย่างแม่นยำมากขึ้น เช่น การระบุเงื่อนไขเกี่ยวกับรูปแบบของข้อมูล หรือข้อจำกัดบางประการในคำตอบ
Chain-of-Thought (CoT)
การใช้ chain of thought (CoT): การขอให้โมเดลให้เหตุผลทีละขั้นตอน ซึ่งช่วยในการแก้ปัญหาที่ซับซ้อน โดยการใช้ prompt ที่กระตุ้นให้โมเดลอธิบายกระบวนการคิดของมัน เช่น การให้โมเดลตอบทีละขั้นตอนในคำถามเชิงตรรกะหรือคณิตศาสตร์
Reverse Prompting
การใช้คำถามย้อนกลับ (Reverse Prompting): การถามคำถามแบบย้อนกลับเพื่อทดสอบความเข้าใจของโมเดล หรือเพื่อตรวจสอบว่าคำตอบที่โมเดลให้มานั้นถูกต้องและสอดคล้องกับบริบทจริงๆ
Context Window
การปรับแต่ง context window: ใน LLMs บางตัว การใช้ prompt ที่มีข้อมูลมากเกินไปอาจทำให้โมเดลสับสน ดังนั้น advanced prompt engineering จึงต้องรู้จักวิธีการกำหนดขอบเขตของ context window ที่จะนำมาใช้ให้เหมาะสมเพื่อให้โมเดลเข้าใจบริบทได้ดีที่สุด
Multi-turn Prompting:
เป็นเทคนิคที่ใช้ในการจัดการบทสนทนาหลายๆ รอบกับโมเดล LLM โดยการออกแบบ prompt ให้เป็นการสนทนาที่มีหลายช่วงและสร้างเงื่อนไขเพิ่มเติมให้กับโมเดลในแต่ละช่วง ซึ่งจะช่วยให้โมเดลสามารถตอบสนองต่อข้อมูลใหม่ๆ ได้อย่างถูกต้อง
Prompt Chaining: การแยกคำสั่งหรือคำถามที่ซับซ้อนออกเป็น prompt หลายๆ ชุดที่เชื่อมโยงกันเพื่อให้โมเดลทำตามขั้นตอนที่ต้องการทีละขั้น เป็นวิธีที่ใช้ในกรณีที่โมเดลอาจไม่สามารถตอบคำถามที่ซับซ้อนในครั้งเดียวได้
Advanced prompt engineering จึงเป็นการต่อยอดจาก prompt engineering แบบพื้นฐาน โดยเน้นการใช้เทคนิคที่มีประสิทธิภาพเพื่อควบคุมและปรับแต่งการทำงานของโมเดลให้ตอบสนองต่อโจทย์เฉพาะได้อย่างแม่นยำและถูกต้อง
1. Few-shot learning: การใส่ตัวอย่างบางส่วนลงใน prompt เพื่อให้โมเดลเรียนรู้รูปแบบหรือแนวทางการตอบ
ในตัวอย่างนี้ โมเดลได้รับตัวอย่างคำถามและคำตอบแล้ว 2 ข้อเพื่อให้รู้จักรูปแบบก่อนตอบคำถามใหม่ในเรื่องเดียวกัน
Q: What is the capital of France?
A: Paris
Q: What is the capital of Japan?
A: Tokyo
Q: What is the capital of Italy?
A:
2. Conditional Prompting (การใช้เงื่อนไขใน prompt)
การระบุเงื่อนไขให้ชัดเจนเพื่อจำกัดขอบเขตคำตอบที่ต้องการ
Explain Newton’s second law of motion in simple terms, in no more than 50 words, and suitable for a 10-year-old.
3. Chain of Thought (CoT)
การกระตุ้นให้โมเดลอธิบายกระบวนการคิดทีละขั้นตอน
การให้เหตุผลทีละขั้นตอนจะช่วยให้โมเดลเข้าใจและแก้ปัญหาที่ซับซ้อนได้ดีขึ้น
Q: If a train travels 60 km in 1 hour, how far will it travel in 4 hours?
A: First, determine the speed of the train, which is 60 km per hour. Then multiply the speed by the number of hours, which is 4 hours. So, 60 km/h * 4 = 240 km. Therefore, the train will travel 240 km in 4 hours.
4. Reverse Prompting (การใช้คำถามย้อนกลับ)
การขอให้โมเดลทบทวนคำตอบก่อนหน้านี้เพื่อเพิ่มความแม่นยำ:
การถามโมเดลให้ทบทวนคำตอบจะช่วยให้มั่นใจว่าโมเดลได้ให้คำตอบที่ถูกต้องและมีเหตุผลรองรับ
What is the square root of 144?
A: 12
Is your answer correct?
A: Yes, because 12 * 12 = 144.
5. Context Window Management
(การจัดการ context window)
ใน LLMs ที่มีขอบเขตข้อมูลจำกัด การเลือกข้อมูลที่สำคัญในการใส่ใน prompt เป็นสิ่งสำคัญ:
การเลือกข้อมูลเฉพาะที่เกี่ยวข้องจะช่วยให้โมเดลตอบคำถามได้อย่างแม่นยำ โดยไม่ต้องประมวลผลข้อมูลที่เกินความจำเป็น
Given the following text:
"The company was founded in 1995 by two entrepreneurs..."
Question: When was the company founded?
Answer:
6. Multi-turn Prompting
(การสร้าง prompt หลายรอบ)
การออกแบบ prompt สำหรับการสนทนาหลายรอบเพื่อสร้างความชัดเจน
User: What is the best way to learn Python?
Model: Start with basic syntax, then move on to functions and object-oriented programming.
User: Can you explain object-oriented programming?
Model: Object-oriented programming (OOP) is a paradigm based on the concept of objects, which can contain data and code. The four main principles are encapsulation, inheritance, abstraction, and polymorphism.
7. Prompt Chaining (การเชื่อมโยงหลาย prompt)
การแบ่งคำสั่งที่ซับซ้อนออกเป็นหลายขั้นตอนแล้วเชื่อมโยงกัน:
Step 1: Identify the key components of a neural network.
A: Neural networks consist of layers, neurons, weights, and activation functions.
Step 2: Explain how neurons in a neural network are connected.
A: Neurons in one layer are connected to neurons in the next layer through weights that determine the strength of the connections.
Step 3: Now describe how backpropagation works in training a neural network.
A: Backpropagation is the process where the error is calculated and propagated backward through the network to adjust the weights.
การเชื่อมโยงหลายคำถามเข้าด้วยกันช่วยให้สามารถสร้างกระบวนการตอบคำถามที่ซับซ้อนมากขึ้นได้ทีละขั้นตอน
เทคนิคเหล่านี้สามารถนำไปปรับใช้ในการทำ prompt engineering เพื่อให้ได้ผลลัพธ์ที่มีประสิทธิภาพมากขึ้นในหลายๆ สถานการณ์
การสอน soft prompt และ hard prompt ในการทำงานกับโมเดลภาษาขนาดใหญ่ (LLMs) มีข้อดีแตกต่างกัน ดังนั้นการเลือกสอนหรือไม่ขึ้นอยู่กับเป้าหมายของการใช้งานและความเข้าใจที่ต้องการให้กับผู้เรียน โดยสรุปดังนี้:
1. Hard Prompt คือการเขียน prompt ด้วยภาษาธรรมชาติที่ชัดเจน ซึ่งเป็นเทคนิคที่นิยมใช้ทั่วไป โดยการออกแบบข้อความสั่ง (prompt) ให้เหมาะสมกับการสร้างคำตอบจากโมเดล
ข้อดีของการสอน Hard Prompt:
เข้าใจง่ายสำหรับผู้เริ่มต้น
เน้นการออกแบบคำถามให้ชัดเจนและตรงประเด็น ซึ่งใช้ในงานต่างๆ ได้ทันที
เหมาะสมกับการใช้งานทั่วไปที่ไม่ต้องการการปรับแต่งมากนัก
ผู้เรียนสามารถทดลองและเรียนรู้การสร้าง prompt ที่ให้ผลลัพธ์ได้ตามต้องการอย่างตรงไปตรงมา
เหตุผลในการสอน: การสอน hard prompt ควรเป็นพื้นฐาน เพราะเป็นวิธีที่ง่ายและชัดเจนสำหรับผู้ใช้ทั่วไปในการเข้าใจว่าต้องเขียน prompt อย่างไรให้โมเดลตอบคำถามที่ถูกต้องและมีประสิทธิภาพ
2. Soft Prompt
Soft Prompt เป็นการฝึกปรับพารามิเตอร์บางอย่างของโมเดลให้สามารถเข้าใจรูปแบบคำถามที่ซับซ้อนขึ้นได้ โดยไม่ต้องกำหนดข้อความสั่งที่ชัดเจนเหมือน hard prompt เช่น การใช้ embeddings ที่ปรับแต่งได้เพื่อให้โมเดลตอบสนองได้อย่างเฉพาะเจาะจงมากขึ้น
ข้อดีของการสอน Soft Prompt:
ช่วยให้ผู้เรียนเข้าใจการปรับแต่งโมเดลเพื่อให้สามารถตอบโจทย์ที่ซับซ้อนได้ดียิ่งขึ้น
ใช้ประโยชน์จากการฝึกโมเดลเพิ่มเติมโดยไม่ต้องเปลี่ยนโครงสร้างหรือ train ใหม่
มีประสิทธิภาพมากขึ้นในกรณีที่ต้องการการปรับจูนโมเดลเฉพาะทาง
เหมาะสำหรับการใช้งานที่ต้องการผลลัพธ์เฉพาะในเชิงลึกหรือมีข้อกำหนดที่เฉพาะเจาะจงมากขึ้น
เหตุผลในการสอน: หากผู้เรียนต้องการความเข้าใจเชิงลึกเกี่ยวกับการปรับจูนโมเดล หรือ การปรับแต่งโมเดลสำหรับงานเฉพาะทาง การสอน soft prompt จะมีประโยชน์อย่างมาก เพราะจะช่วยให้เข้าใจการทำงานของโมเดลในเชิงเทคนิคและการปรับพารามิเตอร์ได้
ควรสอนทั้งสองประเภทหรือไม่?
ในกรณีของการพัฒนาโมเดลหรือการทำงานกับ LLM ในระดับลึก ควรสอนทั้งสองประเภทเพื่อให้ผู้เรียนสามารถเลือกใช้เทคนิคที่เหมาะสมกับงานของตนเอง:
Hard prompt เหมาะกับการใช้งานทั่วไปที่เน้นความรวดเร็วและความง่าย
Soft prompt เหมาะกับการปรับแต่งโมเดลสำหรับการใช้งานที่ซับซ้อนหรือเฉพาะเจาะจง
ดังนั้น การสอนทั้งสองประเภทจะทำให้ผู้เรียนมีความยืดหยุ่นในการใช้ LLM ได้ในหลายสถานการณ์
Training large pretrained language models is very time-consuming and compute-intensive. As they continue to grow in size, there is increasing interest in more efficient training methods such as prompting. Prompting primes a frozen pretrained model for a specific downstream task by including a text prompt that describes the task or even demonstrates an example of the task. With prompting, you can avoid fully training a separate model for each downstream task, and use the same frozen pretrained model instead. This is a lot easier because you can use the same model for several different tasks, and it is significantly more efficient to train and store a smaller set of prompt parameters than to train all the model’s parameters.
The AUTOMAT framework developed by Maximilian Vogel is a systematic approach to designing effective prompts for large language models (LLMs). It breaks down the process into six key elements, each helping to ensure that the prompt guides the model toward accurate and desired responses:
Act as a…: Define the role or persona the model should assume, such as an expert or assistant.
User Persona & Audience: Specify who the model is interacting with and the characteristics of the audience.
Targeted Action: Clearly define what you want the model to do or what action it should take.
Output Definition: Specify the format and type of response you expect, like a summary, list, or structured data.
Mode / Tonality / Style: Determine the tone, style, or manner of communication, such as formal, casual, or technical.
Atypical Cases: Outline how the model should handle uncommon scenarios or edge cases.
Topic Whitelisting: Specify relevant topics or boundaries to keep the model focused on what’s needed.
This structured framework helps ensure that prompts are clear, concise, and tailored for specific tasks, improving the model's performance, particularly in complex applications. It works similarly to the CO-STAR framework, which also focuses on prompt clarity but emphasizes context and tone.
The AUTOMAT framework is especially useful in AI applications, chatbots, and situations where precise control over the model’s output is essential.
There are several other popular frameworks and methodologies for prompt engineering besides AUTOMAT. These frameworks aim to enhance the precision and relevance of responses generated by large language models (LLMs). Some of the most notable ones include:
1. CO-STAR Framework
The CO-STAR framework focuses on creating structured prompts and is widely adopted for ensuring that the model understands the context and provides useful outputs. It includes:
Context: Setting the scene and giving background.
Objective: Defining the task clearly.
Style & Tone: Specifying the desired communication style.
Audience: Tailoring the response to a specific group.
Response: Choosing the output format, such as text, list, or code.
This method helps maintain clear and structured communication, especially for AI-powered systems like chatbots and virtual assistants(
https://www.kdnuggets.com/3-new-prompt-engineering-resources
2. Chain of Thought (CoT)
CoT prompting involves asking the model to “think aloud” by explaining its reasoning step by step before delivering the final answer. This is especially useful for solving complex problems that require logical steps, such as math or decision-making tasks. It encourages the model to perform more accurate reasoning, improving the quality of responses(
Read Medium articles with AI: https://readmedium.com/the-perfect-prompt-prompt-engineering-cheat-sheet-d0b9c62a2bba
3. Few-Shot Learning
Few-shot learning provides a model with a few examples within the prompt to guide it on how to handle a task. This technique is especially powerful for tasks where explicit instructions or examples clarify the expected output. By giving both "normal" and "edge" cases, the model is better equipped to generalize
4. Multi-Prompt Approach
In complex applications, one prompt is sometimes insufficient to capture all aspects of a task. The multi-prompt approach divides tasks into smaller components and provides distinct prompts for each one. It helps improve accuracy, especially for more intricate tasks like insurance claim processing or report generation
5. RAG (Retrieval-Augmented Generation)
RAG integrates external data sources with LLMs, allowing the model to access up-to-date information from documents or databases. This helps overcome the limitations of pre-trained knowledge in LLMs, making responses more comprehensive and contextually relevant
These frameworks complement each other and are used depending on the complexity of the task or the specific needs of the project. Each method provides a different way to guide LLMs to produce more accurate, targeted, and context-aware outputs.