Gợi ý few-shot

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Gợi ý few-shot

  • Mô hình được cung cấp các ví dụ (cặp hỏi–đáp)

Minh họa trực quan về một prompt few-shot cho thấy cấu trúc với các câu hỏi và câu trả lời ví dụ, và cuối cùng là câu hỏi cần mô hình trả lời.

Kỹ thuật Prompt với OpenAI API

Gợi ý few-shot

  • Mô hình được cung cấp các ví dụ (cặp hỏi–đáp)

Sơ đồ trực quan cho thấy cách gửi một prompt few-shot tới LLM.

Kỹ thuật Prompt với OpenAI API

Gợi ý few-shot

  • Mô hình được cung cấp các ví dụ (cặp hỏi–đáp)

Sơ đồ trực quan cho thấy cách gửi prompt few-shot tới LLM và nhận câu trả lời.

Kỹ thuật Prompt với OpenAI API

Gợi ý few-shot

  • Mô hình được cung cấp các ví dụ (cặp hỏi–đáp)

Sơ đồ trực quan cho thấy cách gửi prompt few-shot tới LLM và nhận câu trả lời.

  • Số lượng ví dụ:
    • 0 -> zero-shot prompting
    • 1 -> one-shot prompting
    • 1 -> few-shot prompting

Kỹ thuật Prompt với OpenAI API

Gợi ý zero-shot

  • Cung cấp prompt không có ví dụ
  • Mô hình trả lời dựa trên kiến thức của mình
  • Lý tưởng cho tác vụ nhanh, đơn giản
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.
Kỹ thuật Prompt với OpenAI API

Gợi ý one-shot

  • Cung cấp cho mô hình một ví dụ
  • Hữu ích để giữ định dạng hoặc phong cách nhất quán
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
Kỹ thuật Prompt với OpenAI API

Gợi ý one-shot

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
Kỹ thuật Prompt với OpenAI API

Gợi ý few-shot

  • Cung cấp nhiều hơn một ví dụ
  • Mạnh cho các tác vụ theo ngữ cảnh
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

"""

Kỹ thuật Prompt với OpenAI API

Gợi ý few-shot

  • Cung cấp nhiều hơn một ví dụ
  • Mạnh cho các tác vụ theo ngữ cảnh
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
Kỹ thuật Prompt với OpenAI API

Few-shot với mô hình chat

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
Kỹ thuật Prompt với OpenAI API

Cân nhắc

  • Chọn số lượng shots theo độ phức tạp tác vụ
    • Ít shots -> tác vụ cơ bản
    • Shots đa dạng -> tác vụ phức tạp

Hình ảnh một người đang tự đặt câu hỏi.

Kỹ thuật Prompt với OpenAI API

Let's practice!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...