หลักการสำคัญของ prompt engineering

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

Prompt ที่ชัดเจนและแม่นยำ

ภาพไดอะแกรมแสดงร้านเบเกอรีและบ้านพร้อมเส้นทาง 2 แบบ แบบแรกสั้นและตรง (มีประสิทธิภาพ) แบบที่สองวกวนและซับซ้อน (ไม่มีประสิทธิภาพ)

Prompt Engineering กับ OpenAI API

หลักการสำคัญ

 

  • 🎬 ใช้ action verb ที่เหมาะสม
  • 💬 คำสั่งที่ละเอียดและแม่นยำ
  • ❗ Prompt ที่มีโครงสร้างชัดเจนพร้อม delimiter
Prompt Engineering กับ OpenAI API

การใช้ action verb

  • ชี้นำให้โมเดลรู้ว่าต้องทำอะไร

ตารางแสดง action verb ที่แนะนำให้ใช้ เช่น write, complete, explain, describe, evaluate

Prompt Engineering กับ OpenAI API

หลีกเลี่ยง verb ที่คลุมเครือ

  • ทำให้โมเดลเข้าใจผิด

ตารางแสดง action verb ที่แนะนำ เช่น write, complete, explain, describe, evaluate และ verb ที่ควรหลีกเลี่ยง เช่น understand, think, feel, try, know

Prompt Engineering กับ OpenAI API

Prompt ที่มีประสิทธิภาพด้วย action verb

prompt = "Think about the issue of 
deforestation."
response = get_response(prompt)
print(response)
Deforestation is a significant 
environmental issue involving 
permanently removing
or destroying forests and woodlands. 
It has far-reaching impacts on the 
environment, ecosystems, wildlife, 
and human communities. [...]
prompt = "Propose strategies to 
reduce deforestation."
response = get_response(prompt)
print(response)
Reducing deforestation requires 
a comprehensive and multi-dimensional 
approach involving various stakeholders. 
Here are several strategies 
that can help address the issue:
Strengthen Forest Governance
Promote Sustainable Land Use [...]
Prompt Engineering กับ OpenAI API

การกำหนดคำสั่งที่ละเอียด

 

ระบุคำสั่งที่ชัดเจน ละเอียด และครบถ้วน ครอบคลุม:

  • 📖 บริบท
  • 📏 ความยาวของผลลัพธ์
  • 🖌 รูปแบบและสไตล์
  • 👥👥 กลุ่มเป้าหมาย
Prompt Engineering กับ OpenAI API

Prompt ที่มีประสิทธิภาพด้วยคำสั่งละเอียด

Prompt ที่ไม่มีประสิทธิภาพ: "Tell me about dogs."

Prompt ที่มีประสิทธิภาพ

prompt = "Write a descriptive paragraph about the behavior and characteristics of 
Golden Retrievers, highlighting their friendly nature, intelligence, 
and suitability as family pets."
print(get_response(prompt))
Golden Retrievers are beloved worldwide for their exceptional behavior, remarkable 
characteristics, and friendly nature. They are highly intelligent, trainable, and 
adaptable, making them great companions for families, including those with children.
Prompt Engineering กับ OpenAI API

การจำกัดความยาวของผลลัพธ์

max_tokens:

  • จำกัดจำนวน token
  • ผลลัพธ์ไม่สามารถเกินค่านี้ได้
  • อาจทำให้ผลลัพธ์ไม่ครบหรือถูกตัดกลางคัน

Prompt:

  • จำกัดจำนวนคำ ประโยค หรือย่อหน้า
  • ผลลัพธ์อาจเกินที่กำหนดได้
  • ผลลัพธ์ครบถ้วน
Prompt Engineering กับ OpenAI API

องค์ประกอบของ prompt

  • คำสั่ง และ ข้อมูล input ที่ต้องการประมวลผล
  • ตัวอย่าง: การสรุปข้อความ
    • คำสั่ง: สรุปข้อความที่กำหนด
    • ข้อมูล input: ข้อความที่ต้องการสรุป

ไอคอนแสดงงานสรุปข้อความ

Prompt Engineering กับ OpenAI API

การสร้าง prompt ที่มีโครงสร้างดีด้วย delimiter

  • เริ่ม prompt ด้วยคำสั่ง
  • ใช้ delimiter (วงเล็บ, วงเล็บเหลี่ยม, backtick ฯลฯ) เพื่อระบุส่วนต่าง ๆ ของ input
  • ระบุว่าใช้ delimiter ชนิดใด
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
Prompt Engineering กับ OpenAI API

การใช้ formatted string (f-string)

  • แทรก string หนึ่งเข้าไปใน string อื่น
text = "This is a sample text to summarize"

prompt = f"""Summarize the text delimited by triple backticks into bullet points. ```{text}```"""
print(prompt)
Summarize the text delimited by triple backticks into bullet points.
```This is a sample text to summarize```
Prompt Engineering กับ OpenAI API

มาฝึกกันเถอะ!

Prompt Engineering กับ OpenAI API

Preparing Video For Download...