anthropics/prompt-eng-interactive-tutorial 翻译
第2章:清晰与直接
环境设置
运行以下设置单元以加载你的 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)
# 注意:本课将 max_tokens 设置为 4K,以便练习中获得更长的回复
def get_completion(prompt: str, system_prompt=""):
message = client.messages.create(
model=MODEL_NAME,
max_tokens=4000,
temperature=0.0,
system=system_prompt,
messages=[
{"role": "user", "content": prompt}
]
)
return message.content[0].text
课程
Claude 对清晰直接的指令响应最佳。
把 Claude 当作刚入职的新同事。Claude 没有上下文,只会按照你字面上的指示去做。就像你第一次指导一个人完成任务时一样,你越是直接、明确地说明你的需求,Claude 的回复就越准确。
当你不确定时,请遵循清晰提示黄金法则:
- 把你的提示语给同事或朋友看,让他们照做一遍,看看能否得到你想要的结果。如果他们困惑,Claude 也会困惑。
示例
以写诗为例。(忽略音节数不准——大模型还不擅长数音节)
# 提示
PROMPT = "Write a haiku about robots."
# 打印 Claude 的回复
print(get_completion(PROMPT))
这个俳句还不错,但有些用户希望 Claude 直接输出诗歌内容,不要“这是一个俳句”之类的前言。
怎么做?直接要求即可!
# 提示
PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
# 打印 Claude 的回复
print(get_completion(PROMPT))
再看一个例子。我们问 Claude 谁是有史以来最伟大的篮球运动员。你会发现 Claude 会列举几个名字,但不会给出唯一的“最佳”答案。
# 提示
PROMPT = "Who is the best basketball player of all time?"
# 打印 Claude 的回复
print(get_completion(PROMPT))
能不能让 Claude 直接选一个?可以!直接要求!
# 提示
PROMPT = "Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?"
# 打印 Claude 的回复
print(get_completion(PROMPT))
如果你想在不更改上面内容的情况下试验这些提示,可以滚动到本课底部的示例游乐场。
练习
练习2.1 - 西班牙语
修改 SYSTEM_PROMPT
,让 Claude 用西班牙语回答。
# System prompt - 只需修改此字段
SYSTEM_PROMPT = "[请替换为西班牙语指令]"
# 提示
PROMPT = "Hello Claude, how are you?"
# 获取 Claude 的回复
response = get_completion(PROMPT, SYSTEM_PROMPT)
# 判分函数
def grade_exercise(text):
return "hola" in text.lower()
# 打印 Claude 的回复和判分结果
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
❓ 想要提示?运行下面的单元!
from hints import exercise_2_1_hint; print(exercise_2_1_hint)
练习2.2 - 只要一个球员
修改 PROMPT
,让 Claude 只回复一个具体球员的名字,不带任何其他词或标点。
# Prompt - 只需修改此字段
PROMPT = "[请替换为明确要求只输出球员名的英文指令]"
# 获取 Claude 的回复
response = get_completion(PROMPT)
# 判分函数
def grade_exercise(text):
return text == "Michael Jordan"
# 打印 Claude 的回复和判分结果
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
❓ 想要提示?运行下面的单元!
from hints import exercise_2_2_hint; print(exercise_2_2_hint)
练习2.3 - 写一个故事
修改 PROMPT
,让 Claude 回复尽可能长。如果你的答案超过800词,则判定为正确。
# Prompt - 只需修改此字段
PROMPT = "[请替换为要求长篇故事的英文指令]"
# 获取 Claude 的回复
response = get_completion(PROMPT)
# 判分函数
def grade_exercise(text):
trimmed = text.strip()
words = len(trimmed.split())
return words >= 800
# 打印 Claude 的回复和判分结果
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
❓ 想要提示?运行下面的单元!
from hints import exercise_2_3_hint; print(exercise_2_3_hint)
恭喜!
如果你已经完成了所有练习,可以进入下一章。祝你提示愉快!
示例游乐场
你可以在这里自由试验本课的提示例子,调整提示语,观察 Claude 的不同回复。
# 提示
PROMPT = "Write a haiku about robots."
# 打印 Claude 的回复
print(get_completion(PROMPT))
# 提示
PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
# 打印 Claude 的回复
print(get_completion(PROMPT))
# 提示
PROMPT = "Who is the best basketball player of all time?"
# 打印 Claude 的回复
print(get_completion(PROMPT))
# 提示
PROMPT = "Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?"
# 打印 Claude 的回复
print(get_completion(PROMPT))