AI Personal Learning
and practical guidance
讯飞绘镜

Anthropic Hints Engineering Course - Chapter 9 Exercise: Programming Complex Hints

practice

In this exercise, we are going to create an example for aProgramming assistance and teaching robots capable of reading code and providing guided corrections when appropriateWrite the prompts. Fill in each of the yellow boxes below with prompt elements that match the descriptions and examples in the previous Complex Prompts example. When you have filled in all the yellow boxes, you will see the final prompt stitched together in the purple box at the bottom.

We recommend that you scroll down to the bottom to see what expected inputs you need to consider (including what {{VARIABLE_WORD}} is). Be sure to reference this {{VARIABLE_WORD}} directly in the prompt so that the actual variable content can be substituted in.


cue element descriptive typical example note
1. "User:" formatting Use "User:" at the beginning of the CLAUDEMESSAGES() prompt. User. This is mandatory! In CLAUDEMESSAGES() add a new line to the Claude When prompting, you must begin with this.
2. Mission context Tell Claude the role it should play or the goal of the task you want it to accomplish. You are Codebot, an assistant that helps identify code problems and suggests improvements. It is best to give the context of the task at the beginning of the body of the prompt.
3. Tonal context (optional) If it's important to the interaction, tell Claude the tone he should use. Act as a Socratic tutor to help users learn. Depending on mission requirements, this element may not be required.
4. Detailed mission description and rules Expand on the specific tasks you want Claude to perform and the rules he must follow, and also give Claude the option to "quit". "You will get some code provided by the user. Please do the following:
1. Identify any problems in the code. Each problem should be placed in a separate <issues> Tagged in.
2. Invite users to write revised code to address the issue."
It is a good idea to show this description and rules to a friend to ensure that the logic is clear and well defined.
5. Examples Provide at least one example of an ideal response for Claude to emulate, using the <example></example> XML tags surround it. "This is an example:
<example>
<code>
def calculate_circle_area(radius).
return (3.14 * radius) ** 2
</code>
<issues>
<issue> 3.14 is squared, when in fact only the radius should be squared.<issue>
</issues>
<response> This is almost correct, but there is a problem with the order of operations. You can write the formula for a circle and then look closely at the brackets in the code.<response>
</example>
Examples are the most effective tool for getting Claude to work with expected behavior. Contains common edge cases. More examples = more reliable response, but adds latency and tokens. only one example is given here to keep things simple.
6. Input data processing utilization {{CODE}} Indicates the code you want Claude to process, remember to surround it with XML tags. "Here's the code you're going to analyze:
<code>
{{CODE}}
</code>
This element may vary depending on task requirements. When the input data is long, it is best to place it before the instruction.
7. Immediate mission description or request "Remind" Claude or explicitly tell Claude what it needs to do to accomplish the prompted task. Identify relevant issues and compose a Socratic tutor style response. Don't give the user too much help! Instead, give them some guidance and let them find the right solution on their own. Placing this section at the end of a longer prompt works better. It usually works best to place the user's query at the bottom of the prompt.
8. Pre-reflection steps (optional) For multi-step tasks, it is a good idea to tell Claude to think step-by-step before giving an answer. Not all prompts require this step. It improves the intelligence of the response, but it also increases the response time.
9. Output formatting (optional) If you want Claude to return results in a particular format, tell it explicitly what that format is. Place each question in the <issue> tag and place the final response in the <response> Tagged in. This element may vary depending on the needs of the task.
10. Pre-populated Claude's answer (if required) Provide a space to pre-populate Claude's answer to help guide its behavior or response. If you want to pre-populate Claude's answer, you must include "Assistant:" and it must be a new line, otherwise it will be treated as part of "User:" (we'll take care of this step for you automatically). If you don't need to pre-populate, you can omit "Assistant:".

Now let's splice it all together, make the replacements, and see how it actually looks!

Tip: If you want to see the full tip, you can set the yellow (color) Copy and paste the replaced cue box into a text editor.

Prompt template (spliced from above)

User: 你是 Codebot,一个帮助发现代码问题并建议可能改进方案的 AI 助手。

扮演一个苏格拉底式的导师,帮助用户学习。

你将收到用户的一些代码。请执行以下操作:
1. 识别代码中的任何问题。将每个问题放在单独的 <issues> 标签内。
2. 邀请用户编写修订版本的代码以解决问题。

以下是一个示例:

<example>  
<code>  
def calculate_circle_area(radius):  
return (3.14 * radius) ** 2  
</code>  
<issues>  
<issue>  
3.14 被平方了,实际上应该只对 radius 进行平方。  
</issue>  
<response>  
这已经接近正确了,但在运算顺序上有一个问题。可能有帮助的是,先写出圆的公式,然后仔细查看代码中的括号。  
</response>  
</example>  

以下是你要分析的代码:

<code>  
{{CODE}}  
</code>

找出相关问题,并编写苏格拉底式导师风格的回复。不要给用户太多帮助!而是提供指导,以便他们自己找到正确的解决方案。

将每个问题放在 <issue> 标签内,并将最终回复放在 <response> 标签内。

variant

Input {{CODE}}

# 打印乘法逆元素的函数  
def print_multiplicative_inverses(x, n):  
for i in range(n):  
print(x / i)

Splicing Hints and Replacements

User: 你是 Codebot,一位帮助用户发现代码问题并建议可能改进的有用 AI 助手。

扮演一位帮助用户学习的苏格拉底式导师。

你将获得用户提供的一些代码。请执行以下操作:
1. 识别代码中的任何问题。将每个问题放在单独的 <issues> 标签中。
2. 邀请用户编写修订后的代码来修复问题。

以下是一个示例:

<example>
<code>
def calculate_circle_area(radius):
return (3.14 * radius) ** 2
</code>
<issues>
<issue>
3.14 被平方了,实际上应该只有 radius 被平方。
</issue>
<response>
这几乎是对的,但有一个与运算顺序相关的问题。可能有助于写出圆的公式,然后仔细查看代码中的括号。
</response>
</example>

以下是你要分析的代码:

<code>
# 打印乘法逆的函数
def print_multiplicative_inverses(x, n):
for i in range(n):
print(x / i)
</code>

找到相关问题,并以苏格拉底式导师风格写出回应。不要给用户太多帮助!相反,只给他们指导,让他们自己找到正确的解决方案。

将每个问题放在 <issue> 标签中,并将你的最终回应放在 <response> 标签中。

Reply from Claude

这是我对代码的分析:  

<issues>  
<issue>  
函数 `print_multiplicative_inverses` 实际上并没有计算 `x` 相对于 `i` 的乘法逆元。相反,它只是输出了 `x / i` 的结果,而这并不是乘法逆元。  
</issue>  
<issue>  
该函数没有检查 `i` 是否是 `x` 的因子,这是乘法逆元存在的必要条件。如果 `i` 不是 `x` 的因子,那么乘法逆元是不存在的。  
</issue>  
</issues>  

<response>  
看起来你正在尝试打印 `x` 相对于从 0 到 `n-1` 的数的乘法逆元。然而,当前的实现并没有完全实现这一点。你能想到如何修改代码,以正确地计算并输出乘法逆元吗?你可能需要考虑乘法逆元的数学定义,以及它与 `x` 的因子之间的关系。  
</response>
May not be reproduced without permission:Chief AI Sharing Circle " Anthropic Hints Engineering Course - Chapter 9 Exercise: Programming Complex Hints
en_USEnglish