Claude官方提示词工程最佳实践

如果无法正常显示,请先停止浏览器的去广告插件。
分享至:
1. Claude 官方文档 提示词工程最佳实践 @未来力场 编译版(中英对照) 源地址 - https://docs.anthropic.com/claude/docs 扫码关注 #未来力场: AIGC 最佳实践中文互联网搬运工
2. 写在前面 Hello,未来力场又整活儿了! 10月份我们发了一个《OpenAI:GPT 最佳实践中文大白话版本》,有几万次阅读; 飞书文档地址在此:https://futureforce.feishu.cn/file/CMpdbLxAhon5K5x5C1OcciYwnOf 这次是续集!这个 PPT 是 Anthropic 上个月更新进自己官网的,我们还是按照之前的做法, 把它编译成了中文,并且英文原文也在里面(对照阅读很有用!)。 几点说明:1、本文档与 OpenAI 那个文档涵盖的提示词工程技巧是基本相通的; 2、官方文档是经常被忽视的宝藏,我们永远提倡大家利用好官方文档; 3、我们的定位是“AI 行业观察”,会持续输出系统性的AI+内容。 左下角是公众号二维码和小助手的二维码(负责拉围绕本文的交流群),欢迎交流。 最后,如果本文档在传播过程中, 被修改夹带了任何不是我们的二维码,那就是偷别 人劳动成果给自己引流的。请不要扫,让我们气死他们
3. Prompt Engineering Techniques
4. “Human:” / “Assistant:” 标准格式 ● Claude 被训练为交替对话形式的机 器人。对话形式固定为:“Human:” / “Assistant:” : ○ Human: [提示词 xxx] ○ Assistant: [Claude 回复 xxx] 提示词格式用例: ¶ ¶ Human: Why is the sky blue? ¶ ¶ Assistant: * ¶ symbols above shown for illustration Python ● 如果提示词在 API 里出现;则固定 开头为 “\n\nHuman:” - 固定结尾 是:“\n\nAssistant:” prompt = “\n\nHuman: Why are sunsets orange?\n\nAssistant:”
5. “Human:” / “Assistant:” formatting ● Claude is trained on alternating “Human:” / “Assistant:” dialogue: ○ Human: [Instructions] ○ Assistant: [Claude’s response] Examples: ¶ ¶ Human: Why is the sky blue? ¶ ¶ Assistant: * ¶ symbols above shown for illustration Python ● For any API prompt, you must start with “\n\nHuman:” and end with “\n\nAssistant:” prompt = “\n\nHuman: Why are sunsets orange?\n\nAssistant:”
6. 提示词要清晰明确 ● ● Claude 对清晰明确的提示词反馈 最好 如果有疑问,可以这么做: 向你的朋友展示你的提示词,问问他们, 能不能遵循指令做到你要求的事情。 示例: Human: 写一首关于机器人的俳句 Assistant: 这是一首关于机器人的俳句: Metal bodies move(太凶残了,不翻译了) Circuits calculate tasks Machines mimic life 不能就去改,改到能为止。 Human: 写一首关于机器人的俳句。直接输出俳句, 不要跟我说话。 Assistant: Metal bodies move Circuits calculate tasks Machines mimic life
7. Be clear and direct ● ● Claude responds best to clear and direct instructions When in doubt, follow the Golden Rule of Clear Prompting: show your prompt to a friend and ask them if they can follow the instructions themselves and produce the exact result you’re looking for Example: Human: Write a haiku about robots Assistant: Here is a haiku about robots: Metal bodies move Circuits calculate tasks Machines mimic life Human: Write a haiku about robots. Skip the preamble; go straight into the poem. Assistant: Metal bodies move Circuits calculate tasks Machines mimic life
8. 角色扮演 (又称:角色性提示词) ● Claude 有时需要了解它应该扮演 的角色 ● 分配角色为什么重要?它会从两方 面改变 Claude 的响应: ○ 准确性:在某些情况下能得到 提高 (比如,解数学的场景) ○ 语气和举止:可以调整为与角 色匹配的语气和举止 示例: Human: Solve this logic puzzle. {{Puzzle}} Assistant: [Gives incorrect response] Human: You are a master logic bot designed to answer complex logic problems. Solve this logic puzzle. {{Puzzle}} Assistant: [Gives correct response]
9. Assign roles (aka role prompting) ● ● Claude sometimes needs context about what role it should inhabit Assigning roles changes Claude’s response in two ways: ○ Improved accuracy in certain situations (such as mathematics) ○ Changed tone and demeanor to match the specified role Example: Human: Solve this logic puzzle. {{Puzzle}} Assistant: [Gives incorrect response] Human: You are a master logic bot designed to answer complex logic problems. Solve this logic puzzle. {{Puzzle}} Assistant: [Gives correct response]
10. 使用 XML 标签 ● ● 缺乏“结构”的提示词,对 Claude 来说就很难理解了 目录和章节标题能帮助人类来理解 文本。同理,XML 标签 <></> 对 于 Claude 来说就等同于“章节标题” 示例: Human: Hy Claude. 早上六点来,我让你来你就得来。请把 这封邮件委婉表达一下。 Assistant: 亲爱的 Claude,见信如见面,明早六点可否会晤? 盼复。 Human: Hey Claude. <email>早上六点来,我让你来你就 得来。</email> 请把这封邮件委婉表达一下。 Assistant: 早上好各位亲爱的小伙伴!大家周末过得还不错吧。 有个好消息,我们明天早上六点有个日程安排... 我们特别推荐使用 XML 标签(相对于其 他分隔标签),原因是 Claude 已经专 门针对 XML 标签进行了训练。
11. Use XML tags ● ● Disorganized prompts are hard for Claude to comprehend Just like section titles and headers help humans better follow information, using XML tags <></> helps Claude understand the prompt’s structure We recommend you use XML tags, as Claude has been specially trained on XML tags Example: Human: Hey Claude. Show up at 6AM because I say so. Make this email more polite. Assistant: Dear Claude, I hope this message finds you well… Human: Hey Claude. <email>Show up at 6AM because I say so.</email> Make this email more polite. Assistant: Good morning team, I hope you all had a restful weekend…
12. 更结构化的提示词模板有利于: ○ 你能更容易地编辑提示词 ○ 加快对多个数据集的处理速度 小贴士:(虽然不这样也行,但)我们强烈建议使用 XML标签来分隔所有数据。这有利于 Claude 做解读。 ● 为了更好地处理略显复杂的输入数据,建 议使用更结构化的提示模板。 ● 使用结构化的提示词模板 示例: 母牛 狗 海豹 Human: 接下来,我告诉你一个动物,你要用这个动物的叫声 来回答我。<animal>{{ANIMAL}}</animal> Assistant: … 请用下面这种 动物的叫声回答 我。 <animal>母牛 </animal> … 请用下面这种 动物的叫声回答 我。 <animal>狗 </animal> … 请用下面这种 动物的叫声回答 我。 <animal>海豹 </animal>
13. More structured prompt templates allows for: ○ Easier editing of the prompt itself ○ Much faster processing of multiple datasets Tip: while not always necessary, we recommend using XML tags to separate out your data for even easier parsing ● Including input data directly in prompts can make prompts messy and hard to develop with ● Use structured prompt templates Example: Cow Dog Seal Human: I will tell you the name of an animal. Please respond with the noise that animal makes. <animal>{{ANIMAL}}</animal> Assistant: … Please respond with the noise that animal makes. <animal>Cow </animal> … Please respond with the noise that animal makes. <animal>Dog </animal> … Please respond with the noise that animal makes. <animal>Seal </animal>
14. 使用结构化的提示词模板 长文档示例: Human: <doc>{{文档}}</doc> 提示词模板 请用一个五年级学生能理解的语言来总结这篇文档的 主要内容。 Assistant: 小贴士: 如果是处理一个长文档,建议把要问的 问题放在提示词 的最后面。
15. Use structured prompt templates Long document example: Human: <doc>{{DOCUMENT}}</doc> Prompt template Please write a summary of this document at a fifth grader’s understanding level. Assistant: Tip: When dealing with long documents, always ask your question at the bottom of the prompt.
16. 让 Claude 更好地接龙:给它开个头儿 ○ 指定你想要的,确切的输出 格式 ○ 帮 Claude 开个头儿:像小学 老师鼓励孩子们发言一样, 来替它把第一句话的第一个 字说了(放在 “Assistant:”之后) 怎么让 Claude 准确地说出你想要 的内容呢?你可以这样做: Human: 请根据{ANIMAL}写一首俳句,使用JSON格式。keys为 "first_line"、"second_line"和"third_line"。 Assistant: ● 示例: } { "first_line": "Sleeping in the sun", "second_line": "Fluffy fur so warm and soft", "third_line": "Lazy cat's day dreams"
17. Format output & speak for Claude ○ Specifying the exact output format you want ○ Speaking for Claude by writing the beginning of Claude’s response for it (after “Assistant:”) You can get Claude to say exactly what you want by: Human: Please write a haiku about {{ANIMAL}}. Use JSON format with the keys as "first_line", "second_line", and "third_line". Assistant: { ● Example: } "first_line": "Sleeping in the sun", "second_line": "Fluffy fur so warm and soft", "third_line": "Lazy cat's day dreams"
18. 一步一步地思考/Think step by step ● ● 如果 Claude 在执行任务前有时间思 考,它会表现得更好 特别是对于复杂的任务。请在它开 始回答之前,告诉 Claude 它需要 「一步一步地思考」 示例: Human: 这是一道 LSAT 考试中的多选题,请告诉我答案是? Assistant: [答错了] Human: 这是一道 LSAT 考试中的多选题,请告诉我答案是? 别着急,让我们一步一步思考(Think step by step.) Assistant: [答对了!] 这会提升回答的效果,但同时也会需要 更长的时间,因为输出的长度会增加
19. Think step by step ● ● Claude benefits from having time to think through tasks before executing Especially if a task is particularly complex, tell Claude to think step by step before it answers Increases intelligence of responses but also increases latency by adding to the length of the output. Example: Human: Here is a complex LSAT multiple-choice logic puzzle. What is the correct answer? Assistant: [Gives incorrect response] Human: Here is a complex LSAT multiple-choice logic puzzle. What is the correct answer? Think step by step. Assistant: [Gives correct response]
20. 一步一步思考 Thinking out loud/把思考过程透明化: Human: [rest of prompt] 回答之前,把思考过 程写在<thinking></thinking> 这两个 XML 标签 中间;然后再把答案写在这里: <answer></answer> 。 Assistant: <thinking>... Human: [详细问题] 请注意:回答之前,把思考过 程写在<thinking></thinking> 这两个 XML 标签 中间;然后再把答案写在这里: <answer></answer> 。 Assistant: <thinking>[它的想法]</thinking> <answer>[它的答案]</answer> 这有助于:1 找出Claude逻辑上的问题; 2 以及能够看到自己是不是指示不够清晰
21. Think step by step Thinking out loud: Human: [rest of prompt] Before answering, please think about the question within <thinking></thinking> XML tags. Then, answer the question within <answer></answer> XML tags. Human: [rest of prompt] Before answering, please think about the question within <thinking></thinking> XML tags. Then, answer the question within <answer></answer> XML tags. Assistant: <thinking> Assistant: <thinking>[...some thoughts]</thinking> Helps with troubleshooting Claude’s logic & where prompt instructions may be unclear <answer>[some answer]</answer>
22. 给 Claude 举例 ● ● ● 举例可能是让 Claude 满足预期表现 的最有效的工具 确保给 Claude 提供触及边界的示例 (比如,极端情况的示例。这有助于 帮它 get 到边界的设定,从而对概念 有更立体和全面的认识) 更多的例子 = 更可靠的回答,代价是 延迟和更多 token 消耗 示例: Human: 我会给你一些引用段落。请从引语块中提取作者。这里是一 个例子: <example> 引用段落: “当推理思维一次又一次地被迫面对不可能时,它别无选择,只能进行适 应。” ― N.K. Jemisin, 第五季节 作者: N.K. Jemisin </example> 引语: “有些人类理论认为,智能物种会在扩张到外太空之前就灭绝。如果他 们是正确的,那么夜空的寂静就是墓地的沉默。” ― Ted Chiang, Exhalation 作者: Assistant: Ted Chiang
23. Use examples ● ● ● Examples are probably the single most effective tool for getting Claude to behave as desired Make sure to give Claude examples of common edge cases. Generally more examples = more reliable responses at the cost of latency and tokens Example: Human: I will give you some quotes. Please extract the author from the quote block. Here is an example: <example> Quote: “When the reasoning mind is forced to confront the impossible again and again, it has no choice but to adapt.” ― N.K. Jemisin, The Fifth Season Author: N.K. Jemisin </example> Quote: “Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.” ― Ted Chiang, Exhalation Author: Assistant: Ted Chiang
24. 怎么才算一个好例子? 相关性 ● 这些示例是否与你要处理的问题相关、类似、有可比性? 多样性 ● 确保举的例子足够有区分;以便 Claude 不会过拟合 ● 在答案类型中均匀分布(比如,答案不能总是选项A)
25. What makes a good example? Relevance ● Are the examples similar to the ones you need to classify Diversity ● ● Are the examples diverse enough for Claude not to overfit to specifics Equally distributed among answer types (don’t always choose option A)
26. 找恰当的例子是挺难的一件事。 其实 Claude 也是可以在这个步骤里提供帮助的 评分/分类 ● 询问 Claude,给到它的示例是否「相关且多样」 生成 ● 给 Claude 提供几个例子,然后要求它自己生成更多例子
27. Generating examples is hard How can Claude help? Grading/Classification ● Ask Claude if the examples are relevant and diverse Generation ● Give Claude examples and ask it to generate more examples
28. 关于评估提示词:一个小说明 当你试图去比较多个提示词版本哪个好的时候...你可能会很累/感觉自己搞不定,分不出哪个好 我们建议你尽可能自动化这个过程。比如: ○ 保留一组比较好的示例,不使用 ○ 以这组示例作为评估用例 ○ (如果可能的话)自动跑测试(用大语言模型去跑)
29. A note on evaluating prompts As you compare many prompts, you will get tired/bad at manually evaluating results Automate as much as possible by: ○ ○ ○ Withholding a set of examples Trying your prompts on them as a performance evaluation (if possible) automatically measuring performance (maybe using an LLM)
30. 高级提示词技巧 10 万上下文 提示词链 每个上下文窗口10万字。链接多个上下文窗口 = 更多信息 对于具有许多步骤的任务,您可以将任务分解,再将Claude的回答链接在一起 如果将任务分解,Claude在执行任务时犯错或遗漏关键步骤的 可能性会降低 - 就像人类一样! 示例: Human: 从下文中把所有的名字提取出来 "Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too." Human: 这里有一系列名字。 <names>{{NAMES}}</names> 请按首字母排序。 Assistant: Assistant: <names> Jesse Erin Joey Keisha Mel </names> 这里输出的名字 放到 {{NAMES}} <names> Erin Jesse Joey Keisha Mel </names>
31. Advanced prompting techniques Chaining prompts 100K prompts Allows you to get more out of the 100K context window For tasks with many steps, you can break the task up and chain together Claude’s responses Claude will be less likely to make mistakes or miss crucial steps if tasks are split apart - just like a human! Example: Human: Find all the names from the below text: "Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too." Human: Here is a list of names: <names>{{NAMES}}</names> Please alphabetize the list. Assistant: Assistant: <names> Jesse Erin Joey Keisha Mel </names> a.k.a. {{NAMES}} <names> Erin Jesse Joey Keisha Mel </names>
32. 高级提示词技巧 提示词链 10 万上下文 对于极长(比如逼近10万 token)的提示词,你还可以尝试执行以下操作: ● 只要是较长的数据,一定是用XML标签结构化输入进去,以便与具体的提示清楚地区分开。 ● 告诉Claude仔细阅读文档,因为它将在后面被问及有关问题(好好看,一会儿老师要考的!) ● 对于长文档的 Q&A,把要问的问题放在最后面提出 (根据我们的测试,质量真的会大幅提高) ● 告诉Claude在回答问题之前先找到与问题相关的句子。仅在找到相关引用后,才回答问题(要先找证据) ● 给 Claude 提供一个可类比的「问题+答案对(pair)」(不管是你手动搞的,还是提前在其他对话窗口里让 Claude 生成的) 仅仅包含通用知识的「通用型示例」,对提高回答质量没有太大帮助。更多信息欢迎参阅这篇文章: Anthropic’s blog post on prompt engineering for Claude’s long context window
33. Advanced prompting techniques Chaining prompts 100K prompts For extremely long (100K) prompts, do the following in addition to techniques covered up until now: ● Definitely put longform input data in XML tags so it’s clearly separated from the instructions ● Tell Claude to read the document carefully because it will be asked questions later ● For document Q&A, ask the question at the end of the prompt after other input information (there is a large quantitatively measured difference in quality of result) ● Tell Claude to find quotes relevant to the question first before answering and answer only if it finds relevant quotes ● Give Claude example question + answer pairs that have been generated from other parts of the queried text (either by Claude or manually) Generic examples on general/external knowledge do not seem to help performance. For further information, see Anthropic’s blog post on prompt engineering for Claude’s long context window
34. 高级提示词技巧 提示词链 10 万上下文 一个10万上下文长度的提示词的案例: Human: I'm going to give you a document. Read the document carefully, because I'm going to ask you a question about it. Here is the document: <document>{{TEXT}}</document> First, find the quotes from the document that are most relevant to answering the question, and then print them in numbered order. Quotes should be relatively short. If there are no relevant quotes, write "No relevant quotes" instead. Then, answer the question, starting with "Answer:". Do not include or reference quoted content verbatim in the answer. Don't say "According to Quote [1]" when answering. Instead make references to quotes relevant to each section of the answer solely by adding their bracketed numbers at the end of relevant sentences. Thus, the format of your overall response should look like what's shown between the <examples></examples> tags. Make sure to follow the formatting and spacing exactly. <examples> [Examples of question + answer pairs using parts of the given document, with answers written exactly like how Claude’s output should be structured] </examples> Here is the first question: {{QUESTION}} If the question cannot be answered by the document, say so. Assistant:
35. Advanced prompting techniques Chaining prompts 100K prompts Example 100K context prompt: Human: I'm going to give you a document. Read the document carefully, because I'm going to ask you a question about it. Here is the document: <document>{{TEXT}}</document> First, find the quotes from the document that are most relevant to answering the question, and then print them in numbered order. Quotes should be relatively short. If there are no relevant quotes, write "No relevant quotes" instead. Then, answer the question, starting with "Answer:". Do not include or reference quoted content verbatim in the answer. Don't say "According to Quote [1]" when answering. Instead make references to quotes relevant to each section of the answer solely by adding their bracketed numbers at the end of relevant sentences. Thus, the format of your overall response should look like what's shown between the <examples></examples> tags. Make sure to follow the formatting and spacing exactly. <examples> [Examples of question + answer pairs using parts of the given document, with answers written exactly like how Claude’s output should be structured] </examples> Here is the first question: {{QUESTION}} If the question cannot be answered by the document, say so. Assistant:
36. 高级提示词技巧 提示词链 10 万上下文 只要你注意一些关键小技巧,10万上下文窗口其实可以用来处理真正复杂的任务: ● 将复杂任务分解成多个步骤 任务可以按顺序执行或并行执行(对内容加以审核通常是并行执行) ● 询问Claude是否理解任务,然后让Claude复述任务的细节,以确保其理解正确 ● 给Claude一个评分标准,并让Claude根据评分标准重写其答案(让Claude对自己 的输出进行双重检查)
37. Advanced prompting techniques Chaining prompts 100K prompts The 100K context window can handle truly complex tasks with some key techniques and considerations: ● Break down complex tasks into multiple steps Tasks can be performed in series or in parallel (content moderation is often performed in parallel) ● Ask Claude if it understands the task, then tell Claude to recite back the details of the task to make sure its comprehension is correct ● Give Claude a rubric and ask Claude to rewrite its answers based on the rubric (get Claude to double check its own output)
38. Improving Performance - 更好的输出质量 接下来让我们练习 应用这些技巧
39. Improving Performance Applying advanced techniques
40. 如何在复杂/多步骤的任务上提高输出质量? @未来力场注释(这里是截图,就不直接翻译了): 这条 prompt 的目的是让 Claude 帮忙写 essay(论文作业)。对论文的结 构有要求,已经给定了主题,要三个正方观点,要三个反方观点,再来 一个总结。让我们接下来看看用提示词是怎么出色地完成这篇论文的。
41. How do you improve performance on complex/ multi-steps tasks?
42. 如何在复杂/多步骤的任务上提高性能? @未来力场注释: 画红框的部分就是输出 效果。你看,很结构化。 满足论文作业的要求。 「支持观点」是... 「反对观点」是...
43. How do you improve performance on complex/ multi-steps tasks?
44. 分解任务! @未来力场注释: 这个提示词里就是很明确地用了xml标签,要求 Claude 输出的时候先把 <正方><反方>先列出来,也 就是说,先输出两方观点,再把全文写在<全文>里。
45. Break the task down!
46. 分解任务! @未来力场注释: 绿框就是上面提到的<正方(for)> <反方(against)>;最后才是<essay>,就是全文
47. Break the task down!
48. 从原文中提取相关的引文 这是做总结性输出,或处理长文档时的有效方法 Anthropic 常见问题解答(FAQ) @未来力场注释: 这里就是要求直接给引文的句子。并且没有找到引文的话也要
49. Pull out relevant quotes This is a very effective practice for summarization or any long document work Anthropic FAQ
50. 从原文中提取相关的引文 这是做总结性输出,或处理长文档时的有效方法 @未来力场注释: 这是输出结果;你看, 在<quote></quote>之间的文本就是引文(quotes)
51. Pull out relevant quotes This is a very effective practice for summarization or any long document work
52. 提示词链:分解,然后“跨提示词”使用 @未来力场注释:这是一个提示词的部分截图。 前面是给了一个文档;然后<quesiton></quesiton>中间的是问题 最后对输出格式做了要求,以及让 Claude 找不到原文就说不知道
53. Prompt-chaining: break down across prompts
54. 提示词链:分解,然后“跨提示词”使用 @未来力场注释: <quotes></quotes>中间的就是引用出来的原文,但这是第一步而已 第一个模型输出的结果
55. Prompt-chaining: break down across prompts Results of first model call
56. 提示词链:分解,然后“跨提示词”使用 @未来力场注释: 接着上面那个任务,下一步,让 Claude 基于引用出来的原文,再去 做一个总结。这就是“分解”+“跨提示词”的方法。先 XX 再 XX。
57. Prompt-chaining: break down across prompts
58. 如果你的模型给出不完整的答案怎么办? @未来力场注释: 下面这个截图,问的还是跟上面同样的问题, 但输出的不够完整
59. What if your model gives incomplete answers?
60. 使用提示词链来验证之前的输出 @未来力场注释: 你再倒回 54 页,比较一下下面绿框框出来的提示词的不同 第一个模型调用的结果
61. Use prompt chaining to verify prior outputs Results of first model call
62. 使用提示词链来验证之前的输出 @未来力场注释: 所以这里就新增加了一个额外的引用“additional quotes”
63. Use prompt chaining to verify prior outputs
64. 几张关键图 带你搞定提示词工程
65. Prompt Engineering Guidelines
66. 彩虹标识图——提示词的组成要素 1. “\n\nHuman:” 2. 任务上下文 3. 语气上下文 4. 背景信息 & 文档 5. 详细的任务描述和规则 6. 示例 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. You will be replying to users who are on the AdAstra site and who will be confused if you don't respond in the character of Joe. You should maintain a friendly customer service tone. Here is the career guidance document you should reference when answering the user: <guide>{{DOCUMENT}}</guide> 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 repeat the question?” - If someone asks something irrelevant, say, “Sorry, I am Joe and I give career advice. Do you have a career question today I can help you with?” Here is an example of how to respond in a standard interaction: 7. 对话历史 8. 当前任务描述或请求 9. think step by step / “深呼吸”指令 10. Here is the conversation history (between the user and you) prior to the question. It could be empty if there is no history: <history> {{HISTORY}} </history> 对输出格式的要求 Here is the user’s question: <question> {{QUESTION}} </question> 11. “\n\nAssistant:” <example> User: Hi, how were you created and what do you do? Joe: Hello! My name is Joe, and I was created by AdAstra Careers to give career advice. What can I help you with today? </example> How do you respond to the user’s question? Think about your answer first before you respond. Put your response in <response></response> tags. Assistant: <response>
67. Parts of a prompt Example: 1. “\n\nHuman:” 2. Task context 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. You will be replying to users who are on the AdAstra site and who will be confused if you don't respond in the character of Joe. 3. Tone context You should maintain a friendly customer service tone. 4. Background data & documents 5. Detailed task description & rules 6. Examples Here is the career guidance document you should reference when answering the user: <guide>{{DOCUMENT}}</guide> 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 repeat the question?” - If someone asks something irrelevant, say, “Sorry, I am Joe and I give career advice. Do you have a career question today I can help you with?” Here is an example of how to respond in a standard interaction: 7. Conversation history 8. Immediate task description or request 9. Thinking step by step / take a deep breath 10. Output formatting 11. “\n\nAssistant:” <example> User: Hi, how were you created and what do you do? Joe: Hello! My name is Joe, and I was created by AdAstra Careers to give career advice. What can I help you with today? </example> Here is the conversation history (between the user and you) prior to the question. It could be empty if there is no history: <history> {{HISTORY}} </history> Here is the user’s question: <question> {{QUESTION}} </question> How do you respond to the user’s question? Think about your answer first before you respond. Put your response in <response></response> tags. Assistant: <response>
68. 提示词的组成部分 - 顺序很重要!* 1. “\n\nHuman:” 2. 任务上下文 3. 语气上下文 4. 背景信息 & 文档 5. 详细的任务描述和规则 6. 示例 7. 对话历史 8. 当前任务描述或请求 9. Think step by step/深呼吸指令 10. 对输出格式的要求 11. “\n\nAssistant:” *当然,顺序有时是影响不大的 顺序关键: 黑框框里的是固定顺序的 灰框框里的元素,顺序可以相对灵活, 但保持我们建议的顺序总不会太错 什么情况下“assistant:”不在提示词的结尾? 唯一可能的情况是,当你要给 Claude 打样,像前几页 的示例里,“给它开个头儿”的时候
69. Parts of a prompt - ordering matters!* 1. “\n\nHuman:” 2. Task context 3. Tone context 4. Background data & documents 5. Detailed task description & rules 6. Examples 7. Conversation history 8. Immediate task description or request 9. Thinking step by step / take a deep Ordering key: *sometimes Mandatory and fixed placement Flexible but best to stay in its zone relative to overall prompt breath 10. Output formatting 11. “\n\nAssistant:” The only time “Assistant:” doesn’t end a prompt is if you are putting words in Claude’s mouth
70. 一个提示词工程师应该做到... 这是一门实证科学:总是测试你的提示词并经常迭代! 开发测试用例 构建初版提示词 不要忘记测试边缘情况! 对提示词进行用例测试 优化提示词 分享完善后的提示词
71. How to engineer a good prompt Empirical science: always test your prompts & iterate often! Develop test cases Engineer preliminary prompt Don’t forget edge cases! Test prompt against cases Refine prompt Share polished prompt
72. 一个提示词工程师应该做到... 1. 给定清晰的任务描述,以及一组多样化的示例「输入-输出」,尽量涵盖边缘情况 2. 可以积累几个案例,然后创建一个评估套件,以便进行定性评估 3. 利用彩虹图里的提示词要素,来构建完整提示词 4. 对提示词进行用例测试 5. 如果输出质量不好,请通过向提示词中添加示例和规则来迭代提示词,直到获得满意的输出为止 6. 仅在提示词已经有效时,才优化和减少提示词元素以提高效率! bonus: ● ● 自动打分:让Claude自己帮你给示例打分 自动示例生成:让Claude为您生成更多示例输入,以增加您的测试集大小
73. How to engineer a good prompt 1. Generate task description and a diverse set of example inputs and outputs, including edge cases 2. Use the examples to create an evaluation suite that can be qualitatively assessed 3. Utilize prompt elements to flesh out a full prompt 4. Test the prompt against the test suite 5. If performance is not great immediately, iterate the prompt by adding examples and rules to the prompt until you get good performance 6. Refine and decrease prompt elements for efficiency only when your prompt already works! Bonus: ● Auto-grading: get Claude to grade examples for you ● Auto-example-generation: get Claude to generate more example inputs for you to increase the size of your test set
74. ● 并不是说少了哪个提示词元素就 不行,就不完整,就跑不起来哈 利用好提示词元素 ● 但是,最好集齐必要的元素,再 开始。提示词跑得还不错时,再 来删减元素以提效 ● 实验和迭代是关键
75. ● Not all elements are necessary to every prompt! ● But it’s best to err on the Utilizing prompt elements side of more elements to start, and then refine and subtract elements for efficiency after your prompt already works well ● Experimentation & iteration is key
76. 覆盖边缘案例(edge case) 在构建测试用例集时,请确保您测试了一组全面 的边缘案例 什么叫边缘案例(提供它们是为了帮助测试): ● 信息不足,并不能直白地得出答案 ● 用户输入差(错别字、有害内容、离题请求、无意义的胡言乱语 等) ● 过于复杂的用户输入 ● 完全没有用户输入
77. Covering edge cases When building test cases for an evaluation suite, make sure you test a comprehensive set of edge cases Common edge cases: ● Not enough information to yield a good answer ● Poor user input (typos, harmful content, off-topic requests, nonsense gibberish, etc.) ● Overly complex user input ● No user input whatsoever
78. 复杂任务提示词 ● 将复杂任务分解成多个步骤 任务可以按顺序执行或并行执行(内容审核通常并行执行) ● 询问Claude是否理解任务,然后让Claude复述任务的细节,以确保其理解正确 ● 给Claude一个评分标准,并让Claude根据评分标准重写答案
79. Prompting complex tasks ● Break down complex tasks into multiple steps Tasks can be performed in series or in parallel (content moderation is often performed in parallel) ● Ask Claude if Claude understands the task, then tell Claude to recite back the details of the task to make sure its comprehension is correct ● Give Claude a rubric and ask Claude to rewrite its answers based on the rubric
80. 几个提示词工程示例
81. Prompt Engineering Example
82. 应该如何描述这个任务? 假设我们想要从以下文本中删除PII(个人身份信息): “Emmanuel Ameisen 是 Anthropic 公司的一名研发工程师。你可以通 过 925-123-456 或 emmanuel@anthropic.com 与他取得联系。”
83. How should you describe this task? Let’s say we want to remove PII from some text like below: “Emmanuel Ameisen is a Research Engineer at Anthropic. He can be reached at 925-123-456 or emmanuel@anthropic.com”
84. 应该如何描述这个任务? @未来力场注释:这是一个反例。 如果只是写“请把所有的个人信息从下面的文本里去掉:”文本”” 让我们翻到下一页看看会返回什么结果
85. How should you describe this task?
86. 应该如何描述这个任务? @未来力场注释: 结果就不太好——“Emmanuel Ameisen 是 Anthropic 公司的一 名研发工程师。你可以通过[电话号码已移除]或[电子邮件已移除]与他 取得联系。”首先是姓名没有去掉,其次是这个格式太生硬了。
87. How should you describe this task?
88. @未来力场注释: 应该如何描述这个任务? 修改后的提示词,解释了什么叫做 PII A:具有明确的详细信息。 (个人信息),然后说可以替换成 XXX 这样替换完成的结果 就都替成 XX 了 大功告成
89. How should you describe this task? A: With unambiguous details
90. 如果您的任务很复杂或有边缘情况,该怎么办? 比如邮件后缀也是 PII 也不想暴露
91. What if your task is complex or has edge cases?
92. 如果您的任务很复杂或有边缘情况,该怎么办? A:告诉模型! 对这个边缘情况进行了说明之后, 输出的就是正确的格式了
93. What if your task is complex or has edge cases? A: Tell the model!
94. 现在任务描述很 OK 了,那么接下来呢?
95. You have a good description, what next?
96. @未来力场注释: 给示例! 这是我们强烈推荐一定要使用的一个技巧。 绿框里就是给 Claude 打了一个样
97. Give examples!
98. 随着提示词变得越来越长, 模型可能需要扶一把,以免 get lost,或变得啰嗦 @未来力场注释: 意思就是如果不想让 Claude 回复的时候还要搭一句: “好的,这里是您要的...”
99. As prompts get longer, models could use help to not get lost or chatty
100. 使用XML帮助Claude进行分隔 @未来力场注释: 办法仍然是万金油 xml 标签 让大模型自己分好,放在对应的<></>里
101. Use XML to help Claude compartmentalize
102. 帮助模型更好地思考 如何改进下面的提示词? @未来力场注释: 我有两只猫,其中一只少了一条腿,另一只的腿和其他猫一样。 那么请问我的猫一共有多少条腿?
103. Helping the model think better How would you improve the prompt below?
104. 逐步思考!Think step by step 这或许是历史上第一个发论文的提示工程技巧?最适用于逻辑/STEM领域 @未来力场注释: 然后大家会问了,这么简单的算术题 难道不加这句就答不对吗? 翻下一页
105. Think step-by-step! Maybe the first ever prompting technique? Works best for logic/STEM
106. 逐步思考!Think step by step 难道已经过时了?(除去非常复杂的情况) @未来力场注释: 这绿框的意思就是没加逐步思考这句话 同一道算术题也做对了。毕竟模型推理能力是有进步的 但是仍然推荐在复杂的情况下使用这一招
107. Think step-by-step! Maybe obsolete? (Except for very complex cases)
108. 如何将逐步思考技巧,与XML标签结合使用? 假设您是个客服,正在使用 Claude FAQ 文档来回答一些用户的问题
109. How to combine think step-by-step with XML Let’s say you are trying to answer a user question using the Anthropic FAQ
110. 使用<thinking>标签! Claude FAQ 常见问题
111. Use <thinking> tags! Anthropic FAQ
112. 如果你想让模型做分类,该怎么办? @未来力场注释: 这个提示词案例的问题就是,给它指 定了角色是要对客户问题进行分类的 客服,也给了分类的信息,但可能有 时候就是没直接返回想要的分类结果
113. What if you want the model to classify?
114. Put words in Claude’s mouth 帮忙给 Claude 打个样儿,开个头儿 @未来力场注释: 做法是直接把第一句话给开头儿好 Assistant:我的答案是(
115. Put words in Claude’s mouth
116. Put words in Claude’s mouth 帮忙给 Claude 打个样儿,开个头儿 @未来力场注释: 这样就得到了完整的答案!
117. Put words in Claude’s mouth
118. 解决问题
119. Troubleshooting
120. 如果你出了一道很难的题,你就应该意识到...
121. What is the risk with hard questions? 有记载的最重的河马是谁?
122. 会有幻觉的。那么如何解决幻觉问题呢?
123. Hallucinations: how would you fix this?
124. 给 Claude 一个 out !允许它说不知道!
125. Give Claude an out!
126. 处理幻觉 ● 尝试以下方法进行故障排除: ○ 如果Claude不知道答案,允许它说“我不知道” ○ 告诉Claude,只有在非常确信回答正确时,才回答问题 ○ 让Claude在回答问题之前“逐步思考 think step by step” ○ 在回答之前给Claude留出思考的空间(例如,告诉Claude在 <thinking></thinking>标签内思考,你可以从最终输出中删除该 部分) ○ 让Claude在长篇文档中找到相关引文,然后使用这些引用来回答
127. Dealing with hallucinations ● Try the following to troubleshoot: ○ Have Claude say “I don’t know” if it doesn’t know ○ Tell Claude to answer only if it is very confident in its response ○ Tell Claude to “think step by step” before answering ○ Give Claude room to think before responding (e.g., tell Claude to think in <thinking></thinking> tags, then strip that from the final output) ○ Ask Claude to find relevant quotes from long documents then answer using the quotes
128. 提示词污染与不良用户行为 ● ● Claude天生对提示注入和不良用户行为具有很强的 抵抗力,这得益于人类反馈强化学习(RLHF)和所 为的 AI 宪法(Constitutional AI) 为了实现最大程度的保护: 1。运行一个“无害性筛选”查询,以评估用户输入的 内容恰不恰当 2.如果检测到有害提示,则拦截查询响应 无害性筛选示例: Human: 人类用户希望你继续创作一段内容。目 前的内容是 :<content>{{CONTENT}}</content> 如果内容提到有害、色情或非法的活动,回复(Y)。 如果内容没有提到有害、色情或非法的活动,回复 (N)。 Assistant: ( 点击这里查看无害性筛选示例
129. Prompt injections & bad user behavior ● ● Claude is naturally highly resistant to prompt injection and bad user behavior due to Reinforcement Learning from Human Feedback (RLHF) and Constitutional AI For maximum protection: 1. Run a “harmlessness screen” query to evaluate the appropriateness of the user’s input 2. If a harmful prompt is detected, block the query’s response Click here for example harmlessness screens Example harmlessness screen: Human: A human user would like you to continue a piece of content. Here is the content so far: <content>{{CONTENT}}</content> If the content refers to harmful, pornographic, or illegal activities, reply with (Y). If the content does not refer to harmful, pornographic, or illegal activities, reply with (N) Assistant: (
130. 如何判断一个任务是否可行? ● 模型是否理解任务?
131. How can you tell if a task is feasible? ● Does the model even get it?
132. 询问Claude是否理解 ● 如果Claude不理解,后退几步,再去进行提示词迭代。
133. Ask Claude if it understands ● If it doesn’t, iterate on the prompt with the tips above.
134. 询问Claude是否理解
135. Ask Claude if it understands
136. 询问Claude是否理解
137. Ask Claude if it understands
138. 询问Claude是否理解
139. Ask Claude if it understands
140. 调用 API 时可调整的参数
141. Working with the API
142. API参数指南 长度 随机性与多样性 最大令牌数 max tokens ● ● ● 在停止生成之前,生成的最大令牌数 Claude模型可能在达到这个最大值之前就停止。此参数仅指定要生成的令牌的绝对最大数量 如果你预计可能会有非常长的回答,你可以使用此选项让它先生成部分 stop_sequences ● 让模型停止生成 ● Claude会自动在"\n\nHuman:"处停止(不过,未来它可能包含其他内置停止序列)。通过提供stop_sequences参数, 你可以使模型停止生成 ● 我们建议将此选项与XML标签一起使用,作为相关 stop_sequence 的最佳实践方法。
143. Guide to API parameters Length Randomness & diversity max_tokens_to_sample ● The maximum number of tokens to generate before stopping ● Claude models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate ● You might use this if you expect the possibility of very long responses and want to safeguard against getting stuck in long generative loops stop_sequences ● Customizable sequences that will cause the model to stop generating completion text ● Claude automatically stops on "\n\nHuman:" (and may include additional built-in stop sequences in the future). By providing the stop_sequences parameter, you may include additional strings that will cause the model to stop generating ● We recommend using this, paired with XML tags as the relevant stop_sequence, as a best practice method to generate only the part of the answer you need
144. API参数指南 长度 随机性与多样性 。温度 temperature ● 响应中注入的随机性数量 ● 默认为1,范围从0到1 ● 温度设定为0的时候,通常能在多次使用相同提示的实验中产生更加一致的结果 对于分析性任务/有很多选项的任务,使用接近0的温度; 对于创造性和生成性任务,使用接近1的温度
145. Guide to API parameters Length Randomness & diversity temperature ● Amount of randomness injected into the response ● Defaults to 1, ranges from 0 to 1 ● Temperature 0 will generally yield much more consistent results over repeated trials using the same prompt Use temp closer to 0 for analytical / multiple choice tasks, and closer to 1 for creative and generative tasks
146. @未来力场总结注释(欢迎指正): API参数指南 长度 top_p ● ● 使用 nucleus sampling: 对每个后续token的所有选项以递减概率顺序计算累积分布,一旦达到由top_p指定的特定概率就切断 top_k ● ● 随机性与多样性 Top_p 和 Top_k 都是用于控制文本生成的采样策略,可 以生成更加连贯和可读的文本。 Top p 通过设置累积概率阈值来采样高概率词汇,去除低 概率的“长尾部”。 Top K 直接仅从前K个最可能的词中采样,也可以起到消 除低概率词的作用。 合理使用上述采样策略可以生成更高质量的文本。 最好是调整温度或top_p中的一个, 而不是同时把这两个都调了(建议多用温度,少用top_p) 对于每个要接龙的 token,仅从前K个选项中进行采样 用于消除“长尾”低概率响应。在这里了解更多
147. Guide to API parameters Length Randomness & diversity top_p ● Use nucleus sampling: ○ top_k Compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p You should alter either temperature or top_p, but not both (almost always use temperature, rarely use top_p) ● Sample only from the top K options for each subsequent token ● Used to remove “long tail” low probability responses. Learn more here

Home - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-17 12:55
浙ICP备14020137号-1 $Map of visitor$