-
Notifications
You must be signed in to change notification settings - Fork 500
feat: add MiniMax as first-class LLM provider #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * Copyright (c) The Magic , Distributed under the software license | ||
| */ | ||
|
|
||
| namespace App\Domain\Provider\Service\ConnectivityTest\LLM; | ||
|
|
||
| use App\Domain\Provider\DTO\Item\ProviderConfigItem; | ||
| use App\Domain\Provider\Service\ConnectivityTest\ConnectResponse; | ||
| use App\Domain\Provider\Service\ConnectivityTest\IProvider; | ||
| use Exception; | ||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\Exception\ClientException; | ||
| use Hyperf\Codec\Json; | ||
|
|
||
| use function Hyperf\Translation\__; | ||
|
|
||
| /** | ||
| * MiniMax LLM connectivity test provider. | ||
| * | ||
| * Unlike the DeepSeek provider which only lists models, this provider | ||
| * validates connectivity by sending a lightweight chat completion request | ||
| * with the specified model version (similar to LLMVolcengineProvider). | ||
| * MiniMax requires temperature in (0.0, 1.0], so we clamp it explicitly. | ||
| */ | ||
| class LLMMiniMaxProvider implements IProvider | ||
| { | ||
| protected string $apiBase = 'https://api.minimax.io/v1'; | ||
|
|
||
| public function connectivityTestByModel(ProviderConfigItem $serviceProviderConfig, string $modelVersion): ConnectResponse | ||
| { | ||
| $connectResponse = new ConnectResponse(); | ||
| $connectResponse->setStatus(true); | ||
| $apiKey = $serviceProviderConfig->getApiKey(); | ||
| if (empty($apiKey)) { | ||
| $connectResponse->setStatus(false); | ||
| $connectResponse->setMessage(__('service_provider.api_key_empty')); | ||
| return $connectResponse; | ||
| } | ||
| try { | ||
| $this->testChatCompletion($apiKey, $modelVersion); | ||
| } catch (Exception $e) { | ||
| $connectResponse->setStatus(false); | ||
| if ($e instanceof ClientException) { | ||
| $connectResponse->setMessage(Json::decode($e->getResponse()->getBody()->getContents())); | ||
| } else { | ||
| $connectResponse->setMessage($e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return $connectResponse; | ||
| } | ||
|
|
||
| /** | ||
| * Test connectivity by sending a minimal chat completion request. | ||
| * | ||
| * This validates both the API key and that the specific model version | ||
| * is accessible, rather than just listing available models. | ||
| * MiniMax requires temperature strictly in (0.0, 1.0]. | ||
| */ | ||
| protected function testChatCompletion(string $apiKey, string $modelVersion): array | ||
| { | ||
| $client = new Client(); | ||
| $payload = [ | ||
| 'model' => $modelVersion, | ||
| 'max_tokens' => 1, | ||
| 'temperature' => 0.01, | ||
| 'messages' => [ | ||
| [ | ||
| 'role' => 'user', | ||
| 'content' => 'Hi', | ||
| ], | ||
| ], | ||
| ]; | ||
|
|
||
| $response = $client->request('POST', $this->apiBase . '/chat/completions', [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $apiKey, | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| 'json' => $payload, | ||
| ]); | ||
|
|
||
| return Json::decode($response->getBody()->getContents()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * Copyright (c) The Magic , Distributed under the software license | ||
| */ | ||
|
|
||
| namespace HyperfTest\Cases\Unit\Provider; | ||
|
|
||
| use App\Domain\Provider\DTO\Item\ProviderConfigItem; | ||
| use App\Domain\Provider\Service\ConnectivityTest\LLM\LLMMiniMaxProvider; | ||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\Handler\MockHandler; | ||
| use GuzzleHttp\HandlerStack; | ||
| use GuzzleHttp\Psr7\Response; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class LLMMiniMaxProviderTest extends TestCase | ||
| { | ||
| public function testApiBaseIsCorrect(): void | ||
| { | ||
| $provider = new LLMMiniMaxProvider(); | ||
| $reflection = new \ReflectionProperty($provider, 'apiBase'); | ||
| $reflection->setAccessible(true); | ||
| $this->assertSame('https://api.minimax.io/v1', $reflection->getValue($provider)); | ||
| } | ||
|
|
||
| public function testConnectivityTestFailsWithEmptyApiKey(): void | ||
| { | ||
| $provider = new LLMMiniMaxProvider(); | ||
| $config = new ProviderConfigItem([]); | ||
|
|
||
| $response = $provider->connectivityTestByModel($config, 'MiniMax-M2.7'); | ||
|
|
||
| $this->assertFalse($response->getStatus()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests call nonexistent
|
||
| } | ||
|
|
||
| public function testConnectivityTestSucceedsWithValidApiKey(): void | ||
| { | ||
| // Create a mock HTTP handler that returns a successful response | ||
| $mock = new MockHandler([ | ||
| new Response(200, [], json_encode([ | ||
| 'object' => 'list', | ||
| 'data' => [ | ||
| ['id' => 'MiniMax-M2.7', 'object' => 'model'], | ||
| ['id' => 'MiniMax-M2.5', 'object' => 'model'], | ||
| ], | ||
| ])), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $mockClient = new Client(['handler' => $handlerStack]); | ||
|
|
||
| // Use a subclass to inject the mock client | ||
| $provider = new class($mockClient) extends LLMMiniMaxProvider { | ||
| private Client $mockClient; | ||
|
|
||
| public function __construct(Client $mockClient) | ||
| { | ||
| $this->mockClient = $mockClient; | ||
| } | ||
|
|
||
| protected function fetchModels(string $apiKey): array | ||
| { | ||
| $response = $this->mockClient->request('GET', $this->apiBase . '/models', [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $apiKey, | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| ]); | ||
|
|
||
| return json_decode($response->getBody()->getContents(), true); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests mock nonexistent method, bypassing HTTP mocks entirelyHigh Severity The test subclasses override a Additional Locations (2) |
||
| }; | ||
|
|
||
| $config = new ProviderConfigItem(['api_key' => 'test-valid-key']); | ||
|
|
||
| $response = $provider->connectivityTestByModel($config, 'MiniMax-M2.7'); | ||
|
|
||
| $this->assertTrue($response->getStatus()); | ||
| } | ||
|
|
||
| public function testConnectivityTestFailsWithInvalidApiKey(): void | ||
| { | ||
| // Create a mock HTTP handler that returns an authentication error | ||
| $mock = new MockHandler([ | ||
| new \GuzzleHttp\Exception\ClientException( | ||
| 'Client error', | ||
| new \GuzzleHttp\Psr7\Request('GET', 'https://api.minimax.io/v1/models'), | ||
| new Response(401, [], json_encode([ | ||
| 'error' => [ | ||
| 'message' => 'Invalid API key', | ||
| 'type' => 'authentication_error', | ||
| ], | ||
| ])) | ||
| ), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $mockClient = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $provider = new class($mockClient) extends LLMMiniMaxProvider { | ||
| private Client $mockClient; | ||
|
|
||
| public function __construct(Client $mockClient) | ||
| { | ||
| $this->mockClient = $mockClient; | ||
| } | ||
|
|
||
| protected function fetchModels(string $apiKey): array | ||
| { | ||
| $response = $this->mockClient->request('GET', $this->apiBase . '/models', [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $apiKey, | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| ]); | ||
|
|
||
| return json_decode($response->getBody()->getContents(), true); | ||
| } | ||
| }; | ||
|
|
||
| $config = new ProviderConfigItem(['api_key' => 'invalid-key']); | ||
|
|
||
| $response = $provider->connectivityTestByModel($config, 'MiniMax-M2.7'); | ||
|
|
||
| $this->assertFalse($response->getStatus()); | ||
| } | ||
|
|
||
| public function testConnectivityTestFailsOnNetworkError(): void | ||
| { | ||
| // Create a mock HTTP handler that throws a network exception | ||
| $mock = new MockHandler([ | ||
| new \GuzzleHttp\Exception\ConnectException( | ||
| 'Connection refused', | ||
| new \GuzzleHttp\Psr7\Request('GET', 'https://api.minimax.io/v1/models') | ||
| ), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $mockClient = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $provider = new class($mockClient) extends LLMMiniMaxProvider { | ||
| private Client $mockClient; | ||
|
|
||
| public function __construct(Client $mockClient) | ||
| { | ||
| $this->mockClient = $mockClient; | ||
| } | ||
|
|
||
| protected function fetchModels(string $apiKey): array | ||
| { | ||
| $response = $this->mockClient->request('GET', $this->apiBase . '/models', [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $apiKey, | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| ]); | ||
|
|
||
| return json_decode($response->getBody()->getContents(), true); | ||
| } | ||
| }; | ||
|
|
||
| $config = new ProviderConfigItem(['api_key' => 'test-key']); | ||
|
|
||
| $response = $provider->connectivityTestByModel($config, 'MiniMax-M2.7'); | ||
|
|
||
| $this->assertFalse($response->getStatus()); | ||
| $this->assertNotEmpty($response->getMessage()); | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.