不写代码、年薪百万,带你玩赚ChatGPT提示工程-提示应用程序

时间:2023-01-24 00:48:40

前言

随着ChatGPT的大火,提示工程在大模型中的重要性不言而喻,本文参考国外​​Prompt Engineering Guide​​完成国内中文版本的《提示工程指南》,希望能够和大家一起交流,分享及发现提示工程的美妙之处。文章所有内容可以在​​ChatGPT_Prompt_Chinese​​中找到。

在​​之前的指南​​中,我们介绍并给出了Prompt的高级示例。

在本指南中,我们将介绍一些高级和有趣的方式,我们可以使用提示工程来执行有用和更高级的任务。

需要注意的是,在本章节我们将会引入ChatGPT的API,进行一定量的工程实践。


一、数据生成

LLM 具有生成文本的强大能力。使用有效的提示策略可以引导模型产生更好、更一致、更真实的响应。 LLM 还可以特别用于生成数据,这对于运行各种实验非常有用。例如,我们可以使用它来为情感分类器生成快速样本,如下所示:

Prompt:

为情感分析生成 10 个样本。样本分为正面和负面两类。生成 2 个负面样本和 8 个正面样本。使用以下格式为样本编写问题和答案:
问题:<句子>
答案:<情感>

Output:

问题:这家餐厅的服务员态度很恶劣,让人感到非常不舒服。
答案:负面

问题:我最喜欢的歌手今晚开演唱会了,我太兴奋了!
答案:正面

问题:我刚刚丢了钱包,里面有我所有的信用卡和现金,我感到非常沮丧。
答案:负面

问题:今天天气真是太好了,阳光明媚,心情愉悦。
答案:正面

问题:这本小说真是太好看了,我一口气读完了整本书。
答案:正面

问题:我的手机掉进水里了,无法正常工作了,我感到非常烦恼。
答案:负面

问题:我的朋友们今天都来我家玩,我们一起吃了美食,玩了好几个小时,我感到非常愉快。
答案:正面

问题:我被一辆出租车撞了,虽然没有受伤,但是我的车被撞坏了,我感到很不开心。
答案:负面

问题:我终于找到了一份满意的工作,薪水还不错,我感到非常开心。
答案:正面

问题:我的狗狗生了一窝可爱的小狗,它们都很健康,我感到非常高兴。
答案:正面

这非常有用。实际上,我们在指南的另一个部分中使用此示例进行了不同的测试。

二、PAL (Program-Aided Language Models): Code as Reasoning

​Gao et al., (2022)​​ 该方法使用LLMs读取自然语言问题并生成程序作为中间推理步骤。称为程序辅助语言模型(PAL),它与链式思维(COT)提示不同,因为它不是使用*形式的文本来获取解决方案,而是将解决方案步骤卸载到程序运行时,如 Python 解释器。

不写代码、年薪百万,带你玩赚ChatGPT提示工程-提示应用程序

在这里插入图片描述

让我们使用 LangChain 和 OpenAI GPT-3 来看一个示例。我们有兴趣开发一个简单的应用程序,该应用程序能够解释所提出的问题并通过利用 Python 解释器提供答案。

具体而言,我们有兴趣创建一个功能,允许使用 LLM 来回答需要日期理解的问题。我们将向 LLM 提供一个提示,其中包含一些示例,这些示例是从​​以下内容​​采用的:

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

让我们首先配置一些东西:

load_dotenv()

# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")

# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

设置模型实例:

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'
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)
exec(llm_out)
print(born)

这将输出以下内容: ​​02/27/1998​


总结

在本指南中,我们主要介绍了一些高级和有趣的方法,我们可以使用提示工程来执行有用和更高级的任务例如:生成数据、作为推理的代码