项目文件夹

文件
2026-07-13 21:35:32 +08:00

9.1 KiB

兼容 Anthropic API

使用 Anthropic SDK 调用 MiniMax 模型

为满足开发者对 Anthropic API 生态的需求,我们的 API 现已支持 Anthropic API 格式。通过简单配置,您可以将 MiniMax 能力集成到 Anthropic API 生态中。

快速开始

1. 安装 Anthropic SDK

```bash Python theme={null} pip install anthropic ```
npm install @anthropic-ai/sdk

2. 配置环境变量

国际用户请使用 https://api.minimax.io/anthropic;中国用户请使用 https://api.minimaxi.com/anthropic

export ANTHROPIC_BASE_URL=https://api.minimax.io/anthropic
export ANTHROPIC_API_KEY=${YOUR_API_KEY}

3. 调用 API

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="MiniMax-M2.1",
    max_tokens=1000,
    system="You are a helpful assistant.",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Hi, how are you?"
                }
            ]
        }
    ]
)

for block in message.content:
    if block.type == "thinking":
        print(f"Thinking:\n{block.thinking}\n")
    elif block.type == "text":
        print(f"Text:\n{block.text}\n")

4. 重要说明

在多轮函数调用对话中,必须将完整的模型响应(即 assistant 消息)追加到对话历史中,以保持推理链的连续性。

  • 将完整的 response.content 列表追加到消息历史中(包含所有内容块:thinking/text/tool_use

支持的模型

使用 Anthropic SDK 时,支持 MiniMax-M2.1 MiniMax-M2.1-lightning MiniMax-M2 模型:

模型名称 描述
MiniMax-M2.1 强大的多语言编程能力,全面增强的编程体验(输出速度约 60 tps)
MiniMax-M2.1-lightning 更快更敏捷(输出速度约 100 tps)
MiniMax-M2 Agent 能力,高级推理
Anthropic API 兼容接口目前仅支持 `MiniMax-M2.1` `MiniMax-M2.1-lightning` `MiniMax-M2` 模型。其他模型请使用标准 MiniMax API 接口。

兼容性

支持的参数

使用 Anthropic SDK 时,我们支持以下输入参数:

参数 支持状态 描述
model 完全支持 支持 MiniMax-M2.1 MiniMax-M2.1-lightning MiniMax-M2 模型
messages 部分支持 支持文本和工具调用,不支持图片/文档输入
max_tokens 完全支持 最大生成 token 数
stream 完全支持 流式响应
system 完全支持 系统提示词
temperature 完全支持 范围 (0.0, 1.0],控制输出随机性,推荐值:1
tool_choice 完全支持 工具选择策略
tools 完全支持 工具定义
top_p 完全支持 核采样参数
metadata 完全支持 元数据
thinking 完全支持 推理内容
top_k 被忽略 该参数将被忽略
stop_sequences 被忽略 该参数将被忽略
service_tier 被忽略 该参数将被忽略
mcp_servers 被忽略 该参数将被忽略
context_management 被忽略 该参数将被忽略
container 被忽略 该参数将被忽略

Messages 字段支持

字段类型 支持状态 描述
type="text" 完全支持 文本消息
type="tool_use" 完全支持 工具调用
type="tool_result" 完全支持 工具调用结果
type="thinking" 完全支持 推理内容
type="image" 不支持 暂不支持图片输入
type="document" 不支持 暂不支持文档输入

示例

流式响应

import anthropic

client = anthropic.Anthropic()

print("Starting stream response...\n")
print("=" * 60)
print("Thinking Process:")
print("=" * 60)

stream = client.messages.create(
    model="MiniMax-M2.1",
    max_tokens=1000,
    system="You are a helpful assistant.",
    messages=[
        {"role": "user", "content": [{"type": "text", "text": "Hi, how are you?"}]}
    ],
    stream=True,
)

reasoning_buffer = ""
text_buffer = ""

for chunk in stream:
    if chunk.type == "content_block_start":
        if hasattr(chunk, "content_block") and chunk.content_block:
            if chunk.content_block.type == "text":
                print("\n" + "=" * 60)
                print("Response Content:")
                print("=" * 60)

    elif chunk.type == "content_block_delta":
        if hasattr(chunk, "delta") and chunk.delta:
            if chunk.delta.type == "thinking_delta":
                # Stream output thinking process
                new_thinking = chunk.delta.thinking
                if new_thinking:
                    print(new_thinking, end="", flush=True)
                    reasoning_buffer += new_thinking
            elif chunk.delta.type == "text_delta":
                # Stream output text content
                new_text = chunk.delta.text
                if new_text:
                    print(new_text, end="", flush=True)
                    text_buffer += new_text

print("\n")

工具调用与交错思维

了解如何使用 Anthropic SDK 调用 M2.1 的工具使用(Tool Use)和交错思维(Interleaved Thinking)能力,请参考以下文档。

了解如何利用 MiniMax-M2.1 的工具调用和交错思维能力,在复杂任务中提升表现。

重要说明

1. Anthropic API 兼容接口目前仅支持 `MiniMax-M2.1` `MiniMax-M2` 模型
  1. temperature 参数范围是 (0.0, 1.0],超出此范围的值将返回错误

  2. 部分 Anthropic 参数(如 thinkingtop_kstop_sequencesservice_tiermcp_serverscontext_managementcontainer)将被忽略

  3. 目前不支持图片和文档类型的输入

相关链接

推荐阅读

通过兼容的 Anthropic API 和 OpenAI API 支持文本生成。 将 OpenAI SDK 与 MiniMax 模型配合使用 MiniMax-M2.1 在代码理解、对话和推理方面表现出色。 AI 模型可以调用外部函数来扩展其能力。

要查找本文档中的导航页面及其他页面,请获取 llms.txt 文件:https://platform.minimax.io/docs/llms.txt