-
Notifications
You must be signed in to change notification settings - Fork 3
fix: fixes doulbe v1 prefix in passthrough openai routes
#174
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
pawbana
wants to merge
2
commits into
main
Choose a base branch
from
pb/routing-fix
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
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,106 @@ | ||
| package aibridge | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "cdr.dev/slog/v3/sloggers/slogtest" | ||
| "github.com/coder/aibridge/config" | ||
| "github.com/coder/aibridge/internal/testutil" | ||
| "github.com/coder/aibridge/provider" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPassthroughRoutesForProviders(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| upstreamRespBody := "upstream response" | ||
| tests := []struct { | ||
| name string | ||
| baseURLPath string | ||
| requestPath string | ||
| provider func(string) provider.Provider | ||
| expectPath string | ||
| }{ | ||
| { | ||
| name: "openAI_no_base_path", | ||
| requestPath: "/openai/v1/conversations", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewOpenAIProvider(config.OpenAI{BaseURL: baseURL}) | ||
| }, | ||
| expectPath: "/conversations", | ||
| }, | ||
| { | ||
| name: "openAI_with_base_path", | ||
| baseURLPath: "/v1", | ||
| requestPath: "/openai/v1/conversations", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewOpenAIProvider(config.OpenAI{BaseURL: baseURL}) | ||
| }, | ||
| expectPath: "/v1/conversations", | ||
| }, | ||
| { | ||
| name: "anthropic_no_base_path", | ||
| requestPath: "/anthropic/v1/models", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewAnthropicProvider(config.Anthropic{BaseURL: baseURL}, nil) | ||
| }, | ||
| expectPath: "/v1/models", | ||
| }, | ||
| { | ||
| name: "anthropic_with_base_path", | ||
| baseURLPath: "/v1", | ||
| requestPath: "/anthropic/v1/models", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewAnthropicProvider(config.Anthropic{BaseURL: baseURL}, nil) | ||
| }, | ||
| expectPath: "/v1/v1/models", | ||
| }, | ||
| { | ||
| name: "copilot_no_base_path", | ||
| requestPath: "/copilot/models", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewCopilotProvider(config.Copilot{BaseURL: baseURL}) | ||
| }, | ||
| expectPath: "/models", | ||
| }, | ||
| { | ||
| name: "copilot_with_base_path", | ||
| baseURLPath: "/v1", | ||
| requestPath: "/copilot/models", | ||
| provider: func(baseURL string) provider.Provider { | ||
| return NewCopilotProvider(config.Copilot{BaseURL: baseURL}) | ||
| }, | ||
| expectPath: "/v1/models", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| logger := slogtest.Make(t, nil) | ||
|
|
||
| upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| assert.Equal(t, tc.expectPath, r.URL.Path) | ||
| w.WriteHeader(http.StatusOK) | ||
| _, _ = w.Write([]byte(upstreamRespBody)) | ||
| })) | ||
| t.Cleanup(upstream.Close) | ||
|
|
||
| recorder := testutil.MockRecorder{} | ||
| prov := tc.provider(upstream.URL + tc.baseURLPath) | ||
| bridge, err := NewRequestBridge(t.Context(), []provider.Provider{prov}, &recorder, nil, logger, nil, testTracer) | ||
| require.NoError(t, err) | ||
|
|
||
| req := httptest.NewRequest("", tc.requestPath, nil) | ||
| resp := httptest.NewRecorder() | ||
| bridge.mux.ServeHTTP(resp, req) | ||
|
|
||
| assert.Equal(t, http.StatusOK, resp.Code) | ||
| assert.Contains(t, resp.Body.String(), upstreamRespBody) | ||
| }) | ||
| } | ||
| } |
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.
This should be
/v1/modelsno?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.
With empty base URL (no path to add as a prefix) I believe it should be
/models.This is also how I understand the OpenAI API. Base URL contains
/v1but the endpoint in API is just/models.This is different from Anthropic where base URL doesn't contain
/v1so/v1is part of path "in API".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.
But the actual upstream path is
/v1/models.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.
Yes but base URL contains
v1as specified for example in Codex documentation: https://developers.openai.com/codex/config-advanced#config-and-state-locationsThere 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.
We can't rely on the base URL being set in a specific way.
Our tests should show how we relay requests to the correct upstream path whether the configured base URL includes a
/v1suffix or not.Semantically I'd expect
expectedUpstreamPathto be the full path of the upstream API, otherwise it will confuse future readers like it did me.I only skimmed this; I'll give this a proper review on Monday.