第 2 章:基本函数构建-Claude开发应用教程

2024-09-09  本文已影响0人  AI黑悟空

设置

运行以下设置单元以加载您的 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 也会感到困惑。

示例

让我们以写诗这样的任务为例。(忽略任何音节不匹配 – LLM 还不擅长计算音节。)

# Prompt
PROMPT = "Write a haiku about robots."

# Print Claude's response
print(get_completion(PROMPT))

这首俳句已经很不错了,但用户可能希望 Claude 直接进入诗歌,而不使用“这是一首俳句”的开场白。

我们如何实现这一点?我们要求这样做!

# Prompt
PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."

# Print Claude's response
print(get_completion(PROMPT))

这是另一个例子。让我们问 Claude 谁是有史以来最优秀的篮球运动员。您可以在下面看到,虽然 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))

总结

如果您已经解决了到目前为止的所有练习,那么您就可以进入下一章了。祝您好运!

示例广场

这是一个供您自由试验本课中显示的提示示例的区域,并调整提示以查看它如何影响 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/) 发布!

上一篇 下一篇

猜你喜欢

热点阅读