Prompt工程师攻略[运用篇]:Prompt运用、ChatGPT|Midjouney Prompt Engineering

1.ChatGPT Prompt Engineering

主题:

与 ChatGPT 对话 Python 笔记本

Topics:

  • ChatGPT介绍
  • 检查对话使命
  • 与ChatGPT对话
  • Python笔记本

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时,此示例的外观:

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

关于咱们的示例,更正式的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的示例:

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

更正式地说,这是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参阅资料(英文)

  • Seeing ChatGPT Through Students’ Eyes: An Analysis of TikTok Data (March 2023)
  • Extracting Accurate Materials Data from Research Papers with Conversational Language Models and Prompt Engineering — Example of ChatGPT (Mar 2023)
  • ChatGPT is on the horizon: Could a large language model be all we need for Intelligent Transportation? (Mar 2023)
  • Making a Computational Attorney (Mar 2023)
  • Does Synthetic Data Generation of LLMs Help Clinical Text Mining? (Mar 2023)
  • MenuCraft: Interactive Menu System Design with Large Language Models (Mar 2023)
  • A Comprehensive Survey of AI-Generated Content (AIGC): A History of Generative AI from GAN to ChatGPT (Mar 2023)
  • Exploring the Feasibility of ChatGPT for Event Extraction
  • ChatGPT: Beginning of an End of Manual Annotation? Use Case of Automatic Genre Identification (Mar 2023)
  • Is ChatGPT a Good NLG Evaluator? A Preliminary Study (Mar 2023)
  • Will Affective Computing Emerge from Foundation Models and General AI? A First Evaluation on ChatGPT (Mar 2023)
  • UZH_CLyp at SemEval-2023 Task 9: Head-First Fine-Tuning and ChatGPT Data Generation for Cross-Lingual Learning in Tweet Intimacy Prediction (Mar 2023)
  • How to format inputs to ChatGPT models (Mar 2023)
  • Can ChatGPT Assess Human Personalities? A General Evaluation Framework (Mar 2023)
  • Cross-Lingual Summarization via ChatGPT (Feb 2023)
  • ChatAug: Leveraging ChatGPT for Text Data Augmentation (Feb 2023)
  • Dr ChatGPT, tell me what I want to hear: How prompt knowledge impacts health answer correctness (Feb 2023)
  • An Independent Evaluation of ChatGPT on Mathematical Word Problems (MWP) (Feb 2023)
  • ChatGPT: A Meta-Analysis after 2.5 Months (Feb 2023)
  • Let’s have a chat! A Conversation with ChatGPT: Technology, Applications, and Limitations (Feb 2023)
  • Check Your Facts and Try Again: Improving Large Language Models with External Knowledge and Automated Feedback (Feb 2023)
  • On the Robustness of ChatGPT: An Adversarial and Out-of-distribution Perspective (Feb 2023)
  • How Generative AI models such as ChatGPT can be (Mis)Used in SPC Practice, Education, and Research? An Exploratory Study (Feb 2023)
  • Can ChatGPT Understand Too? A Comparative Study on ChatGPT and Fine-tuned BERT (Feb 2023)
  • A Prompt Pattern Catalog to Enhance Prompt Engineering with ChatGPT (Feb 2023)
  • Zero-Shot Information Extraction via Chatting with ChatGPT (Feb 2023)
  • ChatGPT: Jack of all trades, master of none (Feb 2023)
  • A Pilot Evaluation of ChatGPT and DALL-E 2 on Decision Making and Spatial Reasoning (Feb 2023)
  • Netizens, Academicians, and Information Professionals’ Opinions About AI With Special Reference To ChatGPT (Feb 2023)
  • Linguistic ambiguity analysis in ChatGPT (Feb 2023)
  • ChatGPT versus Traditional Question Answering for Knowledge Graphs: Current Status and Future Directions Towards Knowledge Graph Chatbots (Feb 2023)
  • What ChatGPT and generative AI mean for science (Feb 2023)
  • Applying BERT and ChatGPT for Sentiment Analysis of Lyme Disease in Scientific Literature (Feb 2023)
  • Exploring AI Ethics of ChatGPT: A Diagnostic Analysis (Jan 2023)
  • ChatGPT for Good? On Opportunities and Challenges of Large Language Models for Education (Jan 2023)
  • The political ideology of conversational AI: Converging evidence on ChatGPT’s pro-environmental, left-libertarian orientation (Jan 2023)
  • Techniques to improve reliability – OpenAI Cookbook
  • Awesome ChatGPT Prompts
  • Introducing ChatGPT (Nov 2022)

2.Midjouney Prompt Engineering

2.1 科技品牌插画

**Pormpt要点 _generic productivity illustration for + 你要画的东西+ by slack and dropbox, style of behance **

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

_generic productivity illustration for a tech company, by slack and dropbox, style of behance _ prompt翻译 一家科技公司的通用生产力插画,由 Slack 和 Dropbox 制造,风格参阅 Behance。

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

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

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

Falling in love with your company culture, illustration for a tech company, by slack and dropbox, style of behance prompt翻译: 情人节主题,一家科技公司的插画,由 Slack 和 Dropbox 制造,风格参阅 Behance。

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

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_

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

squared with round edges mobile app logo design, flat vector app icon of a cute shiba inu face, minimalistic, white background prompt翻译: 圆角正方形的移动运用程序徽标规划,是一只可爱的柴犬头像的扁平化矢量运用程序图标,极简主义风格,白色布景。

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

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

minimalistic, wh

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

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

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

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

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

_photography of an iphone Narrow Weak Beachinspired by Behance and Figma and dribbble _ prompt翻译 一张iPhone的相片,创意来自Behance、Figma和dribbble,主题为“窄弱的海滩(Narrow Weak Beach)”。

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

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。

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

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_

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

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

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

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

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

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 来履行有用的更高档别的使命。

请注意,本节正在积极开发中。 主题:

  • 生成数据
  • 程序辅佐言语模型
  • Python笔记本

3.1生成数据

LLMs具有生成文本的强大才能。运用有用的提示战略能够引导模型发生更好、更一致、更真实的呼应。LLMs也能够特别有用于生成数据,这关于运转各种实验十分有用。例如,咱们能够运用它来生成情感分类器的快速样本,如下所示:

Prompt:

生成10个情感剖析的比如。这些比如被分类为正面或负面。生成2个负面比如和8个正面比如。运用以下格式的比如:
问题: <句子>
答案: <情感>

Output:

问题: 我刚刚得到了最好的音讯!
答案: 正面
问题: 咱们刚刚在工作中得到了加薪!
答案: 正面
问题: 我为今日取得的成就感到十分骄傲。
答案: 正面
问题: 我今日过得十分高兴!
答案: 正面
问题: 我真的很等待周末。
答案: 正面
问题: 我刚刚得到了最好的礼物!
答案: 正面
问题: 我现在感到十分高兴。
答案: 正面
问题: 我很幸运有这么棒的家人。
答案: 正面
问题: 外面的天气十分昏暗。
答案: 负面
问题: 我刚刚得到了一些可怕的音讯。
答案: 负面

这十分有用。咱们在攻略的另一部分中将运用此示例进行不同的测验。


3.2 程序辅佐言语模型

Gao et al., (2022)提出了一种方法,运用LLMs来读取自然言语问题并生成程序作为中间推理过程。称为程序辅佐言语模型(PAL),与思想链提示不同,它不是运用自在方式的文原本获取处理方案,而是将处理过程卸载到编程运转时,例如Python解说器。

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

让咱们经过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

参阅链接:github.com/wangxuqi/Pr…