在本指南中,我们将介绍一些高级和有趣的方法,我们可以使用提示工程来执行有用和更高级的任务。
请注意,此部分正在大量开发中。 主题:
生成数据程序辅助语言模型python 笔记本一、生成数据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:
a:
输出:
q: 我刚得到了有史以来最好的消息!
a: 积极的
q: 我们刚刚在工作中得到了加薪!
a: 积极的
q: 我为今天所取得的成就感到非常自豪。
a: 积极的
q: 我度过了有史以来最好的一天!
a: 积极的
q: 我非常期待周末的到来。
a: 积极的
q: 我刚收到了有史以来最好的礼物!
a: 积极的
q: 我现在非常开心。
a: 积极的
q: 我很幸运有这么一支了不起的家庭。
a: 积极的
q: 外面的天气很阴暗。
a: 消极的
q: 我刚得到了一些可怕的消息。
a: 消极的
这非常有用。我们将此示例用于指南另一部分中的不同测试。
二、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 configurationopenai.api_key = os.getenv(openai_api_key)
for langchainos.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
以上就是聊聊如何设计一个优秀的提示应用程序?的详细内容。