Skip to content

Commit b594d4f

Browse files
author
qinjiu
committed
feat: minimax provider
1 parent a9da06b commit b594d4f

24 files changed

+1315
-24
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
---
2+
title: MiniMax
3+
description: Learn how to use MiniMax's models with the AI SDK.
4+
---
5+
6+
# MiniMax Provider
7+
8+
The [MiniMax](https://www.minimaxi.com) provider offers access to powerful language models through the MiniMax API, including their latest [MiniMax-M2 model](https://platform.minimaxi.com/docs/guides/text-generation).
9+
10+
API keys can be obtained from the [MiniMax Platform](https://platform.minimaxi.com).
11+
12+
## Setup
13+
14+
The MiniMax provider is available via the `@ai-sdk/minimax` module. You can install it with:
15+
16+
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
17+
<Tab>
18+
<Snippet text="pnpm add @ai-sdk/minimax" dark />
19+
</Tab>
20+
<Tab>
21+
<Snippet text="npm install @ai-sdk/minimax" dark />
22+
</Tab>
23+
<Tab>
24+
<Snippet text="yarn add @ai-sdk/minimax" dark />
25+
</Tab>
26+
27+
<Tab>
28+
<Snippet text="bun add @ai-sdk/minimax" dark />
29+
</Tab>
30+
</Tabs>
31+
32+
## Provider Instance
33+
34+
The MiniMax provider supports two API compatibility modes:
35+
36+
### Anthropic-Compatible API (Default)
37+
38+
You can import the default provider instance `minimax` from `@ai-sdk/minimax`:
39+
40+
```ts
41+
import { minimax } from '@ai-sdk/minimax';
42+
```
43+
44+
Or explicitly use the Anthropic-compatible instance:
45+
46+
```ts
47+
import { minimaxAnthropic } from '@ai-sdk/minimax';
48+
```
49+
50+
### OpenAI-Compatible API
51+
52+
For OpenAI-compatible API format:
53+
54+
```ts
55+
import { minimaxOpenAI } from '@ai-sdk/minimax';
56+
```
57+
58+
## Custom Configuration
59+
60+
For custom configuration, you can use the `createMinimax` (Anthropic-compatible) or `createMinimaxOpenAI` (OpenAI-compatible) functions:
61+
62+
### Anthropic-Compatible Configuration (Default)
63+
64+
```ts
65+
import { createMinimax } from '@ai-sdk/minimax';
66+
67+
const minimax = createMinimax({
68+
apiKey: process.env.MINIMAX_API_KEY ?? '',
69+
baseURL: 'https://api.minimax.io/anthropic/v1', // optional, this is the default
70+
});
71+
```
72+
73+
### OpenAI-Compatible Configuration
74+
75+
```ts
76+
import { createMinimaxOpenAI } from '@ai-sdk/minimax';
77+
78+
const minimaxOpenAI = createMinimaxOpenAI({
79+
apiKey: process.env.MINIMAX_API_KEY ?? '',
80+
baseURL: 'https://api.minimax.io/v1', // optional, this is the default
81+
});
82+
```
83+
84+
### Configuration Options
85+
86+
You can use the following optional settings to customize the MiniMax provider instance:
87+
88+
- **baseURL** _string_
89+
90+
Use a different URL prefix for API calls.
91+
- Anthropic-compatible default: `https://api.minimax.io/anthropic/v1`
92+
- OpenAI-compatible default: `https://api.minimax.io/v1`
93+
94+
- **apiKey** _string_
95+
96+
API key that is being sent using the `Authorization` header. It defaults to
97+
the `MINIMAX_API_KEY` environment variable.
98+
99+
- **headers** _Record&lt;string,string&gt;_
100+
101+
Custom headers to include in the requests.
102+
103+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_
104+
105+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
106+
107+
## Language Models
108+
109+
You can create language models using a provider instance:
110+
111+
### Anthropic-Compatible (Default)
112+
113+
```ts
114+
import { minimax } from '@ai-sdk/minimax';
115+
import { generateText } from 'ai';
116+
117+
const { text } = await generateText({
118+
model: minimax('MiniMax-M2'),
119+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
120+
});
121+
```
122+
123+
### OpenAI-Compatible
124+
125+
```ts
126+
import { minimaxOpenAI } from '@ai-sdk/minimax';
127+
import { generateText } from 'ai';
128+
129+
const { text } = await generateText({
130+
model: minimaxOpenAI('MiniMax-M2'),
131+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
132+
});
133+
```
134+
135+
You can also use the `.chat()` or `.languageModel()` factory methods:
136+
137+
```ts
138+
// Default (Anthropic-compatible):
139+
const model = minimax.chat('MiniMax-M2');
140+
// or
141+
const model = minimax.languageModel('MiniMax-M2');
142+
143+
// For OpenAI-compatible:
144+
const openaiModel = minimaxOpenAI.chat('MiniMax-M2');
145+
// or
146+
const openaiModel = minimaxOpenAI.languageModel('MiniMax-M2');
147+
```
148+
149+
MiniMax language models can be used in the `streamText` function
150+
(see [AI SDK Core](/docs/ai-sdk-core)).
151+
152+
## API Compatibility
153+
154+
MiniMax provides two API formats. Both are included in this package:
155+
156+
### When to Use Each Format
157+
158+
- **Anthropic-Compatible** (`minimax`, default): Provides better support for advanced features and is recommended for new projects.
159+
- **OpenAI-Compatible** (`minimaxOpenAI`): Use this if you're migrating from OpenAI or prefer the OpenAI API format.
160+
161+
### Key Differences
162+
163+
The main difference is the API request/response format:
164+
165+
- **Anthropic format** (default): Uses Anthropic Messages API format with `anthropic-version` header
166+
- **OpenAI format**: Uses standard OpenAI chat completion format
167+
168+
Both formats access the same MiniMax models with the same capabilities.
169+
170+
## Model Capabilities
171+
172+
| Model | Text Generation | Object Generation | Image Input | Tool Usage | Tool Streaming |
173+
| ------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
174+
| `MiniMax-M2` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
175+
176+
<Note>
177+
Please see the [MiniMax docs](https://platform.minimaxi.com/docs) for a full list
178+
of available models and their capabilities. The provider accepts any model ID as a
179+
string for forward compatibility.
180+
</Note>
181+
182+
## Example Usage
183+
184+
### Basic Text Generation
185+
186+
```ts
187+
import { minimax } from '@ai-sdk/minimax';
188+
import { generateText } from 'ai';
189+
190+
const result = await generateText({
191+
model: minimax('MiniMax-M2'),
192+
prompt: 'Explain quantum computing in simple terms.',
193+
});
194+
195+
console.log(result.text);
196+
```
197+
198+
### Streaming
199+
200+
```ts
201+
import { minimax } from '@ai-sdk/minimax';
202+
import { streamText } from 'ai';
203+
204+
const result = streamText({
205+
model: minimax('MiniMax-M2'),
206+
prompt: 'Write a short story about a robot learning to paint.',
207+
});
208+
209+
for await (const chunk of result.textStream) {
210+
process.stdout.write(chunk);
211+
}
212+
```
213+
214+
### With Tools
215+
216+
```ts
217+
import { minimax } from '@ai-sdk/minimax';
218+
import { generateText } from 'ai';
219+
import { z } from 'zod';
220+
221+
const result = await generateText({
222+
model: minimax('MiniMax-M2'),
223+
tools: {
224+
weather: {
225+
description: 'Get the weather in a location',
226+
parameters: z.object({
227+
location: z.string().describe('The location to get the weather for'),
228+
}),
229+
execute: async ({ location }) => ({
230+
location,
231+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
232+
}),
233+
},
234+
},
235+
prompt: 'What is the weather in San Francisco?',
236+
});
237+
238+
console.log(result.text);
239+
```
240+
241+
### With Image Input
242+
243+
```ts
244+
import { minimax } from '@ai-sdk/minimax';
245+
import { generateText } from 'ai';
246+
247+
const result = await generateText({
248+
model: minimax('MiniMax-M2'),
249+
messages: [
250+
{
251+
role: 'user',
252+
content: [
253+
{ type: 'text', text: 'What is in this image?' },
254+
{
255+
type: 'image',
256+
image: 'https://example.com/image.jpg',
257+
},
258+
],
259+
},
260+
],
261+
});
262+
263+
console.log(result.text);
264+
```
265+

packages/minimax/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['vercel-ai'],
4+
};
5+

packages/minimax/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dist
2+
node_modules
3+
*.tsbuildinfo
4+
.turbo
5+
.env
6+
.env.local
7+
.DS_Store
8+
coverage
9+
*.log

packages/minimax/.npmignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
src
2+
*.test.ts
3+
*.test.tsx
4+
__tests__
5+
__mocks__
6+
__fixtures__
7+
__snapshots__
8+
tsconfig.json
9+
tsconfig.build.json
10+
tsup.config.ts
11+
vitest.*.config.js
12+
turbo.json
13+
.eslintrc.js
14+
.gitignore
15+
.env
16+
.env.*
17+
*.tsbuildinfo
18+
.turbo
19+
coverage
20+
*.log
21+
22+
# Documentation that should not be in the package
23+
INSTALLATION.md
24+
PROJECT_OVERVIEW.md
25+
COMMUNITY_PROVIDER_SUBMISSION.md
26+
SUMMARY.md

packages/minimax/.prettierrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
...require('../../.prettierrc.js'),
3+
};
4+

packages/minimax/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# @ai-sdk/minimax
2+
3+
## 0.0.1
4+
5+
### Initial Release
6+
7+
- Added MiniMax provider with support for MiniMax-M2 text generation model
8+
- **Default provider**: Anthropic-compatible API via `minimax` and `createMinimax` exports
9+
- OpenAI-compatible API interface via `minimaxOpenAI` and `createMinimaxOpenAI` exports
10+
- Anthropic-compatible API interface via `minimaxAnthropic` export
11+
- Both compatibility modes included in a single package
12+
- Streaming and non-streaming text generation
13+
- Custom configuration support for both modes
14+
- Separate test files for each provider for better maintainability
15+
- Provider identifiers: `minimax.anthropic` and `minimax.openai`
16+

0 commit comments

Comments
 (0)