Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
While these tips apply broadly to all Claude models, you can find prompting tips specific to extended thinking models here.
预填充仅适用于非扩展思考模式。目前不支持扩展思考。
使用 Claude 时,您拥有通过预填充 Assistant 消息来指导其响应的独特能力。这种强大的技术允许您指导 Claude 的操作、跳过前言、强制特定格式(如 JSON 或 XML),甚至帮助 Claude 在角色扮演场景中保持角色一致性。
在某些 Claude 表现不如预期的情况下,几句预填充的句子可以大大改进 Claude 的性能。一点点预填充就能产生很大的效果!
要进行预填充,请在 Assistant 消息中包含所需的初始文本(Claude 的响应将从 Assistant 消息结束的地方继续):
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "What is your favorite color?"},
{"role": "assistant", "content": "As an AI assistant, I don't have a favorite color, But if I had to pick, it would be green because"} # Prefill here
]
)预填充内容不能以尾随空格结尾。像 "As an AI assistant, I " 这样的预填充(末尾有空格)会导致错误。
{ 会强制 Claude 跳过前言并直接输出 JSON 对象。这更简洁、更简明,程序更容易解析,无需额外处理。
为了保证输出符合特定架构的 JSON,请考虑使用 Structured Outputs 而不是预填充。结构化输出确保 Claude 的响应始终与您定义的 JSON 架构相匹配,非常适合需要严格格式合规性的生产应用程序。[ROLE_NAME] 可以提醒 Claude 保持角色,即使在更长和更复杂的对话中也是如此。当与 system 参数中的角色提示相结合时,这特别强大。