首页 / 资料库 / 提示工程指南

资料库18 分钟读完MIT提示词数据生成PAL代码推理

提示词的典型应用

译自《Prompt Applications》 · 查看英文原文

原文出处Prompt Applications 原作者:DAIR.AI · Elvis Saravia · 许可证:MIT License 中文译本由诸葛AI学院整理,仅供学习参考,版权归原作者与 DAIR.AI 所有。

这篇指南介绍提示工程(prompt engineering)里一些偏进阶、也挺有意思的用法,教你用提示词(prompt)去完成更复杂的实际任务。

本节仍在大量更新中。

内容目录: - 生成数据(Generating Data) - 程序辅助语言模型(Program-Aided Language Models,PAL) - Python 笔记本


生成数据

大语言模型(large language model,LLM)的文字生成能力很强。用对提示策略,你可以引导模型给出更好、更稳定、更符合事实的回答。LLM 还特别擅长生成数据,做各类实验正好用得上。比如你想给一个情感分类器快速造一批样本,可以这样下提示:

提示词: Produce 10 exemplars for sentiment analysis. Examples are categorized as either positive or negative. Produce 2 negative examples and 8 positive examples. Use this format for the examples: Q: <sentence> A: <sentiment>

输出: ``` Q: I just got the best news ever! A: Positive

Q: We just got a raise at work! A: Positive

Q: I'm so proud of what I accomplished today. A: Positive

Q: I'm having the best day ever! A: Positive

Q: I'm really looking forward to the weekend. A: Positive

Q: I just got the best present ever! A: Positive

Q: I'm so happy right now. A: Positive

Q: I'm so blessed to have such an amazing family. A: Positive

Q: The weather outside is so gloomy. A: Negative

Q: I just got some terrible news. A: Negative ```

要求 2 负 8 正,模型照单执行,格式也严格按 "Q: / A:" 来。这招很实用,指南后面另一节的测试就用到了这里生成的数据。

再看一个例子,展示怎么用 LLM 做数据标注和增广:

提示词: produce 3 wine reviews and label taste, flavor, aroma related token; present the result as a json file, in addition add the coordinate of each term for NER task

输出: [ { "review": "This Chardonnay has a rich, creamy texture with notes of vanilla and buttery oak. The taste is smooth and well-balanced, with a hint of tropical fruit on the finish.", "tokens": [ {"text": "rich", "label": "flavor", "start": 5, "end": 9}, {"text": "creamy", "label": "texture", "start": 10, "end": 16}, {"text": "vanilla", "label": "flavor", "start": 28, "end": 35}, {"text": "buttery", "label": "flavor", "start": 40, "end": 47}, {"text": "oak", "label": "aroma", "start": 48, "end": 51}, {"text": "smooth", "label": "taste", "start": 61, "end": 67}, {"text": "well-balanced", "label": "taste", "start": 68, "end": 81}, {"text": "hint", "label": "flavor", "start": 89, "end": 93}, {"text": "tropical fruit", "label": "flavor", "start": 98, "end": 112}, {"text": "finish", "label": "taste", "start": 116, "end": 122} ] }, { "review": "This Cabernet Sauvignon has a bold, full-bodied flavor with notes of black cherry and dark chocolate. The aroma is rich and complex, with hints of tobacco and leather.", "tokens": [ {"text": "bold", "label": "flavor", "start": 5, "end": 9}, {"text": "full-bodied", "label": "texture", "start": 10, "end": 21}, {"text": "black cherry", "label": "flavor", "start": 30, "end": 42}, {"text": "dark chocolate", "label": "flavor", "start": 47, "end": 60}, {"text": "rich", "label": "aroma", "start": 69, "end": 73}, {"text": "complex", "label": "aroma", "start": 78, "end": 85}, {"text": "hints", "label": "aroma", "start": 90, "end": 96}, {"text": "tobacco", "label": "aroma", "start": 101, "end": 108}, {"text": "leather", "label": "aroma", "start": 113, "end": 120} ] }, { "review": "This Riesling has a crisp, refreshing taste with notes of green apple and honey. The aroma is floral and fruity, with a hint of citrus.", "tokens": [ {"text": "crisp", "label": "texture", "start": 5, "end": 10}, {"text": "refreshing", "label": "texture", "start": 12, "end": 22}, {"text": "green apple", "label": "flavor", "start": 31, "end": 42}, ] }

模型生成 3 条酒评,把每个描述词标上类别(taste 口感、flavor 风味、aroma 香气、texture 质地),还给出每个词在句中的起止位置(start 和 end 是字符下标),正好是命名实体识别(named entity recognition,NER)任务需要的标注格式。原文的输出到第三条就中断了,这里原样保留。


PAL(程序辅助语言模型)

Gao 等人(2022)提出了一种方法:让 LLM 读自然语言题目,把生成的程序代码当作中间推理步骤。这种方法被称为程序辅助语言模型(program-aided language models,PAL)。它和思维链(chain of thought,CoT)提示的区别在于:思维链靠自由文本推出答案,PAL 则把求解这一步交给 Python 解释器这类程序运行时去算。

下面用 LangChain 和 OpenAI GPT-3 举个例子。我们想做一个简单的应用:理解用户的问题,再借助 Python 解释器算出答案。

具体来说,我们想让 LLM 回答需要理解日期的问题。提示词里放了几个示例,取自 PAL 官方仓库

先准备需要的 import:

python import openai from datetime import datetime from dateutil.relativedelta import relativedelta import os from langchain.llms import OpenAI from dotenv import load_dotenv

先做几项配置:

```python load_dotenv()

API 配置

openai.api_key = os.getenv("OPENAI_API_KEY")

供 LangChain 使用

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") ```

创建模型实例:

python llm = OpenAI(model_name='text-davinci-003', temperature=0)

准备提示词和问题:

```python question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"

DATE_UNDERSTANDING_PROMPT = """

Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?

If 2015 is coming in 36 hours, then today is 36 hours before.

today = datetime(2015, 1, 1) - relativedelta(hours=36)

One week from today,

one_week_from_today = today + relativedelta(weeks=1)

The answer formatted with %m/%d/%Y is

one_week_from_today.strftime('%m/%d/%Y')

Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?

If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.

today = datetime(2019, 1, 1) + relativedelta(days=6)

The answer formatted with %m/%d/%Y is

today.strftime('%m/%d/%Y')

Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?

If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.

today = datetime(1943, 6, 1) + relativedelta(days=1)

10 days ago,

ten_days_ago = today - relativedelta(days=10)

The answer formatted with %m/%d/%Y is

ten_days_ago.strftime('%m/%d/%Y')

Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?

It is 4/19/1969 today.

today = datetime(1969, 4, 19)

24 hours later,

later = today + relativedelta(hours=24)

The answer formatted with %m/%d/%Y is

today.strftime('%m/%d/%Y')

Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?

If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.

today = datetime(2002, 3, 12)

24 hours later,

later = today + relativedelta(hours=24)

The answer formatted with %m/%d/%Y is

later.strftime('%m/%d/%Y')

Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?

If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.

today = datetime(2001, 2, 28) + relativedelta(years=16)

Yesterday,

yesterday = today - relativedelta(days=1)

The answer formatted with %m/%d/%Y is

yesterday.strftime('%m/%d/%Y')

Q: {question}

""".strip() + '\n' ```

提示词里每个示例都先推理"今天到底是哪天",再逐步写成日期计算的代码。模型照着这个套路,把我们的新问题也翻译成一段 Python。

让模型生成代码:

python llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question)) print(llm_out)

执行生成的代码,输出结果:

python exec(llm_out) print(born)

最终输出:02/27/1998。日期题不在模型内部计算,全部交给解释器,答案就不会在算术上出错。


Python 笔记本

说明 笔记本
学习如何让语言模型配合 Python 解释器一起解决问题。 Program-Aided Language Models

更多示例筹备中。

原章节导航:上一章:进阶提示技巧 · 下一章:ChatGPT 提示

这篇在讲什么,跟咱们的课怎么对?

资料库是大厂公开教材的中文译本,偏原理和工程做法。想看面向中小企业的白话版本,去入门课场景课