เขียน Prompt อย่างไรให้มีประสิทธิภาพ

การ Prompt เป็นเริ่มเป็นสิ่งที่ขาดไม่ได้ในชีวิตของเรามากขึ้นทุกที การที่เราสามารถ Prompt ได้ดีหรือไม่ดี อาจจะเป็นตัวตัดสินประสิทธิภาพในการทำงานของเราต่อจากนี้ก็เป็นได้ ในบทความนี้ผมได้รวมเอากลยุทธ์ในการ Prompt ที่มีประสิทธิภาพสำหรับการใช้ ChatGPT มาฝากทุกคน

Disclaimer :
ผลลัพธ์ที่แสดงในบทความนี้เป็นเพียงตัวอย่าง ถ้าไปลองเล่น ChatGPT แล้วจะได้ผลไม่เหมือนกันมาจากได้หลายสาเหตุ เช่น

  1. Generative AI อย่าง ChatGPT เขามีนโยบายว่าทุกๆ รอบอาจจะ 1 ปี เขาจะเอาข้อมูลที่ผู้ใช้งาน Prompt มาทำการ Train model ใหม่ (เขาจะไม่ทำบ่อยเพราะด้วยค่าใช้จ่ายที่สูง)
  2. ในการถามโต้ตอบ ถ้าสังเกต ถึงแม้เราจะใช้ Prompt เดียวกับเพื่อน เราก็ได้คำตอบไม่เหมือนกัน เพราะว่าจะมีตัวแปรหนึ่งชื่อว่า temperature จะเป็นการสุ่มคำตอบให้แตกต่างกันอยู่ ซึ่งถ้าใช้ API จะได้เหมือนกันเสมอแต่ ไปทำใน ChatGPT จะได้ไม่เหมือนเดิม

เราใช้คำว่า “Prompt” เพื่ออธิบายคำสั่งที่เรากำหนดให้กับ Generative AI ให้ตอบกลับหรือปฏิบัติตาม ในทางปฏิบัติระบบจะพยายามตอบกลับหรือปฏิบัติตามข้อความนั้น ตามที่มีข้อมูลที่เรียนรู้มาก่อน การเลือกคำหรือลักษณะของ Prompt อาจมีผลต่อความเข้าใจและการตอบสนองของ Generative AI ในบทวิเคราะห์ข้อมูลหรือการส่งข้อมูลสำคัญ เช่น การสอบถามข้อมูล ให้คำแนะนำในการสร้างข้อมูลใหม่ การออกแบบ Prompt ให้เหมาะสมเป็นสิ่งสำคัญเพื่อให้ Generative AI ทำงานอย่างเหมาะสมและถูกต้องตามความต้องการของผู้ใช้

กลยุทธ์ในการ Prompt

กลยุทธ์ที่แนะนำในบทความนี้มาจาก หลักสูตร ChatGPT Prompt Engineering for Developers ใครสนใจเชิงลึกสามารถไปศึกษาเพิ่มเติมได้

ในการ Prompt นั้นมีหลักการสำคัญอยู่ 2 อย่างด้วยกันได้แก่

  • หลักการที่ 1 : เขียนคำสั่งให้ชัดเจนและเจาะจง
  • หลักการที่ 2 : ให้เวลากับ Generative AI ในการคิด

กลยุทธ์ที่ 1-1 : การใช้ตัวขึ้นเพื่อแยกส่วนของข้อมูล

เราสามารถใช้ตัวขึ้น เช่น “`, “””, < >, <tag> </tag>: เพื่อให้ Generative AI ทราบว่าส่วนไหนเป็นส่วนของข้อมูลที่เราให้ ส่วนไหนเป็นคำสั่งที่เราต้องการ

ในบางครั้งข้อมูลที่อยากให้ AI ทำตามคำสั่งอาจจะมี คำสั่งอยู่ในข้อมูลนั้นอีกที เรียกว่าเป็น Prompt Injection ลักษณะคล้าย SQL Injection ที่เวลา Hacker ทำเพื่อเจาะฐานข้อมูลระบบ

ตัวอย่าง Prompt :

Summarize the text delimited by triple backticks 
into a single sentence.

```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.```

ตัวอย่างผลลัพธ์ :

To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which can be achieved through longer prompts that offer more clarity and context.

กลยุทธ์ที่ 1-2 : การระบุรูปแบบของผลลัพธ์ที่ต้องการ

ถ้าเราต้องการข้อมูลในลักษณะที่เอาไปใช้งานต่อได้เลย เช่น ให้สรุปออกมาเป็นขั้นตอน หรือแม้กระทั้ง JSON format ซึ่งมีการใช้งานในการรับส่ง API หรือเก็บข้อมูล ก็สามารถกำหนดรูปแบบได้

ตัวอย่าง Prompt :

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.

ตัวอย่างผลลัพธ์ :

{
  "books": [
    {
      "book_id": 1,
      "title": "The Enigma of Elysium",
      "author": "Evelyn Sinclair",
      "genre": "Mystery"
    },
    {
      "book_id": 2,
      "title": "Whispers in the Wind",
      "author": "Nathaniel Blackwood",
      "genre": "Fantasy"
    },
    {
      "book_id": 3,
      "title": "Echoes of the Past",
      "author": "Amelia Hart",
      "genre": "Romance"
    }
  ]
}

กลยุทธ์ที่ 1-3 : การให้โมเดลประเมินตัวเองว่าผลลัพธ์ที่ได้มีหรือไม่

ถ้าเราอยากให้ AI ไม่ตอบเราไปเรื่อย ในกรณีที่ประเมินแล้วไม่มีข้อมูล เมื่อข้อมูลไม่ถูกต้อง ก็กำหนดว่าถ้ามันหาข้อมูลไม่เจอให้ทำการตอบแบบใดลงไป

ตัวอย่าง Prompt :

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.\"

"""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."""

ตัวอย่างผลลัพธ์ (ทำสำเร็จ) :

Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let it sit for a bit so the tea can steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - If you like, add some sugar or milk to taste.
Step 7 - Enjoy your delicious cup of tea.

ตัวอย่างผลลัพธ์ (ทำไม่สำเร็จ) :

No steps provided.

กลยุทธ์ที่ 1-4 : Few-shot Prompt

เป็นการให้ตัวอย่างที่ทำสำเร็จแล้ว เพื่อให้ผลลัพธ์ออกมาตรงมากยิ่งขึ้น ก็เหมือนเวลาเราสอนเด็ก ถ้ามีตัวอย่างเขาก็มีแนวโน้มที่จะทำตามได้ถูกต้องมากขึ้น

ตัวอย่าง Prompt :

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.

ตัวอย่างผลลัพธ์ :

<grandparent>: Resilience is like a mighty oak tree that withstands the strongest storms, bending but never breaking. It is the unwavering determination to rise again after every fall, and the ability to find strength in the face of adversity. Just as a diamond is formed under immense pressure, resilience is forged through challenges and hardships, making us stronger and more resilient in the process.

กลยุทธ์ที่ 2-1 : ระบุขั้นตอนที่ต้องทำเพื่อให้งานนั้นสำเร็จ

ให้คิดว่าเหมือนสั่งงานลูกน้อง ถ้าเราอธิบายคำสั่งเป็นบรรยาย ยาวๆ ถ้าลูกน้องเก็บจะจดออกมาเป็นข้อๆ ได้ แต่ถ้าไม่ได้เก่งหรือเขาไม่ทัน เขาจะจับประเด็นได้ไม่ครบ ดังนั้นการให้คำสั่งเป็นเป็นข้อๆ ที่ชัดเจนจะช่วยได้ ในมุมของการ Prompt สิ่งนี้จะช่วยให้ Generative AI มีเวลาคิดมากยิ่งขึ้น

ตัวอย่าง 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.

Separate your answers with line breaks.

Text:
```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.```

ตัวอย่างผลลัพธ์ :

1 - Jack and Jill, siblings, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they return home and remain undeterred in their adventurous spirits.

2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.

3 - Jack, Jill

4 - {
  "french_summary": "Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.",
  "num_names": 2
}

กลยุทธ์ที่ 2-2 : ให้คำสั่งกับโมเดลในการหาทางออกของตัวเองก่อนที่จะสรุปผล

ในกรณีของการตรวจสอบการบ้าน ถ้าเราเป็นคุณครู เราก็ต้องคิดเฉลยก่อนตรวจถูกไหม ในการ Prompt ก็เช่นกัน แทนที่จะให้ตอบว่าถูกหรือผิดเลย เราให้ Generative AI คิดคำตอบก่อน

ตัวอย่าง Prompt :

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

ตัวอย่างผลลัพธ์ :

Let × be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 10x
Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000
Is the student's solution the same as actual solution just calculated:
No
Student grade:
Incorrect

บทสรุป

จะเห็นว่าการ Prompt มีวิธีการเขียนที่ให้ผลลัพธ์ที่มีประสิทธิภาพมากยิ่งขึ้นได้อีก หากคุณพบว่าใช้ ChatGPT แล้วไม่ได้ผลตามที่ต้องการเลย ให้ลองดูกลยุทธ์เหล่านี้ว่าสามารถปรับและช่วยให้ได้คำตอบที่ดีขึ้นหรือเปล่า

ฝากไว้สุดท้ายเทคโนโลยี Generative AI เปลี่ยนแปลงไปรวดเร็วมาก วันหนึ่งเราอาจจะไม่ต้องมาเขียนตามกลยุทธ์เหล่านี้แล้วก็ได้ คอยติดตามข่าวสารอยู่เรื่อย ๆ จะช่วยให้ปรับตามเทคโนโลยีได้ทัน

Author

Scroll to Top