Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Langfun is *simple and elegant*:

* An intuitive programming model, graspable in 5 minutes;
* Plug-and-play into any Python codebase, making an immediate difference;
* Comprehensive LLMs under a unified API: Gemini, GPT, Claude, Llama3, and more.
* Comprehensive LLMs under a unified API: Gemini, GPT, Claude, MiniMax, Llama3, and more.
* Designed for agile developement: offering intellisense, easy debugging, with minimal overhead;

## Hello, Langfun
Expand Down
5 changes: 5 additions & 0 deletions langfun/core/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@
from langfun.core.llms.deepseek import DeepSeekV3
from langfun.core.llms.deepseek import DeepSeekR1

# MiniMax models.
from langfun.core.llms.minimax import MiniMax
from langfun.core.llms.minimax import MiniMaxM27
from langfun.core.llms.minimax import MiniMaxM27Highspeed

# LLaMA C++ models.
from langfun.core.llms.llama_cpp import LlamaCppRemote

Expand Down
179 changes: 179 additions & 0 deletions langfun/core/llms/minimax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Copyright 2025 The Langfun Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Language models from MiniMax."""

import datetime
import functools
import os
from typing import Annotated, Any, Final

import langfun.core as lf
from langfun.core.llms import openai_compatible
import pyglove as pg


class MiniMaxModelInfo(lf.ModelInfo):
"""MiniMax model info."""

LINKS = dict(
models='https://platform.minimaxi.com/document/Models',
pricing='https://platform.minimaxi.com/document/Price',
)

provider: Final[str] = 'MiniMax' # pylint: disable=invalid-name


SUPPORTED_MODELS = [
MiniMaxModelInfo(
model_id='MiniMax-M2.7',
in_service=True,
model_type='instruction-tuned',
description='MiniMax M2.7 model with 204K context window.',
url='https://platform.minimaxi.com/document/Models',
release_date=datetime.datetime(2025, 3, 1),
input_modalities=lf.ModelInfo.TEXT_INPUT_ONLY,
context_length=lf.ModelInfo.ContextLength(
max_input_tokens=204_800,
max_output_tokens=65_536,
),
pricing=lf.ModelInfo.Pricing(
cost_per_1m_input_tokens=1.1,
cost_per_1m_output_tokens=4.4,
),
rate_limits=None,
),
MiniMaxModelInfo(
model_id='MiniMax-M2.7-highspeed',
in_service=True,
model_type='instruction-tuned',
description='MiniMax M2.7 Highspeed model with 204K context window.',
url='https://platform.minimaxi.com/document/Models',
release_date=datetime.datetime(2025, 3, 1),
input_modalities=lf.ModelInfo.TEXT_INPUT_ONLY,
context_length=lf.ModelInfo.ContextLength(
max_input_tokens=204_800,
max_output_tokens=65_536,
),
pricing=lf.ModelInfo.Pricing(
cost_per_1m_input_tokens=0.55,
cost_per_1m_output_tokens=2.2,
),
rate_limits=None,
),
]

_SUPPORTED_MODELS_BY_ID = {m.model_id: m for m in SUPPORTED_MODELS}


# MiniMax API uses an API format compatible with OpenAI.
# Reference: https://platform.minimaxi.com/document/ChatCompletion%20v2
@lf.use_init_args(['model'])
class MiniMax(openai_compatible.OpenAIChatCompletionAPI):
"""MiniMax models.

**Quick Start:**

```python
import langfun as lf

# Call MiniMax-M2.7 using API key from environment variable
# 'MINIMAX_API_KEY'.
lm = lf.llms.MiniMaxM27()
r = lm('Who are you?')
print(r)
```

**Setting up API key:**

The MiniMax API key can be specified in following ways:

1. At model instantiation:

```python
lm = lf.llms.MiniMaxM27(api_key='MY_API_KEY')
```
2. via environment variable `MINIMAX_API_KEY`.

**References:**

* https://platform.minimaxi.com/document/ChatCompletion%20v2
"""

model: pg.typing.Annotated[
pg.typing.Enum(
pg.MISSING_VALUE, [m.model_id for m in SUPPORTED_MODELS]
),
'The name of the model to use.',
]

api_endpoint: str = 'https://api.minimax.io/v1/chat/completions'

api_key: Annotated[
str | None,
(
'API key. If None, the key will be read from environment variable '
"'MINIMAX_API_KEY'."
),
] = None

@property
def headers(self) -> dict[str, Any]:
api_key = self.api_key or os.environ.get('MINIMAX_API_KEY', None)
if not api_key:
raise ValueError(
'Please specify `api_key` during `__init__` or set environment '
'variable `MINIMAX_API_KEY` with your MiniMax API key.'
)
headers = super().headers
headers.update({
'Authorization': f'Bearer {api_key}',
})
return headers

@functools.cached_property
def model_info(self) -> MiniMaxModelInfo:
return _SUPPORTED_MODELS_BY_ID[self.model]

def _request_args(
self, options: lf.LMSamplingOptions) -> dict[str, Any]:
"""Returns a dict as request arguments."""
args = super()._request_args(options)
# MiniMax requires temperature in (0.0, 1.0], clamp 0.0 to 0.01.
if 'temperature' in args and args['temperature'] == 0.0:
args['temperature'] = 0.01
return args

@classmethod
def dir(cls):
return [m.model_id for m in SUPPORTED_MODELS if m.in_service]


class MiniMaxM27(MiniMax): # pylint: disable=invalid-name
"""MiniMax M2.7 model with 204K context window."""

model = 'MiniMax-M2.7'


class MiniMaxM27Highspeed(MiniMax): # pylint: disable=invalid-name
"""MiniMax M2.7 Highspeed model with 204K context window."""

model = 'MiniMax-M2.7-highspeed'


def _register_minimax_models():
"""Registers MiniMax models."""
for m in SUPPORTED_MODELS:
lf.LanguageModel.register(m.model_id, MiniMax)

_register_minimax_models()
103 changes: 103 additions & 0 deletions langfun/core/llms/minimax_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2025 The Langfun Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
import langfun.core as lf
from langfun.core.llms import minimax


class MiniMaxTest(unittest.TestCase):
"""Tests for MiniMax language model."""

def test_dir(self):
self.assertIn('MiniMax-M2.7', minimax.MiniMax.dir())
self.assertIn('MiniMax-M2.7-highspeed', minimax.MiniMax.dir())

def test_key(self):
old_key = os.environ.pop('MINIMAX_API_KEY', None)
try:
with self.assertRaisesRegex(ValueError, 'Please specify `api_key`'):
_ = minimax.MiniMaxM27().headers
self.assertEqual(
minimax.MiniMaxM27(api_key='test_key').headers,
{
'Content-Type': 'application/json',
'Authorization': 'Bearer test_key',
}
)
finally:
if old_key is not None:
os.environ['MINIMAX_API_KEY'] = old_key

def test_key_from_env(self):
os.environ['MINIMAX_API_KEY'] = 'env_key'
try:
self.assertEqual(
minimax.MiniMaxM27().headers,
{
'Content-Type': 'application/json',
'Authorization': 'Bearer env_key',
}
)
finally:
del os.environ['MINIMAX_API_KEY']

def test_model_id(self):
self.assertEqual(
minimax.MiniMaxM27(api_key='test_key').model_id,
'MiniMax-M2.7',
)

def test_resource_id(self):
self.assertEqual(
minimax.MiniMaxM27(api_key='test_key').resource_id,
'minimax://MiniMax-M2.7',
)

def test_model_info(self):
lm = minimax.MiniMaxM27(api_key='test_key')
self.assertEqual(lm.model_info.provider, 'MiniMax')
self.assertEqual(lm.model_info.model_id, 'MiniMax-M2.7')
self.assertTrue(lm.model_info.in_service)

def test_request(self):
request = minimax.MiniMaxM27(api_key='test_key').request(
lf.UserMessage('hi'), lf.LMSamplingOptions(temperature=0.5),
)
self.assertEqual(request['model'], 'MiniMax-M2.7')
self.assertEqual(request['temperature'], 0.5)

def test_temperature_clamping(self):
request = minimax.MiniMaxM27(api_key='test_key').request(
lf.UserMessage('hi'), lf.LMSamplingOptions(temperature=0.0),
)
self.assertEqual(request['temperature'], 0.01)

def test_highspeed_model(self):
self.assertEqual(
minimax.MiniMaxM27Highspeed(api_key='test_key').model_id,
'MiniMax-M2.7-highspeed',
)

def test_lm_get(self):
self.assertIsInstance(
lf.LanguageModel.get('MiniMax-M2.7'), minimax.MiniMax
)
self.assertIsInstance(
lf.LanguageModel.get('MiniMax-M2.7-highspeed'), minimax.MiniMax
)


if __name__ == '__main__':
unittest.main()