设置 #
运行以下设置单元以加载您的 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)
# Has been rewritten to take in a messages list of arbitrary length
def get_completion(messages, system_prompt=""):
message = client.messages.create(
model=MODEL_NAME,
max_tokens=2000,
temperature=0.0,
system=system_prompt,
messages=messages
)
return message.content[0].text
课程 #
俗话说,“写作就是重写”。事实证明,当被要求这样做时,Claude 通常可以提高其响应的准确性!
有很多方法可以提示 Claude“再想一想”。要求人类仔细检查其工作感觉自然的方法通常也适用于 Claude。(查看我们的提示链文档,了解何时以及如何使用提示链的更多示例。)
示例 #
在此示例中,我们要求 Claude 想出十个单词……但其中一个或多个不是真正的单词。
# Initial prompt
first_user = "Name ten words that all end with the exact letters 'ab'."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)
要求 Claude 使其答案更准确可以修复错误!
下面,我们从上面删除了 Claude 的错误回答,并在对话中添加了另一个转折,要求 Claude 修复其之前的答案。
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
但是 Claude 会因为我们的命令而修改答案吗?如果我们一开始就有一个正确的答案怎么办?Claude 会失去信心吗?在这里,我们在 first_response 的位置放置了一个正确的答案,并要求它再次仔细检查。
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
您可能会注意到,如果您多次从上面的块生成响应,Claude 大部分时间都会保留原有的单词,但偶尔仍会更改单词,即使它们已经全部正确。我们可以做些什么来缓解这种情况?根据第 8 章,我们可以给 Claude 一个出路!让我们再试一次。
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words. If all the words are real words, return the original list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
尝试从上面的代码生成响应几次,看看 Claude 现在是否能够更好地坚持自己的立场。
您还可以使用提示链要求 Claude 使其响应更好。下面,我们要求 Claude 首先写一个故事,然后改进它所写的故事。您的个人品味可能有所不同,但许多人可能同意 Claude 的第二个版本更好。
首先,让我们生成 Claude 的第一个版本的故事。
# Initial prompt
first_user = "Write a three-sentence short story about a girl who likes to run."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)
现在让我们让克劳德改进它的初稿。
second_user = "Make the story better."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
这种形式的替换非常强大。我们一直在使用替换占位符来传递列表、单词、Claude 以前的回复等等。您还可以使用替换来执行我们所谓的“函数调用”,即要求 Claude 执行某些函数,然后获取该函数的结果并要求 Claude 随后使用结果执行更多操作。它的工作原理与任何其他替换一样。有关此内容的更多信息,请参阅下一个附录。
下面是另一个将一次调用 Claude 的结果插入另一个更长的调用的示例。让我们从第一个提示开始(这次包括预填充 Claude 的回复)。
first_user = """Find all names from the below text:
"Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too."""
prefill = ""
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(first_response)
我们将这个名字列表传递到另一个提示中
second_user = "Alphabetize the list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill + "\n" + first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
现在您已经了解了提示链,请转到附录 10.2 了解如何使用提示链实现函数调用。
示例操场 #
这是一个区域,您可以自由地试验本课中显示的提示示例,并调整提示以查看它如何影响 Claude 的响应。
# Initial prompt
first_user = "Name ten words that all end with the exact letters 'ab'."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)
# Initial prompt
first_user = "Name ten words that all end with the exact letters 'ab'."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words. If all the words are real words, return the original list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
first_user = "Write a three-sentence short story about a girl who likes to run."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)
second_user = "Make the story better."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))
first_user = """Find all names from the below text:
"Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too."""
prefill = ""
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(first_response)
second_user = "Alphabetize the list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill + "\n" + first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))