BLOGS

ใคร ๆ ก็เขียน Prompt ได้หากเข้าใจ! เริ่มต้นกับ Gen AI บน Google Cloud

Thakorn.T • 05/07/2023
Google Cloud Solutions Specialist

พื้นฐานก่อนเป็น Prompt Engineer

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

ก่อนจะเริ่ม Prompt สำหรับใครที่ยังไม่ได้อ่าน Blog ก่อนหน้า
เพื่อปูพื้นฐานความเป็น Prompt Engineering สามารถอ่านกันเพิ่มเติมได้ที่  Blog ด้านล่างนี้

Prompt Design

                    การเขียน Prompt หรือชุดคำสั่ง เป็นสิ่งพื้นฐานที่พวกเราจะป้อนเข้าไปยัง AI โดยบางครั้งก็เป็นการอธิบายที่ง่ายเหมือนภาษามนุษย์ บางครั้งก็อาจซับซ้อนด้วยกฎหรือเงื่อนไขต่าง ๆ ที่เราต้องแจ้งให้ AI ทราบ ดังนั้นจึงควรจะมี Best practices ในการออกแบบ Prompt ด้วยเช่นกัน

                    เครื่องมือที่จะรัน Prompt ใน Blog นี้คือ Generative AI Studio ที่อยู่บน Google Cloud นั่นเอง แต่ใน EP.3 นี้ จะยังไม่ลงลึกถึง Parameter หรือการใช้ใด ๆ เพียงต้องการให้เข้าใจแนวทางปฎิบัติที่ถูกต้องโดยมีตัวอย่างจาก Generative AI Studio ส่วนการใช้เครื่องมือนั้นสามารถติดตามใน EP ถัดไปได้เลย

ภาพตัวอย่างการออกแบบ Prompt บน Generative AI Studio

                    แต่! หากคันไม้คันมืออยากจะ Prompt ไปพร้อมกันแบบเร็ว ๆ ก็สามารถลองเล่นบน Bard (https://bard.google.com) ดูก่อนได้เช่นกัน ซึ่งใน Blog ก่อนหน้าก็ได้มีการพูดถึง Bard ไปแล้วว่าเป็นเสมือน Gen AI ที่ใช้แบบ Personal ไม่ได้ใช้เชิง Enterprise แต่เราก็สามารถใช้แนวทางปฎิบัติเหล่านี้ได้เช่นกัน

” เตรียมคีย์บอร์ดของเราไว้ดี ๆ แล้ว Prompt ไปพร้อมกัน “

Prompt Engineering Best Practices

                    Prompt Engineering คือการที่เราออกแบบชุดคำสั่งเพื่อให้ได้ผลลัพธ์ที่ต้องการ แต่หากเราเขียน Prompt ที่ดูไม่สละสลวยหรือกำกวมอาจส่งผลให้เกิด Noise และ LLM (Large Language Model) อาจตีความคลาดเคลื่อนได้ ดังนั้นในหัวข้อนี้แนวทางปฎิบัติในการออกแบบ Prompt ควรอิงกับ Concept เหล่านี้

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

                    การที่เราบรรยายหรือทักทายเกินความจำเป็นไม่ได้มีประโยชน์มากขึ้นแต่อาจะทำให้ความเข้าใจของ LLM คลาดเคลื่อน

🚫 Prompt ต่อไปนี้มีการใช้คำมากเกินความจำเป็น

Prompt:

What do you think could be a good name for a coffee shop that specializes in extracting specialty coffee more than low grade coffee beans Thank you!

Response:

  • The Coffee Beanery
  • The Daily Grind
  • The Bean Counter
  • Java Jive
  • Café Mocha
  • The Coffee House
  • Starbucks
  • Peet’s Coffee and Tea
  • Caribou Coffee
  • Dunkin’ Donuts

🟢 เขียนให้กระชับได้ใจความ

Prompt:

 Suggest names for a coffee shop that sells specialty coffee

Response:

  • The Daily Grind
  • The Coffee Bean & Tea Leaf
  • Starbucks
  • Peet’s Coffee & Tea
  • Caribou Coffee
  • Seattle’s Best Coffee
  • Intelligentsia Coffee
  • Stumptown Coffee Roasters
  • Blue Bottle Coffee
  • La Colombe Coffee Roasters
2. มีความเจาะจง ถามอย่างชัดเจน

                    บางครั้งการถามกว้าง ๆ เกินไป LLM ก็อาจเข้าใจผลลัพธ์ที่เราต้องการผิดไป ในตอนที่เราต้องการไอเดียเพื่ออธิบายความเป็นกาแฟพิเศษ ตัวอย่างเช่น

🚫 ถามกว้างเกินไป

Prompt:

Tell me about specialty coffee

Response:

Specialty coffee is a term used to describe coffee that is grown, processed, and roasted in a way that emphasizes its unique flavor and characteristics. Specialty coffee beans are typically grown at high altitudes in tropical climates, and they are hand-picked and processed with care. This attention to detail results in a coffee that is rich, flavorful, and complex.

Specialty coffee is different from regular coffee in several ways. First, the beans are grown in specific regions that are ideal for coffee production. These regions have the right climate, soil, and elevation to produce high-quality coffee beans. Second, the beans are hand-picked and processed with care. This ensures that the beans are not damaged and that their flavor is preserved. Third, specialty coffee is roasted in a way that brings out its unique flavor and characteristics. This roasting process is carefully controlled to ensure that the beans are not over- or under-roasted.

The result of all of this care and attention is a coffee that is truly special. Specialty coffee is rich, flavorful, and complex, with a unique taste that is unlike any other coffee you’ve ever tasted. If you’re looking for a coffee experience that is truly memorable, then specialty coffee is the way to go.

🟢 Prompt ต่อไปนี้เป็นการถามอย่างเจาะจงและชัดเจน

Prompt:

List for what is that make specialty coffee compare with non-specialty coffee

Response:

Specialty coffee is different from non-specialty coffee in a number of ways. Here are some of the key differences:

  • Growing conditions: Specialty coffee is grown in specific areas with ideal climate and soil conditions. This results in coffee beans with a higher quality and more complex flavor profile.
  • Processing methods: Specialty coffee is processed using more careful and precise methods. This results in coffee beans that are clean and free of defects.
  • Roasting: Specialty coffee is roasted to a higher degree than non-specialty coffee. This brings out the coffee’s natural flavors and aromas.
  • Brewing: Specialty coffee is brewed using a variety of methods, each of which can bring out different flavors in the coffee.

As a result of these differences, specialty coffee has a richer, more complex flavor than non-specialty coffee. It is also less acidic and has a smoother, more balanced taste. Specialty coffee is also more expensive than non-specialty coffee, but many people believe that the higher price is worth it for the superior quality.

3. ถามทีละอย่าง

                    เราควรถามทีละคำถาม ยิ่งหากเรามีหลายคำถามใน Prompt เดียวกันและไม่เกี่ยวข้องกัน อาจะทำให้ผลลัพธ์นั้นแตกต่างจากที่ควรจะเป็นหรือเราอยากได้ ทั้งนี้การ Prompt แต่ละครั้งก็มี Parameter ที่ควบคุมเช่น จำนวนคำ (Word) ที่ตอบกลับมา ซึ่งเราจะมีการ Set Token Limit ไว้ (1 Token เท่ากับประมาณ 4 ตัวอักษร) ผลลัพธ์จึงมีจำกัดเพราะว่าตัว LLM พยายามที่จะจำกัด Output ให้ไม่เกินจำนวน Token ที่เราตั้งไว้

สำหรับ Parameter ต่าง ๆ ของ Generative AI ทางผู้เขียนขอยกไปอธิบายใน EP ถัดไป

🚫 Prompt นี้มีการถาม 2 คำถามที่ไม่เกี่ยวข้องกัน ซึ่งเราสามารถแยกเป็นคำถาม ๆ ได้

Prompt:

What’s the best method for extracting light roasted coffee and why do farmers like to grow bananas and guava on a single farm?

Response:

The best method for extracting light roasted coffee is the pour over method. This method allows for a more delicate extraction of the coffee’s flavors, as it does not require as much heat as other methods. The pour over method also allows for more control over the extraction process, as the brewer can adjust the amount of water and the speed at which it is poured over the coffee grounds.

Farmers like to grow bananas and guava on a single farm because these plants help to improve the soil quality and provide shade for the coffee plants. Bananas are nitrogen-fixing plants, which means that they can convert atmospheric nitrogen into a form that can be used by other plants. This helps to improve the soil quality and make it more fertile. Guava trees also provide shade for the coffee plants, which helps to protect them from the sun’s heat and helps to keep the coffee beans from roasting too quickly.

🟢 แยกถามกันทีละอย่างในแต่ละ Prompt จะได้คำตอบที่ดีขึ้น

Prompt:

What’s the best method for extracting light roasted coffee?

Response:

 The best method for extracting light roasted coffee is pour over. Pour over is a brewing method that uses a filter to separate the coffee grounds from the brewed coffee. The coffee grounds are placed in a filter cone, which is then placed on top of a carafe or mug. Hot water is then poured slowly over the grounds, allowing the coffee to drip through the filter into the carafe or mug. Pour over is a great method for extracting light roasted coffee because it allows for a lot of control over the brewing process. The brewer can adjust the grind size, water temperature, and brewing time to create a cup of coffee that is perfectly suited to their taste.

Prompt:

Why do farmers like to grow bananas and guava on a single farm?

Response:

Farmers like to grow bananas and guava on the same farm because they are both tropical plants that require similar growing conditions. Bananas and guava are both warm-weather plants that need plenty of sunlight and water. They also both do well in soil that is rich in organic matter.

In addition, bananas and guava can help each other out by providing shade and nutrients. Bananas are tall plants that can provide shade for the smaller guava plants. Guava plants can also help to improve the soil by adding nutrients and organic matter.

By growing bananas and guava together, farmers can take advantage of the complementary growing conditions of the two plants. This can help them to increase their yields and profits.

4. ระวังภาพหลอน

                    เนื่องจาก LLM ถูก Train บนข้อมูลมหาศาลจึงสามารถสร้างข้อความหรือบริบทต่าง ๆ ที่ไม่ได้มาจากพื้นฐานความเป็นจริงหรือความถูกต้องได้ ซึ่งเราเรียกเหตุการณ์นี้ว่า “Hallucinations” หรืออาการประสาทหลอนคล้ายอาการ AI ที่เมากาว เกิดจากความสามารถที่จดจำหรือ Memorization อย่างจำกัด

                    การให้ LLM บอกแหล่งที่มาของข้อมูลที่มันสร้างขึ้นมานั้นไม่ได้ช่วยแก้ไขอาการเมากาวนี้ เนื่องจาก LLM อาจจะให้แหล่งที่มาที่ผิดหรือไม่ถูกต้องก็เป็นได้ อย่างไรก็ดีนี่ถือเป็นความท้าทายพื้นฐานของ LLM ที่ต้องศึกษาและวิจัย ดังนั้นสิ่งสำคัญจึงต้องตระหนักว่า LLM จะให้คำตอบที่ดูเหมือนถูกต้องและน่าเชื่อถือแต่ความจริงอาจจะไม่ได้ถูกต้อง จึงควรระมัดระวังการใช้งาน

                    แต่หากใช้ LLM กับงานด้าน Creative ที่อาศัยความคิดสร้างสรรค์ อาการเมากาวหรือประสาทหลอน (Hallucination) ที่คิดฟุ๊งซ่านนอกกรอบเหมือนความรู้สึกล่องลอยเช่นนี้ ก็อาจมีประโยชน์มาก ๆ ในด้านงานนี้

Prompt:

Who was the first dinosaur that grew the first coffee plant on the sun?

Response:

The first dinosaur that grew the first coffee plant on the sun was named Stella. Stella was a small, green dinosaur with a long tail and a tuft of feathers on her head. She was very curious and loved to explore. One day, Stella was exploring the forest when she came across a strange plant. The plant had red berries and a strong smell. Stella had never seen anything like it before, so she decided to take a closer look.

Stella took a bite of the berry and was immediately surprised by how delicious it was. She had never tasted anything like it before. Stella knew that she had found something special, so she took some of the berries back to her family. Stella’s family loved the berries too, and they soon started to grow their own coffee plants.

Stella’s coffee plants were very successful, and soon they were being grown all over the world. Stella’s family became very wealthy, and Stella was known as the first dinosaur to grow the first coffee plant on the sun.

ลองเปลี่ยนงาน Generative เป็น Classification

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

Prompt:

I’m a 18 year old girl. Recommend me a coffee extraction type to drink in the morning.

Response:

I would recommend a pour-over coffee. Pouring over coffee is a great way to make a delicious and smooth cup of coffee. It’s also a relatively simple process, so it’s perfect for beginners. To make a pour-over coffee, you’ll need:

  • A pour-over coffee maker
  • Coffee beans
  • A grinder
  • A kettle
  • A carafe
  • A filter
  1. Grind your coffee beans to a medium-coarse grind.
  2. Place the filter in the carafe.
  3. Add hot water to the kettle and bring it to a boil.
  4. Pour a small amount of hot water into the filter to wet it.
  5. Start pouring the hot water over the coffee grounds in a circular motion.
  6. Continue pouring until all of the water has been added.
  7. Let the coffee drip into the carafe for 4-5 minutes.
  8. Enjoy your pour-over coffee!

                    Pouring over coffee is a great way to start your day because it’s a delicious and flavorful way to get your caffeine fix. It’s also a great way to experiment with different coffee beans and brewing methods.

💬 หากเราถาม LLM ว่าจะสกัดกาแฟด้วยวิธีแบบไหนดี LLM ก็แนะนำให้ดริปกาแฟเพราะง่ายและรสชาติดีด้วย
พร้อมบรรยายและแนะนำขั้นตอนต่าง ๆ ให้เรา ซึ่งเป็นสิ่งที่ดีแต่อาจเกินความจำเป็นที่เราต้องการสำหรับผลลัพธ์
ดังนั้นเราจึงสามารถใช้เทคนิคเพื่อให้ LLM ตอบแบบ Classification ได้

Prompt:

                    I’m a 18 year old girl. Which coffee extraction type do you recommend me to do and why?:
1) Drip
2) Espresso
3) Moka Pot

Response:

I would recommend you to try drip coffee. Drip coffee is a simple and easy way to make coffee, and it produces a smooth, balanced cup of coffee. It is also a relatively inexpensive way to make coffee, making it a good option for college students on a budget.

💬 คำตอบจะถูกควบคุมให้เป็นแบบที่เราต้องการมากขึ้น เมื่อ Prompt ให้เป็นแบบ Classification

เพิ่มคุณภาพของผลลัพธ์ด้วยการยกตัวอย่าง

                    การยกตัวอย่างจะเป็นตัวบอก LLM ว่าคำตอบควรไปในแนวทางใด โดยปกติเราจะยกตัวอย่าง 1-5 ตัวอย่าง (เรียกว่า Shots) การยกตัวอย่างมากไปอาจทำให้ Model Overfit และลดคุณภาพของผลลัพธ์ลงก็เป็นได้

                    ไม่ต่างจากการทำ Model ประเภท Classification ข้อมูลตัวอย่างที่เราใช้มา Train มีความสำคัญมาก ๆ ทั้งคุณภาพและการกระจายตัวของ Sample ต่าง ๆ ดังนั้นจึงต้องยกตัวอย่างที่ใกล้เคียงความเป็นจริงหรือ Use-cases ที่เราจะใช้เพื่อไม่ให้แหวกแนวมากเกินไป

Zero-shot prompt

                    Zero-shot คือการ Prompt ให้ LLM โดยไม่ยกตัวอย่างใด ๆ LLM จะมีอิสระในการตอบ

Prompt:

Decide whether a sentiment is positive, neutral, or negative.

Text: Coffee from Ethiopia tasted great. Very fruity, must try!!

  Sentiment:

Response:

Positive

One-shot Prompt

                    เป็นการยกตัวอย่าง 1 ตัวอย่าง ที่พอจะให้ LLM เข้าใจว่าคำตอบแบบไหนที่เราต้องการ

Prompt:

Decide whether a sentence is related to food, technology, or office supply category.

Text: We bought roasted coffee from www.kohi.day last week. It was very surprised me from this shop because the Colombia Rose Valley coffee was great and high aroma, long sweet and clean finished

Category: food

Text: Yesterday, I bought paper from a convenience store near my home by mobile application for delivery at a door. The price is a bit high. But it’s better than traveling far from home to buy.

Category:

Response:

Office supply

Few-shot Prompt

                    เป็นการยกตัวอย่างให้ LLM มากกว่า 1 ตัวอย่าง

Prompt:

Decide whether a sentence is related to food, technology, or office supply category.

Text: We bought roasted coffee from www.kohi.day last week. It was very surprised me from this shop because the Colombia Rose Valley coffee was great and high aroma, long sweet and clean finished

Category: food

Text: Yesterday, I bought paper from a convenience store near my home by mobile application for delivery at a door. The price is a bit high. But it’s better than traveling far from home to buy.

Category: office supply

Text: Last week, We ordered roasted coffee from a convenience store near our house. But before I could order it, the mobile application malfunctioned very often. Until I want to delete the app, but fortunately it’s back to work.

 Category:

Response:

Technology

💬 สังเกตว่า LLM จะมีความเข้าใจในบริบทและเนื้อหาทั้งการยกตัวอย่างและคำถามทำให้สามารถจัดกลุ่มข้อความได้อย่างถูกต้อง

การเลือกใช้ Zero-shot, One-shot และ Few-shot Prompt

                  เมื่อเราได้เห็นตัวอย่างของการยกตัวอย่างมาพอสมควร ก็พอจะเดาได้ว่าควรจะเลือก Prompt แบบไหนดี คำตอบก็คือ อยู่ที่ Use-cases หรือเป้าหมายว่าอยากได้ผลลัพธ์ในลักษณะใดนั่นเอง เช่น Zero-shot Prompt ก็จะให้ความ Creative มากกว่า มีความเป็นปลายเปิด ขณะที่ One-shot และ Few-shot Prompt จะเป็นการบอกโมเดลว่าคำตอบที่ให้เราควรมีโครงสร้างแบบไหนให้สอดคล้องกับตัวอย่าง ทำให้เราสามารถคาดหวังผลลัพธ์ในรูปแบบนั้น ๆ ไปใช้งานต่อกับ Products หรือองค์กรของเราได้

Conclusion

                    การใช้ Generative AI สร้างสรรค์ผลงานหรือคำตอบนั้นไม่มีรูปแบบที่ตายตัว นี่เป็นเพียงพื้นฐานในการออกแบบ Prompt ให้ได้ผลลัพธ์ที่ดีและมีคุณภาพมากขึ้น และหวังว่าผู้อ่านจะนำ LLM ไปปรับใช้ให้เกิดประโยชน์กับองค์กรมากขึ้น ใน EP หน้า จะพาไปรู้จักกับ Generative AI Studio บน Google Cloud เพื่อรัน Prompt ต่าง ๆ จะเป็นอย่างไรรอติดตามได้เลย

สำหรับผู้ที่สนใจในงานด้านการจัดการข้อมูล, เหมืองข้อมูล หรือปัญญาประดิษฐ์สามารถ
ติดต่อทีม Tangerine ได้ทันที
เรามีผู้เชี่ยวชาญที่พร้อมจะดูแลทุกท่านและทำให้นวัตกรรมขององค์กรเป็นจริงได้

Get a Free Consultation
Contact Form_EN Sources (#25)