提示工程的关键原则

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

清晰而精准的提示

一个示意图展示面包店与房子之间的两条路线:一条简短高效,另一条复杂低效。

使用 OpenAI API 的 Prompt Engineering

关键原则

 

  • 🎬 选择合适的动作动词
  • 💬 指令要具体且准确
  • ❗ 使用良好结构的分隔提示
使用 OpenAI API 的 Prompt Engineering

使用动作动词

  • 指导模型要做什么

包含建议动词(如 write、complete、explain、describe、evaluate)的表格。

使用 OpenAI API 的 Prompt Engineering

避免含糊动词

  • 会混淆模型的理解

包含建议使用的动词(write、complete、explain、describe、evaluate)与应避免的动词(understand、think、feel、try、know)的表格。

使用 OpenAI API 的 Prompt Engineering

结合动词的有效提示

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

编写详细指令

 

就以下方面提供具体、清晰、详细的指令

  • 📖 背景
  • 📏 输出长度
  • 🖌 格式与风格
  • 👥👥 受众
使用 OpenAI API 的 Prompt Engineering

包含指令的有效提示

低效提示"Tell me about dogs."

有效提示

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

限制输出长度

max_tokens:

  • 令牌数量上限
  • 输出不能绕过该限制
  • 可能导致不完整或被截断的回答

Prompt:

  • 限制词、句或段数
  • 输出可能绕过该限制
  • 回答通常完整
使用 OpenAI API 的 Prompt Engineering

提示的组成

  • 提供指令与要处理的输入数据
  • 例如:文本摘要
    • 指令: 概括给定文本
    • 输入数据: 待概括文本

表示文本摘要任务的图标

使用 OpenAI API 的 Prompt Engineering

用分隔符构造良好结构的提示

  • 以指令开头
  • 使用分隔符(括号、方括号、反引号等)标注输入部分
  • 说明所用分隔符
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
使用 OpenAI API 的 Prompt Engineering

使用格式化字符串(f-strings)

  • 将已定义字符串嵌入到另一字符串中
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```
使用 OpenAI API 的 Prompt Engineering

让我们来练习!

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...