-
Notifications
You must be signed in to change notification settings - Fork 404
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
feat: support minimax ai model #1033
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b04b3a1
feat: support minimax ai model
hanxiantao 715ca4a
Merge branch 'alibaba:main' into minimax-ai-proxy
hanxiantao 1646735
feat: support minimax chat completion pro
hanxiantao d0423a2
Merge remote-tracking branch 'origin/minimax-ai-proxy' into minimax-a…
hanxiantao 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 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 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 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,97 @@ | ||
package provider | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util" | ||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" | ||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm" | ||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" | ||
) | ||
|
||
// minimaxProvider is the provider for minimax service. | ||
|
||
const ( | ||
minimaxDomain = "api.minimax.chat" | ||
minimaxChatCompletionPath = "/v1/text/chatcompletion_v2" | ||
) | ||
|
||
type minimaxProviderInitializer struct { | ||
} | ||
|
||
func (m *minimaxProviderInitializer) ValidateConfig(config ProviderConfig) error { | ||
return nil | ||
} | ||
|
||
func (m *minimaxProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) { | ||
return &minimaxProvider{ | ||
config: config, | ||
contextCache: createContextCache(&config), | ||
}, nil | ||
} | ||
|
||
type minimaxProvider struct { | ||
config ProviderConfig | ||
contextCache *contextCache | ||
} | ||
|
||
func (m *minimaxProvider) GetProviderType() string { | ||
return providerTypeMinimax | ||
} | ||
|
||
func (m *minimaxProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) (types.Action, error) { | ||
if apiName != ApiNameChatCompletion { | ||
return types.ActionContinue, errUnsupportedApiName | ||
} | ||
_ = util.OverwriteRequestPath(minimaxChatCompletionPath) | ||
_ = util.OverwriteRequestHost(minimaxDomain) | ||
_ = proxywasm.ReplaceHttpRequestHeader("Authorization", "Bearer "+m.config.GetRandomToken()) | ||
_ = proxywasm.RemoveHttpRequestHeader("Content-Length") | ||
return types.ActionContinue, nil | ||
} | ||
|
||
func (m *minimaxProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) (types.Action, error) { | ||
if apiName != ApiNameChatCompletion { | ||
return types.ActionContinue, errUnsupportedApiName | ||
} | ||
|
||
request := &chatCompletionRequest{} | ||
if err := decodeChatCompletionRequest(body, request); err != nil { | ||
return types.ActionContinue, err | ||
} | ||
|
||
// 使用OpenAI接口协议,映射模型 | ||
if m.config.protocol == protocolOpenAI { | ||
model := request.Model | ||
if model == "" { | ||
return types.ActionContinue, errors.New("missing model in chat completion request") | ||
} | ||
mappedModel := getMappedModel(model, m.config.modelMapping, log) | ||
if mappedModel == "" { | ||
return types.ActionContinue, errors.New("model becomes empty after applying the configured mapping") | ||
} | ||
request.Model = mappedModel | ||
} | ||
|
||
if m.contextCache == nil { | ||
return types.ActionContinue, replaceJsonRequestBody(request, log) | ||
} | ||
|
||
err := m.contextCache.GetContent(func(content string, err error) { | ||
defer func() { | ||
_ = proxywasm.ResumeHttpRequest() | ||
}() | ||
if err != nil { | ||
log.Errorf("failed to load context file: %v", err) | ||
_ = util.SendResponse(500, util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err)) | ||
} | ||
insertContextMessage(request, content) | ||
if err := replaceJsonRequestBody(request, log); err != nil { | ||
_ = util.SendResponse(500, util.MimeTypeTextPlain, fmt.Sprintf("failed to replace request body: %v", err)) | ||
} | ||
}, log) | ||
if err == nil { | ||
return types.ActionPause, nil | ||
} | ||
return types.ActionContinue, err | ||
} |
This file contains 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
Oops, something went wrong.
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.
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.
这里的判定是不是有问题?如果用户配置的不适用 OpenAI 的契约,那么上面就不能按照 OpenAI 的契约进行请求数据反序列化了。而且这个 protocol 指的是契约,不是 model。
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.
这里的意思是只有使用openai协议时,才会进行模型映射。如果使用原生的接口协议,就使用接口传入的模型直接调用,不进行模型映射了
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.
不过 modelMapping 字段在定义上并没有说明只有在 OpenAI 协议上生效。这个逻辑可能会让用户感到迷惑。如果用户配置 Original 协议,一般也不会配 modelMapping 吧。
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.
嗯,好的,这里我调整一下