作者 Ben 是 AI 自动化公司 Ben AI 的创始人,《MASTER Prompt Engineering in 50 min》这个视频 (Bilibili 源)发布于 2024 年 9 月。
视频中使用的提示词均为英语,本文保留了这部分原文。不过就笔者过去的经验而言,换成中文也未尝不可。
虽然本文(视频)提供了一些提示词技巧,实际任务中对最终效果的评价还是较为主观的。如何工程化地衡量不同提示词的好坏,甚至进行相应的 debug 和 profile,笔者目前只找到了 Arize Prompt Playground 和 Microsoft Semantic Kernel,这方面的内容之后另有机会再深入探讨。
最后,因为 Ben AI 总是要推广一下他们自己的平台产品(Flow Builder 之类的),感兴趣的可以自行观看原视频,这部分就不介绍了。
提示词工程的必要性
英语是新的编程语言 English is the new programming language —— Andrei Karpathy
在构建 AI 自动化系统时,掌握与 LLM 交流的方式将是最重要的技能。
结构化提示词
「结构化提示词」是相较于「对话式提示词」这个概念提出的:
这是「对话式提示词」:
这是「结构化提示词」:
结构化提示词通常面向单一任务优化,其结构分为:
- 角色(Role)
- 目标(Objective)
- 上下文(Context)
- 指令(Instructions)
- 示例(Example)
- 备注(Notes)
提示词链则是:
- 将复杂任务拆解成多个独立的任务
- 将上个任务的输出添加到下个任务的输入
- 可以提高可靠性和稳定性
一般用于:
- 数据提取
- 内容生成
- 分类
- 求值
- 数据转换
- 决策
短提示、长提示
这是「短提示」:
这是「长提示」:
使用 Markdown 语法可以方便人类和 LLM(没错)阅读和理解。
长提示词框架
设置角色 / 个性 / 特质(Role)
- 赋予角色特性
- 使用一些正向的表达强烈的赞美
- 「你是一个世界级的……(You are World-class…)」
- 「尤其擅长……(with particular expertise in ….)」
- 添加到 System Prompt
# Role
You are a world class CV Formatter with a particular expertise in carefully reformatting an original CV into a new layout.
给出目标 / 任务(Objective)
- 描述要做什么
- 「你的任务是……(Your goal is to…)」
- 「一步步思考……(Think step by step…)」
# Objective
Your Goal is carefully read the original CV profile and reformat the CV into the new format. You will think step by step through the following process to ensure a good outcome.
1. Carefully Read the original CV
2. Add content from the original CV into the new layout for the new CV
3. Add any information that was present in the original CV but that doesn't have a clear place to put at the end of the newly formatted CV.
给出上下文(Context)
- 为什么需要完成这个任务
- 「搞定这个对我的饭碗很重要(It is vital to my career)」
也可以是画饼
# Context
This task is crucial for our recruitment firm, the reformatted CV will be sent to potential clients and therefore It is vital to my career that the CV is reformatted in the specific format mentioned below. It is also vital to my firm that no information from the original gets lost or is changed as this would undermine the quality of the CV.
发出指令(Instructions)
- 给予详细的指令
- 描述所有规则
- 指定输出格式,例如要求以 JSON 格式输出等
- 尽可能指出应该避免的错误
- 常见句子
- 「重要!!!(IMPORTANT)」
# Instructions
- it is absolutely vital to my career that you do NOT add text, make new facts up, or edit anything from the original text.
- If some info is not available for a category then just add a placeholder (example: [Location not provided], you will never make up information.)
- IMPORTANT: Make sure to be comprehensive and that ALL data is added to the new format, if there is no clear section to add info from the original CV into the new CV, add it to the end.
- Be particularly meticulous when looking at dates and years. Sometimes dates and times are mentioned above or below the referenced text.
- You can use more or less bullet points than in the example format below depending on the amount of info available in the CV, the most important thing is that ALL DATA FROM ORIGINAL CV IS PRESENT IN THE NEW ONE.
## Output specifications
Here is the new format for the CV:
**CURRICULUM VITAE FORMAT**
**NAME:**
- [Candidate Name]
- **LOCATION:**
- [Location]
**EDUCATION & QUALIFICATIONS:**
- [Degree/Certification]
- [Institution/Association]
**CURRENT EMPLOYER:**
- [Current Employer]
**CURRENT POSITION:**
- [Current Position]
**PROFILE:**
- [Professional Summary]
**EMPLOYMENT HISTORY:**
提供示例(Example)
- 非常重要
- 至少要提供一个输入 / 输出示例
# Examples
## Example 1:
**Input:**
07784 300943 PAOLO PEZZANGORA paolo.pezzangora@gmail.com Stratford-upon-Avon, Warwickshire, UK Marketing Specialist C A R E E R S U M M A R Y 10+ years of experience in the creative industry and a special focus on Digital Marketing. Versatile, creative, bilingual professional with the ability to find solutions to complex problems, I develop effective and successful marketing strategies that increase brand awareness, promote customer engagement and ultimately drive traffic and conversions. Familiar with multiple marketing digital tools and with a proactive approach, I am up-to-date with changes in the industry through continuing professional development. A R E A S O F E X P E R T I S E Strong leadership & problem-solving skills Knowledge of B2B & B2C Brand positioning & logo design Excellent social media knowledge & skills Good SEO & SEM skills Highly creative with technical skills Extensive experience organising events UX design skills Quick learner & self-starter P R O F E S S I O N A L E X P E R I E N C E 07/2023 – to date EBCO ebikes, Warwick Head of Marketing Developed and executed marketing strategies, propelling EBCO to an industry- leading position.
**Output:**
**CURRICULUM VITAE FORMAT**
**NAME:**
- Paolo Pezzangora
**LOCATION:**
- Stratford-upon-Avon, Warwickshire, UK
**EDUCATION & QUALIFICATIONS:**
- Diploma in Professional Digital Marketing
- Chartered Institute of Marketing (CIM)
- Digital Lab by AMA
- Arts Marketing Association (Ongoing)
- Fundamentals of Digital Marketing
- Google Digital Training
- Small-Scale Development Programme
- Arts Marketing Association (AMA)
**CURRENT EMPLOYER:**
- EBCO ebikes
**CURRENT POSITION:**
- Head of Marketing
**PROFILE:**
- 10+ years of experience in the creative industry with a special focus on Digital Marketing.
插入变量(The Variable)
- 每个任务中不一样的地方
# The CV you will reformat today:
{{original CV}}
添加备注(Notes)
- LLM 似乎对提示词的头尾部分有更多的关注
- 可以再次强调重要规则
- 可以再次强调输出格式
- 可以再次强调最佳实践
- 如果哪里出了问题,在这里补充修正
# Notes
- it is absolutely vital to my career that you do NOT add text, make new facts up, or edit anything from the original text.
- IMPORTANT: Make sure to that all data is added into the new format
- Make sure to be comprehensive and that ALL data is added to the new format, if there is no clear section to add info from the original CV into the new CV, add it to the end.
短提示词框架
- 目标+指令
- 例子
- 变量
# Objective
Please identify the name from the person in the CV below.
- ONLY output the full name, no summary, no explanation, nothing but the full name.
- If you don't find a name ONLY output: "no name found", nothing else, no summary, no explanation.
** example output**:
Ben van Sprundel
** the cv you have to identify the name from:**
{{formatted_cv}}
代理提示,Agent Prompting
解决 AI 问题的方法是使用更多 AI
AI Agent 的唯二职责应该是 决策(调用工具 / 子代理(Subagent)) 和 (与子代理 / 人类)沟通。所有其他工作都应当转移给工具和子代理。
与长提示词框架类似:
- 角色(Role)
- 目标(Objective)
- 上下文(Context)
- 操作流程(SOP,Standard Operating Procedure,标准作业程序)
- 指令(Instructions)
- 工具 & 子代理(Tools & Subagents)
- 示例(Examples)
- 备注(Notes)
为推理模型编写提示词
- 使用简洁的提示词
- 避免使用思维链提示(Chain of Thought Prompting)
- 思维链技术已经是模型推理的一部分,在提示词中加入你自己的推理可能没什么用,也可能会起到反效果
- 继续使用 Markdown 编写提示词