Đầu ra có cấu trúc và nhắc lệnh có điều kiện

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Đầu ra có cấu trúc

  • LLM chỉ tạo đầu ra có cấu trúc khi có hướng dẫn rõ ràng
  • Cấu trúc đầu ra:
    • Bảng
    • Danh sách
    • Đoạn văn có cấu trúc
    • Định dạng tùy chỉnh

Sơ đồ minh họa rằng khi yêu cầu ChatGPT đầu ra có cấu trúc, ta nhận được cấu trúc mong muốn; còn nếu không chỉ định, câu trả lời có thể ngẫu nhiên hoặc không có cấu trúc.

Kỹ thuật Prompt với OpenAI API

Bảng

  • Nêu rõ cột mong muốn
prompt = "Generate a table containing 5 movies I should watch if I am an action 
lover, with columns for Title and Rating."
print(get_response(prompt))
| Title                           | Rating |
|---------------------------------|--------|
| Mad Max: Fury Road              | 8.1    |
| John Wick                       | 7.4    |
| The Dark Knight                 | 9.0    |
| Die Hard                        | 8.2    |
| Gladiator                       | 8.5    |
Kỹ thuật Prompt với OpenAI API

Danh sách

  • Hữu ích cho liệt kê
prompt = "Generate a list containing the names of the top 5 cities to visit."
print(get_response(prompt))
1. Tokyo, Japan
2. Paris, France
3. Rome, Italy
4. New York City, United States
5. Sydney, Australia
Kỹ thuật Prompt với OpenAI API

Danh sách

  • Cần nêu rõ yêu cầu đánh số trong nhắc lệnh
prompt = "Generate an unordered list containing the names of the top 5 cities to visit."
print(get_response(prompt))
- Paris, France
- Tokyo, Japan
- Rome, Italy
- New York City, United States
- Sydney, Australia
Kỹ thuật Prompt với OpenAI API

Đoạn văn có cấu trúc

  • Nêu yêu cầu về cấu trúc trong nhắc lệnh
prompt = "Provide a structured paragraph with clear headings and subheadings 
about the benefits of regular exercise on overall health and well-being."
print(get_response(prompt))
Kỹ thuật Prompt với OpenAI API

Đoạn văn có cấu trúc

I. Giới thiệu
Tập thể dục thường xuyên rất cần thiết để duy trì sức khỏe và hạnh phúc tổng thể. [...]

II. Lợi ích thể chất
a. Quản lý cân nặng: Tập thể dục thường xuyên [...]
b. Giảm nguy cơ bệnh mạn tính: Tập thể dục có liên quan đến [...]
c. Tăng sức mạnh và linh hoạt: Tập thể dục thường xuyên cải thiện sức mạnh cơ bắp và [...]

III. Lợi ích tinh thần
a. Giảm căng thẳng và lo âu: Tập thể dục có tác động sâu sắc đến [...]
b. Cải thiện chất lượng giấc ngủ: Tập thể dục thường xuyên liên quan đến giấc ngủ tốt hơn [...]
c. Tăng cường chức năng não: Nghiên cứu cho thấy tập thể dục nâng cao nhận thức [...]
Kỹ thuật Prompt với OpenAI API

Định dạng đầu ra tùy chỉnh

text = "Once upon a time in a quaint little village, there lived a curious young boy named 
David. David was [...]"

instructions = "You will be provided with a text delimited by triple backticks. Generate a suitable title for it. "
output_format = """Use the following format for the output: - Text: <text we want to title> - Title: <the generated title>"""
prompt = instructions + output_format + f"```{text}```" print(get_response(prompt))
- Text: Once upon a time in a quaint little village, there lived a curious young boy [...]
- Title: The Extraordinary Adventure of David and the Mysterious Book
Kỹ thuật Prompt với OpenAI API

Nhắc lệnh có điều kiện

  • Lồng ghép logic hoặc điều kiện
  • Nhắc lệnh có điều kiện theo kiểu if-else

Sơ đồ minh họa một điều kiện kiểm tra. Nếu điều kiện đúng, thực hiện X; nếu sai, thực hiện Y.

Kỹ thuật Prompt với OpenAI API

Nhắc lệnh có điều kiện

text = "Le printemps est ma saison préférée. Quand les premières fleurs commencent 
à éclore, et que les arbres se parent de feuilles vertes et tendres, je me sens 
revivre [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, suggest a suitable title for it.
         Otherwise, write 'I only understand English'.
         ```{text}```"""

print(get_response(prompt))
I only understand English
Kỹ thuật Prompt với OpenAI API

Nhắc lệnh có điều kiện

  • Có thể lồng nhiều điều kiện
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 


         ```{text}```"""
print(get_response(prompt))
Kỹ thuật Prompt với OpenAI API

Nhắc lệnh có điều kiện

  • Có thể lồng nhiều điều kiện
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.

         ```{text}```"""
print(get_response(prompt))
Kỹ thuật Prompt với OpenAI API

Nhắc lệnh có điều kiện

  • Có thể lồng nhiều điều kiện
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.
         If the text is not written in English, reply with 'I only understand English'.
         ```{text}```"""
print(get_response(prompt))
Keyword not found
Kỹ thuật Prompt với OpenAI API

Ayo berlatih!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...