Prompt Engineering
文章目录
Prompt Engineering 提示工程
参考:
- ChatGPT火爆,最全prompt工程指南登GitHub热榜,标星4.7k! - 知乎
- GitHub - dair-ai/Prompt-Engineering-Guide: Guides, papers, lecture, and resou…
官方教程
- 由吴恩达和 openai 员工一起完成
- 包含 jupyter notebook 演示
文档:
自动提示词工程工具
参考:
GitHub - stanfordnlp/dspy: DSPy: The framework for programming—not prompting—…
- DSPy: 自动优化 pipeline 中使用到的 prompt
博客
RAG 重排:
prompt 组成
成分:
- Instructions 指令
- Context 上下文
- Input data 输入数据
- Output indicator 输出指示器

技巧
Few-shot prompts(小样本举例 prompt)
- 在 prompt 中举例子
- 举例子能更准确的告诉 chatgpt 要做什么,只有描述性 instruct 态隐晦,不够具体
概念解释:
- few-shot learning 是少量样本学习的意思
- zero-shot learning 零样本学习
- in-context learning 上下文学习
参考:
Chain-of-thought (CoT) prompting(思想链 prompt)
参考:
例子中有推导出结果的步骤,而不是只有问题和结果
# 这是 few-shot prompt The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: The answer is False. # 这是 CoT prompt, The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False. # ---> Adding all the odd numbers (9, 15, 1) gives 25 --> 这是推导步骤
zero-shot Chain-of-thought prompting(零样本思想链 prompt)
参考:
# 直接发问 prompt I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with? # zero-shot CoT 方法 prompt I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with? Let's think step by step. # 这里强制chatgpt 分布处理
Self-Consistency(一致性)
提供多个同一类型但解决方法不同的例子,帮助 chatgpt 找到合适的解决方案
to replace the naive greedy decoding used in chain-of-thought prompting
- 把 CoT 方法的简单问题分解,替代成多方案选择最优方案
- to sample multiple, diverse reasoning paths through few-shot CoT
- and use the generations to select the most consistent answer.
个人理解:
- 即,找多个解题方法,让 chatgpt 选择一个最合适的方法
Generated Knowledge Prompting(生成知识 prompt)
- 先教会 chatgpt 如何生成知识(如何解释用户要发问的现象), 通过 few-shot 举例
教会之后,再
- 让 chatgpt 解释发问的现象
- 像 chatgpt 发问,要结果的问题
Automatic Prompt Engineer (APE, 自动 prompt 工程)
限制 chatgpt 说话啰嗦
给定回答,不允许说句子
Give the unit only, do not use a full sentence. Give the material name only, do not use a full sentence. Answer "Yes" or "No" only.
禁止解释
Do not write explanations or examples of how the regular expressions work; simply provide only the regular expressions themselves. No explanations.
论文解读
应届生招聘广告分类
英语:
posting
- 网络帖子
- 公告,招聘广告
最终的 prompt
system = "You are Frederick, an AI expert in career advice. You
are tasked with sorting through jobs by analysing their content
and deciding whether they would be a good fit for a recent
graduate or not.",
user 1 =
"""A job is fit for a graduate if it's a
junior-level position that does not require extensive prior
professional experience.
When analysing the experience required, take into account that
requiring internships is still fit for a graduate. I will
give you a job posting and you will analyse it, step-by-step,
to know whether or not it describes a position fit for a
graduate. Got it?"""
assistant 1 = "Yes, I understand. I am Frederick, and I will
analyse your job posting.",
user 2 = """Great! Let's begin then :)
For the given job:
{job_posting}
---------
Is this job (A) a job fit for a recent graduate, or
(B) a job requiring more professional experience.
Answer: Let's think step by step to reach the right
conclusion"""
prompt 技巧
技巧分类:
推理:诱导 GPT 推理分析问题
CoT
- Few-Shot CoT
- Zero-Shot CoT
baseline 方法
Provide a a job posting and asking if it is fit for a graduate.
直接提供招聘广告,学问是否适合应届毕业生
prompt:
For the given job:
{job_posting}
---------
Is this job (A) a job fit for a recent graduate,
or (B) a job requiring more professional experience.
Answer:
诱导推论(CoT 思考链方法)
Few-shot CoT 方法
给出一个例子广告,对这个例子广告做推理分析,让 GPT 学习这个例子,然后执行任务
步骤:
- 提供广告
- 指令 instruct
- CoT 解决问题例子
- 需要真正处理的广告
prompt:
user message 1 = """For the given job:
{job posting}
---------
Is this job (A) a job fit for a recent graduate, or (B) a job
requiring more professional experience."""
assistant message 1 = "This job appears to be a senior position,
as it mentions requiring experience interacting with C-level
stakeholder in intense environments and [...]. Therefore,
this is (B) a job requiring more professional experience
user message 2 = [...]
Zero-shot CoT
无 CoT 步骤说明,通过一些语言诱导 GPT 逐步分析问题
实例:
- Let's think step by step
prompt:
For the given job:
{job_posting}
---------
Is this job (A) a job fit for a recent graduate,
or (B) a job requiring more professional experience.
Answer: Let's think step by step,
Instruction 处理技巧
成分(instruction 包括两部分内容):
role
- GPT 担任的角色,比如合理是一个职业建议专家
task
- GPT 需要完成的任务,即具体需要完成什么事情,比如:这里是完成职位广告分类
prompt:
role = """You are an AI expert in career advice. You are tasked with sorting through jobs by analysing their content and deciding whether they would be a good fit for a recent graduate or not.""" task = """A job is fit for a graduate if it's a junior-level position that does not require extensive prior professional experience. I will give you a job posting and you will analyse it, to know whether or not it describes a position fit for a graduate.""
instruction 放置位置
直接把 instruction 放到对话的正文(user query)中,即 user 的 message 内容中
把 instruction 整个放到 system query (system role 角色) 中,即 system 的 message 内容中
把 Instruction 两个部分(user, system) 都放一部分。
分配方法:
- system query: role
- user query: task
mock (Mocked-exchange 欺骗交谈)
你自己先问 Assistant 是否理解了,再假装自己是 Assistant, 回答理解了
注意:这是基于 bothinst 的技巧。
举例:
- User: Got it?
- Assistant: Yes, I understand.
prompt:
user_message_1 = """A job is fit for a graduate [...] Got it?""" assistant message 1 = "Yes, I understand. I am ready to analyse your job posting.
reit (Re-Iterating Instructions 重复指令)
把 instruction 直接(或换一种说法)重复一遍,以加强确保 GPT 明白指令
举例:
- 把任务角色再强调一遍
- 把 CoT, 强调一遍: step-by-step
prompt:
| |
Wording the prompt (prompt 中的措辞)
Anwser template (回答问题的模板)
prompt:
loose = """[...]Your answer must end with: Final Answer: This is a (A) job fit for a recent graduate ora student OR (B) a job requiring more professional experience. Answer: Let's think step-by-step,""" strict = """[...]You will answer following this template: Reasoning step 1:\n Reasoning step 2:\n Reasoning step 3:\n Final Answer: This is a (A) job fit for a recent graduate ora student OR (B) a job requiring more professional experience. Answer: Reasoning Step 1:"""
只要求最终答案,推理等过程不做规范
实践发现的特点:
- 这种方法会导致,GPT 在得到最终答案前自由发挥
可能的回答方式
- 分步骤分析(步骤步数不定),得到最终答案
- 可能一句话完成分析,得到最终答案
可能的原因(暂时没有一一验证):
- 有用使用了 analyze 和 step-by-step
prompt:
loose = """[...]Your answer must end with: Final Answer: This is a (A) job fit for a recent graduate ora student OR (B) a job requiring more professional experience. Answer: Let's think step-by-step,"""
严格规范回答的格式
prompt:
strict = """[...]You will answer following this template: Reasoning step 1:\n Reasoning step 2:\n Reasoning step 3:\n Final Answer: This is a (A) job fit for a recent graduate ora student OR (B) a job requiring more professional experience. Answer: Reasoning Step 1:"""
right (the right conclusion, 正确答案)
再次强调,通过推理得到正确答案(最终答案)
prompt:
Answer: Let's think step-by-step to reach the right conclusion,
info (Addressing reasoning gaps)
针对 GPT 对 instruct 产生误解的问题,提供 额外信息 解决
prompt:
| |
其他微妙技巧(subtle tweaks)
name (给 GPT 起一个名字)
prompt:
| |
pos (Positive Feedback)
给 GPT 的回答一个积极的鼓励
prompt:
| |
评估 ChatGPT IE(信息抽取能力)
论文: Evaluating ChatGPT's Information Extraction Capabilities: An Assessment of Performance, Explainability, Calibration, and Faithfulness
任务类 prompt
RE 关系抽取
prompt:
| |
NER 命名实体识别
prompt:
| |
回答结果是否可信排端 prompt
confidence 置信度
prompt:
| |
reason 原因生成
prompt:
| |
reasonable 是否解释的通
| |
fictitous 是否是瞎说 (无中生有)
| |
慎用引号
引号可能会导致 chatgpt 不识别引号内部的内容
- eg: "I d", 可能导致明明输入文本中包含 I d, 但是 gpt-3.5-turbo 不识别
多个变形词列举
错误方式:The quantity name has many variants like dark current, dark currents, darkcurrent.
- 导致不识别句子:to overcome the disadvantage of large depletion dark currents.
- 改进方式:The quantity name has many variants like dark currents, darkcurrent, etc.
符号语意处理
符号语义不是给定语义的说明:
错误说法:
If the given text contain the symbol you should anaylize its meaing first. If the given text contain "η" you should anaylize its meaing first. If its meaning in the text is quantum efficiency, then the text belong the the topic "quantum efficiency".
- 这种说法,只覆盖了正确的情况,没有覆盖无关语义的情况
改进说法:
If the given text contain the symbol you should anaylize its meaing first. If the symbol in the text does not mean quantum efficiency, then the text does not belong the the topic quantum efficiency.
改进方法
- 直接说明语义 != 期望语义
文章作者
上次更新 2025-02-24 (77fafc9)