Claude Platform Docs
  • 消息
  • 托管智能体
  • 管理

Search...
⌘K
用例
概览工单路由客户支持智能体内容审核法律摘要
提示工程
概览提示最佳实践为 Claude Fable 5 编写提示为 Claude Opus 4.8 编写提示控制台提示工具
测试与评估
定义成功标准并构建评估在控制台中使用评估工具降低延迟
加强防护措施
减少幻觉提高输出一致性缓解越狱攻击减少提示泄露
参考
术语表

Log in
提高输出一致性
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Claude on AWS
  • Claude on Google Cloud

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
最佳实践/加强防护措施

提高输出一致性



如需保证 JSON 模式一致性

如果您需要 Claude 始终输出符合特定模式的有效 JSON,请使用 Structured Outputs(结构化输出),而不是下面的提示工程技巧。结构化输出可保证模式合规性,并且是专门为此用例设计的。

下面的技巧适用于一般的输出一致性需求,或当您需要超出严格 JSON 模式的灵活性时。

以下是使 Claude 的响应更加一致的方法:

指定所需的输出格式

使用 JSON、XML 或自定义模板精确定义您所需的输出格式,以便 Claude 理解您要求的每个输出格式元素。

预填充 Claude 的响应


Claude Fable 5、Claude Mythos 5、Claude Mythos Preview、Claude Opus 4.8、Claude Opus 4.7、Claude Opus 4.6 和 Claude Sonnet 4.6 不支持预填充。请在支持的模型上使用结构化输出,或改用系统提示指令。

使用您所需的格式预填充 Assistant 回合。此技巧可绕过 Claude 友好的开场白并强制执行您的结构。

使用示例进行约束

提供您所需输出的示例。这比抽象的指令更能训练 Claude 的理解能力。

使用检索实现上下文一致性

对于需要一致上下文的任务(例如聊天机器人、知识库),使用检索将 Claude 的响应锚定在固定的信息集中。

为复杂任务链接提示

将复杂任务分解为更小、更一致的子任务。每个子任务都能获得 Claude 的充分关注,从而减少大规模工作流程中的不一致错误。

让 Claude 保持角色

对于基于角色的应用,保持一致的角色需要刻意的提示设计。

  • 使用系统提示设置角色: 使用系统提示来定义 Claude 的角色和个性。这为一致的响应奠定了坚实的基础。
    
    在设置角色时,提供有关个性、背景以及任何特定特征或癖好的详细信息。这将帮助模型更好地模拟和泛化角色的特征。
  • 让 Claude 为可能的场景做好准备: 在您的提示中提供常见场景和预期响应的列表。这可以"训练" Claude 在不脱离角色的情况下处理各种情况。

Was this page helpful?

  • 指定所需的输出格式
  • 预填充 Claude 的响应
  • 使用示例进行约束
  • 使用检索实现上下文一致性
  • 为复杂任务链接提示
  • 让 Claude 保持角色