第1章:基本提示结构-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)

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

课程

Anthropic 提供两个 API,即旧版文本完成 API 和当前消息 API。在本教程中,我们将专门使用消息 API。

使用消息 API 调用 Claude 至少需要以下参数:

每条输入消息都必须是具有角色和内容的对象。您可以指定单个用户角色消息,也可以包含多个用户和助手消息(如果是,它们必须交替出现)。第一条消息必须始终使用用户角色。

还有可选参数,例如:

示例

让我们看看 Claude 如何响应一些格式正确的提示。对于以下每个单元格,运行单元格(shift+enter),Claude 的响应将显示在块下方。

# Prompt
PROMPT = "Hi Claude, how are you?"

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

# Prompt
PROMPT = "Can you tell me the color of the ocean?"
# Print Claude's response
print(get_completion(PROMPT))

# Prompt
PROMPT = "What year was Celine Dion born in?"

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

现在让我们来看看一些不包含正确 Messages API 格式的提示。对于这些格式错误的提示,Messages API 会返回错误。

首先,我们有一个 Messages API 调用的示例,该调用的消息数组中缺少角色和内容字段。

# Get Claude's response
response = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        messages=[
          {"Hi Claude, how are you?"}
        ]
    )

# Print Claude's response
print(response[0].text)

这是用户与助手角色切换失败的提示。

# Get Claude's response
response = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        messages=[
          {"role": "user", "content": "What year was Celine Dion born in?"},
          {"role": "user", "content": "Also, can you tell me some other facts about her?"}
        ]
    )

# Print Claude's response
print(response[0].text)

用户和助手消息必须交替出现,并且消息必须以用户回合开始。您可以在提示中拥有多个用户和助手对(就像模拟多回合对话一样)。您还可以将单词放入终端助手消息中,以便 Claude 从您上次中断的地方继续(有关详细信息,请参阅后面的章节)。

System Prompts

您还可以使用系统提示。系统提示是一种在“用户”回合中向 Claude 提出问题或任务之前向其提供上下文、说明和指南的方法。

从结构上讲,系统提示与用户和助手消息列表分开存在,因此属于单独的系统参数(请查看笔记本的“设置”部分中 get_completion 辅助函数的结构)。

在本教程中,无论我们在哪里使用系统提示,我们都会在完成函数中为您提供系统字段。如果您不想使用系统提示,只需将 SYSTEM_PROMPT 变量设置为空字符串。

System Prompt 示例

# System prompt
SYSTEM_PROMPT = "Your answer should always be a series of critical thinking questions that further the conversation (do not provide answers to your questions). Do not actually answer the user question."

# Prompt
PROMPT = "Why is the sky blue?"

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

为什么要使用System Prompt?编写良好的System Prompt可以通过多种方式提高 Claude 的表现,例如提高 Claude 遵守规则和指令的能力。有关更多信息,请访问我们的文档,了解如何将系统提示与 Claude 结合使用。

现在我们将深入研究一些练习。如果您想在不更改上述任何内容的情况下尝试课程提示,请一直滚动到课程笔记本的底部以访问示例操场。

练习

练习 1.1 – 数到三

使用正确的用户/助手格式,编辑下面的提示,让 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):
    pattern = re.compile(r'^(?=.*1)(?=.*2)(?=.*3).*$', re.DOTALL)
    return bool(pattern.match(text))

# Print Claude's response and the corresponding grade
print(response)
print("\n--------------------------- GRADING ---------------------------")
print("This exercise has been correctly solved:", grade_exercise(response))

练习 1.2 – System Prompt

修改 SYSTEM_PROMPT,让 Claude 像对待 3 岁小孩一样做出反应。

# System prompt - this is the only field you should change
SYSTEM_PROMPT = "[Replace this text]"

# Prompt
PROMPT = "How big is the sky?"

# Get Claude's response
response = get_completion(PROMPT, SYSTEM_PROMPT)

# Function to grade exercise correctness
def grade_exercise(text):
    return bool(re.search(r"giggles", text) or re.search(r"soo", text))

# 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 = "Hi Claude, how are you?"

# Print Claude's response
print(get_completion(PROMPT))
# Prompt
PROMPT = "Can you tell me the color of the ocean?"

# Print Claude's response
print(get_completion(PROMPT))
# Prompt
PROMPT = "What year was Celine Dion born in?"

# Print Claude's response
print(get_completion(PROMPT))
# Get Claude's response
response = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        messages=[
          {"Hi Claude, how are you?"}
        ]
    )

# Print Claude's response
print(response[0].text)

# Get Claude's response
response = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        messages=[
          {"role": "user", "content": "What year was Celine Dion born in?"},
          {"role": "user", "content": "Also, can you tell me some other facts about her?"}
        ]
    )

# Print Claude's response
print(response[0].text)

# System prompt
SYSTEM_PROMPT = "Your answer should always be a series of critical thinking questions that further the conversation (do not provide answers to your questions). Do not actually answer the user question."

# Prompt
PROMPT = "Why is the sky blue?"

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

更多AI教程请访问:第1章:基本提示结构-Claude应用开发教程 – 欢迎来到ClaudeAI博客网站 (assh83.com)

上一篇下一篇

猜你喜欢

热点阅读