Prompt工程師指南[應用篇]:Prompt應用、ChatGPT|Midjouney Prompt Engineering

汀、人工智慧發表於2023-05-15

Prompt工程師指南[應用篇]:Prompt應用、ChatGPT|Midjouney Prompt Engineering

1.ChatGPT Prompt Engineering

主題:

與 ChatGPT 對話
Python 筆記本

Topics:


ChatGPT介紹

ChatGPT是OpenAI訓練的一種新型模型,可以進行對話互動。該模型經過訓練,可以按照提示中的指令,在對話上下文中提供適當的回應。ChatGPT 可以幫助回答問題、建議菜譜、按某種風格寫歌詞、生成程式碼等等。

ChatGPT 使用人類反饋強化學習(RLHF)進行訓練。雖然這個模型比之前的 GPT 版本更具有能力(還經過訓練以減少有害和不真實的輸出),但它仍然存在限制。讓我們透過具體的例子來了解一些能力和限制。

您可以在這裡 使用ChatGPT的研究預覽,但在下面的例子中,我們將使用 OpenAI Playground 上的“Chat”模式。


1.1 審查對話任務

In one of the previous guides, we covered a bit about conversation capabilities and role prompting. We covered how to instruct the LLM to have a conversation in a specific style, with a specific intent, behavior, and identity.
之前的指南中,我們介紹了一些關於對話能力和角色提示的內容。我們講解了如何指導LLM以特定的風格、意圖、行為和身份進行對話。

讓我們回顧一下之前的基本示例,我們建立了一個對話系統,能夠生成更技術和科學的回答。

Prompt:

以下是與AI研究助手進行的對話。助手的語氣是技術和科學的。

人類:你好,你是誰?
AI:您好!我是一個AI研究助手。我今天可以幫您什麼?
人類:你能告訴我關於黑洞的形成嗎?
AI:

從上面的例子中,您可以看到兩個重要組成部分:

意圖或者解釋聊天機器人是什麼
身份,它指導聊天機器人用什麼風格或語氣來回復

上述簡單示例適用於使用text-davinci-003的文字完成API。最近,OpenAI宣佈了ChatGPT APIs,它是一種更強大和更便宜的模型,名為gpt-3.5-turbo,專門用於這種型別的功能(聊天完成)。OpenAI推薦它作為它們的最佳模型,即使是非聊天的用例也是如此。使用ChatGPT API的其他好處是顯著的成本降低(90%)和效率。
Snap Inc.和Instacart等大公司已經在他們的產品上整合了由ChatGPT支援的對話功能,從個性化推薦到開放式購物目標不等。


1.2 使用ChatGPT進行對話

1.2.1多輪對話

為了展示ChatGPT的能力,我們將使用上面的聊天機器人助手示例並討論結果。與text-davinci-003相比,為ChatGPT提供動力的gpt-3.5-turbo模型使用聊天格式作為輸入。模型期望一系列訊息作為輸入,然後使用這些訊息生成響應。

Input:

系統: 你是一個使用技術性和科學性語氣的AI研究助手。
使用者: 你好,你是誰?
助手: 你好!我是一名AI研究助手。今天我能為你做些什麼?
使用者: 你能告訴我黑洞是如何產生的嗎?

Output:

助手: 當然可以!黑洞是由於一個大質量星體耗盡燃料並在自身引力作用下崩塌形成的。這種崩塌導致星體變得極為緻密,具有極強的引力場,甚至連光線都無法逃脫。這個無法回頭的點稱為事件視界。一旦一顆星體穿過事件視界,它就被認為是一個黑洞...

請注意,在上面的示例中,我已經簡化了輸入和輸出,但是ChatGPT聊天補全API要求訊息以特定的格式提供。我在下面新增了一個快照,展示了在OpenAI Playground中使用Chat Mode時,此示例的外觀:

對於我們的示例,更正式的API呼叫可能類似於以下示例:

import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "系統", "content": "你是一個使用技術性和科學性語氣的AI研究助手。"},
        {"role": "使用者", "content": "你好,你是誰?"},
        {"role": "助手", "content": "你好!我是一名AI研究助手。今天我能為你做些什麼?"},
        {"role": "使用者", "content": "你能告訴我黑洞是如何產生的嗎?"}
    ]
)

未來開發人員與ChatGPT互動的方式預計將透過Chat Markup Language(簡稱ChatML)完成。

1.2.2單輪任務

聊天格式使得多輪對話成為可能,但它也支援類似我們使用text-davinci-003進行的單輪任務。這意味著我們可以使用ChatGPT執行類似於我們對原始GPT模型進行演示的任務。例如,讓我們嘗試使用ChatGPT執行以下問答任務:

Input:

USER:根據下文回答問題。保持答案簡短明瞭。如果不確定答案,請回答“不確定答案”。

背景:Teplizumab的根源可以追溯到名為Ortho Pharmaceutical的新澤西州製藥公司。在那裡,科學家生成了抗體的早期版本,稱為OKT3。最初從小鼠中獲得,這種分子能夠結合到T細胞的表面並限制它們的細胞殺傷潛力。 1986年,它獲得批准,用於幫助預防腎臟移植後器官排斥,成為首個允許人類使用的治療性抗體。

問題:OKT3最初是從哪裡獲得的?

回答:

Output:

ASSISTANT:小鼠。

請注意,我新增了“USER”和“ASSISTANT”標籤,以更好地演示如何使用ChatGPT執行任務。以下是使用Playground的示例:

更正式地說,這是API呼叫(我僅包括請求的訊息元件):

CONTENT = """Answer the question based on the context below. Keep the answer short and concise. Respond \"Unsure about answer\" if not sure about the answer.

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Answer:
"""

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "user", "content": CONTENT},
    ],
    temperature=0,
)

1.2.3Instructing Chat Models

根據官方OpenAI文件,gpt-3.5-turbo模型的快照也將提供。例如,我們可以訪問3月1日的快照gpt-3.5-turbo-0301。這使得開發人員可以選擇特定的模型版本。這也意味著指導模型的最佳實踐可能會從一個版本變化到另一個版本。

目前針對gpt-3.5-turbo-0301的推薦做法是,在user訊息中新增指令,而不是在可用的system訊息中新增指令。


1.3Python筆記本

描述 Python筆記本
學習如何使用openai庫呼叫ChatGPT API ChatGPT API介紹
學習如何使用LangChain庫使用ChatGPT特性。 ChatGPT API with LangChain

1.4參考資料(英文)

2.Midjouney Prompt Engineering

2.1 科技品牌插畫

**Pormpt重點 _generic productivity illustration for + 你要畫的東西+ by slack and dropbox, style of behance **

image.png

_generic productivity illustration for a tech company, by slack and dropbox, style of behance _
prompt翻譯 一家科技公司的通用生產力插畫,由 Slack 和 Dropbox 製作,風格參考 Behance。

image.png

generic productivity illustration for a tech company, by slack and behance

image.png

Falling in love with your company culture, illustration for a tech company, by slack and dropbox, style of behance
prompt翻譯: 情人節主題,一家科技公司的插畫,由 Slack 和 Dropbox 製作,風格參考 Behance。

image.png

users at a coffeeshop, illustration for a tech company, by slack and dropbox, style of behance

2.2 Ios App Icons設計

提示詞重點 _squared with round edges mobile app logo design, flat vector app icon of a + 你要的icon_

image.png

squared with round edges mobile app logo design, flat vector app icon of a cute shiba inu face, minimalistic, white background
prompt翻譯: 圓角正方形的移動應用程式徽標設計,是一隻可愛的柴犬頭像的扁平化向量應用程式圖示,極簡主義風格,白色背景。

image.png

squared with round edges mobile app logo design, flat vector app icon of a rocket

minimalistic, whimage.png

te background
squared with round edges mobile app logo design, flat vector app icon of a skull, minimalistic, white background

image.png

squared with round edges mobile app logo design, flat vector app icon of a cute onigiri, minimalistic, white background

2.3手機應用程式設計稿

**Prompt邏輯為 _photography of an iphone + 你要在iphone上顯示的內容 + ****inspired ****by Behance and Figma and dribbble **_

image.png

_photography of an iphone Narrow Weak Beachinspired by Behance and Figma and dribbble _
prompt翻譯 一張iPhone的照片,靈感來自Behance、Figma和dribbble,主題為“窄弱的海灘(Narrow Weak Beach)”。

image.png

photography of an iphone [with a modern user interface food ordering app on the screen] inspired by Behance and Figma and dribbble
prompt翻譯:一張iPhone的照片,螢幕上顯示著一個現代的使用者介面和美食訂餐應用程式。靈感來自Behance、Figma和dribbble。

image.png

photography of an iphone [with a modern user interface of [vector illustration of plants] plant identification app on the screen]inspired by Behance and Figma and dribbble
Prompt翻譯: 一張iPhone的照片,螢幕上顯示著一個現代的使用者介面和[植物向量插畫]的植物識別應用程式。靈感來自Behance、Figma和dribbble。

2.4網頁設計UI設計稿

**Prompt邏輯為 _photo of macbook M1 with [+ 你要在Mac上顯示的內容 + ] **style of dribbble and Behance and Figma ] on the screen_

image.png

photo of macbook M1 with [with a modern user interface of plant identification app on the screen] inspired by Behance and Figma and dribbble

image.png

photo of macbook m1 with [modern web user interface of nutrition website, style of dribbble and Behance and Figma ] on the screen

image.png

photo of macbook M1 with [with a modern user interface of plant identification app on the screen] inspired by Behance and Figma and dribbble

2.5 基本引數

  • 縱橫比
    --aspect或--ar 更改生成影像的縱橫比。

  • 混沌度
    --chaos <數字0-100> 更改結果的多樣性程度。更高的值會產生更不尋常和意外的生成影像。

  • 否定提示
    --no,或--no plants 會嘗試從影像中移除植物。

  • 質量
    --quality <.25,.5,1或2>,或--q <.25,.5,1或2> 表示您要花費多少時間渲染質量。預設值為1。更高的值成本更高,更低的值成本更低。

  • 種子
    --seed <介於0-4294967295之間的整數> Midjourney bot使用種子號建立一組視覺噪聲(如電視靜態),作為生成初始影像網格的起點。種子號是為每個影像隨機生成的,但可以使用--seed或--sameseed引數指定。使用相同的種子號和提示將產生類似的最終影像。

  • 停止
    --stop <介於10-100之間的整數> 使用--stop引數在過程的中途完成作業。在較早的百分比處停止作業可能會產生模糊、細節不清晰的結果。

  • 風格
    --style <4a、4b或4c> 在Midjourney模型版本4的不同版本之間切換。

  • 樣式化
    --stylize <數字>,或--s <數字> 引數影響Midjourney的預設美學風格對作業的應用強度。

  • 提亮
    --uplight 在選擇U按鈕時使用替代的“提亮”升頻器。結果更接近原始網格影像。升頻後的影像較不詳細,更加平滑。

  • 上調beta
    --upbeta 在選擇U按鈕時使用替代的beta升頻器。結果更接近原始網格影像。升頻後的影像新增的細節要少得多。

3.Prompt 應用

在這個指南中,將介紹一些高階和有趣的方式,透過 prompt engineering 來執行有用的更高階別的任務。

請注意,本節正在積極開發中。
主題:


3.1生成資料

LLMs具有生成文字的強大能力。使用有效的提示策略可以引導模型產生更好、更一致、更真實的響應。LLMs也可以特別有用於生成資料,這對於執行各種實驗非常有用。例如,我們可以使用它來生成情感分類器的快速樣本,如下所示:

Prompt:

生成10個情感分析的例子。這些例子被分類為正面或負面。生成2個負面例子和8個正面例子。使用以下格式的例子:
問題: <句子>
答案: <情感>

Output:

問題: 我剛剛得到了最好的訊息!
答案: 正面

問題: 我們剛剛在工作中得到了加薪!
答案: 正面

問題: 我為今天取得的成就感到非常自豪。
答案: 正面

問題: 我今天過得非常開心!
答案: 正面

問題: 我真的很期待週末。
答案: 正面

問題: 我剛剛得到了最好的禮物!
答案: 正面

問題: 我現在感到非常高興。
答案: 正面

問題: 我很幸運有這麼棒的家人。
答案: 正面

問題: 外面的天氣非常陰暗。
答案: 負面

問題: 我剛剛得到了一些可怕的訊息。
答案: 負面

這非常有用。我們在指南的另一部分中將使用此示例進行不同的測試。


3.2 程式輔助語言模型

Gao et al., (2022)提出了一種方法,使用LLMs來讀取自然語言問題並生成程式作為中間推理步驟。稱為程式輔助語言模型(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)

設定prompt + question:

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

相關文章