前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Prompt提示工程上手指南:基础原理及实践(二)-Prompt主流策略

Prompt提示工程上手指南:基础原理及实践(二)-Prompt主流策略

原创
作者头像
fanstuck
发布2024-02-05 17:43:18
7150
发布2024-02-05 17:43:18
举报

前言

上篇文章将Prompt提示工程大体概念和具体工作流程阐述清楚了,我们知道Prompt工程是指人们向生成性人工智能(AI)服务输入提示以生成文本或图像的过程中,对这些提示进行精炼的过程。生成人工智能是一个根据人类和机器产生的数据训练的机器人,它不具备筛选你正在交流的内容以理解你实际在说什么的能力。也就是说你说的就是你得到的。那么我们使用Prompt在不进行高成本的调参,显得尤为重要,因此有很多Prompt策略适用于不同的语言大模型中,在图像大模型中Prompt策略可谓是核心必学科目了。那么本章我们将了解Prompt主流策略有什么。

定义prompt流程

在上篇文章中我们了解到Prompt基本种类有以下几种:

我们可以根据Prompt的类别去定义不同的Prompt格式,那么我们也可以根据Prompt规则建立一套流程化的Prompt公式,首先我们来慢慢拆解一个成功的Prompt功能,以微软OpenAI给出为Prompt engineering techniques为例子:.

定义人设

如果大家经常用语言大模型的一些原生应用或者是去看过一些Prompt模板,开头的第一句往往都是说:我想让你担任xxx或者是你是一名xxx诸如此类定义人设的话:

诸如此类的Prompt模板,为方便其中指定人设这段话我称之为System message,System message包含着Prompt开头,用于为模型提供上下文、指令或与使用案例相关的其他信息。可以使用系统消息来描述助手的个性,定义模型应该和不应该回答的问题,以及定义模型回应的格式,再来看微软给的例子:

System message

User

Assistant

You are an AI assistant that helps people find information and responds in rhyme. If the user asks you a question you don't know the answer to, say so.

What can you tell about me, John Doe?

Dear John, I'm sorry to say, But I don't have info on you today. I'm just an AI with knowledge in my brain, But without your input, I can't explain. So please tell me more about what you seek, And I'll do my best to give you an answer unique.

简而言之,系统消息就像是对话的"预设条件",它为接下来的交互设定了一个基础框架。通过明确指出助手的行为方式、回应的边界以及期望的交互风格,系统消息帮助确保模型的回应更加符合用户的期望和需求,我们可以基于原始内容应用进行修改变得更加多样化:

  • “Assistant is a large language model trained by OpenAI.”
    • 明确了助手的基础,助于设定用户对助手能力的基本期望。
  • “Assistant is an intelligent chatbot designed to help users answer technical questions about Azure OpenAI Service. Only answer questions using the context below and if you're not sure of an answer, you can say "I don't know".
    • 设定了一个更具体的角色和使用范围:解答有关Azure OpenAI服务的技术问题。同时,它还指示了助手在不确定答案时应如何回应,即说“我不知道”。
  • “Assistant is an intelligent chatbot designed to help users answer their tax related questions.”
    • 类似于第二个例子,这个消息定义了助手的专长领域:税务相关的咨询。这有助于用户理解应当向助手提出哪类问题。
  • “You are an assistant designed to extract entities from text. Users will paste in a string of text and you will respond with entities you've extracted from the text as a JSON object. Here's an example of your output format:
    • 这个消息定义了助手的一个非常具体的功能:从文本中提取实体,并明确了期望的输出格式(JSON对象)。这对于那些需要此类功能的用户非常有用。

重要的是要理解,即使系统消息中指示模型在不确定答案时回答“我不知道”,这也不能保证模型总是会遵守这一请求。设计良好的系统消息可以增加某种结果的可能性,但仍有可能生成与系统消息中的指示相悖的错误回应。这意味着,在设计系统消息时,尽管它可以指导模型的行为,但不能完全控制模型的所有输出。因此,开发者和用户都应该准备好处理意外或不准确的回答。

当我们完成这一步之后,我们就可以进入到下一个环节去补足模型效果。

少量样本学习

“少量样本学习(Few-shot learning)”是一种让语言模型适应新任务的常用方法。在这种方法中,作为提示(prompt)的一部分,提供了一组训练样例,以给模型提供额外的上下文。也就是我们如果无法把控模型会生成什么内容,那么我们就给出少量回答样本,让模型自己去学习,这些示例可以用来“启动”(prime)模型,使其以特定的方式回应,模仿特定的行为,并对常见问题提供答案。

System message

Few-shot examples

Assistant is an intelligent chatbot designed to help users answer their tax related questions. Instructions - Only answer questions related to taxes. - If you're unsure of an answer, you can say "I don't know" or "I'm not sure" and recommend users go to the IRS website for more information.

User - "When do I need to file my taxes by?" Assistant - "In 2023, you will need to file your taxes by April 18th. The date falls after the usual April 15th deadline because April 15th falls on a Saturday in 2023. For more details, see https://www.irs.gov/filing/individuals/when-to-file " User - "How can I check the status of my tax refund?" Assistant - "You can check the status of your tax refund by visiting https://www.irs.gov/refunds "

将这二者结合起来可以用于回答:

  • 客户服务:模型可以被训练来模仿客服代表的行为,回答常见的客户查询。
  • 技术支持:提供特定于技术问题的对话,可以让模型学会回答复杂的技术问题。
  • 教育辅导:通过少量样本学习,模型可以被训练为辅导学生解决特定的学术问题。

总之,“少量样本学习”是一种灵活且高效的方法,使得即使在只有少量数据的情况下,模型也能快速适应并执行新的任务。通过精心设计的示例,可以显著提高模型在特定场景下的表现,在不进行大模型调优的环境下尤为重要。

倘若我们不是对话交流类场景,而是作为应用功能来使用的话,可以精简prompt.

非聊天场景

例如,对于情绪分析场景,可以使用以下提示:

System message

User

Assistant

You are an assistant designed to analyze sentiment from speech data. Users will paste in a string of text and you will respond with an assessment of the response of the speaker. Rate on a scale of 1-10 (10 being highest). Explain why this rating was given.

hi thank you for calling contoso pharmacy who am i speaking with today i understand your frustration mary um can you tell me what exactly you're trying to accomplish OK uh certainly happy to check that for you one moment please i see here that you were on a generic form of lipitor is that right OK uh so i see that your doctor stopped prescribing these drugs in twenty twenty one actually OK uh yeah i'd be happy to check that for you uh because sometimes there's a gap in the system and it just doesn't get reported so let me take a look here just a moment so i'm seeing here that your doctor had these drugs prescribed to you from twenty twelve through twenty twenty one OK um yeah according to the latest records uh provided by doctor's office you're now on a different drug metformin would you like us to go ahead and fill that prescription for you for pick up a bit of form in five hundred milligrams uh you're very welcome mary please let us know if there's anything else we can do for you today.

Sentiment rating: 8 The speaker in this text seems to be polite, helpful, and patient while addressing the concerns of the customer. They take the time to check the information, provide clear explanations, and offer assistance. However, the rating is not a 10 because there is still a hint of uncertainty in some parts of the conversation, which may not convey complete confidence to the customer.

这是微软给我们的例子,如果我们想要做一个信息抽取的应用,可以试试这段Prompt:

input format :

你是一个自然语言处理专业机器人,你需要从我给出的内容中抽取所有出现的名词并通过列表的形式进行展示。接下来你需要抽取的内容是:老师在教室给学生教授各种知识。他解释了人体的结构,从脑部到肌肉到骨骼都详细描述了。然后他转向自然科学,讲解了地球的成分,从水到土壤到空气,再到天文学,从星球到星云到银河,有关宇宙的一切他都简要概括了。

output format:

以下是从文本中抽取的名词列表: 老师 教室 学生 知识 人体 脑部 肌肉 骨骼 自然科学 地球 成分 水 土壤 空气 天文学 星球 星云 银河 宇宙。

以上设定回答格式完毕之后就暂时不用担心回答多样化了,但是也会存在一些偏离格式的回答,取决于给出的问题,这点需要注意。

有始有终

提示中显示信息的顺序很重要。 GPT 风格的模型内核是以格式理解方式构建的,这定义了它们处理输入的方式。 在共享其他上下文信息或示例之前,在提示开始时告诉模型你希望它执行的任务有助于生成更高质量的输出。模型可能容易受到近因偏差的影响,在此上下文中,这意味着提示结束时的信息对输出的影响可能比提示开头的信息更大。 因此在提示结束时重复指令,可以更有效的控制LLM的回答。

我们以引导输出的Prompt来看:

System message

User

Assistant

You are an AI assistant that helps people find information. Answer in as few words as possible.

John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should I do to fact-check this? ## One possible search query is:

"John Smith married Lucy Smith five kids software engineer Microsoft"

在上述提示中,文本“一个可能的搜索查询是:”引导模型生成单个输出。 如果没有此提示,模型将生成多个搜索查询作为输出。

这是指在提示的末尾包含几个字词或短语,以获取遵循所需形式的模型响应。 例如,使用 “Here’s a bulleted list of key points:\n- ” 等提示有助于确保输出的格式为项目符号列表。

添加明确的语法

在提示中使用清晰的语法——包括标点符号、标题和小节标记——有助于传达意图,通常使输出更容易解析。

在下面的例子中,在不同的信息源或步骤之间添加了分隔符(在本例中为---)。这允许使用---作为生成的停止条件。此外,章节标题或特殊变量以大写字母表示,以区分它们。

System message

User

Assistant

You are an AI assistant that helps people find information.

You will read a paragraph, and then issue queries to a search engine in order to fact-check it. Also explain the queries. --- PARAGRAPH John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should I do to fact-check this? --- QUERIES

  1. "John Smith Microsoft" - To check if John Smith is indeed employed at Microsoft. 2. "John Smith Lucy Smith" - To check if John Smith is married to Lucy Smith. 3. "John Smith children" - To check if John Smith has five children.

模型已经在很多XML和Markdown的网页内容上进行了训练,如果你不确定使用什么语法可以考虑使用Markdown或XML。

分解任务

大型语言模型(LLMs)在任务被分解成较小步骤时,往往表现得更好。例如,在之前提到的搜索查询提示中,可以重新构建提示,使模型首先被指示提取相关事实,然后被指示生成可以用来验证这些事实的搜索查询。

System message

User

Assistant

You are an AI assistant that helps people find information.

You will read a paragraph, and then issue queries to a search engine in order to fact-check it. --- PARAGRAPH John Smith is married to Lucy Smith. They have five kids, and he works as a software engineer at Microsoft. What search queries should I do to fact-check this? --- Now you will extract factual claims, and issue queries to fact-check them. When issuing a query, use the function SEARCH("query") FACTUAL CLAIMS

  1. John Smith is married to Lucy Smith 2. They have five kids 3. He works as a software engineer at Microsoft QUERIES SEARCH("John Smith married Lucy Smith") SEARCH("John Smith family size") SEARCH("John Smith Microsoft software engineer")

比如看以上例子,使用清晰的语法来区分不同的部分并引导输出。在这个简单的例子中,将任务从一个步骤分解成两个步骤并不是无规则的变化,可以尝试对包含许多事实大量文本段落进行此操作时,分解任务可以产生显著的效果。

自发功能

有时我们可以让模型使用一种工具(affordance),而不是仅依赖其自身的参数来获取信息和答案。例如,搜索可以作为一种工具来帮助减轻捏造答案的问题,并获取最新信息。也就是阶段性Prompt生成,在模型生成工具调用后停止生成,然后将结果粘贴回提示中。这种方法比较平常,先用LLM去搜索寻找一些内容,然后自我调用,这个模式十分常见暂不作演示。

思维链提示

不同于将任务分解成更小的步骤,在这种方法中,模型的响应被指示逐步进行并呈现涉及的所有步骤。这样做减少了结果不准确的可能性,并使评估模型响应变得更容易。通过逐步展示处理过程,模型不仅提供了最终的答案或解决方案,而且还展示了达到这个答案的具体步骤。这种方法对于教育、解释复杂问题或在需要详细说明决策过程的情境中尤其有用。它允许用户跟踪模型的思考过程,理解模型是如何从问题描述到达最终结论的。

System message

User

You are an AI assistant that helps people find information.

Who was the most decorated (maximum medals) individual athlete in the Olympic games that were held at Sydney? Take a step-by-step approach in your response, cite sources and give reasoning before sharing final answer in the below format: ANSWER is: <name>

此外,通过将任务分解为明确的步骤,可以更容易地识别和纠正过程中可能出现的错误,因为每一步都可以单独验证和评估。这种逐步方法也有助于提高模型透明度和用户对模型生成内容的信任。

指定输出结构

使用提示指定输出的结构可能会对结果的性质和质量产生重大影响。有时,系统消息输入为“只写真实事实”或“不编造信息”可能不足以缓解。相反,要求模型回复也包括引用可以帮助降低错误回复的普遍性。

如果指示模型在进行陈述时引用源材料,则这些陈述更有可能是有根据的。请求引用使得模型每次生成响应时都必须犯两个错误:第一个错误是捏造的响应,第二个错误的引用。引文越接近其支持的文本,模型预测引文所需的距离就越短,这表明内联引文比内容末尾的引文更好地缓解虚假内容的生成。

同样,如果你要求模型从段落中提取事实陈述,它可能会提取复合陈述,如“X正在做Y和Z”(这可能更难验证)。这可以通过指定诸如(entity1,relationship,entity2)之类的输出结构来避免。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 定义prompt流程
    • 定义人设
      • 少量样本学习
        • 非聊天场景
          • 有始有终
            • 添加明确的语法
              • 分解任务
                • 自发功能
                  • 思维链提示
                    • 指定输出结构
                    相关产品与服务
                    NLP 服务
                    NLP 服务(Natural Language Process,NLP)深度整合了腾讯内部的 NLP 技术,提供多项智能文本处理和文本生成能力,包括词法分析、相似词召回、词相似度、句子相似度、文本润色、句子纠错、文本补全、句子生成等。满足各行业的文本智能需求。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                    http://www.vxiaotou.com