設置
運行以下設置單元以加載您的 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)
# Note that we changed max_tokens to 4K just for this lesson to allow for longer completions in the exercises
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 的回應就會越好星持、越準確抢埋。”如有疑問钉汗,請遵循明確提示的黃金法則:
向同事或朋友展示您的提示羹令,讓他們自己按照說明操作,看看他們是否能產生您想要的結果损痰。如果他們感到困惑福侈,Claude 也會感到困惑。
示例
讓我們以寫詩這樣的任務為例卢未。(忽略任何音節(jié)不匹配 – LLM 還不擅長計算音節(jié)肪凛。)
# Prompt
PROMPT = "Write a haiku about robots."
# Print Claude's response
print(get_completion(PROMPT))
這首俳句已經很不錯了,但用戶可能希望 Claude 直接進入詩歌辽社,而不使用“這是一首俳句”的開場白伟墙。
我們如何實現(xiàn)這一點?我們要求這樣做滴铅!
# Prompt
PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
# Print Claude's response
print(get_completion(PROMPT))
這是另一個例子戳葵。讓我們問 Claude 誰是有史以來最優(yōu)秀的籃球運動員。您可以在下面看到汉匙,雖然 Claude 列出了幾個名字拱烁,但它并沒有用明確的“最佳”來回答生蚁。
# Prompt
PROMPT = "Who is the best basketball player of all time?"
# Print Claude's response
print(get_completion(PROMPT))
我們能讓Claude下定決心,選出最佳球員嗎戏自?是的邦投!只要問!
# 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?"
# Print Claude's response
print(get_completion(PROMPT))
如果您想嘗試課程提示而不更改上述任何內容擅笔,請一直滾動到課程筆記本的底部以訪問示例操場志衣。
練習
練習 2.1 – Spanish
修改 SYSTEM_PROMPT,讓 Claude 以西班牙語輸出答案猛们。
# System prompt - this is the only field you should chnage
SYSTEM_PROMPT = "[Replace this text]"
# Prompt
PROMPT = "Hello Claude, how are you?"
# Get Claude's response
response = get_completion(PROMPT, SYSTEM_PROMPT)
# Function to grade exercise correctness
def grade_exercise(text):
return "hola" in text.lower()
# Print Claude's response and the corresponding grade
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
練習 2.2 – 僅限一名玩家
修改提示念脯,使 Claude 完全不含糊其辭,并且僅用一名特定玩家的名字來回答阅懦,而不使用其他單詞或標點符號和二。
# Prompt - this is the only field you should change
PROMPT = "[Replace this text]"
# Get Claude's response
response = get_completion(PROMPT)
# Function to grade exercise correctness
def grade_exercise(text):
return text == "Michael Jordan"
# Print Claude's response and the corresponding grade
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))
練習 2.3 – 寫一個故事
修改提示,讓 Claude 盡可能長地回答耳胎。如果您的答案超過 800 個字,Claude 的回答將被評為正確惕它。
# Prompt - this is the only field you should change
PROMPT = "[Replace this text]"
# Get Claude's response
response = get_completion(PROMPT)
# Function to grade exercise correctness
def grade_exercise(text):
trimmed = text.strip()
words = len(trimmed.split())
return words >= 800
# 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 = "Write a haiku about robots."
# Print Claude's response
print(get_completion(PROMPT))
# Prompt
PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
# Print Claude's response
print(get_completion(PROMPT))
# Prompt
PROMPT = "Who is the best basketball player of all time?"
# Print Claude's response
print(get_completion(PROMPT))
# 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?"
# Print Claude's response
print(get_completion(PROMPT))
本文由AI技術博客平臺 [ClaudeAI]http://assh83.com/) 發(fā)布甲锡!