prompts-提示应用程序


文档摘要

提示应用程序 在这份指南中,我们将介绍一些高级且有趣的方法,利用提示工程来执行有用且更复杂的任务。 请注意,本节正在积极开发中。 主题: 生成数据 程序辅助语言模型 Python 笔记本 生成数据 LLM 具有强大的文本生成能力。通过有效的提示策略,可以引导模型生成更好、更一致且更真实的信息。LLM 对于生成实验所需的各类数据也非常有用。例如,我们可以使用它来为情感分类器生成快速样本,如下所示: 提示: 输出: 这非常有用。我们用这个例子在指南的另一部分进行不同的测试。 这里还有一个提示示例,展示了如何利用 LLM 进行数据标注/增强任务。 提示: 输出: PAL(程序辅助语言模型) Gao 等,2022 提出了一种方法,使用 LLM 读取自然语言问题并生成程序作为中间推理步骤。

提示应用程序

在这份指南中,我们将介绍一些高级且有趣的方法,利用提示工程来执行有用且更复杂的任务。

请注意,本节正在积极开发中。

主题:

生成数据

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

这非常有用。我们用这个例子在指南的另一部分进行不同的测试。

这里还有一个提示示例,展示了如何利用 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}, ] }

PAL(程序辅助语言模型)

Gao 等,2022 提出了一种方法,使用 LLM 读取自然语言问题并生成程序作为中间推理步骤。称为程序辅助语言模型(PAL),与思维链提示不同的是,它不是通过自由形式文本获得解决方案,而是将解决方案步骤卸载到程序化运行时,如 Python 解释器。

让我们看一个使用 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")

设置模型实例:

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

设置提示+问题:

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

Python 笔记本

描述 笔记本
了解如何结合使用 Python 解释器和语言模型来解决问题。 程序辅助语言模型

更多示例即将推出!

上一节 (高级提示)

下一节 (ChatGPT)

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


发布者: 作者: 转发
评论区 (0)
U