回到首页

输入关键词,多个关键词用空格分隔。

【AI Work】09 提示工程指南 学习笔记

约 6 分钟

摘要

2023 年 10 月的提示工程指南学习笔记:模型设置(temperature、top_p)、基本概念(零样本、Few-shot)、通用技巧(信息提取、问答、文本分类、角色扮演)、提示技术(CoT、思维树 ToT、ReAct)。

本页目录

2023 年 10 月 23 日,我学习提示工程指南时记录了一份笔记。这是一份对指南的快速通读,目的是了解各种提示技术的基本用法,而不是深度教程。

这是从 2023 年 10 月的笔记整理而来的。文中涉及的指南结构、模型设置和提示技术都是当时的记录,今天的指南可能已更新。

简介

模型设置

  • temperature 的参数值越小,模型就会返回越确定的一个结果。如果调高该参数值,大语言模型可能会返回更随机的结果。
  • Top_p:同样,使用 top_p(与 temperature 一起称为核采样的技术),可以用来控制模型返回结果的真实性。如果你需要准确和事实的答案,就把参数值调低。如果你想要更多样化的答案,就把参数值调高一些。
  • 一般建议是改变其中一个参数就行,不用两个都调整。

基本概念

零样本提示(zero-shot prompting),简单来说就是不提供任何例子,直接让模型给出回答:

Q: <问题>?
A:

更高效的小样本提示(Few-shot Prompting),一个简单的例子:

This is awesome! // Positive
This is bad! // Negative
Wow that movie was rad! // Positive
What a horrible show! //

输出结果 Negative

通用技巧

直接看例子:

提取以下文本中的地名。
所需格式:
地点:<逗号分隔的公司名称列表>
输入:"虽然这些发展对研究人员来说是令人鼓舞的,但仍有许多谜团。里斯本未知的香帕利莫德中心的神经免疫学家Henrique Veiga-Fernandes说:"我们经常在大脑和我们在周围看到的效果之间有一个黑匣子。""如果我们想在治疗背景下使用它,我们实际上需要了解机制。""

输出:
地点:里斯本,香帕利莫德中心

信息提取

Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.
Mention the large language model based product mentioned in the paragraph above:
// 指出上文中提到的大语言模型:

输出:
The large language model based product mentioned in the paragraph above is ChatGPT.

问答

Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer. // 基于以下语境回答问题。如果不知道答案的话,请回答"不确定答案"。
Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.
Question: What was OKT3 originally sourced from?
Answer:

输出:
Mice.

文本分类

Classify the text into neutral, negative or positive. // 将文本按中立、负面或正面进行分类
Text: I think the food was okay.
Sentiment:

输出:
Neutral

角色扮演

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific. // 以下是与 AI 助理的对话,语气应该专业、技术性强。
Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?
AI:

输出:
Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

提示技术

CoT 提示

简单来说就是利用 Few-Shot 教会大模型如何思考:

这组数中的奇数加起来是偶数:4、8、9、15、12、2、1。
A:将所有奇数相加(9、15、1)得到25。答案为False。
这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。
A:

输出:
将所有奇数相加(15、5、13、7、1)得到41。答案为False。

还有更骚的,就是将 Let's think step by step(让我们逐步思考)添加到原始的 Prompt 中。

一个更好的版本 让我们一步一步地解决这个问题,以确保我们有正确的答案。

思维树 ToT

思维树(ToT)提示法,将 ToT 框架的主要概念概括成了一段简短的提示词,指导 LLM 在一次提示中对中间思维做出评估。ToT 提示词的例子如下:

假设三位不同的专家来回答这个问题。
所有专家都写下他们思考这个问题的第一个步骤,然后与大家分享。
然后,所有专家都写下他们思考的下一个步骤并分享。
以此类推,直到所有专家写完他们思考的所有步骤。
只要大家发现有专家的步骤出错了,就让这位专家离开。
请问...

ReAct 提示

生成推理轨迹使模型能够诱导、跟踪和更新操作计划,甚至处理异常情况。操作步骤允许与外部源(如知识库或环境)进行交互并且收集信息。

ReAct 框架允许 LLMs 与外部工具交互来获取额外信息,从而给出更可靠和实际的回应。

问题 科罗拉多造山带东部区域延伸到的区域的海拔范围是多少?
思考 1 我需要搜索科罗拉多造山带,找到科罗拉多造山带东段延伸的区域,然后找到该区域的海拔范围。
操作 1 搜索 [科罗拉多造山带]
观察 1 科罗拉多造山带是科罗拉多及其周边地区造山运动的一段。
思考 2 它没有提到东部地区。所以我需要查一下东部地区。
操作 2 查找 [东部地区]
观察 2 (结果 1 / 1) 东部区域延伸至高平原,称为中原造山带。
思考 3 科罗拉多造山运动的东部区域延伸到高平原。所以我需要搜索高平原,找到它的海拔范围。
操作 3 搜索 [高平原]
观察 3 高平原指的是两个截然不同的陆地区域之一。
思考 4 我需要搜索高平原(美国)
操作 4 搜索 [高平原(美国)]
观察 4 高平原是大平原的一个分区。从东到西,高平原的海拔从 1800 到 7000 英尺 (550 到 2130 米) 不等。[3]
思考 5 高平原海拔高度从 1800 到 7000 英尺,所以答案是 1800 到 7000 英尺。
操作 5 结束 [1800 到 7000 英尺]

可以通过 langchain 的 agent 模块构造,但实际上可以自行构造,参考 这里。

拓展阅读


注:本文保留了 2023 年 10 月提示工程指南学习笔记的原文和立场,发布时整理为标准 Markdown。原笔记是学习笔记,不是多篇整理稿,因此保留原始日期 2023-10-23。笔记中的指南结构、模型设置和提示技术是当时的记录,今天的模型和指南可能已更新,未对 Prompt 做验证。

本文 Markdown 版本机器可读站点索引:llms.txt

相关文章