-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat: add MiniMax provider support (M2.7, M2.5, M2.5-highspeed) #1062
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
Open
octo-patch
wants to merge
2
commits into
ItzCrazyKns:master
Choose a base branch
from
octo-patch:feature/add-minimax-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { UIConfigField } from '@/lib/config/types'; | ||
| import { getConfiguredModelProviderById } from '@/lib/config/serverRegistry'; | ||
| import { Model, ModelList, ProviderMetadata } from '../../types'; | ||
| import BaseEmbedding from '../../base/embedding'; | ||
| import BaseModelProvider from '../../base/provider'; | ||
| import BaseLLM from '../../base/llm'; | ||
| import MiniMaxLLM from './miniMaxLLM'; | ||
|
|
||
| interface MiniMaxConfig { | ||
| apiKey: string; | ||
| baseURL?: string; | ||
| } | ||
|
|
||
| const DEFAULT_CHAT_MODELS: Model[] = [ | ||
| { key: 'MiniMax-M2.7', name: 'MiniMax M2.7' }, | ||
| { key: 'MiniMax-M2.5', name: 'MiniMax M2.5' }, | ||
| { key: 'MiniMax-M2.5-highspeed', name: 'MiniMax M2.5 High Speed' }, | ||
| ]; | ||
|
|
||
| const providerConfigFields: UIConfigField[] = [ | ||
| { | ||
| type: 'password', | ||
| name: 'API Key', | ||
| key: 'apiKey', | ||
| description: 'Your MiniMax API key', | ||
| required: true, | ||
| placeholder: 'MiniMax API Key', | ||
| env: 'MINIMAX_API_KEY', | ||
| scope: 'server', | ||
| }, | ||
| { | ||
| type: 'string', | ||
| name: 'Base URL', | ||
| key: 'baseURL', | ||
| description: 'MiniMax API base URL (default: https://api.minimax.io/v1)', | ||
| required: false, | ||
| placeholder: 'https://api.minimax.io/v1', | ||
| env: 'MINIMAX_BASE_URL', | ||
| scope: 'server', | ||
| }, | ||
| ]; | ||
|
|
||
| class MiniMaxProvider extends BaseModelProvider<MiniMaxConfig> { | ||
| constructor(id: string, name: string, config: MiniMaxConfig) { | ||
| super(id, name, config); | ||
| } | ||
|
|
||
| async getDefaultModels(): Promise<ModelList> { | ||
| return { | ||
| embedding: [], | ||
| chat: DEFAULT_CHAT_MODELS, | ||
| }; | ||
| } | ||
|
|
||
| async getModelList(): Promise<ModelList> { | ||
| const defaultModels = await this.getDefaultModels(); | ||
| const configProvider = getConfiguredModelProviderById(this.id)!; | ||
|
|
||
| return { | ||
| embedding: [], | ||
| chat: [...defaultModels.chat, ...configProvider.chatModels], | ||
| }; | ||
| } | ||
|
|
||
| async loadChatModel(key: string): Promise<BaseLLM<any>> { | ||
| const modelList = await this.getModelList(); | ||
|
|
||
| const exists = modelList.chat.find((m) => m.key === key); | ||
|
|
||
| if (!exists) { | ||
| throw new Error( | ||
| 'Error Loading MiniMax Chat Model. Invalid Model Selected', | ||
| ); | ||
| } | ||
|
|
||
| return new MiniMaxLLM({ | ||
| apiKey: this.config.apiKey, | ||
| model: key, | ||
| baseURL: this.config.baseURL || 'https://api.minimax.io/v1', | ||
| }); | ||
| } | ||
|
|
||
| async loadEmbeddingModel(key: string): Promise<BaseEmbedding<any>> { | ||
| throw new Error('MiniMax provider does not support embedding models.'); | ||
| } | ||
|
|
||
| static parseAndValidate(raw: any): MiniMaxConfig { | ||
| if (!raw || typeof raw !== 'object') | ||
| throw new Error('Invalid config provided. Expected object'); | ||
| if (!raw.apiKey) | ||
| throw new Error('Invalid config provided. API key must be provided'); | ||
|
|
||
| return { | ||
| apiKey: String(raw.apiKey), | ||
| ...(raw.baseURL && { baseURL: String(raw.baseURL) }), | ||
| }; | ||
| } | ||
|
|
||
| static getProviderConfigFields(): UIConfigField[] { | ||
| return providerConfigFields; | ||
| } | ||
|
|
||
| static getProviderMetadata(): ProviderMetadata { | ||
| return { | ||
| key: 'minimax', | ||
| name: 'MiniMax', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export default MiniMaxProvider; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import OpenAILLM from '../openai/openaiLLM'; | ||
| import { | ||
| GenerateObjectInput, | ||
| GenerateOptions, | ||
| GenerateTextInput, | ||
| GenerateTextOutput, | ||
| StreamTextOutput, | ||
| } from '../../types'; | ||
| import z from 'zod'; | ||
| import { parse } from 'partial-json'; | ||
| import { repairJson } from '@toolsycc/json-repair'; | ||
|
|
||
| class MiniMaxLLM extends OpenAILLM { | ||
| async generateText(input: GenerateTextInput): Promise<GenerateTextOutput> { | ||
| const clampedInput = { | ||
| ...input, | ||
| options: this.clampTemperature(input.options), | ||
| }; | ||
| return super.generateText(clampedInput); | ||
| } | ||
|
|
||
| async *streamText( | ||
| input: GenerateTextInput, | ||
| ): AsyncGenerator<StreamTextOutput> { | ||
| const clampedInput = { | ||
| ...input, | ||
| options: this.clampTemperature(input.options), | ||
| }; | ||
| yield* super.streamText(clampedInput); | ||
| } | ||
|
|
||
| async generateObject<T>(input: GenerateObjectInput): Promise<T> { | ||
| const jsonSchema = z.toJSONSchema(input.schema); | ||
| const jsonPrompt = `You must respond with valid JSON only, no other text. The JSON must conform to this schema:\n${JSON.stringify(jsonSchema, null, 2)}`; | ||
|
|
||
| const systemMessage = { role: 'system' as const, content: jsonPrompt }; | ||
| const messages = [systemMessage, ...input.messages]; | ||
|
|
||
| const response = await this.openAIClient.chat.completions.create({ | ||
| model: this.config.model, | ||
| messages: this.convertToOpenAIMessages(messages), | ||
| temperature: | ||
| this.clampTemperature(input.options)?.temperature ?? | ||
| this.clampTemperature(this.config.options)?.temperature ?? | ||
| 1.0, | ||
| top_p: input.options?.topP ?? this.config.options?.topP, | ||
| max_completion_tokens: | ||
| input.options?.maxTokens ?? this.config.options?.maxTokens, | ||
| stop: input.options?.stopSequences ?? this.config.options?.stopSequences, | ||
| frequency_penalty: | ||
| input.options?.frequencyPenalty ?? | ||
| this.config.options?.frequencyPenalty, | ||
| presence_penalty: | ||
| input.options?.presencePenalty ?? this.config.options?.presencePenalty, | ||
| }); | ||
|
|
||
| if (response.choices && response.choices.length > 0) { | ||
| try { | ||
| return input.schema.parse( | ||
| JSON.parse( | ||
| repairJson(response.choices[0].message.content!, { | ||
| extractJson: true, | ||
| }) as string, | ||
| ), | ||
| ) as T; | ||
| } catch (err) { | ||
| throw new Error(`Error parsing response from MiniMax: ${err}`); | ||
| } | ||
| } | ||
|
|
||
| throw new Error('No response from MiniMax'); | ||
| } | ||
|
|
||
| async *streamObject<T>(input: GenerateObjectInput): AsyncGenerator<T> { | ||
| const jsonSchema = z.toJSONSchema(input.schema); | ||
| const jsonPrompt = `You must respond with valid JSON only, no other text. The JSON must conform to this schema:\n${JSON.stringify(jsonSchema, null, 2)}`; | ||
|
|
||
| const systemMessage = { role: 'system' as const, content: jsonPrompt }; | ||
| const messages = [systemMessage, ...input.messages]; | ||
|
|
||
| let receivedObj = ''; | ||
|
|
||
| const stream = await this.openAIClient.chat.completions.create({ | ||
| model: this.config.model, | ||
| messages: this.convertToOpenAIMessages(messages), | ||
| temperature: | ||
| this.clampTemperature(input.options)?.temperature ?? | ||
| this.clampTemperature(this.config.options)?.temperature ?? | ||
| 1.0, | ||
| top_p: input.options?.topP ?? this.config.options?.topP, | ||
| max_completion_tokens: | ||
| input.options?.maxTokens ?? this.config.options?.maxTokens, | ||
| stop: input.options?.stopSequences ?? this.config.options?.stopSequences, | ||
| frequency_penalty: | ||
| input.options?.frequencyPenalty ?? | ||
| this.config.options?.frequencyPenalty, | ||
| presence_penalty: | ||
| input.options?.presencePenalty ?? this.config.options?.presencePenalty, | ||
| stream: true, | ||
| }); | ||
|
|
||
| for await (const chunk of stream) { | ||
| if (chunk.choices && chunk.choices.length > 0) { | ||
| const content = chunk.choices[0].delta.content || ''; | ||
| receivedObj += content; | ||
|
|
||
| try { | ||
| yield parse(receivedObj) as T; | ||
| } catch { | ||
| yield {} as T; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private clampTemperature( | ||
| options?: GenerateOptions, | ||
| ): GenerateOptions | undefined { | ||
| if (!options) return options; | ||
| if ( | ||
| options.temperature !== undefined && | ||
| options.temperature !== null && | ||
| options.temperature <= 0 | ||
| ) { | ||
| return { ...options, temperature: 0.01 }; | ||
| } | ||
| return options; | ||
| } | ||
| } | ||
|
|
||
| export default MiniMaxLLM; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Temperature normalization is incomplete for MiniMax: values above 1 are not clamped, and inherited text paths can still send invalid config-level temperatures.
Prompt for AI agents