-
Notifications
You must be signed in to change notification settings - Fork 11.8k
feat(mineru): add MinerU /v1/file_parse relay endpoint (channel type 64) #7588
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
2388832
wants to merge
4
commits into
QuantumNous:main
Choose a base branch
from
2388832:feat/mineru-relay
base: main
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.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eb7a062
feat(mineru): add MinerU /v1/file_parse relay endpoint
invalid-email-address 2523a2f
fix(mineru): address code review findings
invalid-email-address 47d1872
fix(mineru): close credential-transport bypasses found in review
invalid-email-address 736cc30
fix(mineru): pin validated address to close DNS rebinding window
invalid-email-address 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,96 @@ | ||
| package mineru | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/relay/channel" | ||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
| "github.com/QuantumNous/new-api/relaykit/dto" | ||
| "github.com/QuantumNous/new-api/relaykit/types" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| const ChannelName = "mineru" | ||
|
|
||
| var ModelList = []string{"mineru"} | ||
|
|
||
| type Adaptor struct { | ||
| } | ||
|
|
||
| func (a *Adaptor) Init(info *relaycommon.RelayInfo) { | ||
| } | ||
|
|
||
| // GetRequestURL 返回渠道 base_url + /file_parse。 | ||
| // 本地 MinerU: base_url = http://mineru-api:8000 | ||
| // 上游 New-API: base_url = https://api.playground.ai.gcable.cc/v1 | ||
| func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { | ||
| baseUrl := strings.TrimRight(info.ChannelBaseUrl, "/") | ||
| if baseUrl == "" { | ||
| return "", errors.New("mineru channel base_url is empty") | ||
| } | ||
| return fmt.Sprintf("%s/file_parse", baseUrl), nil | ||
| } | ||
|
|
||
| func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error { | ||
| // multipart 透传:Content-Type(含 boundary)由 DoFormRequest 按入站请求设置 | ||
| if info.ApiKey != "" { | ||
| req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) { | ||
| return channel.DoFormRequest(a, c, info, requestBody) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) { | ||
| // 响应体已在 MinerUHelper 中原样透传,此处仅返回零 usage(按次计费) | ||
| return &dto.Usage{}, nil | ||
| } | ||
|
|
||
| func (a *Adaptor) GetModelList() []string { | ||
| return ModelList | ||
| } | ||
|
|
||
| func (a *Adaptor) GetChannelName() string { | ||
| return ChannelName | ||
| } | ||
|
|
||
| // 以下为 channel.Adaptor 接口的占位实现(MinerU 转发不涉及) | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
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
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,66 @@ | ||
| package relay | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
| "github.com/QuantumNous/new-api/relaykit/dto" | ||
| "github.com/QuantumNous/new-api/relaykit/types" | ||
| "github.com/QuantumNous/new-api/service" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // MinerUHelper 将 /v1/file_parse(multipart/form-data)请求转发到 | ||
| // 渠道 base_url + /file_parse,并将上游响应原样透传给客户端。 | ||
| // 渠道约定: | ||
| // - 本地 MinerU: base_url = http://mineru-api:8000 | ||
| // - 上游 New-API: base_url = https://api.playground.ai.gcable.cc/v1 | ||
| func MinerUHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) { | ||
| info.InitChannelMeta(c) | ||
|
|
||
| if _, ok := info.Request.(*dto.MinerURequest); !ok { | ||
| return types.NewError(errors.New("invalid request type"), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry()) | ||
| } | ||
|
|
||
| adaptor := GetAdaptor(info.ApiType) | ||
| if adaptor == nil { | ||
| return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry()) | ||
| } | ||
| adaptor.Init(info) | ||
|
|
||
| // multipart 请求体已由 controller.Relay 置为可重放的 BodyStorage,直接透传 | ||
| resp, err := adaptor.DoRequest(c, info, c.Request.Body) | ||
| if err != nil { | ||
| return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError) | ||
| } | ||
| httpResp, ok := resp.(*http.Response) | ||
| if !ok || httpResp == nil { | ||
| return types.NewError(errors.New("invalid upstream response"), types.ErrorCodeDoRequestFailed, types.ErrOptionWithSkipRetry()) | ||
| } | ||
| defer httpResp.Body.Close() | ||
|
|
||
| statusCodeMappingStr := c.GetString("status_code_mapping") | ||
| if httpResp.StatusCode != http.StatusOK { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) | ||
| service.ResetStatusCode(newAPIError, statusCodeMappingStr) | ||
| return newAPIError | ||
| } | ||
|
|
||
| // 原样回传响应(JSON / zip 均透传) | ||
| if contentType := httpResp.Header.Get("Content-Type"); contentType != "" { | ||
| c.Writer.Header().Set("Content-Type", contentType) | ||
| } | ||
| if cd := httpResp.Header.Get("Content-Disposition"); cd != "" { | ||
| c.Writer.Header().Set("Content-Disposition", cd) | ||
| } | ||
| c.Status(http.StatusOK) | ||
| _, _ = io.Copy(c.Writer, httpResp.Body) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| // 按次计费(mineru 为 quota_type=1 按次价格,usage 置零) | ||
| service.PostTextConsumeQuota(c, info, &dto.Usage{}, nil) | ||
| return nil | ||
| } | ||
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,15 @@ | ||
| package dto | ||
|
|
||
| // MinerURequest 表示 /v1/file_parse 的 multipart 表单请求。 | ||
| // 表单内容(files 及解析参数)原样透传给上游 MinerU 服务, | ||
| // 这里只提取渠道选择所需的元信息(model,可省略,默认 mineru)。 | ||
| type MinerURequest struct { | ||
| BaseRequest | ||
| Model string `json:"model" form:"model"` | ||
| } | ||
|
|
||
| func (r *MinerURequest) SetModelName(modelName string) { | ||
| if modelName != "" { | ||
| r.Model = modelName | ||
| } | ||
| } |
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
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
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.