提示验证
Anthropic API 执行基本的提示清理和验证,以帮助确保您的提示对于 Claude 来说具有良好的格式。
如果您的提示不符合指定的格式,API 首先会尝试进行轻微的清理(例如去除尾随空格)。确切的行为可能会发生变化,我们强烈建议您使用推荐的交替\n\nHuman
:和\n\nAssistant
:的格式来编写提示。
然后,API 将在以下条件下验证您的提示:
- 提示中的第一个对话轮必须是一个
\n\nHuman
:turn - 提示中最后一个对话轮必须是一个
\n\nAssistant
:turn - 提示的长度必须小于
100,000 - 1
个标记。
例子
以下提示将导致 API 错误:
# Missing "\n\nHuman:" and "\n\nAssistant:" turns
prompt = "Hello, world"
# Missing "\n\nHuman:" turn
prompt = "Hello, world\n\nAssistant:"
# Missing "\n\nAssistant:" turn
prompt = "\n\nHuman: Hello, Claude"
# "\n\nHuman:" turn is not first
prompt = "\n\nAssistant: Hello, world\n\nHuman: Hello, Claude\n\nAssistant:"
# "\n\nAssistant:" turn is not last
prompt = "\n\nHuman: Hello, Claude\n\nAssistant: Hello, world\n\nHuman: How many toes do dogs have?"
# "\n\nAssistant:" only has one "\n"
prompt = "\n\nHuman: Hello, Claude \nAssistant:"
以下内容当前已被 API 接受并自动清理,但您不应依赖此行为,因为它将来可能会发生变化:
# No leading "\n\n" for "\n\nHuman:"
prompt = "Human: Hello, Claude\n\nAssistant:"
# Trailing space after "\n\nAssistant:"
prompt = "\n\nHuman: Hello, Claude:\n\nAssistant: "
最后修改时间: 1 年前