写好提示词

1087 字
5 分钟
写好提示词

Prompt Engineering = improving prompts to get more reliable, higher-quality outputs from language models.

Being Clear and Direct#

Key components = Action verb at start + direct task statement + expected output details.

  • 避免提问,也避免说“不要做某件事”,直接告诉模型要做什么。
  • 例如:Instead of “Can you summarize this article?”, say “Summarize the following article in 3 sentences: [article text]”.

Examples:

  • “Write three paragraphs about how solar panels work”
  • “Identify three countries that use geothermal energy and for each include generation stats”
  • “Generate a one day meal plan for an athlete that meets their dietary restrictions”

Being Specific#

  1. Type A (Attributes) = list qualities/attributes desired in output (length, structure, format)
    • 适用于一般情况下的大多数任务,帮助模型理解输出的具体要求。
  2. Type B (Steps) = provide specific steps for model to follow in reasoning process
    • 通常用于复杂任务,帮助模型分解问题并逐步解决。
    • 需要模型有更广阔的视角,能够考虑更多因素并进行更深入的分析。

Structure with XML Tags#

一种已经在现代模型中能够以几乎确定性的方式被正确解析的结构化提示方法,通过结构化的标签区分内容边界,而非依靠模型对自然语言提示的理解推理边界。

对于传统的自然语言提示,边界是隐式的,以理解 “First analyze the data then provide recommendations.” 这句话的语义来推断出 “analyze” 和 “recommendations” 为例,逐词解析时对每一步的隶属关系都保持着“不确定性”,假设模型在解析时对每个词的概率分布如下:

词P(analysis)P(recommendations)
First0.950.05
analyze0.90.1
the0.850.15
data0.80.2
then0.40.6
provide0.20.8
recommendations0.10.9

其中,“then” 是一个边界词 (boundary token),这就是边界不确定性出现的时刻,这种不确定性会向前传播,导致模型可能错误地将 “provide” 归到 “analysis” 的部分,从而导致后续对 “recommendations” 的理解也受到影响。

而 XML 标签直接在算法层面,让模型能直接通过语法解析来区分不同部分的内容,消除了边界不确定性,例如:

<analysis>
analyze the data
</analysis>
<recommendations>
provide recommendations
</recommendations>

它的可能解析结果如下:

tokenP(analysis)P(recommendations)
<analysis>10
analyze10
the10
data10
</analysis>10
<recommendations>01
provide01
recommendations01
</recommendations>01

概率经过标签直接跳变,直接阻断了边界不确定性的传播。

几点建议:

  • Tag naming = Use descriptive, specific tag names (e.g., “sales_records” better than “data”) to provide context about content nature.
  • Example use case = Debugging prompt with mixed code and documentation becomes clearer when separated into <my_code> and tags.

Providing Examples#

即 Few-shot,通过提供示例来引导模型理解任务要求和预期输出格式。

  • 为边界情况提供示例,帮助模型理解如何处理特殊或复杂的输入。
  • 提供的示例包括对为什么该输出理想的解释。
  • 将示例放在主要提示和指导原则之后。

Chain of Thought (CoT)#

进一步的,Chain of Thought (CoT) 是一种通过引导模型逐步推理来提高复杂任务性能的提示方法。它通过让模型在回答前先生成一系列中间推理步骤,从而帮助模型更好地理解问题并生成更准确的答案。只需要在提示词末尾加入:Let’s think step by step. 即可。

参数调整#

  1. Temperature = 控制输出的随机性,值越高,输出越随机;值越低,输出越确定。
  2. Top-p (nucleus sampling) = 控制输出的多样性,值越高,模型会考虑更多冷门词汇,输出越多样化;值越低,模型只选最有把握的那部分词汇,输出越集中;
    • 注意:Temperature 和 Top-p 一般不同时使用,通常选择其中一个进行调整;
  3. Max tokens = 控制输出的最大长度,避免输出过长。
  4. Stop Sequences = 控制输出的终止条件,指定一个字符串,让模型生成到这里就自动停下来。
  5. Frequency Penalty = 控制输出中重复词汇的惩罚,值越高,模型越不容易重复输出相同的词汇,适合减少单词重复的情况。
  6. Presence Penalty = 控制输出中是否出现新词汇的惩罚,只要出现过,惩罚力度都一样,不管出现了几次,值越高,模型能不断引入新话题、更有创意。
    • 注意:Frequency Penalty 和 Presence Penalty 一般不同时使用,通常选择其中一个进行调整。

其他#

  • 设定角色:你是谁、它是谁、结果给谁看;
  • 给定上下文:提供相关背景信息,帮助模型理解任务的背景和限制条件;

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

写好提示词
https://blog.yokumi.cn/posts/llm/prompt-engineering/
作者
Yokumi
发布于
2025-04-15
许可协议
CC BY-NC-SA 4.0

评论区

文章目录