Skip to content

armgit / LEANN

Public Imported from GitHub
0

[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.

feat: add MiniMax provider support (#282)

ximi3cab4ef40dcf
diff --git a/packages/leann-core/src/leann/chat.py b/packages/leann-core/src/leann/chat.py
index 0ceb8d5..0206a0e 100644
--- a/packages/leann-core/src/leann/chat.py
+++ b/packages/leann-core/src/leann/chat.py
@@ -15,6 +15,8 @@ import torch
from .settings import (
resolve_anthropic_api_key,
resolve_anthropic_base_url,
+ resolve_minimax_api_key,
+ resolve_minimax_base_url,
resolve_ollama_host,
resolve_openai_api_key,
resolve_openai_base_url,
@@ -940,6 +942,74 @@ class AnthropicChat(LLMInterface):
return f"Error: Could not get a response from Anthropic. Details: {e}"
+class MiniMaxChat(LLMInterface):
+ """LLM interface for MiniMax models via the OpenAI-compatible API.
+
+ Supported models:
+ - MiniMax-M2.5 (default): Peak Performance. Ultimate Value.
+ - MiniMax-M2.5-highspeed: Same performance, faster and more agile.
+
+ Both models support a 204,800-token context window.
+ """
+
+ def __init__(
+ self,
+ model: str = "MiniMax-M2.5",
+ api_key: Optional[str] = None,
+ base_url: Optional[str] = None,
+ ):
+ self.model = model
+ self.base_url = resolve_minimax_base_url(base_url)
+ self.api_key = resolve_minimax_api_key(api_key)
+
+ if not self.api_key:
+ raise ValueError(
+ "MiniMax API key is required. Set MINIMAX_API_KEY environment variable or pass api_key parameter."
+ )
+
+ logger.info(
+ "Initializing MiniMax Chat with model='%s' and base_url='%s'",
+ model,
+ self.base_url,
+ )
+
+ try:
+ import openai
+
+ self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url)
+ except ImportError:
+ raise ImportError(
+ "The 'openai' library is required for MiniMax models. Please install it with 'pip install openai'."
+ )
+
+ def ask(self, prompt: str, **kwargs) -> str:
+ # Default parameters for MiniMax (OpenAI-compatible)
+ params = {
+ "model": self.model,
+ "messages": [{"role": "user", "content": prompt}],
+ "temperature": kwargs.get("temperature", 0.7),
+ "max_tokens": kwargs.get("max_tokens", 1000),
+ }
+
+ # Add optional parameters
+ if "top_p" in kwargs:
+ params["top_p"] = kwargs["top_p"]
+
+ logger.info(f"Sending request to MiniMax with model {self.model}")
+
+ try:
+ response = cast(Any, self.client.chat.completions).create(**params)
+ print(
+ f"Total tokens = {response.usage.total_tokens}, prompt tokens = {response.usage.prompt_tokens}, completion tokens = {response.usage.completion_tokens}"
+ )
+ if response.choices[0].finish_reason == "length":
+ print("The query is exceeding the maximum allowed number of tokens")
+ return response.choices[0].message.content.strip()
+ except Exception as e:
+ logger.error(f"Error communicating with MiniMax: {e}")
+ return f"Error: Could not get a response from MiniMax. Details: {e}"
+
+
class SimulatedChat(LLMInterface):
"""A simple simulated chat for testing and development."""
@@ -998,6 +1068,12 @@ def get_llm(llm_config: Optional[dict[str, Any]] = None) -> LLMInterface:
api_key=llm_config.get("api_key"),
base_url=llm_config.get("base_url"),
)
+ elif llm_type == "minimax":
+ return MiniMaxChat(
+ model=model or "MiniMax-M2.5",
+ api_key=llm_config.get("api_key"),
+ base_url=llm_config.get("base_url"),
+ )
elif llm_type == "simulated":
return SimulatedChat()
else:
diff --git a/packages/leann-core/src/leann/cli.py b/packages/leann-core/src/leann/cli.py
index a650750..faa18dc 100644
--- a/packages/leann-core/src/leann/cli.py
+++ b/packages/leann-core/src/leann/cli.py
@@ -22,6 +22,8 @@ from .interactive_utils import create_cli_session
from .registry import register_project_directory
from .settings import (
resolve_anthropic_base_url,
+ resolve_minimax_api_key,
+ resolve_minimax_base_url,
resolve_ollama_host,
resolve_openai_api_key,
resolve_openai_base_url,
@@ -493,7 +495,7 @@ Examples:
"--llm",
type=str,
default="ollama",
- choices=["simulated", "ollama", "hf", "openai", "anthropic"],
+ choices=["simulated", "ollama", "hf", "openai", "anthropic", "minimax"],
help="LLM provider (default: ollama)",
)
ask_parser.add_argument(
@@ -556,7 +558,7 @@ Examples:
"--llm",
type=str,
default="ollama",
- choices=["simulated", "ollama", "hf", "openai", "anthropic"],
+ choices=["simulated", "ollama", "hf", "openai", "anthropic", "minimax"],
help="LLM provider (default: ollama)",
)
react_parser.add_argument(
@@ -2712,6 +2714,11 @@ Examples:
llm_config["base_url"] = resolve_anthropic_base_url(args.api_base)
if args.api_key:
llm_config["api_key"] = args.api_key
+ elif args.llm == "minimax":
+ llm_config["base_url"] = resolve_minimax_base_url(args.api_base)
+ resolved_api_key = resolve_minimax_api_key(args.api_key)
+ if resolved_api_key:
+ llm_config["api_key"] = resolved_api_key
chat = LeannChat(index_path=index_path, llm_config=llm_config)
@@ -2804,6 +2811,11 @@ Examples:
llm_config["base_url"] = resolve_anthropic_base_url(args.api_base)
if args.api_key:
llm_config["api_key"] = args.api_key
+ elif args.llm == "minimax":
+ llm_config["base_url"] = resolve_minimax_base_url(args.api_base)
+ resolved_api_key = resolve_minimax_api_key(args.api_key)
+ if resolved_api_key:
+ llm_config["api_key"] = resolved_api_key
from .react_agent import create_react_agent
diff --git a/packages/leann-core/src/leann/settings.py b/packages/leann-core/src/leann/settings.py
index 9a8aef1..e3b8e9e 100644
--- a/packages/leann-core/src/leann/settings.py
+++ b/packages/leann-core/src/leann/settings.py
@@ -10,6 +10,7 @@ from typing import Any
_DEFAULT_OLLAMA_HOST = "http://localhost:11434"
_DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1"
_DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com"
+_DEFAULT_MINIMAX_BASE_URL = "https://api.minimax.io/v1"
def _clean_url(value: str) -> str:
@@ -88,6 +89,31 @@ def resolve_anthropic_api_key(explicit: str | None = None) -> str | None:
return os.getenv("ANTHROPIC_API_KEY")
+def resolve_minimax_base_url(explicit: str | None = None) -> str:
+ """Resolve the base URL for MiniMax-compatible services."""
+
+ candidates = (
+ explicit,
+ os.getenv("LEANN_MINIMAX_BASE_URL"),
+ os.getenv("MINIMAX_BASE_URL"),
+ )
+
+ for candidate in candidates:
+ if candidate:
+ return _clean_url(candidate)
+
+ return _clean_url(_DEFAULT_MINIMAX_BASE_URL)
+
+
+def resolve_minimax_api_key(explicit: str | None = None) -> str | None:
+ """Resolve the API key for MiniMax services."""
+
+ if explicit:
+ return explicit
+
+ return os.getenv("MINIMAX_API_KEY")
+
+
def encode_provider_options(options: dict[str, Any] | None) -> str | None:
"""Serialize provider options for child processes."""
diff --git a/tests/test_minimax_provider.py b/tests/test_minimax_provider.py
new file mode 100644
index 0000000..7a9eb76
--- /dev/null
+++ b/tests/test_minimax_provider.py
@@ -0,0 +1,231 @@
+"""
+Tests for MiniMax provider integration.
+
+These tests validate MiniMax provider settings, chat class, and factory integration.
+We import from leann.settings and leann.chat directly to avoid triggering
+the full leann.__init__ import chain which requires C++ backend builds.
+"""
+
+import os
+import sys
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+# Add the leann-core source to sys.path so we can import submodules
+# without triggering __init__.py's backend imports.
+_LEANN_SRC = os.path.join(os.path.dirname(__file__), "..", "packages", "leann-core", "src")
+if _LEANN_SRC not in sys.path:
+ sys.path.insert(0, os.path.abspath(_LEANN_SRC))
+
+# Prevent leann.__init__ from running its heavy imports by pre-registering
+# a lightweight stub in sys.modules (if not already present).
+if "leann" not in sys.modules:
+ import types
+
+ _stub = types.ModuleType("leann")
+ _stub.__path__ = [os.path.join(os.path.abspath(_LEANN_SRC), "leann")]
+ sys.modules["leann"] = _stub
+
+# Now we can safely import the modules we actually test.
+from leann.settings import ( # noqa: E402
+ resolve_minimax_api_key,
+ resolve_minimax_base_url,
+)
+
+
+class TestMiniMaxSettings:
+ """Test MiniMax settings resolver functions."""
+
+ def test_resolve_minimax_api_key_explicit(self):
+ assert resolve_minimax_api_key("test-key") == "test-key"
+
+ def test_resolve_minimax_api_key_from_env(self):
+ with patch.dict(os.environ, {"MINIMAX_API_KEY": "env-key"}):
+ assert resolve_minimax_api_key() == "env-key"
+
+ def test_resolve_minimax_api_key_none(self):
+ with patch.dict(os.environ, {}, clear=True):
+ assert resolve_minimax_api_key() is None
+
+ def test_resolve_minimax_base_url_default(self):
+ with patch.dict(os.environ, {}, clear=True):
+ assert resolve_minimax_base_url() == "https://api.minimax.io/v1"
+
+ def test_resolve_minimax_base_url_explicit(self):
+ assert resolve_minimax_base_url("https://custom.url/v1") == "https://custom.url/v1"
+
+ def test_resolve_minimax_base_url_from_env(self):
+ with patch.dict(os.environ, {"MINIMAX_BASE_URL": "https://env.url/v1"}):
+ assert resolve_minimax_base_url() == "https://env.url/v1"
+
+ def test_resolve_minimax_base_url_leann_env(self):
+ with patch.dict(os.environ, {"LEANN_MINIMAX_BASE_URL": "https://leann.url/v1"}):
+ assert resolve_minimax_base_url() == "https://leann.url/v1"
+
+ def test_resolve_minimax_base_url_strips_trailing_slash(self):
+ assert resolve_minimax_base_url("https://api.minimax.io/v1/") == "https://api.minimax.io/v1"
+
+
+class TestMiniMaxChat:
+ """Test MiniMaxChat class."""
+
+ def test_init_requires_api_key(self):
+ from leann.chat import MiniMaxChat
+
+ with patch.dict(os.environ, {}, clear=True):
+ with pytest.raises(ValueError, match="MiniMax API key is required"):
+ MiniMaxChat(api_key=None)
+
+ @patch("openai.OpenAI")
+ def test_init_with_api_key(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ chat = MiniMaxChat(api_key="test-key")
+ assert chat.model == "MiniMax-M2.5"
+ assert chat.api_key == "test-key"
+ assert chat.base_url == "https://api.minimax.io/v1"
+ mock_openai_cls.assert_called_once_with(
+ api_key="test-key", base_url="https://api.minimax.io/v1"
+ )
+
+ @patch("openai.OpenAI")
+ def test_init_custom_model(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ chat = MiniMaxChat(model="MiniMax-M2.5-highspeed", api_key="test-key")
+ assert chat.model == "MiniMax-M2.5-highspeed"
+
+ @patch("openai.OpenAI")
+ def test_init_custom_base_url(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ chat = MiniMaxChat(api_key="test-key", base_url="https://api.minimaxi.com/v1")
+ assert chat.base_url == "https://api.minimaxi.com/v1"
+
+ @patch("openai.OpenAI")
+ def test_ask_returns_response(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ # Mock the response chain
+ mock_client = MagicMock()
+ mock_openai_cls.return_value = mock_client
+
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock()]
+ mock_response.choices[0].message.content = "Hello from MiniMax!"
+ mock_response.choices[0].finish_reason = "stop"
+ mock_response.usage.total_tokens = 100
+ mock_response.usage.prompt_tokens = 50
+ mock_response.usage.completion_tokens = 50
+ mock_client.chat.completions.create.return_value = mock_response
+
+ chat = MiniMaxChat(api_key="test-key")
+ result = chat.ask("Hello")
+
+ assert result == "Hello from MiniMax!"
+ mock_client.chat.completions.create.assert_called_once()
+
+ @patch("openai.OpenAI")
+ def test_ask_with_kwargs(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ mock_client = MagicMock()
+ mock_openai_cls.return_value = mock_client
+
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock()]
+ mock_response.choices[0].message.content = "Response"
+ mock_response.choices[0].finish_reason = "stop"
+ mock_response.usage.total_tokens = 50
+ mock_response.usage.prompt_tokens = 25
+ mock_response.usage.completion_tokens = 25
+ mock_client.chat.completions.create.return_value = mock_response
+
+ chat = MiniMaxChat(api_key="test-key")
+ chat.ask("Hello", temperature=0.5, max_tokens=500, top_p=0.9)
+
+ call_kwargs = mock_client.chat.completions.create.call_args[1]
+ assert call_kwargs["temperature"] == 0.5
+ assert call_kwargs["max_tokens"] == 500
+ assert call_kwargs["top_p"] == 0.9
+
+ @patch("openai.OpenAI")
+ def test_ask_handles_error(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat
+
+ mock_client = MagicMock()
+ mock_openai_cls.return_value = mock_client
+ mock_client.chat.completions.create.side_effect = Exception("API error")
+
+ chat = MiniMaxChat(api_key="test-key")
+ result = chat.ask("Hello")
+
+ assert "Error" in result
+ assert "MiniMax" in result
+
+
+class TestGetLLMFactory:
+ """Test get_llm factory function with minimax type."""
+
+ @patch("openai.OpenAI")
+ def test_get_llm_minimax(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat, get_llm
+
+ llm = get_llm({"type": "minimax", "api_key": "test-key"})
+ assert isinstance(llm, MiniMaxChat)
+ assert llm.model == "MiniMax-M2.5"
+
+ @patch("openai.OpenAI")
+ def test_get_llm_minimax_custom_model(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat, get_llm
+
+ llm = get_llm({"type": "minimax", "model": "MiniMax-M2.5-highspeed", "api_key": "test-key"})
+ assert isinstance(llm, MiniMaxChat)
+ assert llm.model == "MiniMax-M2.5-highspeed"
+
+ @patch("openai.OpenAI")
+ def test_get_llm_minimax_custom_base_url(self, mock_openai_cls):
+ from leann.chat import MiniMaxChat, get_llm
+
+ llm = get_llm(
+ {
+ "type": "minimax",
+ "api_key": "test-key",
+ "base_url": "https://api.minimaxi.com/v1",
+ }
+ )
+ assert isinstance(llm, MiniMaxChat)
+ assert llm.base_url == "https://api.minimaxi.com/v1"
+
+
+ not os.getenv("MINIMAX_API_KEY"),
+ reason="MINIMAX_API_KEY not set; skipping live API test",
+)
+class TestMiniMaxLiveAPI:
+ """Live API tests for MiniMax provider (requires MINIMAX_API_KEY)."""
+
+ def test_minimax_m25_live(self):
+ from leann.chat import MiniMaxChat
+
+ chat = MiniMaxChat(model="MiniMax-M2.5")
+ response = chat.ask("Say hello in one word.", max_tokens=10)
+ assert isinstance(response, str)
+ assert len(response) > 0
+
+ def test_minimax_m25_highspeed_live(self):
+ from leann.chat import MiniMaxChat
+
+ chat = MiniMaxChat(model="MiniMax-M2.5-highspeed")
+ response = chat.ask("Say hello in one word.", max_tokens=10)
+ assert isinstance(response, str)
+ assert len(response) > 0
+
+ def test_minimax_via_get_llm_live(self):
+ from leann.chat import get_llm
+
+ llm = get_llm({"type": "minimax"})
+ response = llm.ask("What is 1+1? Reply with just the number.", max_tokens=10)
+ assert isinstance(response, str)
+ assert len(response) > 0