LLM 具有強(qiáng)大的文本生成能力。使用有效的提示策略可以引導(dǎo)模型產(chǎn)生更好、更一致和更符合事實(shí)的響應(yīng)。LLM 對(duì)于生成對(duì)運(yùn)行各種實(shí)驗(yàn)有用的數(shù)據(jù)也特別有用。
在本指南中,我們將介紹一些高級(jí)和有趣的方法,我們可以使用提示工程來執(zhí)行有用和更高級(jí)的任務(wù)。
請注意,此部分正在大量開發(fā)中。 主題:
- 生成數(shù)據(jù)
- 程序輔助語言模型
- Python 筆記本
一、生成數(shù)據(jù)
LLM 具有強(qiáng)大的文本生成能力。使用有效的提示策略可以引導(dǎo)模型產(chǎn)生更好、更一致和更符合事實(shí)的響應(yīng)。LLM 對(duì)于生成對(duì)運(yùn)行各種實(shí)驗(yàn)有用的數(shù)據(jù)也特別有用。例如,我們可以使用它為情感分類器生成快速樣本,如下所示:
迅速的:
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: 我剛得到了有史以來最好的消息!
A: 積極的
Q: 我們剛剛在工作中得到了加薪!
A: 積極的
Q: 我為今天所取得的成就感到非常自豪。
A: 積極的
Q: 我度過了有史以來最好的一天!
A: 積極的
Q: 我非常期待周末的到來。
A: 積極的
Q: 我剛收到了有史以來最好的禮物!
A: 積極的
Q: 我現(xiàn)在非常開心。
A: 積極的
Q: 我很幸運(yùn)有這么一支了不起的家庭。
A: 積極的
Q: 外面的天氣很陰暗。
A: 消極的
Q: 我剛得到了一些可怕的消息。
A: 消極的
這非常有用。我們將此示例用于指南另一部分中的不同測試。
二、PAL(程序輔助語言模型)
Gao 等人 (2022)提出了一種使用 LLM 閱讀自然語言問題并生成程序作為中間推理步驟的方法。創(chuàng)造的程序輔助語言模型 (PAL) 與思維鏈提示的不同之處在于,它不是使用自由格式的文本來獲得解決方案,而是將解決方案步驟卸載到編程運(yùn)行時(shí),例如 Python 解釋器。

讓我們看一個(gè)使用 LangChain 和 OpenAI GPT-3 的例子。我們有興趣開發(fā)一個(gè)簡單的應(yīng)用程序,該應(yīng)用程序能夠解釋所提出的問題并通過利用 Python 解釋器提供答案。
具體來說,我們有興趣創(chuàng)建一個(gè)函數(shù),允許使用 LLM 來回答需要理解日期的問題。我們將為 LLM 提供提示,其中包括從此處采用的一些范例。
這些是我們需要的導(dǎo)入:
import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv
讓我們首先配置一些東西:
API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
設(shè)置模型實(shí)例:
llm = OpenAI(model_name='text-davinci-003', temperature=0)
設(shè)置提示+問題:
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)
這將輸出以下內(nèi)容:02/27/1998