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的交互方式预计将通过Chat Markup Language (在新标签页中打开)(简称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消息中添加。

笔记本

这里有一个笔记本,可以了解更多关于如何使用官方的openai库调用ChatGPT API的信息:


参考文献