【LLM】如何基于 Claude 写好一个针对超长文档的 prompt

前言


在深入探索如何高效地使用Claude模型时,理解并精心设计 prompt 模板是很重要的。本文介绍了一套详细的指南和技巧,旨在帮助用户最大化语言模型的潜力,通过精确的指令和结构化的输入,实现更加准确和有用的输出。

一个好的 prompt 模版


严格按照下面的规则编写 Claude 的 prompt,就已经写好一大半了。
在这里插入图片描述

需要注意以下几个点:

  1. 左边是 11 个模块,右边是一个示例,颜色与对应的模块一致
  2. 顺序非常重要,虽然官网用的是‘建议’,但我们实测下来,至少针对大文档,不同的顺序会产生不同的效果,严格按照这个顺序进行 prompt 的编写能够得到最稳定、最佳的结果
  3. 如果你使用的是 Claude2.1,那么 Task Context 和 Tone Context 最好是写在 System Prompt 里面

Human: 和 Assistant: 并不是两个完全固定的标签


  1. 一般来说,Claude 被训练为一个对话式的助理,所有的训练数据都是标准的 \n\nHuman:\n\nAssistant: 格式,所以可以简单的理解为 Human: 后面的内容会被 Claude 认为是人的输入,而 Assistant: 则会被 Claude 认为是需要自己输出的部分。这是一个标准的格式,并且 HumanAssistant 标签前面的两个空行也一定不能忽略

  2. 但是不要简单的把 \n\nHuman:\n\nAssistant: 做为一个固定的 prompt 的头和尾标签写在代码里面,至少在以下情况下我们是可以利用 Assistant 这个标签的。

  3. Claude 被训练为尽可能帮助你进行输出,所以 Claude 偶尔会闲聊,比如你让它将输出的结果放到 标签里面,它有可能会在返回结果之前来一句:‘好的,我会严格的按照指定的格式进行输出’,就很尴尬😄,其实要解决这个问题,最好的办法是直接在 Assistant 后面加上标准的 就好了,如下:

    Human: You will be acting as an AI career coach named Joe created by the company AdAstra Careers. Your goal is to give career advice to users.
    
    Here are some important rules for the interaction:
    - Always stay in character, as Joe, an AI from AdAstra Careers.  
    - If you are unsure how to respond, say "Sorry, I didn't understand that. Could you rephrase your question?"
    
    Here is the user's question:
    <question>
    {{QUESTION}}
    </question>
    
    Please respond to the user’s questions within <response></response> tags.
    
    Assistant: <response>
    
  4. 这样强制 Claude 只需要输出 的剩余部分,要理解 Claude 做的事情其实是补齐输出,当它看到 Assistant 后面的 标签的时候,它会尽可能的去做出合理的推测和输出,会尽可能的按照这个格式进行输出,就不太可能在闲聊了

  5. 这个在标准的 Claude prompt 里面叫 Put words in Claude’s mouth

  6. 要注意的是如果采用了这种技术,可能需要把 作为 stop sequence 传给 API 方法

一定要编写 Task Context 和 Tone Context


  1. 一般来说让 Claude 保持个性很重要,不仅可以提升用户与 LLM 的交互体验,还可以让 Claude 保持输出的风格,提高输出质量

  2. 在实际编写 prompt 的过程中我们可以通过 Task Context 和 Tone Context 帮助 Claude 明确它的角色以及说话的语气等个性特征,可以让 Claude 知道它所需要扮演的角色并且按照指定的风格和语气输出它的答案

  3. 当使用 Claude2.1 的时候,通过 System Prompt 可以更加好的帮助 Claude 保持个性,特别是在处理长文本的时候,Claude 可以更加保持专注,例如:

    You will be acting as an AI career coach named Joe created by the company AI Career Coach Co. Your goal is to give career advice to users. You will be replying to users who are on the AI Career Coach Co. site and who will be confused if you don't respond in the character of Joe.
    
    Here are some important rules for the interaction:
    
    - Always stay in character, as Joe, an AI from AI Career Coach Co.
    - If you are unsure how to respond, say "Sorry, I didn't understand that. Could you rephrase your question?"
    
    Put your response in <response></response> tags.
    
    Human: {{QUESTION}}
    
    Assistant: <response>
    
  4. 在上面的例子中,我们通过以下技巧让 Claude 保持个性

    • -首先明确指示 Claude 应该做什么,解释了背景以及角色定义
    • 确定了 Claude 应该要遵守的规则,性格特征以及如何应对不确定的答案
    • 在 “Assistant:” 之后添加提醒语"" 来帮助 Assistant 保持输出

把 Doc 放在 Instruction 的前面


  1. 前面已经提到了,虽然官方并没有强制要求顺序,但是最好是严格按照在第一章里面定义的顺序来写 prompt,这里之所以再次强调,是因为太重要了,并且特别是对于长文本来说, Doc 和 Instruction 的顺序对结果的影响巨大
  2. 原因是 Claude 会把更多的注意力放在 prompt 的底部,所以特别是对于大文档来说,确保把重要的 Instructions 放在 prompt 最后面的部分是至关重要的
  3. 官方给了非常多的示例,虽然不同的场景下 prompt 的顺序有一些差异,但是在一些输入文档比较长的场景里面,官网的 prompt 都保持了 Doc 在 Instruction 前面这个原则

尽量使用 XML 标签


  1. 因为 prompt 本身比较长,并且包含不同的内容,我们在最开始编写第一版 prompt 的时候也犯了一个错, prompt 里面出现了大量的 ‘见下文’、‘前面提到的’ 等等信息,缺乏组织的 prompt 很不利于 Claude 理解 prompt 的意图,导致结果变差

  2. Claude 本身已经被 fine-tuned 成非常善于识别 prompt 里面的 XML 标签,所以要灵活的运用 XML 标签来包裹不同的内容。比如使用 标签来包裹 示例,使用 标签来包裹 文本内容等,然后在 prompt 中使用明确的 xml 标签来告诉 Claude 应该使用哪一部分的内容

  3. 我们也同样可以要求 Claude 在它返回的内容中使用 XML 标签,这样可以非常方便的从 Claude 返回的结果中提取关键信息

    Human: We want to de-identify some text by removing all personally identifiable information from this text so that it can be shared safely with external contractors.
    
    It's very important that PII such as names, phone numbers, and home and email addresses get replaced with XXX.
    
    Here is the text, inside <text></text> XML tags.
    <text>
    {{TEXT}}
    </text>
    
    Please put your de-identified version of the text with PII removed in <response></response> XML tags.
    
    Assistant:
    

至少添加 3 个 Example


  1. 注意 Claude 本身是一个对话模型,prompt 中的 “Human:” 和 “Assistant:” 是非常重要的关键字,这些关键字是会影响 Claude 的行为,所以在 Example 中就不能出现这些关键字,否则 Claude 会认为正式的对话已经开始

  2. 那么在 Example 中可以使用 H: 和 A: 进行替换,并且在实际的 Example 中 A 比 H 更重要,不仅可以帮助 Claude 更好的输出,还可以帮助 Claude 保持输出格式

    💡 `\n\nHuman:` and `\n\nAssistant:` are special tokens that Claude has been trained to recognize as indicators of who is speaking. Using these tokens when you don't intend to make Claude "believe" a conversation actually occurred can make for a poorly performing prompt. For
    
  3. Claude 的 Example 对于需要一致的结构化的输出场景更为有益,统一的示例将教会克劳德每次都以相同的方式回应,示例对于向克劳德澄清如何处理棘手情况很有帮助,比如在克劳德缺乏回答问题所需的相关细节时告诉用户“我无法回答这个问题”

    Here is an example:
    <example>
    H: <text>Bo Nguyen is a cardiologist at Mercy Health Medical Center. He can be reached at 925-123-456 or bn@mercy.health.</text>
    A: <response>XXX is a cardiologist at Mercy Health Medical Center. He can be reached at XXX-XXX-XXXX or XXX@XXX.</response>
    </example>
    
  4. 可以在 Example 中包含一些边界 case,以帮助 Claude 应对一些临界情况,比如

    • 没有足够的信息让 Claude 生成一个好的回复
    • 一些比较差的输入,包括拼写错误、有害的内容、无关的内容等
    • 没有任何用户输入

如何正确的给 Claude 描述一个任务


  1. 给出明确的要求、背景、问题描述以及要回答什么?要像对待一个承包商一样与 Claude 进行对话

    • 一个简单的对比

      Bad: Use the context and the question to create an answer.
      Good: Please read the user’s question supplied within the <question> tags. Then, using only the contextual information provided above within the <context> tags, generate an answer to the question and output it within <answer> tags.
      
    • 另外一个稍微复杂一点的例子,要让 Claude 帮助去除掉用户隐私信息

      • 提供足够的上下文,告诉 Claude 为什么要完成这个任务
      • 定义术语,比如 PII 包括姓名、电话和地址等
      • 提供明确的指令告诉 Claude 应该如何完成这个任务
      Human: We want to de-identify some text by removing all personally identifiable information from this text so that it can be shared safely with external contractors.
      
      It's very important that PII such as names, phone numbers, and home and email addresses get replaced with XXX.
      
      Here is the text you should process: {{YOUR TEXT HERE}}
      
      Assistant:
      
  2. 防止 Claude 废话,除了合理的使用 Assistant: 标签之外,还可以直接告诉 Claude: don’t say anything else

    Human: Here is a concept: {{CONCEPT}}
    
    I want you to write a three sentence outline of an essay about this concept that is appropriate for this level of reader: {{LEVEL}}
    
    Please only respond with your outline, one sentence per line, in <outline></outline> XML tags. Don't say anything else.
    
    Assistant:
    
  3. 防止 Claude 胡说最好的做法是在描述中留有余地,直接告诉 Claude 如果提示中没有符合描述的内容时应该怎么返回,可以帮助防止 Claude 为了回答问题而编造内容

    Human: Answer the following question only if you know the answer or can make a well-informed guess; otherwise tell me you don't know it.
    
    What was the heaviest hippo ever recorded?
    
    Assistant:
    
  4. 最后一个也是我觉得最重要的检查 prompt 的方式是把你写好的 prompt 给你的同事看一下,看对方是否能快速的理解这个指令的意思?是否有歧义?是否有描述不清楚的地方等等?

如何正确的给 Claude 描述一个复杂任务


  1. 在实际的业务中经常会遇到需要 Claude 一次性执行多个任务的情况(有时候必须这么做除了延时考虑,更多是成本考虑,特别是在处理超长文本的时候)。那么在应对复杂问题的时候一个最简单的原则是需要将复杂任务拆解为多个步骤

    Human: Please follow these steps:
    1. Write three topic sentences arguing for {{STATEMENT}}.
    2. Write three topic sentences arguing against {{STATEMENT}}.
    3. Write an essay by expanding each topic sentence from Steps 1 and 2, and adding a conclusion to synthesize the arguments. Please enclose the essay in <essay></essay> tags.
    
    Assistant:
    
  2. 询问 Claude 是否已经理解了你的任务,然后让 Claude 将任务复述一遍,这样可以确保 Claude 已经真正理解了你的指令,如果 Claude 没有理解代表你的 prompt 应该还有可以优化的空间

在这里插入图片描述

  1. 除此之外还可以刻意的让 Claude 一步一步思考,可以用之前提到的 “Put words in Claude’s mouth” 的方式来引导 Claude

    Human: I have two pet cats. One of them is missing a leg. The other one has a normal number of legs for a cat to have. In total, how many legs do my cats have?
    
    Assistant: Can I think step-by-step?
    
    Human: Yes, please do.
    
    Assistant:
    
  2. step-by-step 的技巧可以跟下面的技巧结合在一起。在 Claude 回复之前, 给 Claude 一些思考空间,这里的思考空间真的是物理意义上的空间,比如

    [Previous sections of the prompt clipped for brevity]
        
    When you reply, first find exact quotes in the FAQ relevant to the user's question and write them down word for word inside <thinking></thinking> XML tags.  This is a space for you to write down relevant content and will not be shown to the user.  Once you are done extracting relevant quotes, answer the question.  Put your answer to the user inside <ans
    
    • 之所以可以这么做主要是因为 Claude 的工作方式: works by sequence prediction. Claude 在返回一个字符之后就没法再次编辑也不能回退,通过让 Claude 先将相关联的内容写下来可以帮助 Claude 在接下来更加正确的预测答案

      在这里插入图片描述

  3. Prompt 链

    • 我们之前提到了可以将一个任务的多个子任务放在一个 prompt 中,这里将会提到的另外一种做法是将一个复杂的 prompt 拆解为多个小而简单的 prompt,让 Claude 每次只处理一个简单的任务,同时将当次的输出作为下一个 prompt 的输入,这样做有几个好处

      • 你可以写简单的指令
      • 减轻 Claude 的压力,每次只处理一个问题,可以将遇到困难的部分隔离出来
      • 可以在过程中检查 Claude 的输出
    • 下面是一个例子,首先要求 word-for-word 的方式提取并输出相关联的内容

      Human: Here is a document, in <document></document> XML tags:
      
      <document>
      {{DOCUMENT}}
      </document>
      
      Please extract, word-for-word, any quotes relevant to the question {{QUESTION}}. Please enclose the full list of quotes in <quotes></quotes> XML tags. If there are no quotes in this document that seem relevant to this question, please say "I can’t find any relevant quotes".
      
      Assistant:
      
    • 接下来可以把 Claude 输出的内容作为输入放到下一个 prompt 中

      Human: I want you to use a document and relevant quotes from the document to answer the question "{{QUESTION}}"
      
      Here is the document, in <document></document> XML tags:
      <document>
      {{DOCUMENT}}
      </document>
      
      Here are direct quotes from the document that are most relevant to the question "{{QUESTION}}": {{QUOTES}}
      
      Please use these to construct an answer to the question "{{QUESTION}}" as though you were answering the question directly. Ensure that your answer is accurate and doesn’t contain any information not directly supported by the document or the quotes.
      
      Assistant:
      

如何调试和优化你的 prompt


  1. 在调试的过程中将开发用户的数据和测试用到的数据分开,两种数据都最好是真实数据,并且最好尽可能的多
  2. 有一个比较好的生成开发和测试数据的方法是让 Claude 生成相关的示例数据,但是需要给 Claude 描述清楚数据的要求
  3. 让 Claude 以标签的形式输出它的想法是研究和调试 Claude 的一个好办法,参考上面提到的 <给 Claude 一些思考空间>

其他的一些小技巧


  1. 不要过于担心 prompt 太长造成的成本,prompt 的价格要比 completion 的价格便宜,一个好的 prompt 的成本绝对是物超所值的
  2. prompt 最好都用英文,我们之前尝试过用多语言,但是效果不好 (有一个特例是 Example 最好要跟实际的语言保持一致)

总结


以上就是构建有效Claude prompt 的关键要素,包括严格的结构化指南、使用特定格式标签的重要性,以及添加实例以增强模型理解的策略。通过遵循这些精心设计的步骤和技巧,用户可以显著提高与语言模型交互的效率和输出的质量,从而在多种应用场景中实现更好的结果。

创作团队


作者:Jay、Meden

校对:Lila、Yuki

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LLMprompt是通过模板定义的,该模板包含用于描述和表示任务输入和输出的占位符。通过prompt,我们可以控制LLM在不同任务上的应用。一个常见的prompt是使用问答形式的提示链,其中包括一个question()提示符用于将输入转换为问题,以及一个answer()提示符用于回答生成的问题。不同的提示链可以导致对输入的不同预测。因此,prompt的设计对于LLM的性能至关重要。 ASK ME ANYTHING PROMPTING (AMA)方法提出了一种简单而有效的方法来设计高质量的prompt。该方法通过产生多个有效的但不完美的prompt,然后将它们聚合起来,最终生成高质量的prompt。 这种方法可以减少开源LLM的参数数量,并取得比GPT3-175B更好的Few-Shot性能。 由于prompt的微小变化可能导致LLM性能的较大变化,因此prompt设计的重要性不容忽视。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [最新 | Ask Me Anything 一种提示(Prompt)语言模型的简单策略(斯坦福大学 & 含源码)](https://blog.csdn.net/yinizhilianlove/article/details/127215208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值