结构化输出与条件式提示

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

结构化输出

  • 仅当给出明确指令时,LLM 才会生成结构化输出
  • 输出结构:
    • 表格
    • 列表
    • 结构化段落
    • 自定义格式

可视化图示:当我们要求 ChatGPT 提供结构化输出时,会得到所需结构;未指定结构时,答案随机或无结构。

使用 OpenAI API 的 Prompt Engineering

表格

  • 清晰说明所需列
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    |
使用 OpenAI API 的 Prompt Engineering

列表

  • 适用于枚举
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
使用 OpenAI API 的 Prompt Engineering

列表

  • 需在提示中说明编号要求
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
使用 OpenAI API 的 Prompt Engineering

结构化段落

  • 在提示中说明结构要求
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))
使用 OpenAI API 的 Prompt Engineering

结构化段落

I. 引言
经常锻炼对保持健康与整体幸福至关重要。[...]

II. 身体健康益处
a. 体重管理:规律锻炼有助于 [...]
b. 降低慢性病风险:锻炼与 [...]
c. 提高力量与灵活性:规律锻炼可增强肌力并 [...]

III. 心理健康益处
a. 减压与缓解焦虑:锻炼对 [...] 有显著影响
b. 改善睡眠质量:规律锻炼与更佳的睡眠模式 [...] 相关
c. 增强脑功能:研究表明锻炼可提升认知功能 [...]
使用 OpenAI API 的 Prompt Engineering

自定义输出格式

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
使用 OpenAI API 的 Prompt Engineering

条件式提示

  • 融入逻辑或条件
  • 条件式提示采用 if-else 风格

可视化图示:显示测试条件。若条件为真,执行 X;若为假,执行 Y。

使用 OpenAI API 的 Prompt Engineering

条件式提示

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
使用 OpenAI API 的 Prompt Engineering

条件式提示

  • 可包含多个条件
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))
使用 OpenAI API 的 Prompt Engineering

条件式提示

  • 可包含多个条件
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))
使用 OpenAI API 的 Prompt Engineering

条件式提示

  • 可包含多个条件
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
使用 OpenAI API 的 Prompt Engineering

¡Vamos a practicar!

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...