-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMaxClient.php
More file actions
194 lines (169 loc) · 6.97 KB
/
Copy pathMiniMaxClient.php
File metadata and controls
194 lines (169 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\MiniMax;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\JsonBodyEncodingTrait;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\ModelClientInterface;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\RawResultInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class MiniMaxClient implements ModelClientInterface
{
use JsonBodyEncodingTrait;
public function __construct(
private readonly HttpClientInterface $httpClient,
#[\SensitiveParameter] private readonly string $apiKey,
private readonly string $endpoint = 'https://api.minimax.io/v1',
) {
}
public function supports(Model $model): bool
{
return $model instanceof MiniMax;
}
public function request(Model $model, array|string $payload, array $options = []): RawResultInterface
{
return match (true) {
$model->supports(Capability::INPUT_MESSAGES) => $this->doTextGeneration($model, $payload, $options),
$model->supports(Capability::TEXT_TO_SPEECH) => $this->doSpeechGeneration($model, $payload, $options),
$model->supports(Capability::TEXT_TO_IMAGE),
$model->supports(Capability::IMAGE_TO_IMAGE) => $this->doImageGeneration($model, $payload, $options),
$model->supports(Capability::MUSIC) => $this->doMusicGeneration($model, $payload, $options),
$model->supports(Capability::TEXT_TO_VIDEO),
$model->supports(Capability::IMAGE_TO_VIDEO),
$model->supports(Capability::VIDEO_WITH_SUBJECT) => $this->doVideoGeneration($model, $payload, $options),
default => throw new InvalidArgumentException(\sprintf('The "%s" model is not supported.', $model->getName())),
};
}
/**
* @param array<string, mixed>|string $payload
* @param array<string, mixed> $options
*/
private function doTextGeneration(Model $model, array|string $payload, array $options): RawResultInterface
{
if (!\is_array($payload)) {
throw new InvalidArgumentException(\sprintf('The payload is not an array, given "%s".', get_debug_type($payload)));
}
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/chat/completions', $this->endpoint), [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $this->encodeJsonBody([
...$options,
'model' => $model->getName(),
'messages' => $payload['messages'],
]),
]));
}
/**
* @param array<string, mixed>|string $payload
* @param array<string, mixed> $options
*/
private function doSpeechGeneration(Model $model, array|string $payload, array $options): RawResultInterface
{
$text = $this->extractText($payload);
$async = (bool) ($options['async'] ?? false);
unset($options['async']);
$json = $options;
$json['model'] = $model->getName();
$json['text'] = $text;
if (!$async && !\array_key_exists('output_format', $json)) {
$json['output_format'] = 'hex';
}
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/%s', $this->endpoint, $async ? 't2a_async_v2' : 't2a_v2'), [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $this->encodeJsonBody($json),
]));
}
/**
* @param array<string, mixed>|string $payload
* @param array<string, mixed> $options
*/
private function doImageGeneration(Model $model, array|string $payload, array $options): RawResultInterface
{
$json = $options;
$json['model'] = $model->getName();
$json['prompt'] = $this->extractText($payload);
if (!\array_key_exists('response_format', $json)) {
$json['response_format'] = 'base64';
}
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/image_generation', $this->endpoint), [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $this->encodeJsonBody($json),
]));
}
/**
* @param array<string, mixed>|string $payload
* @param array<string, mixed> $options
*/
private function doMusicGeneration(Model $model, array|string $payload, array $options): RawResultInterface
{
if (!\array_key_exists('lyrics', $options)) {
throw new InvalidArgumentException('The "lyrics" option is required when generating music.');
}
$json = $options;
$json['model'] = $model->getName();
$json['prompt'] = $this->extractText($payload);
if (!\array_key_exists('output_format', $json)) {
$json['output_format'] = 'hex';
}
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/music_generation', $this->endpoint), [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $this->encodeJsonBody($json),
]));
}
/**
* @param array<string, mixed>|string $payload
* @param array<string, mixed> $options
*/
private function doVideoGeneration(Model $model, array|string $payload, array $options): RawResultInterface
{
$json = $options;
$json['model'] = $model->getName();
$json['prompt'] = $this->extractText($payload);
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/video_generation', $this->endpoint), [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $this->encodeJsonBody($json),
]));
}
/**
* Extracts the plain text from either a raw string payload or the array shape produced by the
* Text content normalizer (`['type' => 'text', 'text' => '...']`).
*
* @param array<string, mixed>|string $payload
*/
private function extractText(array|string $payload): string
{
if (\is_string($payload)) {
return $payload;
}
if (\array_key_exists('text', $payload) && \is_string($payload['text'])) {
return $payload['text'];
}
throw new InvalidArgumentException('The payload must be a string or contain a "text" key.');
}
}