更多教程见:
设置
运行以下设置单元以加载您的 API 密钥并建立 get_completion 辅助函数。
!pip install anthropic
# Import python's built-in regular expression library
import re
import anthropic
# Retrieve the API_KEY & MODEL_NAME variables from the IPython store
%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
课程
继续讨论克劳德除了你说的话之外没有其他背景这一主题,有时需要提示克劳德扮演一个特定的角色(包括所有必要的背景)。这也称为角色提示。角色背景越详细越好。
用角色引导克劳德可以提高克劳德在写作、编码和总结等各个领域的表现。这就像人类有时被告知“像__一样思考”时会得到帮助一样。角色提示还可以改变克劳德回应的风格、语气和方式。
注意:角色提示可以在系统提示中发生,也可以作为用户消息轮换的一部分发生。
示例
在下面的例子中,我们看到,在没有角色提示的情况下,当被要求用一句话来描述滑板时,克劳德给出了一个直截了当、非程式化的答案。
然而,当我们让克劳德扮演一只猫的角色时,克劳德的视角发生了变化,因此克劳德的回应语气、风格和内容都适应了新角色。
注意:您可以使用的额外技巧是向克劳德提供其目标受众的背景。下面,我们可以调整提示,告诉克劳德它应该和谁说话。“你是一只猫”产生的反应与“你是一只在和一群滑板手说话的猫”完全不同。
以下是系统提示中没有角色提示的提示:
# Prompt
PROMPT = "In one sentence, what do you think about skateboarding?"
# Print Claude's response
print(get_completion(PROMPT))
这是相同的用户问题,但有角色提示。
# System prompt
SYSTEM_PROMPT = "You are a cat."
# Prompt
PROMPT = "In one sentence, what do you think about skateboarding?"
# Print Claude's response
print(get_completion(PROMPT, SYSTEM_PROMPT))
您可以使用角色提示让 Claude 模仿某些写作风格、用特定语气说话或引导其答案的复杂性。角色提示还可以让 Claude 更好地执行数学或逻辑任务。
例如,在下面的例子中,有一个明确的正确答案,即是。然而,Claude 答错了,并认为它缺乏信息,但事实并非如此:
# 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?"
# Print Claude's response
print(get_completion(PROMPT))
现在,如果我们让 Claude 扮演逻辑机器人的角色会怎么样?这会如何改变 Claude 的答案?
事实证明,通过这项新的角色分配,Claude 做对了。(尽管显然不是出于所有正确的原因)
# System prompt
SYSTEM_PROMPT = "You are a logic bot designed to answer complex logic problems."
# 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?"
# Print Claude's response
print(get_completion(PROMPT, SYSTEM_PROMPT))
注意:您将在本课程中学习到,您可以使用多种提示工程技术来获得类似的结果。使用哪种技术取决于您和您的偏好!我们鼓励您进行实验以找到自己的提示工程风格。
如果您想在不更改上述任何内容的情况下尝试课程提示,请一直滚动到课程笔记本的底部以访问示例游乐场。
练习
练习 3.1 – 数学更正
在某些情况下,Claude 可能会在数学方面遇到困难,即使是简单的数学。下面,Claude 错误地将数学问题评估为已正确解决,即使第二步中存在明显的算术错误。请注意,Claude 在逐步执行时实际上发现了错误,但并没有得出整体解决方案是错误的结论。
修改 PROMPT 和/或 SYSTEM_PROMPT,使 Claude 将解决方案评为错误解决,而不是正确解决。
# System prompt - if you don't want to use a system prompt, you can leave this variable set to an empty string
SYSTEM_PROMPT = ""
# Prompt
PROMPT = """Is this equation solved correctly below?
2x - 3 = 9
2x = 6
x = 3"""
# Get Claude's response
response = get_completion(PROMPT, SYSTEM_PROMPT)
# Function to grade exercise correctness
def grade_exercise(text):
if "incorrect" in text or "not correct" in text.lower():
return True
else:
return False
# Print Claude's response and the corresponding grade
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
总结
如果您已经解决了到目前为止的所有练习,那么您就可以进入下一章了。祝您好运!
示例广场
这是一个供您自由试验本课中显示的提示示例的区域,并调整提示以查看它如何影响 Claude 的回答。
# Prompt
PROMPT = "In one sentence, what do you think about skateboarding?"
# Print Claude's response
print(get_completion(PROMPT))
# System prompt
SYSTEM_PROMPT = "You are a cat."
# Prompt
PROMPT = "In one sentence, what do you think about skateboarding?"
# Print Claude's response
print(get_completion(PROMPT, SYSTEM_PROMPT))
# 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?"
# Print Claude's response
print(get_completion(PROMPT))
# System prompt
SYSTEM_PROMPT = "You are a logic bot designed to answer complex logic problems."
# 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?"
# Print Claude's response
print(get_completion(PROMPT, SYSTEM_PROMPT))