Skip to main content

Basic Conversation

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.minimax.io/anthropic",
    api_key="", # Your MiniMax API Key
)

message = client.messages.create(
    model="MiniMax-M2",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Explain machine learning in simple terms"
        }
    ]
)

print(message.content[0].text)

Streaming Response

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.minimax.io/anthropic",
    api_key="", # Your MiniMax API Key
)

with client.messages.stream(
    model="MiniMax-M2",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Write a poem about spring"
        }
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Tool Calling (Function Calling)

For more details, see MiniMax-M2 Function Calling Guide
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.minimax.io/anthropic",
    api_key="", # Your MiniMax API Key
)

tools = [
    {
        "name": "get_weather",
        "description": "Get weather information for a specified city",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "City name"
                }
            },
            "required": ["city"]
        }
    }
]

message = client.messages.create(
    model="MiniMax-M2",
    max_tokens=1024,
    tools=tools,
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Beijing today?"
        }
    ]
)

print(message.content)

Important Notes

  1. The Anthropic API compatibility interface currently only supports the MiniMax-M2 model
  2. Set ANTHROPIC_BASE_URL to https://api.minimax.io/anthropic when using
  3. Set ANTHROPIC_API_KEY to your MiniMax API Key
  4. The temperature parameter range is (0.0, 1.0], values outside this range will return an error
  5. Some Anthropic parameters (such as thinking, top_k, stop_sequences, service_tier, mcp_servers, context_management, container) will be ignored
  6. Image and document type inputs are not currently supported

Compatible OpenAI API

Basic Conversation

from openai import OpenAI

client = OpenAI(
    base_url="https://api.minimax.io/v1",
    api_key="", # Your MiniMax API Key
)

response = client.chat.completions.create(
    model="MiniMax-M2",
    messages=[
        {
            "role": "user",
            "content": "Explain machine learning in simple terms"
        }
    ]
)

print(response.choices[0].message.content)

Streaming Response

from openai import OpenAI

client = OpenAI(
    base_url="https://api.minimax.io/v1",
    api_key="", # Your MiniMax API Key
)

stream = client.chat.completions.create(
    model="MiniMax-M2",
    messages=[
        {
            "role": "user",
            "content": "Write a poem about spring"
        }
    ],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Tool Calling (Function Calling)

For more details, see MiniMax-M2 Function Calling Guide
from openai import OpenAI

client = OpenAI(
    base_url="https://api.minimax.io/v1",
    api_key="", # Your MiniMax API Key
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather information for a specified city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "City name"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="MiniMax-M2",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Beijing today?"
        }
    ],
    tools=tools
)

print(response.choices[0].message)

Important Notes

  1. Set OPENAI_BASE_URL to https://api.minimax.io/v1 when using
  2. Set OPENAI_API_KEY to your MiniMax API Key
  3. The temperature parameter range is (0.0, 1.0], recommended value: 1.0, values outside this range will return an error
  4. Some OpenAI parameters (such as presence_penalty, frequency_penalty, logit_bias, etc.) will be ignored
  5. Image and audio type inputs are not currently supported
  6. The n parameter only supports value 1
  7. The deprecated function_call is not supported, please use the tools parameter