Skip to content

填空提示

填空式提示 (Fill-in-the-Blank prompting) 是一种技术,您向大语言模型 (LLM) 提供一个不完整的句子或模板,模型的任务是补全缺失的部分。这种方法对于生成特定信息、完善想法或引导 LLM 生成结构化输出非常有效。

在本章中,我们将探讨填空式提示的概念,以及如何利用它与 ChatGPT 等 LLM 进行有针对性和可控的交互。

填空式提示涉及构建包含显式占位符(例如 [BLANK]、____ 或 <INSERT_HERE>)的提示,或通过在一个句子中途结束来隐式指示模型应提供缺失信息的位置。LLM 利用周围的上下文生成连贯且相关的补全内容。

  • 控制输出:让您对 LLM 响应的结构和内容有更多的控制权。
  • 上下文补全:允许 LLM 基于您提供的精确上下文填充信息,从而获得更相关的答案。
  • 特定信息提取:对于提取特定信息片段或生成符合预定义模式的文本很有用。
  • 效率:如果您知道期望答案的结构,这种方法可能比写一个完整的问题要快。

要实现填空式提示,您需要构建提示,明确指示缺失信息应放置的位置。以下是一些示例:

简单补全:

User: Complete the sentence: "The primary colors are red, yellow, and [BLANK]."
Assistant: The primary colors are red, yellow, and blue.

上下文补全:

User: In the field of artificial intelligence, a neural network is inspired by the structure of the human [BLANK].
Assistant: In the field of artificial intelligence, a neural network is inspired by the structure of the human brain.

创意补全:

User: My dream vacation involves relaxing on a beach in [COUNTRY_NAME] and exploring ancient [TYPE_OF_RUINS].
Assistant: My dream vacation involves relaxing on a beach in Greece and exploring ancient temples.

注意:虽然像 [BLANK] 这样的占位符有助于人类理解任务,但 LLM 主要依赖于句子的不完整性以及周围的上下文来生成填充内容。

  • 数据提取:“ExampleCorp 的 CEO 是 [NAME]。” -> LLM 填充姓名。
  • 内容生成:创建产品描述:“我们的新款 [PRODUCT_TYPE] 非常适合想要 [BENEFIT] 的 [TARGET_AUDIENCE]。”
  • 教育练习:语言学习:“翻译成法语:‘Good morning’ 是 ‘[FRENCH_PHRASE]’。”
  • 少样本提示 (Few-Shot Prompting):以填空格式提供示例可以帮助 LLM 快速理解新任务。

为了充分利用填空式提示,请考虑以下最佳实践:

  • 提供足够的上下文:周围的文本应提供清晰的线索,说明需要填入哪种类型的信息。
  • 占位符要具体(可选但有益):虽然对 LLM 并非绝对必要,但描述性占位符(例如 [CITY_NAME]、[YEAR])可以使提示对人类用户更清晰,有时也能更精确地引导 LLM。
  • 保持自然:即使有空白,句子结构也应流畅自然。生硬的措辞可能会混淆 LLM。
  • 测试和迭代:尝试不同的措辞和上下文量,看看哪种方法对您的特定任务产生最佳结果。

让我们探索一个使用 OpenAI API 来完成句子的 Python 示例。

from openai import OpenAI
client = OpenAI(api_key='YOUR_API_KEY') # 确保 API 密钥已设置
def complete_the_blank(prompt_with_blank):
try:
response = client.chat_completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an AI assistant that completes sentences and fills in blanks."},
{"role": "user", "content": prompt_with_blank}
],
max_tokens=50, # 根据需要调整
temperature=0.5, # 适中的 temperature 值以获得合理的补全
n=1,
stop=None
)
# 响应可能就是补全后的完整句子。
# 或者它可能只提供填充内容。这里我们假设它会完成整个句子。
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {e}"
# 示例 1: 简单空白
user_prompt_1 = "User: Complete this: The chemical symbol for water is [BLANK]."
# 对于 API,我们可能将指令和句子分开发送或组合发送。
# 我们假设我们要求模型完成它接收到的句子。
api_prompt_1 = "The chemical symbol for water is"
# 示例 2: 更多上下文
user_prompt_2 = "User: Fill in the blank: To make a website interactive, developers often use JavaScript, which is a [BLANK] programming language."
api_prompt_2 = "To make a website interactive, developers often use JavaScript, which is a"
# 在此示例中,为了 API 调用的清晰起见,我们将发送完整的用户指令。
full_user_instruction = "Complete the following sentence: To make a website interactive, developers often use JavaScript, which is a ____ programming language."
completed_sentence = complete_the_blank(full_user_instruction)
print(f"Prompt: {full_user_instruction}")
print(f"Completed: {completed_sentence}")

使用 Python 示例中的 full_user_instruction:

Prompt: Complete the following sentence: To make a website interactive, developers often use JavaScript, which is a ____ programming language.
Completed: To make a website interactive, developers often use JavaScript, which is a client-side scripting programming language.

(注意:具体的填充内容可能有所不同。其他有效的补全可能包括“多用途的”、“流行的”、“高级的”等。提供更多上下文或更具体的占位符可以缩小范围。)

填空式提示是提示工程中一种多功能且强大的技术。通过策略性地让提示的部分内容不完整,您可以引导 LLM 生成特定、上下文相关的 信息,从而提高交互效率并更好地控制输出。这是利用 LLM 理解和补全模式能力的基础方法。