說明

Claude 系列模型支援透過官方原生介面呼叫,使用前請先安裝或升級 anthropic 依賴:
pip install -U anthropic
非 claude 模型請用 openai 的介面格式呼叫。

模型信息

模型名稱Claude Opus 4Claude Sonnet 4Claude Sonnet 3.7Claude Sonnet 3.5Claude Haiku 3.5Claude Opus 3Claude Haiku 3
是否支持擴展思考
上下文窗口大小200K200K200K200K200K200K200K
最長輸出長度32000 tokens64000 tokens64000 tokens8192 tokens8192 tokens4096 tokens4096 tokens
訓練數據截止時間2025年3月2025年3月2024年11月2024年4月2024年7月2023年8月2023年8月
  1. 對於 3.5 及以上的模型,如果需要超過 4096 Tokens 的輸出,請傳入明確的 “max_tokens” 數值,參考上方表格中的最長輸出長度
  2. 對於 Sonnet 3.7,你可以透過在請求體中傳入 extra_headers={"anthropic-beta": "output-128k-2025-02-19"} 來把最大輸出從 64K 擴展到 128K,見下方「流式 128K」呼叫,或者參考 Claude 官方的Beta headers 說明

Claude 4 新功能

新的拒絕停止原因 (Refusal Stop Reason)

Claude 4 模型引入了新的 refusal 停止原因,用於處理模型因安全原因拒絕生成的內容:
{
  "id": "msg_014XEDjypDjFzgKVWdFUXxZP",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4-20250514",
  "content": [{"type": "text", "text": "I would be happy to assist you. You can "}],
  "stop_reason": "refusal",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 564,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0,
    "output_tokens": 22
  }
}
在遷移到 Claude 4 時,您應該更新應用程序以處理 refusal 停止原因。

擴展思考 (Extended Thinking)

啟用擴展思考後,Claude 4 模型的 Messages API 會返回 Claude 完整思考過程的摘要。摘要思考提供了擴展思考的全部智能優勢,同時防止濫用。 雖然 API 在 Claude 3.7 和 4 模型之間保持一致,但擴展思考的流式響應可能以”塊狀”傳遞模式返回,流式事件之間可能存在延遲。 摘要由與您在請求中指定的模型不同的模型處理。思考模型不會看到摘要輸出。

交錯思考 (Interleaved Thinking)

Claude 4 模型支援將工具使用與擴展思考交錯,允許更自然的對話,其中工具使用和響應可以與常規消息混合。 交錯思考目前處於測試階段。要啟用交錯思考,請在 API 請求中添加測試頭 interleaved-thinking-2025-05-14
extra_headers={
    "anthropic-beta": "interleaved-thinking-2025-05-14"
}
端點(Endpoint): POST /v1/messages

呼叫

curl https://aihubmix.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \ # 換成你在 AiHubMix 生成的密鑰
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-5-sonnet-20241022",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ]
}'

Body 請求結構

{
  "model": "claude-3-5-sonnet-20241022",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}

請求參數

名稱位置類型必選說明
x-api-keyheaderstringBearer AIHUBMIX_API_KEY
Content-Typeheaderstringnone
bodybodyobjectnone
» modelbodystringnone
» messagesbody[object]none
»» rolebodystringnone
»» contentbodystringnone
» max_tokensbodynumbernone

返回示例

200 Response
{
  "id": "msg_013Uf6CwwyjSe35n3yVaPbLM",
  "type": "message",
  "role": "assistant",
  "model": "claude-3-5-sonnet-20241022",
  "content": [
    {
      "type": "text",
      "text": "That's one of humanity's most enduring and complex philosophical questions! While there's no universal answer, I aim to explore such questions thoughtfully while acknowledging their complexity. I try to focus on having meaningful conversations and helping where I can. What does meaning in life mean to you?"
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 14,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0,
    "output_tokens": 61
  }
}

返回結果

狀態碼狀態碼含義說明數據模型
200OKnoneInline

遷移到 Claude 4

如果您正在從 Claude 3.7 遷移到 Claude 4 模型,請注意以下變化:

更新模型名稱

# 從 Claude 3.7
model="claude-3-7-sonnet-20250219"

# 遷移到 Claude 4
model="claude-sonnet-4-20250514"  # 或 "claude-opus-4-20250514"

處理新的停止原因

更新您的應用程序以處理新的 refusal 停止原因:
if response.stop_reason == "refusal":
    print("Claude 拒絕生成此內容")
elif response.stop_reason == "end_turn":
    print("正常完成")

移除不支持的功能

  • Token 高效工具使用:僅在 Claude Sonnet 3.7 中可用,Claude 4 中不再支持
  • 擴展輸出output-128k-2025-02-19 測試頭僅在 Claude Sonnet 3.7 中可用
如果您正在從 Claude Sonnet 3.7 遷移,建議從請求中移除這些測試頭:
# 移除這些頭部(如果存在)
# "token-efficient-tools-2025-02-19"
# "output-128k-2025-02-19"

在應用中使用(以 Lobe-Chat 為例)

  • 進入設置頁面選擇模型服務商 Claude
  • API key 輸入本站的 Key
  • 接口代理地址,直接輸入下方的網址:
https://aihubmix.com
  • 建議打開「使用客户端請求模式」
  • 最後在模型列表添加自己要使用的模型(建議從我們網站的設置頁面複製粘貼模型名後選擇)
    圖片
    圖片

Claude 文本編輯工具

Claude 可以使用 Anthropic 定義的文本編輯工具來查看和修改文本文件,幫助你調試、修復和改進代碼或其他文本文檔。這使得 Claude 能够直接與你的文件進行交互,提供實際的操作幫助,而不僅僅是提出建議。

使用文本編輯工具前的準備

選擇兼容的模型 Anthropic 的文本編輯工具支持以下 Claude 模型:
  • Claude 4 系列模型 (claude-opus-4-20250514, claude-sonnet-4-20250514): text_editor_20250429
  • Claude 3.7 Sonnet (claude-3-7-sonnet-20250219): text_editor_20250124
  • Claude 3.5 Sonnet (claude-3-5-sonnet-20241022): text_editor_20241022
Claude 4 模型使用了全新的文本編輯工具,與舊版本不兼容。請確保使用正確的工具類型和名稱。

模型對應的工具配置

模型版本工具類型工具名稱支持的功能
Claude 4 (Opus/Sonnet)text_editor_20250429str_replace_based_edit_toolview, str_replace, create
Claude 3.7 Sonnettext_editor_20250124str_replace_editorview, str_replace, create, undo_edit
Claude 3.5 Sonnettext_editor_20241022str_replace_editorview, str_replace, create, undo_edit
重要變化: Claude 4 模型不再支持 undo_edit 命令,請在代碼中移除對此功能的依賴。

評估使用場景

以下是使用文本編輯工具的一些典型場景:
  • 代碼調試:幫助識別和修復代碼中的問題,從語法錯誤到邏輯問題。
  • 代碼重構:通過有針對性的編輯來改進代碼結構、可讀性和性能。
  • 文檔生成:為你的代碼庫添加文檔字符串、注釋或 README 文件。
  • 測試創建:根據對實現的理解創建單元測試。

使用文本編輯工具

通過 Messages API 向 Claude 提供文本編輯工具(命名為 str_replace_editor): 需要安裝 anthropic 包:
pip install anthropic
調用示例:
import anthropic

client = anthropic.Anthropic(
    api_key="sk-***", # 換成你在 AiHubMix 生成的密鑰
    base_url="https://aihubmix.com"
)

response = client.messages.create(
    model="claude-sonnet-4-20250514",  # 或 claude-opus-4-20250514
    max_tokens=1024,
    tools=[
        {
            "type": "text_editor_20250429",
            "name": "str_replace_based_edit_tool"
        }
    ],
    messages=[
        {
            "role": "user", 
            "content": "There's a syntax error in my primes.py file. Can you help me fix it?"
        }
    ]
)

print("Response content:")
for message in response.content:
    print(message.text)

遷移到 Claude 4(文本編輯工具)

如果您正在從 Claude 3.7 Sonnet 遷移到 Claude 4 模型,請注意以下變化:

更新工具配置

python
# Claude 3.7 Sonnet
tools=[
    {
        "type": "text_editor_20250124",
        "name": "str_replace_editor"
    }
]

# Claude 4 (Opus/Sonnet)
tools=[
    {
        "type": "text_editor_20250429",
        "name": "str_replace_based_edit_tool"
    }
]

移除不支持的功能

  • undo_edit 命令:Claude 4 模型不再支持撤銷編輯功能
  • 請從您的代碼中移除任何依賴 undo_edit 的邏輯
返回示例:
{
  "id": "msg_bdrk_012xyNaFCQg4zsVcTk5VkDAe",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I'd be happy to help you fix the syntax error in your `primes.py` file. First, let me take a look at the file to identify the issue."
    },
    {
      "type": "tool_use",
      "text": "",
      "id": "toolu_bdrk_01P6jQG6suDSsDjzugqGVHWC",
      "name": "str_replace_based_edit_tool",
      "input": {
        "command": "view",
        "path": "/repo/primes.py"
      }
    }
  ],
  "model": "claude-sonnet-4-20250514",
  "stop_reason": "tool_use",
  "usage": {
    "input_tokens": 1042,
    "output_tokens": 115
  }
}