-
-
Notifications
You must be signed in to change notification settings - Fork 662
Support Vertex AI publisher URLs for Gemini #425
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
hijos
wants to merge
2
commits into
tbphp:main
Choose a base branch
from
hijos:codex/vertex-ai-publisher-gemini
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.
+286
−8
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package channel | ||
|
|
||
| import ( | ||
| "context" | ||
| "gpt-load/internal/models" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGeminiBuildUpstreamURLPreservesDeveloperAPIPath(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://generativelanguage.googleapis.com") | ||
| originalURL := mustParseURL(t, "http://localhost:3001/proxy/gemini/v1beta/models/gemini-2.5-pro:generateContent?key=proxy-key") | ||
|
|
||
| got, err := ch.BuildUpstreamURL(originalURL, "gemini") | ||
| if err != nil { | ||
| t.Fatalf("BuildUpstreamURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=proxy-key" | ||
| if got != want { | ||
| t.Fatalf("BuildUpstreamURL() = %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiBuildUpstreamURLDoesNotDuplicateDeveloperAPIBasePath(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://generativelanguage.googleapis.com/v1beta") | ||
| originalURL := mustParseURL(t, "http://localhost:3001/proxy/gemini/v1beta/models/gemini-2.5-pro:generateContent?key=proxy-key") | ||
|
|
||
| got, err := ch.BuildUpstreamURL(originalURL, "gemini") | ||
| if err != nil { | ||
| t.Fatalf("BuildUpstreamURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=proxy-key" | ||
| if got != want { | ||
| t.Fatalf("BuildUpstreamURL() = %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiBuildUpstreamURLConvertsNativePathForVertexPublisherBase(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://aiplatform.googleapis.com/v1/publishers/google") | ||
| originalURL := mustParseURL(t, "http://localhost:3001/proxy/gemini/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse&key=proxy-key") | ||
|
|
||
| got, err := ch.BuildUpstreamURL(originalURL, "gemini") | ||
| if err != nil { | ||
| t.Fatalf("BuildUpstreamURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := "https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.5-pro:streamGenerateContent?alt=sse&key=proxy-key" | ||
| if got != want { | ||
| t.Fatalf("BuildUpstreamURL() = %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiBuildUpstreamURLConvertsNativePathForBareAiplatformBase(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://aiplatform.googleapis.com/") | ||
| originalURL := mustParseURL(t, "http://localhost:3001/proxy/gemini/v1beta/models/gemini-2.5-pro:generateContent?key=proxy-key") | ||
|
|
||
| got, err := ch.BuildUpstreamURL(originalURL, "gemini") | ||
| if err != nil { | ||
| t.Fatalf("BuildUpstreamURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := "https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.5-pro:generateContent?key=proxy-key" | ||
| if got != want { | ||
| t.Fatalf("BuildUpstreamURL() = %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiBuildUpstreamURLDoesNotDuplicateVertexPublisherPath(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://aiplatform.googleapis.com/v1/publishers/google") | ||
| originalURL := mustParseURL(t, "http://localhost:3001/proxy/gemini/v1/publishers/google/models/gemini-2.5-flash:generateContent") | ||
|
|
||
| got, err := ch.BuildUpstreamURL(originalURL, "gemini") | ||
| if err != nil { | ||
| t.Fatalf("BuildUpstreamURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := "https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.5-flash:generateContent" | ||
| if got != want { | ||
| t.Fatalf("BuildUpstreamURL() = %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiApplyModelRedirectWorksWithVertexPublisherPath(t *testing.T) { | ||
| ch := newTestGeminiChannel(t, "https://aiplatform.googleapis.com/v1/publishers/google") | ||
| req := &http.Request{ | ||
| URL: mustParseURL(t, "https://aiplatform.googleapis.com/v1/publishers/google/models/source-model:generateContent"), | ||
| } | ||
| group := &models.Group{ | ||
| Name: "gemini", | ||
| ModelRedirectMap: map[string]string{"source-model": "target-model"}, | ||
| } | ||
| body := []byte(`{"contents":[]}`) | ||
|
|
||
| gotBody, err := ch.ApplyModelRedirect(req, body, group) | ||
| if err != nil { | ||
| t.Fatalf("ApplyModelRedirect returned error: %v", err) | ||
| } | ||
| if string(gotBody) != string(body) { | ||
| t.Fatalf("ApplyModelRedirect body = %s, want %s", gotBody, body) | ||
| } | ||
|
|
||
| wantPath := "/v1/publishers/google/models/target-model:generateContent" | ||
| if req.URL.Path != wantPath { | ||
| t.Fatalf("redirected path = %q, want %q", req.URL.Path, wantPath) | ||
| } | ||
| } | ||
|
|
||
| func TestGeminiValidateKeyUsesVertexPublisherPath(t *testing.T) { | ||
| var gotPath string | ||
| var gotKey string | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotPath = r.URL.Path | ||
| gotKey = r.URL.Query().Get("key") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| ch := newTestGeminiChannel(t, server.URL+"/v1/publishers/google") | ||
| ch.HTTPClient = server.Client() | ||
| ch.TestModel = "gemini-test" | ||
|
|
||
| ok, err := ch.ValidateKey(context.Background(), &models.APIKey{KeyValue: "secret-key"}, &models.Group{Name: "gemini"}) | ||
| if err != nil { | ||
| t.Fatalf("ValidateKey returned error: %v", err) | ||
| } | ||
| if !ok { | ||
| t.Fatal("ValidateKey returned false, want true") | ||
| } | ||
|
|
||
| wantPath := "/v1/publishers/google/models/gemini-test:generateContent" | ||
| if gotPath != wantPath { | ||
| t.Fatalf("validation path = %q, want %q", gotPath, wantPath) | ||
| } | ||
| if gotKey != "secret-key" { | ||
| t.Fatalf("validation key = %q, want %q", gotKey, "secret-key") | ||
| } | ||
| } | ||
|
|
||
| func newTestGeminiChannel(t *testing.T, upstream string) *GeminiChannel { | ||
| t.Helper() | ||
|
|
||
| upstreamURL := mustParseURL(t, upstream) | ||
| return &GeminiChannel{ | ||
| BaseChannel: &BaseChannel{ | ||
| Name: "gemini", | ||
| Upstreams: []UpstreamInfo{{URL: upstreamURL, Weight: 1}}, | ||
| HTTPClient: http.DefaultClient, | ||
| TestModel: "gemini-2.0-flash-lite", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func mustParseURL(t *testing.T, rawURL string) *url.URL { | ||
| t.Helper() | ||
|
|
||
| parsed, err := url.Parse(rawURL) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse url %q: %v", rawURL, err) | ||
| } | ||
| return parsed | ||
| } |
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.