汀丶人工智能 · 2023年05月22日 · 浙江

Prompt工程指南[应用篇]:Prompt应用、ChatGPT|Midjouney

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

推荐阅读
NLP
关注数
42
内容数
147
NLP任务项目落地教学
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息