Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    開始使用
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.5 新功能遷移至 Claude 4.5模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API上下文窗口提示詞最佳實踐
    功能
    提示詞快取上下文編輯擴展思考努力串流消息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實現工具使用細粒度工具串流Bash 工具代碼執行工具程式化工具調用計算機使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶體工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐使用 API 的 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2 (預覽)Python SDK遷移指南
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞生成器使用提示詞範本提示詞改進器清晰直接使用範例 (多次提示)讓 Claude 思考 (CoT)使用 XML 標籤給 Claude 一個角色 (系統提示詞)預填 Claude 的回應鏈接複雜提示詞長上下文提示擴展思考提示
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    加強防護欄
    減少幻覺增加輸出一致性緩解越獄串流拒絕減少提示詞洩露保持 Claude 的角色
    管理和監控
    Admin API 概覽使用量和成本 APIClaude Code Analytics API
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

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

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • 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
    • Catalog
    • 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
    提示詞工程

    預填 Claude 的回應以獲得更好的輸出控制

    通過預填 Assistant 訊息來引導 Claude 的回應,跳過前言,強制執行特定格式,並在角色扮演場景中保持角色一致性。

    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 的表現。一點點預填就能帶來很大的效果!

    如何預填 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 " 這樣的預填(末尾有空格)會導致錯誤。

    範例

    範例 1:控制輸出格式並跳過前言

    進階使用者提示:預填 { 會強制 Claude 跳過前言並直接輸出 JSON 物件。這樣更簡潔、更簡潔,程式更容易解析,無需額外處理。 為了保證輸出符合特定架構的 JSON,請考慮使用 結構化輸出 而不是預填。結構化輸出確保 Claude 的回應始終符合您定義的 JSON 架構,非常適合需要嚴格格式合規性的生產應用程式。

    範例 2:在角色扮演場景中保持角色

    角色扮演提示:預填括號內的 [ROLE_NAME] 可以提醒 Claude 保持角色,即使在更長和更複雜的對話中也是如此。當與 system 參數中的角色提示結合使用時,這特別強大。


    使用訊息

    查看更多預填和其他訊息 API 模式的範例。

    提示庫

    從為各種任務和使用案例精選的提示中獲得靈感。

    GitHub 提示教程

    一個充滿範例的教程,涵蓋我們文件中的提示工程概念。

    Google Sheets 提示教程

    通過互動式試算表提供的更輕量級版本的提示工程教程。

    • 如何預填 Claude 的回應