首頁 >後端開發 >Python教學 >聊聊如何設計一個優秀的提示應用程式?

聊聊如何設計一個優秀的提示應用程式?

王林
王林轉載
2023-04-24 18:49:091127瀏覽

在本指南中,我們將介紹一些進階且有趣的方法,我們可以使用提示工程來執行有用和更高級的任務。

請注意,此部分正在大量開發中。主題:

  • 產生資料
  • 程式輔助語言模型
  • 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: <sentence>
A: <sentiment>

輸出:

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 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

以上是聊聊如何設計一個優秀的提示應用程式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除