-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Support Qiniu provider with OpenAI API compatibility #4256
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
liangchaoboy
wants to merge
4
commits into
QuantumNous:main
Choose a base branch
from
liangchaoboy:feat/qiniu-provider
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 all commits
Commits
Show all changes
4 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,49 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/constant" | ||
| "github.com/QuantumNous/new-api/model" | ||
| ) | ||
|
|
||
| // TestFetchChannelUpstreamModelIDs_Qiniu 验证七牛渠道通过 /v1/models 接口拉取上游模型列表的逻辑, | ||
| // 使用 httptest 模拟七牛 API 响应,确保请求路径、认证头和返回结果解析均正确。 | ||
| func TestFetchChannelUpstreamModelIDs_Qiniu(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet { | ||
| t.Fatalf("unexpected method: %s", r.Method) | ||
| } | ||
| if r.URL.Path != "/v1/models" { | ||
| t.Fatalf("unexpected path: %s", r.URL.Path) | ||
| } | ||
| if got := r.Header.Get("Authorization"); got != "Bearer test-key" { | ||
| t.Fatalf("unexpected Authorization header: %q", got) | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"object":"list","data":[{"id":"deepseek/deepseek-v3.1-terminus-thinking"},{"id":"gpt-4"}]}`)) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| ch := &model.Channel{ | ||
| Id: 123, | ||
| Type: constant.ChannelTypeQiniu, | ||
| Key: "test-key", | ||
| Status: common.ChannelStatusEnabled, | ||
| BaseURL: common.GetPointer[string](srv.URL), | ||
| } | ||
|
|
||
| got, err := fetchChannelUpstreamModelIDs(ch) | ||
| if err != nil { | ||
| t.Fatalf("fetchChannelUpstreamModelIDs returned error: %v", err) | ||
| } | ||
| if len(got) != 2 || got[0] != "deepseek/deepseek-v3.1-terminus-thinking" || got[1] != "gpt-4" { | ||
| t.Fatalf("unexpected models: %#v", got) | ||
| } | ||
| } | ||
|
|
||
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.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1291
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 207
Avoid
t.Fatalfinside the HTTP handler goroutine.At lines 18, 21, and 24,
t.Fatalfis invoked from the server handler goroutine.Fatalf/FailNoware only safe from the test goroutine; calling them from other goroutines can lead to unreliable test failures. Capture request fields in the handler and assert them afterfetchChannelUpstreamModelIDsreturns.Suggested fix
func TestFetchChannelUpstreamModelIDs_Qiniu(t *testing.T) { t.Parallel() + type requestSnapshot struct { + method string + path string + auth string + } + reqCh := make(chan requestSnapshot, 1) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodGet { - t.Fatalf("unexpected method: %s", r.Method) - } - if r.URL.Path != "/v1/models" { - t.Fatalf("unexpected path: %s", r.URL.Path) - } - if got := r.Header.Get("Authorization"); got != "Bearer test-key" { - t.Fatalf("unexpected Authorization header: %q", got) - } + reqCh <- requestSnapshot{ + method: r.Method, + path: r.URL.Path, + auth: r.Header.Get("Authorization"), + } w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"object":"list","data":[{"id":"deepseek/deepseek-v3.1-terminus-thinking"},{"id":"gpt-4"}]}`)) })) defer srv.Close() @@ got, err := fetchChannelUpstreamModelIDs(ch) if err != nil { t.Fatalf("fetchChannelUpstreamModelIDs returned error: %v", err) } + req := <-reqCh + if req.method != http.MethodGet { + t.Fatalf("unexpected method: %s", req.method) + } + if req.path != "/v1/models" { + t.Fatalf("unexpected path: %s", req.path) + } + if req.auth != "Bearer test-key" { + t.Fatalf("unexpected Authorization header: %q", req.auth) + } if len(got) != 2 || got[0] != "deepseek/deepseek-v3.1-terminus-thinking" || got[1] != "gpt-4" { t.Fatalf("unexpected models: %#v", got) } }🤖 Prompt for AI Agents