🚀 在我们的新课程中掌握提示工程并构建 AI 代理!使用 PROMPTING20 可享受 8 折优惠 ➜ 立即报名
ChatGPT

ChatGPT 提示工程

在本节中,我们将介绍 ChatGPT 的最新提示工程技术,包括技巧、应用、局限性、论文和延伸阅读材料。

主题


ChatGPT 简介

ChatGPT 是一个由 OpenAI 训练的新模型 (在新标签页中打开),能够以对话的方式进行交互。该模型经过训练,能够遵循提示中的指令,在对话语境中提供适当的响应。ChatGPT 可以帮助回答问题、提供食谱建议、以特定风格创作歌词、生成代码等等。

ChatGPT 使用人类反馈强化学习 (RLHF) 进行训练。虽然该模型比以前的 GPT 版本功能更强大(并且也经过训练以减少有害和不真实的输出),但它仍然存在局限性。让我们通过具体的示例来介绍一些功能和局限性。

您可以在此处使用 ChatGPT 的研究预览版,但对于下面的示例,我们将使用 OpenAI Playground 中的 Chat 模式。


回顾对话任务

在之前的一份指南中,我们稍微介绍了对话功能和角色提示。我们讨论了如何指导 LLM 以特定风格、特定意图、行为和身份进行对话。

让我们回顾一下我们之前的基本示例,在该示例中,我们创建了一个能够对问题生成更具技术性和科学性回复的对话系统。

提示

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of black holes?
AI:

从上面的示例中,您可以看到两个重要的组成部分

  • 意图或对聊天机器人的解释
  • 身份,指示聊天机器人将使用的回复风格或语气

上面简单的示例适用于使用 text-davinci-003 的文本补全 API。最近,OpenAI 宣布了 ChatGPT API (在新标签页中打开),这是一种更强大、更便宜的模型,称为 gpt-3.5-turbo,专门为这种类型的聊天补全功能构建。实际上,OpenAI 推荐它作为即使是非聊天用例的最佳模型。使用 ChatGPT API 的其他优势包括显着的成本降低(90%)和效率提高。

Snap Inc. 和 Instacart 等大公司已经在他们的产品中集成了由 ChatGPT 驱动的对话功能,这些功能涵盖了从个性化推荐到开放式购物目标等多种应用。


与 ChatGPT 对话

多轮对话

为了开始演示 ChatGPT 的功能,我们将使用上面提到的聊天机器人助手示例并讨论结果。与 text-davinci-003 相比,为 ChatGPT 提供支持的 gpt-3.5-turbo 模型使用聊天格式作为输入。该模型期望一系列消息作为输入,并使用这些消息生成响应。

输入

SYSTEM: You are an AI research assistant. You use a tone that is technical and scientific.
USER: Hello, who are you?
ASSISTANT: Greeting! I am an AI research assistant. How can I help you today?
USER: Can you tell me about the creation of black holes?

输出

ASSISTANT: Of course! Black holes are created when a massive star runs out of fuel and collapses under its own gravity. This collapse causes the star to become incredibly dense, with a gravitational field so strong that not even light can escape it. This point of no return is known as the event horizon. Once a star crosses the event horizon, it is considered a black hole...

请注意,在上面的示例中,我简化了输入和输出,但 ChatGPT 聊天补全 API 要求消息采用特定格式。我在下面添加了一张快照,展示了在 OpenAI Playground 中使用 Chat Mode 时此示例的外观

CHATGPT1

我们示例中更正式的 API 调用类似于下面的示例

import openai
 
openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are an AI research assistant. You use a tone that is technical and scientific."},
        {"role": "user", "content": "Hello, who are you?"},
        {"role": "assistant", "content": "Greeting! I am an AI research assistant. How can I help you today?"},
        {"role": "user", "content": "Can you tell me about the creation of black holes?"}
    ]
)

实际上,开发者未来与 ChatGPT 交互的方式预计将通过聊天标记语言 (在新标签页中打开)(简称 ChatML)进行。

单轮任务

聊天格式支持多轮对话,但也支持类似于我们与 text-davinci-003 一起使用的单轮任务。这意味着我们可以使用 ChatGPT 执行与我们为原始 GPT 模型演示的类似任务。例如,让我们尝试使用 ChatGPT 执行以下问答任务

输入

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

输出

ASSISTANT: Mice.

请记住,我添加了 USERASSISTANT 标签,以便更好地演示如何使用 ChatGPT 执行该任务。以下是使用 Playground 的示例

CHATGPTCLASSIC

更正式地说,这是 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,
)

指导聊天模型

根据 OpenAI 官方文档,gpt-3.5-turbo 模型的快照也将提供。例如,我们可以访问 3 月 1 日的快照 gpt-3.5-turbo-0301。这使得开发者可以选择特定的模型版本。这也意味着指导模型的最佳实践可能会因版本而异。

当前对于 gpt-3.5-turbo-0301 的建议是将指令添加到 user 消息中,而不是使用可用的 system 消息。

Notebooks

这是一个 Notebook,可以帮助您了解如何使用官方的 openai 库调用 ChatGPT API


参考资料