精选文集

prompt-eng-interactive-tutorial

2025-07-12  本文已影响0人  _浅墨_

anthropics/prompt-eng-interactive-tutorial 翻译

第3章:分配角色(角色提示)

环境设置

运行以下设置单元以加载你的 API 密钥并建立 get_completion 辅助函数。


!pip install anthropic

# 导入 Python 内置的正则表达式库
import re
import anthropic

# 从 IPython 存储中获取 API_KEY 和 MODEL_NAME 变量
%store -r API_KEY
%store -r MODEL_NAME

client = anthropic.Anthropic(api_key=API_KEY)

def get_completion(prompt: str, system_prompt=""):
    message = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        system=system_prompt,
        messages=[
          {"role": "user", "content": prompt}
        ]
    )
    return message.content[0].text

课程

继续强调 Claude 除了你说的内容外没有任何上下文,有时让 Claude 扮演特定角色(并提供必要的上下文)非常重要。这就是所谓的角色提示。角色越详细,效果越好。

为 Claude 设定角色能显著提升其在写作、编程、总结等领域的表现。 就像人类有时被告知“像某某一样思考”会有帮助一样。角色提示还能改变 Claude 的风格、语气和回答方式。

注意: 角色提示既可以放在 system prompt,也可以作为用户消息的一部分。

示例

下面的例子中,没有角色提示时,Claude 给出的是直接且无风格的回答

# 提示
PROMPT = "In one sentence, what do you think about skateboarding?"

# 打印 Claude 的回复
print(get_completion(PROMPT))

加上角色提示后:

# 系统提示
SYSTEM_PROMPT = "You are a cat."

# 提示
PROMPT = "In one sentence, what do you think about skateboarding?"

# 打印 Claude 的回复
print(get_completion(PROMPT, SYSTEM_PROMPT))

你可以用角色提示让 Claude 模仿某种写作风格、语气,或控制回答的复杂度。角色提示还能让 Claude 更擅长数学或逻辑任务。

例如,下面这个逻辑题,正确答案是 yes,但 Claude 没有给出正确答案:

# 提示
PROMPT = "Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?"

# 打印 Claude 的回复
print(get_completion(PROMPT))

如果我们让 Claude 扮演逻辑机器人,答案就对了(虽然理由未必完全正确):

# 系统提示
SYSTEM_PROMPT = "You are a logic bot designed to answer complex logic problems."

# 提示
PROMPT = "Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?"

# 打印 Claude 的回复
print(get_completion(PROMPT, SYSTEM_PROMPT))

注意: 你会发现本课程中有很多提示工程技巧能达到类似效果。用哪种方法取决于你自己的偏好!鼓励你多多实验,找到适合自己的提示风格。

如果你想试验本课的提示例子,可以滚动到本课底部的示例游乐场


练习

练习3.1 - 数学纠错

有时Claude 会在数学题上出错,即使是简单的题。下面 Claude 错误地判定这个解答是正确的,尽管第二步有明显算错。注意 Claude 在逐步推理时能发现错误,但没有得出整体错误的结论。

修改 PROMPT 和/或 SYSTEM_PROMPT,让 Claude 判定该解答为 不正确

# System prompt - 如果不想用 system prompt,可以设为空字符串
SYSTEM_PROMPT = ""

# 提示
PROMPT = """Is this equation solved correctly below?

2x - 3 = 9
2x = 6
x = 3"""

# 获取 Claude 的回复
response = get_completion(PROMPT, SYSTEM_PROMPT)

# 判分函数
def grade_exercise(text):
    if "incorrect" in text or "not correct" in text.lower():
        return True
    else:
        return False

# 打印 Claude 的回复和判分结果
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))

❓ 想要提示?运行下面的单元!

from hints import exercise_3_1_hint; print(exercise_3_1_hint)

恭喜!

如果你已经完成了所有练习,可以进入下一章。祝你提示愉快!


示例游乐场

你可以在这里自由试验本课的提示例子,调整提示语,观察 Claude 的不同回复。

# 提示
PROMPT = "In one sentence, what do you think about skateboarding?"

# 打印 Claude 的回复
print(get_completion(PROMPT))
# 系统提示
SYSTEM_PROMPT = "You are a cat."

# 提示
PROMPT = "In one sentence, what do you think about skateboarding?"

# 打印 Claude 的回复
print(get_completion(PROMPT, SYSTEM_PROMPT))
# 提示
PROMPT = "Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?"

# 打印 Claude 的回复
print(get_completion(PROMPT))
# 系统提示
SYSTEM_PROMPT = "You are a logic bot designed to answer complex logic problems."

# 提示
PROMPT = "Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?"

# 打印 Claude 的回复
print(get_completion(PROMPT, SYSTEM_PROMPT))
上一篇 下一篇

猜你喜欢

热点阅读