|
| 1 | +# AI SDK - MiniMax Provider |
| 2 | + |
| 3 | +The **[MiniMax provider](https://ai-sdk.dev/providers/ai-sdk-providers/minimax)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [MiniMax](https://www.minimaxi.com) platform. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +The MiniMax provider is available in the `@ai-sdk/minimax` module. You can install it with |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i @ai-sdk/minimax |
| 11 | +``` |
| 12 | + |
| 13 | +## Provider Instance |
| 14 | + |
| 15 | +You can import the default provider instance `minimax` from `@ai-sdk/minimax`: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { minimax } from '@ai-sdk/minimax'; |
| 19 | +``` |
| 20 | + |
| 21 | +> **Note**: The default `minimax` instance uses the Anthropic-compatible API format, which provides better support for advanced features. If you need the OpenAI-compatible format, use `minimaxOpenAI` instead. |
| 22 | +
|
| 23 | +## Example |
| 24 | + |
| 25 | +```ts |
| 26 | +import { minimax } from '@ai-sdk/minimax'; |
| 27 | +import { generateText } from 'ai'; |
| 28 | + |
| 29 | +const { text } = await generateText({ |
| 30 | + model: minimax('MiniMax-M2'), |
| 31 | + prompt: 'Write a JavaScript function that sorts a list:', |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +## API Compatibility |
| 36 | + |
| 37 | +MiniMax provides two API compatibility modes, both included in this package: |
| 38 | + |
| 39 | +### Anthropic-Compatible API (Default) |
| 40 | + |
| 41 | +```ts |
| 42 | +import { minimax } from '@ai-sdk/minimax'; |
| 43 | +import { generateText } from 'ai'; |
| 44 | + |
| 45 | +const { text } = await generateText({ |
| 46 | + model: minimax('MiniMax-M2'), |
| 47 | + prompt: 'Hello!', |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +Or explicitly: |
| 52 | + |
| 53 | +```ts |
| 54 | +import { minimaxAnthropic } from '@ai-sdk/minimax'; |
| 55 | +import { generateText } from 'ai'; |
| 56 | + |
| 57 | +const { text } = await generateText({ |
| 58 | + model: minimaxAnthropic('MiniMax-M2'), |
| 59 | + prompt: 'Hello!', |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +### OpenAI-Compatible API |
| 64 | + |
| 65 | +```ts |
| 66 | +import { minimaxOpenAI } from '@ai-sdk/minimax'; |
| 67 | +import { generateText } from 'ai'; |
| 68 | + |
| 69 | +const { text } = await generateText({ |
| 70 | + model: minimaxOpenAI('MiniMax-M2'), |
| 71 | + prompt: 'Hello!', |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +### Custom Configuration |
| 76 | + |
| 77 | +You can create custom provider instances with specific settings: |
| 78 | + |
| 79 | +```ts |
| 80 | +import { createMinimax, createMinimaxOpenAI } from '@ai-sdk/minimax'; |
| 81 | + |
| 82 | +// Anthropic-compatible with custom settings (default) |
| 83 | +const customAnthropic = createMinimax({ |
| 84 | + apiKey: process.env.MINIMAX_API_KEY, |
| 85 | + baseURL: 'https://api.minimax.io/anthropic/v1', // optional, this is the default |
| 86 | + headers: { |
| 87 | + 'Custom-Header': 'value', |
| 88 | + }, |
| 89 | +}); |
| 90 | + |
| 91 | +// OpenAI-compatible with custom settings |
| 92 | +const customOpenAI = createMinimaxOpenAI({ |
| 93 | + apiKey: process.env.MINIMAX_API_KEY, |
| 94 | + baseURL: 'https://api.minimax.io/v1', // optional, this is the default |
| 95 | + headers: { |
| 96 | + 'Custom-Header': 'value', |
| 97 | + }, |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +## Configuration Options |
| 102 | + |
| 103 | +Both `createMinimax` (Anthropic-compatible) and `createMinimaxOpenAI` (OpenAI-compatible) accept the following options: |
| 104 | + |
| 105 | +- **apiKey** _string_ |
| 106 | + |
| 107 | + API key for authenticating with the MiniMax API. Defaults to the `MINIMAX_API_KEY` environment variable. |
| 108 | + |
| 109 | +- **baseURL** _string_ |
| 110 | + |
| 111 | + Custom base URL for API calls. |
| 112 | + - Anthropic-compatible default: `https://api.minimax.io/anthropic/v1` |
| 113 | + - OpenAI-compatible default: `https://api.minimax.io/v1` |
| 114 | + |
| 115 | +- **headers** _Record<string, string>_ |
| 116 | + |
| 117 | + Custom headers to include in API requests. |
| 118 | + |
| 119 | +- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_ |
| 120 | + |
| 121 | + Custom fetch implementation for intercepting requests or testing. |
| 122 | + |
| 123 | +## Streaming Example |
| 124 | + |
| 125 | +```ts |
| 126 | +import { minimax } from '@ai-sdk/minimax'; |
| 127 | +import { streamText } from 'ai'; |
| 128 | + |
| 129 | +const result = streamText({ |
| 130 | + model: minimax('MiniMax-M2'), |
| 131 | + prompt: 'Write a short story about a robot.', |
| 132 | +}); |
| 133 | + |
| 134 | +for await (const chunk of result.textStream) { |
| 135 | + process.stdout.write(chunk); |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Exports |
| 140 | + |
| 141 | +This package exports the following: |
| 142 | + |
| 143 | +- **Functions**: |
| 144 | + - `createMinimax` (Anthropic-compatible, default) |
| 145 | + - `minimax` (Anthropic-compatible, default) |
| 146 | + - `minimaxAnthropic` (Anthropic-compatible) |
| 147 | + - `createMinimaxOpenAI` (OpenAI-compatible) |
| 148 | + - `minimaxOpenAI` (OpenAI-compatible) |
| 149 | +- **Types**: |
| 150 | + - `MinimaxProvider` (Anthropic-compatible) |
| 151 | + - `MinimaxProviderSettings` (Anthropic-compatible) |
| 152 | + - `MinimaxAnthropicProvider` |
| 153 | + - `MinimaxAnthropicProviderSettings` |
| 154 | + - `MinimaxOpenAIProvider` |
| 155 | + - `MinimaxOpenAIProviderSettings` |
| 156 | + - `MinimaxErrorData` |
| 157 | + |
| 158 | +## Documentation |
| 159 | + |
| 160 | +Please check out the **[MiniMax provider](https://ai-sdk.dev/providers/ai-sdk-providers/minimax)** for more information. |
| 161 | + |
0 commit comments