第9章 使用 Anthropic API
第9章:使用 Anthropic API
为了迎合使用 Anthropic API 生态的需求,我们的 API 已支持 Anthropic API 格式,其 base_url 为 https://api.deepseek.com/anthropic。
通过简单配置,即可将 DeepSeek 的能力集成到 Anthropic API 生态中。
在 Claude Code 中使用 DeepSeek 请参考《集成 Claude Code》。
通过 Anthropic API 调用 DeepSeek 模型
安装 Anthropic SDK
pip install anthropic
配置环境变量
export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic export ANTHROPIC_API_KEY=${YOUR_API_KEY}
调用 API
import anthropic
client = anthropic.Anthropic()
message = client.messages.create( model="deepseek-flash", max_tokens=1000, system="You are a helpful assistant.", messages=[ { "role": "user", "content": [ { "type": "text", "text": "Hi, how are you?" } ] } ] ) print(message.content)
注:当向 DeepSeek 的 Anthropic API 传入不支持的模型名称时,API 后端会自动将其映射到 deepseek-flash 模型。
Anthropic 模型映射 使用 Anthropic API 时,我们会将传入的 Claude 模型名称映射如下:
以 claude-opus 开头的模型映射到 deepseek-v4-pro 以 claude-haiku 或 claude-sonnet 开头的模型映射到 deepseek-flash
claude-opus 的映射指向 deepseek-v4-pro,按 V4 Pro 的价格计费。 通过此映射,在使用新版 Claude Desktop APP 的开发模式时,只需修改 base_url 和 api_key,即可绕过 APP 的模型名称限制,连接 DeepSeek 模型。
Anthropic API 兼容性详情 本节列出 DeepSeek API 与 Anthropic API 的兼容性详情。完整的 Anthropic API 格式定义,请参阅 Anthropic API 官方文档。
HTTP 请求头 anthropic-beta:在 /messages 端点中被忽略;在 Files API 端点中为必需项(files-api-2025-04-14)——详见 Files API anthropic-version:被忽略 x-api-key:完全支持
简单字段 model:使用 DeepSeek 模型代替 max_tokens:完全支持 container:被忽略 mcp_servers:被忽略 metadata:支持 user_id,其他字段被忽略
请参见“速率限制与隔离”部分,以获取更多关于 user_id 参数的信息。
service_tier:忽略 stop_sequences:完全支持 stream:完全支持 system:完全支持 temperature:完全支持(取值范围 [0.0 ~ 2.0]) thinking:支持(budget_tokens 参数被忽略) output_config:仅支持 effort 参数 top_k:忽略 top_p:仅在 thinking 模式下生效(下限为 0.95);在非 thinking 模式下固定为 1.0
Tool 字段 tools 字段:name,支持状态:完全支持 字段:input_schema,支持状态:完全支持 字段:description,支持状态:完全支持 字段:cache_control,支持状态:忽略
tool_choice 取值:none,支持状态:完全支持 取值:auto,支持状态:支持(disable_parallel_tool_use 参数被忽略) 取值:any,支持状态:支持(disable_parallel_tool_use 参数被忽略) 取值:tool,支持状态:支持(disable_parallel_tool_use 参数被忽略)
Message 字段 content 字段: - 类型:string,支持状态:完全支持 - 类型:array,子字段 type="text" - 子字段:text,支持状态:完全支持 - 子字段:cache_control,支持状态:忽略 - 子字段:citations,支持状态:忽略 - 类型:array,子字段 type="image" - 子字段:source,支持状态:支持。source.type 可以是 base64(支持的媒体类型:jpeg、png、gif、webp)、url 或 file(使用 file 变体时需在请求头中添加 anthropic-beta: files-api-2025-04-14) - 类型:array,子字段 type = "document",支持状态:不支持 - 类型:array,子字段 type = "search_result",支持状态:不支持 - 类型:array,子字段 type = "thinking",支持状态:支持 - 类型:array,子字段 type="redacted_thinking",支持状态:不支持 - 类型:array,子字段 type = "tool_use" - 子字段:id,支持状态:完全支持 - 子字段:input,支持状态:完全支持 - 子字段:name,支持状态:完全支持 - 子字段:cache_control,支持状态:忽略 - 类型:array,子字段 type = "tool_result" - 子字段:tool_use_id,支持状态:完全支持 - 子字段:content,支持状态:完全支持 - 子字段:cache_control,支持状态:忽略 - 子字段:is_error,支持状态:忽略 - 类型:array,子字段 type = "server_tool_use",支持状态:支持 - 类型:array,子字段 type = "web_search_tool_result",支持状态:支持 - 类型:array,子字段 type = "code_execution_tool_result",支持状态:不支持 - 类型:array,子字段 type = "mcp_tool_use",支持状态:不支持 - 类型:array,子字段 type = "mcp_tool_result",支持状态:不支持 - 类型:array,子字段 type = "container_upload",支持状态:不支持