Few-shot prompting

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

Few-shot prompting

  • โมเดลได้รับตัวอย่าง (คู่คำถาม-คำตอบ)

ภาพแสดงโครงสร้างของ few-shot prompt พร้อมตัวอย่างคำถามและคำตอบ และคำถามที่ต้องการให้โมเดลตอบ

Prompt Engineering กับ OpenAI API

Few-shot prompting

  • โมเดลได้รับตัวอย่าง (คู่คำถาม-คำตอบ)

ไดอะแกรมแสดงการส่ง few-shot prompt ไปยัง LLM

Prompt Engineering กับ OpenAI API

Few-shot prompting

  • โมเดลได้รับตัวอย่าง (คู่คำถาม-คำตอบ)

ไดอะแกรมแสดงการส่ง few-shot prompt ไปยัง LLM และรับคำตอบกลับ

Prompt Engineering กับ OpenAI API

Few-shot prompting

  • โมเดลได้รับตัวอย่าง (คู่คำถาม-คำตอบ)

ไดอะแกรมแสดงการส่ง few-shot prompt ไปยัง LLM และรับคำตอบกลับ

  • จำนวนตัวอย่าง:
    • ศูนย์ -> zero-shot prompting
    • หนึ่ง -> one-shot prompting
    • มากกว่าหนึ่ง -> few-shot prompting
Prompt Engineering กับ OpenAI API

Zero-shot prompting

  • ส่ง prompt โดยไม่มีตัวอย่าง
  • โมเดลตอบโดยอาศัยความรู้ของตัวเอง
  • เหมาะกับงานที่รวดเร็วและไม่ซับซ้อน
prompt = "What is prompt engineering?"
print(get_response(prompt))
Prompt engineering refers to designing and refining prompts or instructions given 
to a language model like ChatGPT to elicit desired responses or behaviors. It 
involves formulating specific guidelines or hints to guide the model's output 
towards a desired outcome.
Prompt Engineering กับ OpenAI API

One-shot prompting

  • ให้ตัวอย่างแก่โมเดลหนึ่งตัวอย่าง
  • มีประโยชน์สำหรับการกำหนดรูปแบบหรือสไตล์ที่สม่ำเสมอ
prompt =  """ 
Q: Sum the numbers 3, 5, and 6. A: 3+5+6=14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
2+4+7=13
Prompt Engineering กับ OpenAI API

One-shot prompting

prompt = """
Q: Sum the numbers 3, 5, and 6. A: The sum of 3, 5, and 6 is 14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
The sum of 2, 4, and 7 is 13
Prompt Engineering กับ OpenAI API

Few-shot prompting

  • ให้ตัวอย่างมากกว่าหนึ่งตัวอย่าง
  • มีประสิทธิภาพสูงสำหรับงานที่ต้องใช้บริบท
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative

"""

Prompt Engineering กับ OpenAI API

Few-shot prompting

  • ให้ตัวอย่างมากกว่าหนึ่งตัวอย่าง
  • มีประสิทธิภาพสูงสำหรับงานที่ต้องใช้บริบท
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative
Text: That shot selection was awful -> Classification: 
"""
print(get_response(prompt))
negative
Prompt Engineering กับ OpenAI API

Few-shot prompting กับ chat model

response = client.chat.completions.create(
  model = "gpt-4o-mini",

messages = [{"role": "user", "content": "Today the weather is fantastic"},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "I don't like your attitude"}, {"role": "assistant", "content": "negative"},
{"role": "user", "content": "That shot selection was awful"} ], temperature = 0 )
print(response.choices[0].message.content)
negative
Prompt Engineering กับ OpenAI API

ข้อควรพิจารณา

  • เลือกจำนวน shot ตามความซับซ้อนของงาน
    • Shot น้อย -> งานพื้นฐาน
    • Shot หลากหลาย -> งานซับซ้อน

ภาพแสดงบุคคลกำลังตั้งคำถามกับตัวเอง

Prompt Engineering กับ OpenAI API

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

Prompt Engineering กับ OpenAI API

Preparing Video For Download...