大型语言模型 Prompt 工程的核心原则与最佳实践
一、引言
大型语言模型(Large Language Model, LLM)是通过深度学习技术训练出的,能理解和生成人类语言的机器学习模型。如 OpenAI 的 GPT-3、GPT-4,以及各类开源基座模型。这些模型能够处理各种自然语言处理任务,包括机器翻译、文本写作、问答系统、代码生成等。
Prompt(提示词)则是与 LLM 交互的关键接口。它就像是指令,明确告诉模型我们需要什么。一个好的 Prompt 能让模型生成质量高、相关性强的回答,从而最大化模型的能力。因此,有效地编写 Prompt 对于使用 LLM 至关重要。这种使用 Prompt 的技术也被称为 Prompt Engineering(提示工程),它属于自然语言编程的范畴,是人与机器交互的关键沟通技巧。通过 Prompt Engineering,我们可以更有效地进行人机对话,实现更精准的意图传达。
近期,吴恩达教授发布了 Prompt 教程,在教程中他提出了写出高质量 Prompt 的两个基本原则:
- 写出清晰而具体的指示。
- 给模型更多的思考空间。
下面让我们更深入地探讨这两个原则及其在实际应用中的展开方式。
二、两个基本原则详解
1. 写出清晰而具体的指示
无论我们给 ChatGPT 或其他大模型布置何种任务,都需要提供清晰且具体的指示。这是一条通用的原则,因为无论模型处理的是何种问题,明确的指示都能帮助它更好地理解我们的需求,从而产生满足需求的回应。清晰和具体的指示也能减少模型进行不必要的推断,提高回应的准确性。
如何写出清晰具体的指示,可以从以下几个维度展开:
- 输出内容的长度:例如,"输出 3 句话","输出内容不超过 500 字","用一句话总结"。
- 输出内容的风格:例如,"简洁/详尽","轻松活泼/正式","用乔布斯的画风输出","模仿鲁迅的语气"。
- 输出内容的语言:例如,"用英文输出","内容包含 Python 关键词","使用专业术语"。
- 输出内容的格式:
- "将结果以表格的形式输出,第一列是 xx,第二列是 xx"。
- "将邮件中的行动项以列表的形式输出"。
- "请给出 10 本关于人工智能的经典书籍,包括:书名、作者、作者的国籍,并用 JSON 格式输出,对应的 key 分别是:title,author,nationality"。
- 任务描述:明确具体动作,如"改写一段话"、"写一封邮件"、"写一段 Python 代码"、"生成一段文字"、"总结一段话"。
- 背景信息:对 AI 进行角色设定(Role Prompting),处于怎样的情境中,任务具体解决的是什么问题,生成的文字用来做什么(上下文 Context)。
- 输入内容说明:用分隔符来明确指出输入的不同部分,例如使用
"""或###包裹输入文本,防止指令注入。
对于复杂的任务,可以通过举例来进一步说明。如:对于问答类任务,可以给出少量的问题和答案作为示例,让模型更好地理解问题(Few-shot prompting);举例说明在 xx 情况下,应该输出 xx 内容。
在实际应用中,为了适应不同的场景,我们可能需要考虑更多的维度,但是,核心要素始终是明确地表达需求。理解这一点,我们可以更好地编写有效的 Prompt。
下面是一个来自斯坦福大学的 Prompt 示例,他们在使用 ChatGPT 生成微调任务以训练自己的 Alpaca 模型时,其使用的 Prompt 便体现出了明确和详细的优点,确保 AI 模型的输出能够满足自己的需求:
You are asked to come up with a set of 20 diverse task instructions. These task instructions will be given to a GPT model and we will evaluate the GPT model for completing the instructions.
【任务 + 背景】
Here are the requirements:
1) Try not to repeat the verb for each instruction to maximize diversity. 【内容其他要求:多样性、语法】
2) The language used for the instruction also should be diverse. For example, you should combine questions with imperative instructions. 【举例说明】
3) The type of instructions should be diverse. The list should include diverse types of tasks like open-ended generation, classification, editing, etc. 【其他要求:多样性 + 举例说明】
4) A GPT language model should be able to complete the instruction. For example, do not ask the assistant to create any visual or audio output. For another example, do not ask the assistant to wake you up at 5pm or set a reminder because it cannot perform any action. 【任务 + 举例说明】
5) The instructions should be in English. 【输出内容语言】
6) The instructions should be 1 to 2 sentences long. Either an imperative sentence or a question is permitted. 【输出内容长度】
7) You should generate an appropriate input to the instruction. The input field should contain a specific example provided for the instruction. It should involve realistic data and should not contain simple placeholders. The input should provide substantial content to make the instruction challenging but should ideally not exceed 100 words. 【输出内容的格式】
8) Not all instructions require input. For example, when a instruction asks about some general information, 'what is the highest peak in the world', it is not necessary to provide a specific context. In this case, we simply put '' in the input field. 【举例说明】
9) The output should be an appropriate response to the instruction and the input. Make sure the output is less than 100 words. 【输出内容的长度】


