什么是提示工程

提示工程通过设计模型的文本输入,引导大语言模型(LLM),最大化其在特定任务上的性能表现。

常见的大语言模型:ChatGPT文心一言Bard,LLAMA

提示工程是如何起作用的

提示工程根据不同的任务需求,设计合适的提示格式,以引导指令遵循模型生成期望的输出。

由于指令遵循模型的训练方式或它们所使用的数据,有一些特定的提示格式能够非常好地与手头的任务相匹配。

接下来,我们会介绍一些可靠有效的提示格式。但是也欢迎您探索不同的格式,以适配您的特定任务。

设计提示工程的原则

1:使用最优秀的模型

作为用户我们应该使用最新最强大的模型,当前最强大的模型是GPT4

当然,针对与不同任务类型,可以选择其他模型比如”code-davinci-002″,这个模型是为代码生成训练的。

2:使用英文(翻译回中文)

模型的训练数据中,英文占有较大比重,因此,在执行英文的提示输入时,模型会生成质量更高的文本。

特别是在与你需要大量文本生成的场景下。在使用GPT模型时候,中文的回复可能会显著缩短生成的文本长度。但是你可以在提示词中声明 “用英文回答”。

3:在提示的开头放置指令,并使用###或”””来分隔指令和上下文。

在提示的开头放置指令可以提高生成文本的质量。

例如:

Summarize the text below as a bullet point list of the most important points.

{text input here}

使用分隔符号对上下文分隔可以提升生成文本的质量。

例如:

Summarize the text below as a bullet point list of the most important points.

Text: “””

{text input here}

“””

4:给出具体,详细的提示词

给出具体,详细的提示词,以说明你想生成文本的结果样式、长度、行文风格、格式等。

例如:

效果不好的提示词:

Write a poem about OpenAI.

效果更好的提示词:

Write a short inspiring poem about OpenAI, focusing on the recent GPT4 product launch (GPT4 is a multi modal model) in the style of {预期风格}

5:通过例子,为语言模型限定期望的输出格式

效果不好的提示词:

Extract the entities mentioned in the text below. Extract the following 4 entity types: company names, people names, specific topics and themes. Text: {text}

效果较好的提示词:

Extract the important entities mentioned in the text below. First extract all company names, then extract all people names, then extract specific topics which fit the content and finally extract general overarching themes Desired format: Company names: <

comma_separated_list_of_company_names> People names: -||- Specific topics: -||- General themes: -||- Text: {text}

6:渐进地提升提示工程提示词质量 从零样本,少样本到模型微调。

对应深度学习中,零样本,少样本,模型微调的概念,后续会提供

零样本,仅给出提示词:

Extract keywords from the below text. Text: {text}

少样本,为模型提供提示词与少量示例:

Extract keywords from the corresponding texts below. Text 1: Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications. Keywords 1: Stripe, payment processing, APIs, web developers, websites, mobile applications ## Text 2: OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language. Keywords 2: OpenAI, language models, text processing, API. ## Text 3: {text} Keywords 3:

模型微调:

OPENAI的模型微调文档:后续会单独写一篇教程

7:减少模糊的描述

效果不好的提示词:

The description for this product should be fairly short, a few sentences only, and not too much more.效果较好的提示词:Use a 3 to 5 sentence paragraph to describe this product.

8:给正面提示,告诉模型去做什么而不是告诉模型不做什么

效果不好的提示词:

The following is a conversation between an Agent and a Customer. DO NOT ASK USERNAME OR PASSWORD. DO NOT REPEAT. Customer: I can’t log in to my account. Agent:

效果较好的提示词:

The following is a conversation between an Agent and a Customer. The agent will attempt to diagnose the problem and suggest a solution, whilst refraining from asking any questions related to PII. Instead of asking for PII, such as username or password, refer the user to the help article

http://www.samplewebsite.com/help/faq Customer: I can’t log in to my account. Agent:

9:使用“引导词”来引导模型生成特定模式的文本

效果不好的提示词:

# Write a simple python function that # 1. Ask me for a number in mile # 2. It converts miles to kilometers

效果较好的提示词:

在下面的提示词示例中,添加“import”提示模型应该开始用Python编写。

# Write a simple python function that # 1. Ask me for a number in mile # 2. It converts miles to kilometers import

10:模型参数

如果你使用OPENAI-API得出你想要的结果,那么你可以获得更为定制化的信息。要改变魔性的输出结果,model,temprature是最常见的参数。

model- 性能更高的模型更贵,也有更高的延迟。

temprature – 衡量模型输出一个不太可能的token的频率。温度越高,输出越随机(通常也越有创意)。但这并不等同于“真实性”。对于大多数基于事实的用例,如数据提取和真实的问答,温度为0最好。

max_tokens(最大长度)- 不控制输出的长度,而是对token生成的一个硬性截止限制。理想情况下,你不会经常碰到这个限制,因为你的模型会在它认为完成了,或者遇到了你定义的停止序列时停止。

stop(停止序列)- 一组字符(token),当生成时,会导致文本生成停止。