关于chatgpt:Prompt工程师指南应用篇Prompt应用ChatGPTMidjouney-Prompt-Engineering

15次阅读

共计 12406 个字符,预计需要花费 32 分钟才能阅读完成。

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

对于咱们的示例,更正式的 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 参考资料(英文)

  • 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

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

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

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

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_

squared with round edges mobile app logo design, flat vector app icon of a cute shiba inu face, minimalistic, white background
prompt 翻译: 圆角正方形的挪动应用程序徽标设计,是一只可恶的柴犬头像的扁平化矢量应用程序图标,极简主义格调,红色背景。

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

minimalistic, wh

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

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 _

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

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。

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_

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

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

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 解释器。

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

正文完
 0