第 2 章:基本函數構建-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)

# 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ā)布甲锡!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末兆蕉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子缤沦,更是在濱河造成了極大的恐慌虎韵,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缸废,死亡現(xiàn)場離奇詭異包蓝,居然都是意外死亡,警方通過查閱死者的電腦和手機企量,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門测萎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人届巩,你說我怎么就攤上這事硅瞧。” “怎么了恕汇?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵腕唧,是天一觀的道長冒嫡。 經常有香客問我,道長四苇,這世上最難降的妖魔是什么孝凌? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮月腋,結果婚禮上蟀架,老公的妹妹穿的比我還像新娘。我一直安慰自己榆骚,他們只是感情好片拍,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著妓肢,像睡著了一般捌省。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碉钠,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天纲缓,我揣著相機與錄音,去河邊找鬼喊废。 笑死祝高,一個胖子當著我的面吹牛,可吹牛的內容都是我干的污筷。 我是一名探鬼主播工闺,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼瓣蛀!你這毒婦竟也來了陆蟆?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤惋增,失蹤者是張志新(化名)和其女友劉穎叠殷,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體器腋,經...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡溪猿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了纫塌。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戴已。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡蹲缠,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情契沫,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響瓶摆,放射性物質發(fā)生泄漏。R本人自食惡果不足惜性宏,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一群井、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧毫胜,春花似錦书斜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至口渔,卻和暖如春样屠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背缺脉。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工痪欲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人枪向。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓勤揩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秘蛔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內容