-
Notifications
You must be signed in to change notification settings - Fork 784
feat: auto-detect ChatGPT subscription tokens and route to chatgpt.com #4548
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
Purvi09
wants to merge
12
commits into
maximhq:dev
Choose a base branch
from
Purvi09:feat/chatgpt-passthrough-auto-detect
base: dev
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 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
69b20e7
fix: fixes redaction setting in plugins config (#4486)
roroghost17 e8f8c38
feat: adds scripts to seed and delete bifrost and litellm entities
sammaji 30eebc1
feat: adds scripts to migrate litellm entities to bifrost entities
sammaji 12e4f2f
feat: adds conformance tests
sammaji 60bff2b
fix: fix 403 errors for list models request
roroghost17 0f6cb3b
chore: updated the default network config timings
roroghost17 e55c549
fix: helm release tag to the correct commit
BearTS b266465
[docs] : docs for source of truth
Madhuvod 96bb2bd
fixes canonical_model_view migration
akshaydeo a601215
feat: auto-detect ChatGPT subscription tokens and route to chatgpt.com
Purvi09 8ce8f93
fix: address coderabbit and greptile review comments
Purvi09 6783c5a
fix: skip responses-to-chat fallback for ChatGPT passthrough requests
Purvi09 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
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,57 @@ | ||
| package openai | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "strings" | ||
|
|
||
| "github.com/bytedance/sonic" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| ) | ||
|
|
||
| const ( | ||
| chatGPTAccountIDKey = "chatgpt_account_id" | ||
| openAIAuthClaim = "https://api.openai.com/auth" | ||
|
|
||
| // ChatGPTCodexURL is the full upstream URL for ChatGPT subscription token requests. | ||
| ChatGPTCodexURL = "https://chatgpt.com/backend-api/codex/responses" | ||
| ) | ||
|
|
||
| // ParseChatGPTJWT parses a raw bearer token, checks for the ChatGPT subscription | ||
| // JWT claim, and returns the chatgpt_account_id. No signature verification is | ||
| // Returns ("", false) for any non-ChatGPT or malformed token. | ||
| func ParseChatGPTJWT(token string) (accountID string, ok bool) { | ||
| parts := strings.Split(token, ".") | ||
| if len(parts) != 3 { | ||
| return "", false | ||
| } | ||
|
|
||
| payload, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
|
|
||
| // Extract the nested claim: {"https://api.openai.com/auth": {"chatgpt_account_id": "..."}} | ||
| var claims map[string]interface{} | ||
| if err := sonic.Unmarshal(payload, &claims); err != nil { | ||
| return "", false | ||
| } | ||
|
|
||
| authClaim, ok := claims[openAIAuthClaim].(map[string]interface{}) | ||
| if !ok { | ||
| return "", false | ||
| } | ||
|
|
||
| accountID, ok = authClaim[chatGPTAccountIDKey].(string) | ||
| if !ok || accountID == "" { | ||
| return "", false | ||
| } | ||
|
|
||
| return accountID, true | ||
| } | ||
|
|
||
| // IsChatGPTPassthrough reports whether the current request was auto-detected | ||
| // as a ChatGPT subscription token and should be routed to chatgpt.com. | ||
| func IsChatGPTPassthrough(ctx *schemas.BifrostContext) bool { | ||
| v, _ := ctx.Value(schemas.BifrostContextKeyChatGPTPassthrough).(bool) | ||
| return v | ||
| } | ||
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,90 @@ | ||
| package openai | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|
|
||
| // makeTestJWT builds a syntactically valid JWT with arbitrary header/payload JSON. | ||
| // The signature segment is a fixed placeholder — ParseChatGPTJWT never verifies it. | ||
| func makeTestJWT(payloadJSON string) string { | ||
| header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"RS256","typ":"JWT"}`)) | ||
| payload := base64.RawURLEncoding.EncodeToString([]byte(payloadJSON)) | ||
| return fmt.Sprintf("%s.%s.fakesig", header, payload) | ||
| } | ||
|
|
||
| func TestParseChatGPTJWT(t *testing.T) { | ||
| validAccountID := "9dce4683-94cd-4aeb-ade4-4ecce82ebac5" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| token string | ||
| wantID string | ||
| wantOK bool | ||
| }{ | ||
| { | ||
| name: "valid ChatGPT JWT returns account ID", | ||
| token: makeTestJWT(fmt.Sprintf( | ||
| `{"aud":["https://api.openai.com/v1"],"https://api.openai.com/auth":{"chatgpt_account_id":%q}}`, | ||
| validAccountID, | ||
| )), | ||
| wantID: validAccountID, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "JWT missing chatgpt_account_id claim returns false", | ||
| token: makeTestJWT(`{"aud":["https://api.openai.com/v1"],"sub":"user-abc"}`), | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "JWT with https://api.openai.com/auth but no chatgpt_account_id returns false", | ||
| token: makeTestJWT(`{"https://api.openai.com/auth":{"other_field":"value"}}`), | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "not a JWT (sk- API key) returns false", | ||
| token: "sk-proj-abcdefghijklmnopqrstuvwxyz", | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "empty string returns false", | ||
| token: "", | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "only two segments returns false", | ||
| token: "header.payload", | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "invalid base64 in payload returns false", | ||
| token: "header.!!!invalid!!!.sig", | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "payload is valid base64 but not JSON returns false", | ||
| token: fmt.Sprintf("header.%s.sig", base64.RawURLEncoding.EncodeToString([]byte("not-json"))), | ||
| wantID: "", | ||
| wantOK: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gotID, gotOK := ParseChatGPTJWT(tt.token) | ||
| if gotOK != tt.wantOK { | ||
| t.Errorf("ParseChatGPTJWT() ok = %v, want %v", gotOK, tt.wantOK) | ||
| } | ||
| if gotID != tt.wantID { | ||
| t.Errorf("ParseChatGPTJWT() accountID = %q, want %q", gotID, tt.wantID) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
ParseChatGPTJWThas a truncated sentence on the second line. "No signature verification is" is missing its predicate, making the doc incomplete and potentially confusing.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!