更多教程,請訪問claude應用開發(fā)教程
設置
運行以下設置單元以加載您的 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
課程
繼續(xù)討論克勞德除了你說的話之外沒有其他背景這一主題,有時需要提示克勞德扮演一個特定的角色(包括所有必要的背景)。這也稱為角色提示。角色背景越詳細越好。
用角色引導克勞德可以提高克勞德在寫作、編碼和總結等各個領域的表現。這就像人類有時被告知“像__一樣思考”時會得到幫助一樣。角色提示還可以改變克勞德回應的風格、語氣和方式。
注意:角色提示可以在系統提示中發(fā)生,也可以作為用戶消息輪換的一部分發(fā)生。
示例
在下面的例子中,我們看到,在沒有角色提示的情況下,當被要求用一句話來描述滑板時,克勞德給出了一個直截了當、非程式化的答案。
然而,當我們讓克勞德扮演一只貓的角色時,克勞德的視角發(fā)生了變化,因此克勞德的回應語氣、風格和內容都適應了新角色。
注意:您可以使用的額外技巧是向克勞德提供其目標受眾的背景。下面,我們可以調整提示,告訴克勞德它應該和誰說話?!澳闶且恢回垺碑a生的反應與“你是一只在和一群滑板手說話的貓”完全不同。
以下是系統提示中沒有角色提示的提示:
# 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 更好地執(zhí)行數學或邏輯任務。
例如,在下面的例子中,有一個明確的正確答案,即是。然而,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 在逐步執(zhí)行時實際上發(fā)現了錯誤,但并沒有得出整體解決方案是錯誤的結論。
修改 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))
總結
如果您已經解決了到目前為止的所有練習,那么您就可以進入下一章了。祝您好運!
示例廣場
這是一個供您自由試驗本課中顯示的提示示例的區(qū)域,并調整提示以查看它如何影響 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))