如何写好大模型提示词?来自大赛冠军的经验分享(基础篇)

发布时间:2024-01-17 13:33:55

编者按:近期,如何通过 Prompt Engineering 最大程度发挥大模型的潜力已成为一个热点话题。人们越来越关注如何通过 Prompt Engineering 技术低成本地用好大模型。

今天我们推荐的这篇文章,作者认为 Prompt Engineering 是一门融合了艺术和科学的学科,需要在理解技术背景的同时,发挥创造力和战略思维。

本系列文章详细介绍了作者在新加坡首届 GPT-4 Prompt Engineering 大赛中使用的策略技巧,包括:使用 CO-STAR 框架构建提示语、使用分隔符明确语义单元、利用 system prompts 添加行为约束、仅依靠GPT-4对数据集进行分析等。这些技巧都得到了实例验证,证明了 Prompt Engineering 的重要作用。

本文属于该系列文章的第一部分,介绍适合初学者的提示语(prompt)技巧,相关的高级策略将在下一篇文章详细介绍,敬请期待!

作者 | Sheila Teo

编译 | 岳扬

🚢🚢🚢欢迎小伙伴们加入AI技术软件及技术交流群,追踪前沿热点,共探技术难题~

上个月,我有幸获得新加坡首届 GPT-4 提示工程(Prompt Engineering)大赛相关奖项,该比赛由新加坡政府科技署(GovTech)组织,汇聚了超过 400 位优秀的参与者。

提示工程(Prompt Engineering)是一门融合了艺术和科学的学科——这门学科不仅需要理解技术,还需要一定的创造力和战略思维。 以下是我在学习过程中学到的提示工程(Prompt Engineering)策略汇编,这些策略可以驱动任何大语言模型(LLM)精准执行需求,甚至超常发挥!

作者注:

在撰写本文时,我力图摒弃已在网上广泛讨论和记录的传统提示工程(Prompt Engineering)技术。相反,撰写本文的目的是给大家介绍我在实验中学到的新见解,以及对某些技术的不同理解。希望你会喜欢阅读这篇文章!

本系列文章包括以下内容,其中🔵指的是适合初学者的提示语(prompt)技巧,而🔴指的是高级策略(具体内容后续马上推送给大家):

1.[🔵]使用 CO-STAR 框架构建提示语

2.[🔵] 使用分隔符(delimiters)将提示语分段

3.[🔴] 使用 LLM guardrails 创建 system prompts(译者注:“guardrails” 指的是一种保护机制或限制,用于确保大语言模型生成的内容符合特定标准或要求,防止产生不准确、不合适或有害的信息。)

4.[🔴] 仅使用 LLM(无需插件或代码)分析数据集——将介绍一个使用 GPT-4 分析真实 Kaggle 数据集的实践示例

01 [🔵]使用 CO-STAR 框架构建提示语

有效的提示语(prompt)结构对于从 LLM 那里获得最佳回答至关重要。CO-STAR 框架是新加坡政府科技署数据科学与人工智能团队的心血结晶,是构建提示语(prompt)的便捷模板。其考虑到了影响 LLM 回答的有效性和相关性的所有关键方面,从而有效地优化 LLM 的回答。

CO-STAR framework — Image by author

其工作原理如下:

  • (C)Context:提供任务的背景信息

    有助于 LLM 理解正在讨论的具体语境,确保其回答是相关的。

  • (O)Objective:确定希望 LLM 执行的任务是什么

    明确目标任务有助于 LLM 将其回答重点放在实现某一特定任务上。

  • (S)Style:指明希望 LLM 使用的写作风格

    可以设置为某位著名人物的写作风格,也可以是某个专业领域的专家,比如商业分析专家或首席执行官。这样能够引导 LLM 以符合用户需求的方式和用词做出回答。

  • (T)Tone:需要确定大模型回答用户提示词的态度

    这样可确保 LLM 的回答与所需的情感或语境相呼应。例如,正式、幽默、感同身受等。

  • (A)Audienc:确定回答的对象

    根据回答对象的身份(如某一领域的专家、初学者、儿童等)量身定制 LLM 的回答,确保其在所需的语境中是恰当和可以理解的。

  • (R)Response:提供 LLM 回答的格式

    该功能可以让 LLM 以下游任务所需的准确格式输出。例如,列表、JSON(JavaScript Object Notation)、专业报告等。对于大多数以编程方式在下游操作 LLM 回答的 LLM 应用程序来说,JSON输出格式是最理想的。

1.1 CO-STAR的实际应用

以下是一个 CO-STAR 框架为何实用的实际案例。假设您是一名社交媒体经理(social media manager),需要帮助起草一则 Facebook 帖子,以宣传公司的新产品。

没有使用 CO-STAR 的提示语(prompt)可能如下所示:

Write a facebook post to advertise my company’s new product. My company’s name is Alpha and the product is called Beta, a new ultra-fast hairdryer.

以下是 GPT-4 的输出结果:

Introducing the Revolutionary Beta Hairdryer by Alpha!

  • Say goodbye to long, tedious hair drying sessions! Alpha is proud to unveil the Beta, our latest innovation in haircare technology.

  • Ultra-Fast Drying: The Beta is designed to cut your drying time in half, thanks to its advanced, powerful motor. Get ready faster and enjoy more time doing what you love!

Shop Now: [Link to product page]

虽然内容已经足够了,但这个输出实在过于普通,缺乏引起公司受众产生共鸣所需的,与其他产品的必要细微差异和有针对性的吸引力。

下面将介绍一个 CO-STAR 框架的使用案例,它会提醒您在提示语(prompt)中添加目标任务的其他信息,特别是在之前的 quick prompt 中缺少的STYLE(风格)、TONE(语气)和AUDIENCE(受众)等信息:

# CONTEXT #

I want to advertise my company’s new product. My company’s name is Alpha and the product is called Beta, which is a new ultra-fast hairdryer.

# OBJECTIVE #

Create a Facebook post for me, which aims to get people to click on the product link to purchase it.

# STYLE #

Follow the writing style of successful companies that advertise similar products, such as Dyson.

# TONE #

Persuasive

# AUDIENCE #

My company’s audience profile on Facebook is typically the older generation. Tailor your post to target what this audience typically looks out for in hair products.

# RESPONSE #

The Facebook post, kept concise yet impactful.

通过使用 CO-STAR 框架,GPT-4 的回答变得更有针对性、更有效:

Embrace Effortless Elegance with Alpha Beta — The Ultra-Fast Hairdryer for the Wise Generation.

Rediscover the joy of simple, effective hair care with Alpha Beta. Our latest innovation is more than just a hairdryer; it’s a promise of swift, gentle, and

reliable hair styling for those who appreciate the finer things in life.

- Easy and Intuitive Use: Say goodbye to complicated gadgets. Alpha Beta is

crafted for comfort and simplicity, perfect for those who value straightforward,

hassle-free technology.

- Time-Saving Technology: We understand your time is precious. That’s why Alpha Beta cuts down drying time significantly, giving you more moments to enjoy life’s pleasures.

Make every day a good hair day with Alpha Beta. Experience the blend of

sophistication and simplicity today.

[Your Product Link Here]

CO-STAR 框架指导我们有条不紊地向 LLM 提供相关任务的所有关键信息,确保根据我们的需求提供量身定制的优化回答。

02 [🔵] 使用分隔符(delimiters)将提示语分段

Image generated by DALL·E 3

分隔符(Delimiters)是一种特殊的 tokens ,LLM 通过分隔符(Delimiters)将提示语分成不同的部分,以便确定哪些部分构成一个意义单元。这一点很重要,因为整个提示语(prompt)是以一长串 tokens 的形式到达 LLM 的。分隔符通过将提示语(prompt)的特定部分划分开再以不同的方式对待,为整个 tokens 序列提供了合理的结构。

值得注意的是,对于简单的任务,分隔符可能不会对 LLM 的回答质量产生影响。然而,任务越复杂,使用分隔符对 LLM 回答的影响就越大。

2.1 将特殊字符作为分隔符

分隔符可以是任何通常不会一起出现的特殊字符序列,例如

  • ###
  • ===
  • >>>

所选特殊字符的数量和类型并不重要,只要它们足够独特,能让 LLM 将其理解为内容分隔符而非普通标点符号即可。

下面举例说明如何在提示语(prompt)中使用此类分隔符:

Classify the sentiment of each conversation in <<<CONVERSATIONS>>> as

‘Positive’ or ‘Negative’. Give the sentiment classifications without any other preamble text.

EXAMPLE CONVERSATIONS

[Agent]: Good morning, how can I assist you today?

[Customer]: This product is terrible, nothing like what was advertised!

[Customer]: I’m extremely disappointed and expect a full refund.

[Agent]: Good morning, how can I help you today?

[Customer]: Hi, I just wanted to say that I’m really impressed with your

product. It exceeded my expectations!

EXAMPLE OUTPUTS

Negative

Positive

<<<

[Agent]: Hello! Welcome to our support. How can I help you today?

[Customer]: Hi there! I just wanted to let you know I received my order, and

it’s fantastic!

[Agent]: That’s great to hear! We’re thrilled you’re happy with your purchase.

Is there anything else I can assist you with?

[Customer]: No, that’s it. Just wanted to give some positive feedback. Thanks

for your excellent service!

[Agent]: Hello, thank you for reaching out. How can I assist you today?

[Customer]: I’m very disappointed with my recent purchase. It’s not what I expected at all.

[Agent]: I’m sorry to hear that. Could you please provide more details so I can help?

[Customer]: The product is of poor quality and it arrived late. I’m really

unhappy with this experience.

>>>

上述案例使用分隔符 ### 进行分隔,分隔内容的标题 EXAMPLE CONVERSATIONS(示例对话)和 EXAMPLE OUTPUTS(示例输出)用大写字母与其他内容进行区分。需要进行情感分析的对话被划分在<<<CONVERSATIONS>>>内,这些对话随后在提示语(prompt)的底部直接提供给LLM,没有任何解释性文本,但由于分隔符 <<< 和 >>> 的存在,LLM 知道这些是它应该进行情感分析的对话。

以下是来自 GPT-4 的输出结果,直接输出情感分析结果,没有其他乱七八糟的内容,就是我们要求的那样。

Positive

Negative

2.2 将 XML 标签作为分隔符

使用 XML 标签作为分隔符是另一种使用分隔符的方法。XML 标签是用尖括号括起来的标签,包括开始标签和结束标签。例如,<tag>和</tag>。这种方法是比较有效的,因为大语言模型(LLM)已经在许多 XML 格式的 Web 内容上进行了训练,并学会了理解这种格式。

以下是相同的提示语(prompt),但是使用了 XML 标签作为分隔符来构造其结构:

Classify the sentiment of the following conversations into one of two classes, using the examples given. Give the sentiment classifications without any other

preamble text.

<classes>

Positive

Negative

</classes>

<example-conversations>

[Agent]: Good morning, how can I assist you today?

[Customer]: This product is terrible, nothing like what was advertised!

[Customer]: I’m extremely disappointed and expect a full refund.

[Agent]: Good morning, how can I help you today?

[Customer]: Hi, I just wanted to say that I’m really impressed with your

product. It exceeded my expectations!

</example-conversations>

<example-classes>

Negative

Positive

</example-classes>

<conversations>

[Agent]: Hello! Welcome to our support. How can I help you today?

[Customer]: Hi there! I just wanted to let you know I received my order, and

it’s fantastic!

[Agent]: That’s great to hear! We’re thrilled you’re happy with your purchase.

Is there anything else I can assist you with?

[Customer]: No, that’s it. Just wanted to give some positive feedback. Thanks

for your excellent service!

[Agent]: Hello, thank you for reaching out. How can I assist you today?

[Customer]: I’m very disappointed with my recent purchase. It’s not what I

expected at all.

[Agent]: I’m sorry to hear that. Could you please provide more details so I

can help?

[Customer]: The product is of poor quality and it arrived late. I’m really

unhappy with this experience.

</conversations>

在 XML 标签中使用与您在 instructions 中用来描述它们的词相同的名词是有好处的。我们在上述提示语(prompt)中给出的 instructions 是 (在本文中,instructions指的是前面提到的提示语(prompt)中给出的具体操作步骤或规则。)

Classify the sentiment of the following conversations into one of two classes, using the examples given. Give the sentiment classifications without any other

preamble text.

我们在 instructions 中使用了名词 “conversations” 、 “classes” 和 “examples” 。因此,可以用作分隔符的 XML 标签为 <conversations> 、 <classes> 、 <example-conversations> 和 <example-classes> 。这可以确保 LLM 理解您的 instructions 与用作分隔符的 XML 标签之间的关系。

同样,通过使用这些分隔符,我们可以清晰有序地划分 instructions,从而确保 GPT-4 能够按照我们的要求准确地生成回答。

Positive

Negative

Thanks for reading!


下一期,我们将介绍高级技巧,分享使用 LLM guardrails 创建 system prompts 和使用 LLM 分析数据集的提示词策略。下期见!

🚢🚢🚢欢迎小伙伴们加入AI技术软件及技术交流群,追踪前沿热点,共探技术难题~

END

本文经原作者授权,由Baihai IDP编译。如需转载译文,请联系获取授权。

原文链接:

https://towardsdatascience.com/how-i-won-singapores-gpt-4-prompt-engineering-competition-34c195a93d41

文章来源:https://blog.csdn.net/Baihai_IDP/article/details/135646212
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。