Các nguyên tắc chính của thiết kế prompt

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Prompt rõ ràng, chính xác

Sơ đồ cho thấy một tiệm bánh và một ngôi nhà với hai tuyến đường giữa chúng: một tuyến đơn giản, ngắn (hiệu quả) và một tuyến phức tạp, dài (kém hiệu quả).

Kỹ thuật Prompt với OpenAI API

Nguyên tắc chính

 

  • 🎬 Động từ hành động phù hợp
  • 💬 Hướng dẫn chi tiết, chính xác
  • ❗ Prompt có cấu trúc, dùng dấu phân tách tốt
Kỹ thuật Prompt với OpenAI API

Dùng động từ hành động

  • Hướng dẫn mô hình cần làm gì

Bảng các động từ nên dùng gồm write, complete, explain, describe, hoặc evaluate.

Kỹ thuật Prompt với OpenAI API

Tránh động từ mơ hồ

  • Gây nhiễu hiểu biết của mô hình

Bảng các động từ nên dùng gồm write, complete, explain, describe, hoặc evaluate, và các động từ cần tránh như understand, think, feel, try, và know.

Kỹ thuật Prompt với OpenAI API

Prompt hiệu quả với động từ

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

Soạn hướng dẫn chi tiết

 

Đưa ra hướng dẫn cụ thể, mô tả, chi tiết về:

  • 📖 Ngữ cảnh
  • 📏 Độ dài đầu ra
  • 🖌 Định dạng và phong cách
  • 👥👥 Đối tượng
Kỹ thuật Prompt với OpenAI API

Prompt hiệu quả với hướng dẫn

Prompt kém hiệu quả: "Tell me about dogs."

Prompt hiệu quả

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

Giới hạn độ dài đầu ra

max_tokens:

  • Giới hạn số token
  • Đầu ra không thể vượt quá
  • Có thể dẫn đến câu trả lời thiếu/đứt đoạn

Prompt:

  • Giới hạn theo từ, câu, hoặc đoạn
  • Đầu ra có thể vượt quá
  • Câu trả lời trọn vẹn
Kỹ thuật Prompt với OpenAI API

Thành phần của prompt

  • Hướng dẫndữ liệu đầu vào để xử lý
  • Ví dụ: tóm tắt văn bản
    • Hướng dẫn: tóm tắt văn bản đã cho
    • Dữ liệu đầu vào: văn bản cần tóm tắt

Biểu tượng minh họa tác vụ tóm tắt văn bản

Kỹ thuật Prompt với OpenAI API

Tạo prompt có cấu trúc với dấu phân tách

  • Bắt đầu prompt bằng hướng dẫn
  • Dùng dấu phân tách (ngoặc, ngoặc vuông, backticks, v.v.) để chỉ định phần đầu vào
  • Nêu rõ đang dùng dấu phân tách nào
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
Kỹ thuật Prompt với OpenAI API

Dùng f-string (formatted string)

  • Chèn chuỗi đã định nghĩa vào chuỗi khác
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```
Kỹ thuật Prompt với OpenAI API

Ayo berlatih!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...