|
| 1 | +import unittest |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from memos.configs.llm import MinimaxLLMConfig |
| 7 | +from memos.llms.minimax import MinimaxLLM |
| 8 | + |
| 9 | + |
| 10 | +class TestMinimaxLLM(unittest.TestCase): |
| 11 | + def test_minimax_llm_generate_with_and_without_think_prefix(self): |
| 12 | + """Test MinimaxLLM generate method with and without <think> tag removal.""" |
| 13 | + |
| 14 | + # Simulated full content including <think> tag |
| 15 | + full_content = "Hello from MiniMax!" |
| 16 | + reasoning_content = "Thinking in progress..." |
| 17 | + |
| 18 | + # Mock response object |
| 19 | + mock_response = MagicMock() |
| 20 | + mock_response.model_dump_json.return_value = '{"mock": "true"}' |
| 21 | + mock_response.choices[0].message.content = full_content |
| 22 | + mock_response.choices[0].message.reasoning_content = reasoning_content |
| 23 | + |
| 24 | + # Config with think prefix preserved |
| 25 | + config_with_think = MinimaxLLMConfig.model_validate( |
| 26 | + { |
| 27 | + "model_name_or_path": "MiniMax-M2.7", |
| 28 | + "temperature": 0.7, |
| 29 | + "max_tokens": 512, |
| 30 | + "top_p": 0.9, |
| 31 | + "api_key": "sk-test", |
| 32 | + "api_base": "https://api.minimax.io/v1", |
| 33 | + "remove_think_prefix": False, |
| 34 | + } |
| 35 | + ) |
| 36 | + llm_with_think = MinimaxLLM(config_with_think) |
| 37 | + llm_with_think.client.chat.completions.create = MagicMock(return_value=mock_response) |
| 38 | + |
| 39 | + output_with_think = llm_with_think.generate([{"role": "user", "content": "Hello"}]) |
| 40 | + self.assertEqual(output_with_think, f"<think>{reasoning_content}</think>{full_content}") |
| 41 | + |
| 42 | + # Config with think tag removed |
| 43 | + config_without_think = config_with_think.model_copy(update={"remove_think_prefix": True}) |
| 44 | + llm_without_think = MinimaxLLM(config_without_think) |
| 45 | + llm_without_think.client.chat.completions.create = MagicMock(return_value=mock_response) |
| 46 | + |
| 47 | + output_without_think = llm_without_think.generate([{"role": "user", "content": "Hello"}]) |
| 48 | + self.assertEqual(output_without_think, full_content) |
| 49 | + |
| 50 | + def test_minimax_llm_generate_stream(self): |
| 51 | + """Test MinimaxLLM generate_stream with content chunks.""" |
| 52 | + |
| 53 | + def make_chunk(delta_dict): |
| 54 | + # Create a simulated stream chunk with delta fields |
| 55 | + delta = SimpleNamespace(**delta_dict) |
| 56 | + choice = SimpleNamespace(delta=delta) |
| 57 | + return SimpleNamespace(choices=[choice]) |
| 58 | + |
| 59 | + # Simulate chunks: content only (MiniMax standard response) |
| 60 | + mock_stream_chunks = [ |
| 61 | + make_chunk({"content": "Hello"}), |
| 62 | + make_chunk({"content": ", "}), |
| 63 | + make_chunk({"content": "MiniMax!"}), |
| 64 | + ] |
| 65 | + |
| 66 | + mock_chat_completions_create = MagicMock(return_value=iter(mock_stream_chunks)) |
| 67 | + |
| 68 | + config = MinimaxLLMConfig.model_validate( |
| 69 | + { |
| 70 | + "model_name_or_path": "MiniMax-M2.7", |
| 71 | + "temperature": 0.7, |
| 72 | + "max_tokens": 512, |
| 73 | + "top_p": 0.9, |
| 74 | + "api_key": "sk-test", |
| 75 | + "api_base": "https://api.minimax.io/v1", |
| 76 | + "remove_think_prefix": False, |
| 77 | + } |
| 78 | + ) |
| 79 | + llm = MinimaxLLM(config) |
| 80 | + llm.client.chat.completions.create = mock_chat_completions_create |
| 81 | + |
| 82 | + messages = [{"role": "user", "content": "Say hello"}] |
| 83 | + streamed = list(llm.generate_stream(messages)) |
| 84 | + full_output = "".join(streamed) |
| 85 | + |
| 86 | + self.assertEqual(full_output, "Hello, MiniMax!") |
| 87 | + |
| 88 | + def test_minimax_llm_config_defaults(self): |
| 89 | + """Test MinimaxLLMConfig default values.""" |
| 90 | + config = MinimaxLLMConfig.model_validate( |
| 91 | + { |
| 92 | + "model_name_or_path": "MiniMax-M2.7", |
| 93 | + "api_key": "sk-test", |
| 94 | + } |
| 95 | + ) |
| 96 | + self.assertEqual(config.api_base, "https://api.minimax.io/v1") |
| 97 | + self.assertEqual(config.temperature, 0.7) |
| 98 | + self.assertEqual(config.max_tokens, 8192) |
| 99 | + |
| 100 | + def test_minimax_llm_config_custom_values(self): |
| 101 | + """Test MinimaxLLMConfig with custom values.""" |
| 102 | + config = MinimaxLLMConfig.model_validate( |
| 103 | + { |
| 104 | + "model_name_or_path": "MiniMax-M2.7-highspeed", |
| 105 | + "api_key": "sk-test", |
| 106 | + "api_base": "https://custom.api.minimax.io/v1", |
| 107 | + "temperature": 0.5, |
| 108 | + "max_tokens": 2048, |
| 109 | + } |
| 110 | + ) |
| 111 | + self.assertEqual(config.model_name_or_path, "MiniMax-M2.7-highspeed") |
| 112 | + self.assertEqual(config.api_base, "https://custom.api.minimax.io/v1") |
| 113 | + self.assertEqual(config.temperature, 0.5) |
| 114 | + self.assertEqual(config.max_tokens, 2048) |
0 commit comments