|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import json |
| 7 | +import unittest |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +import anthropic |
| 11 | +import httpx |
| 12 | +import openai |
| 13 | + |
| 14 | +from llama_cookbook.inference.llm import MINIMAX |
| 15 | + |
| 16 | +OPENAI_CLIENT = openai.OpenAI |
| 17 | +ANTHROPIC_CLIENT = anthropic.Anthropic |
| 18 | + |
| 19 | + |
| 20 | +class MiniMaxTest(unittest.TestCase): |
| 21 | + def test_supported_models_and_endpoint_paths(self) -> None: |
| 22 | + cases = [ |
| 23 | + ( |
| 24 | + "openai", |
| 25 | + "MiniMax-M3", |
| 26 | + None, |
| 27 | + "https://api.minimax.io/v1/chat/completions", |
| 28 | + ), |
| 29 | + ( |
| 30 | + "openai", |
| 31 | + "MiniMax-M2.7", |
| 32 | + "https://api.minimaxi.com/v1", |
| 33 | + "https://api.minimaxi.com/v1/chat/completions", |
| 34 | + ), |
| 35 | + ( |
| 36 | + "anthropic", |
| 37 | + "MiniMax-M3", |
| 38 | + None, |
| 39 | + "https://api.minimax.io/anthropic/v1/messages", |
| 40 | + ), |
| 41 | + ( |
| 42 | + "anthropic", |
| 43 | + "MiniMax-M2.7", |
| 44 | + "https://api.minimaxi.com/anthropic", |
| 45 | + "https://api.minimaxi.com/anthropic/v1/messages", |
| 46 | + ), |
| 47 | + ] |
| 48 | + |
| 49 | + for api_format, model, base_url, expected_url in cases: |
| 50 | + with self.subTest(api_format=api_format, base_url=base_url): |
| 51 | + requests = [] |
| 52 | + |
| 53 | + def handler(request: httpx.Request) -> httpx.Response: |
| 54 | + requests.append(request) |
| 55 | + if api_format == "openai": |
| 56 | + body = { |
| 57 | + "id": "chatcmpl-test", |
| 58 | + "object": "chat.completion", |
| 59 | + "created": 0, |
| 60 | + "model": model, |
| 61 | + "choices": [ |
| 62 | + { |
| 63 | + "index": 0, |
| 64 | + "message": { |
| 65 | + "role": "assistant", |
| 66 | + "content": "Test response", |
| 67 | + }, |
| 68 | + "finish_reason": "stop", |
| 69 | + } |
| 70 | + ], |
| 71 | + } |
| 72 | + else: |
| 73 | + body = { |
| 74 | + "id": "msg_test", |
| 75 | + "type": "message", |
| 76 | + "role": "assistant", |
| 77 | + "content": [{"type": "text", "text": "Test response"}], |
| 78 | + "model": model, |
| 79 | + "stop_reason": "end_turn", |
| 80 | + "stop_sequence": None, |
| 81 | + "usage": {"input_tokens": 1, "output_tokens": 1}, |
| 82 | + } |
| 83 | + return httpx.Response(200, json=body, request=request) |
| 84 | + |
| 85 | + http_client = httpx.Client(transport=httpx.MockTransport(handler)) |
| 86 | + kwargs = {"api_format": api_format} |
| 87 | + if base_url is not None: |
| 88 | + kwargs["base_url"] = base_url |
| 89 | + |
| 90 | + if api_format == "openai": |
| 91 | + client = OPENAI_CLIENT( |
| 92 | + api_key="test-key", |
| 93 | + base_url=base_url or "https://api.minimax.io/v1", |
| 94 | + http_client=http_client, |
| 95 | + ) |
| 96 | + patch_target = "llama_cookbook.inference.llm.openai.OpenAI" |
| 97 | + else: |
| 98 | + client = ANTHROPIC_CLIENT( |
| 99 | + api_key="test-key", |
| 100 | + base_url=base_url or "https://api.minimax.io/anthropic", |
| 101 | + http_client=http_client, |
| 102 | + ) |
| 103 | + patch_target = "llama_cookbook.inference.llm.anthropic.Anthropic" |
| 104 | + |
| 105 | + with patch(patch_target, return_value=client): |
| 106 | + provider = MINIMAX(model, "test-key", **kwargs) |
| 107 | + self.assertEqual(provider.query("Test prompt"), "Test response") |
| 108 | + self.assertEqual( |
| 109 | + provider.valid_models(), ["MiniMax-M3", "MiniMax-M2.7"] |
| 110 | + ) |
| 111 | + |
| 112 | + self.assertEqual(len(requests), 1) |
| 113 | + self.assertEqual(str(requests[0].url), expected_url) |
| 114 | + self.assertEqual(json.loads(requests[0].content)["model"], model) |
| 115 | + client.close() |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + unittest.main() |
0 commit comments