用系統訊息控管行為

Claude 模型入門

Nikhil Rangarajan

Data Scientist

什麼是系統訊息?

  • 客服機器人
  • 要求:
    • 有禮
    • 精簡
    • 每則回覆結尾加上「Is there anything else I can help you with today?」

一個機器人

Claude 模型入門

什麼是系統訊息?

  • 系統訊息為 Claude 設定全域行為規則
  • 作用於整段對話,而非單一回覆
  • 控制語氣、限制、輸出格式與人設

一個機器人問「Is there anything else I can help you with today?」

Claude 模型入門

撰寫有效的系統提示

  • 明確描述行為與限制
  • 定義 Claude 應採取的角色
system_msg = "Be helpful." # Weak

system_msg = "You are a technical writer. Use active voice, under 100 words, actionable advice only." # Strong
messages = [{"role": "user", "content": "How do I improve documentation?"}] response = client.messages.create(model="claude-sonnet-4-6", max_tokens=150, system=system_msg, messages=messages)
Claude 模型入門

常見的系統訊息樣式

  • 角色導向:定義 Claude 是誰

$$

  • 限制導向:設定長度或格式限制

$$

  • 語氣導向:控制情緒風格

$$

  • 任務導向:提供專門指示
# Role-based
"You are a customer service rep."

# Constraint-based  
"Respond in exactly 3 bullet points,
max 50 words."

# Tone-based
"Use enthusiastic, encouraging tone."

# Task-specific
"Debug Python code. Always ask for 
error messages first."
Claude 模型入門

測試與迭代系統訊息

  • 用多樣使用者輸入測試以找邊界情況
  • 監測不期望的輸出或行為漂移
  • 需要時細化語句、提高明確度
# System message causing issues
"Be concise and helpful."

# Problem: Responses too short, 
# missing context
"Provide helpful responses in 2-3 
sentences. Include relevant context 
but avoid unnecessary details."

# Test with various prompts 
Claude 模型入門

進階系統訊息技巧

$$

  • 結合指示並標明優先順序
  • 以條件邏輯處理情境
  • 在系統訊息內加入範例
  • 搭配 few-shot 提示分層
system_msg = """Code reviewer. 
Priority: 1) Security 2) Performance 
3) Style
If no issues: "Code looks good" 
+ suggest improvement
Example: "Security: No issues. 
Performance: Consider caching here."
"""

messages = [{"role": "system", 
             "content": system_msg},
    {"role": "user", 
     "content": "Review: [code]"}]
Claude 模型入門

一起來練習吧!

Claude 模型入門

Preparing Video For Download...