-
Notifications
You must be signed in to change notification settings - Fork 567
feat: add LiteLLM router integration with model provider detection #67
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
Merged
akshaydeo
merged 1 commit into
main
from
06-10-feat_add_litellm_integration_to_http_transport
Jun 10, 2025
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package litellm | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "slices" | ||
|
|
||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/anthropic" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/genai" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/openai" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // LiteLLMRequestWrapper wraps any provider-specific request type | ||
| type LiteLLMRequestWrapper struct { | ||
| Model string `json:"model"` | ||
| ActualRequest interface{} `json:"-"` // This will hold the actual provider-specific request | ||
| Provider schemas.ModelProvider `json:"-"` | ||
| } | ||
|
|
||
| // LiteLLMRouter holds route registrations for LiteLLM endpoints. | ||
| // It supports standard chat completions and image-enabled vision capabilities. | ||
| // LiteLLM is fully OpenAI-compatible, so we reuse OpenAI types | ||
| // with aliases for clarity and minimal LiteLLM-specific extensions | ||
| type LiteLLMRouter struct { | ||
| *integrations.GenericRouter | ||
| } | ||
|
|
||
| // NewLiteLLMRouter creates a new LiteLLMRouter with the given bifrost client. | ||
| func NewLiteLLMRouter(client *bifrost.Bifrost) *LiteLLMRouter { | ||
| paths := []string{ | ||
| "/chat/completions", | ||
| "/v1/messages", | ||
| } | ||
|
|
||
| getRequestTypeInstance := func() interface{} { | ||
| return &LiteLLMRequestWrapper{} | ||
| } | ||
|
|
||
| availableProviders := []schemas.ModelProvider{ | ||
| schemas.OpenAI, | ||
| schemas.Anthropic, | ||
| schemas.Vertex, | ||
| schemas.Azure, | ||
| } | ||
|
|
||
| // Pre-hook to determine provider and parse request with correct type | ||
| preHook := func(ctx *fasthttp.RequestCtx, req interface{}) error { | ||
| wrapper, ok := req.(*LiteLLMRequestWrapper) | ||
| if !ok { | ||
| return errors.New("invalid request wrapper type") | ||
| } | ||
|
|
||
| if wrapper.Model == "" { | ||
| return errors.New("model field is required") | ||
| } | ||
|
|
||
| // Determine provider from model | ||
| provider := integrations.GetProviderFromModel(wrapper.Model) | ||
| if !slices.Contains(availableProviders, provider) { | ||
| return errors.New("unsupported provider: " + string(provider)) | ||
| } | ||
|
|
||
| // Get the request body | ||
| body := ctx.Request.Body() | ||
| if len(body) == 0 { | ||
| return errors.New("request body is required") | ||
| } | ||
|
|
||
| // Create the appropriate request type based on provider and re-parse | ||
| var actualReq interface{} | ||
| switch provider { | ||
| case schemas.OpenAI, schemas.Azure: | ||
| actualReq = &openai.OpenAIChatRequest{} | ||
| case schemas.Anthropic: | ||
| actualReq = &anthropic.AnthropicMessageRequest{} | ||
| case schemas.Vertex: | ||
| actualReq = &genai.GeminiChatRequest{} | ||
| default: | ||
| return errors.New("unsupported provider: " + string(provider)) | ||
| } | ||
|
Pratham-Mishra04 marked this conversation as resolved.
|
||
|
|
||
| // Parse the body into the correct request type | ||
| if err := json.Unmarshal(body, actualReq); err != nil { | ||
| return errors.New("failed to parse request for provider " + string(provider) + ": " + err.Error()) | ||
| } | ||
|
|
||
| // Store the parsed request and provider in the wrapper | ||
| wrapper.ActualRequest = actualReq | ||
| wrapper.Provider = provider | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| requestConverter := func(req interface{}) (*schemas.BifrostRequest, error) { | ||
| wrapper, ok := req.(*LiteLLMRequestWrapper) | ||
| if !ok { | ||
| return nil, errors.New("invalid request wrapper type") | ||
| } | ||
|
|
||
| if wrapper.ActualRequest == nil { | ||
| return nil, errors.New("request was not properly processed by pre-hook") | ||
| } | ||
|
|
||
| // Handle different provider-specific request types | ||
| switch actualReq := wrapper.ActualRequest.(type) { | ||
| case *openai.OpenAIChatRequest: | ||
| bifrostReq := actualReq.ConvertToBifrostRequest() | ||
| bifrostReq.Provider = wrapper.Provider | ||
| return bifrostReq, nil | ||
|
|
||
| case *anthropic.AnthropicMessageRequest: | ||
| bifrostReq := actualReq.ConvertToBifrostRequest() | ||
| bifrostReq.Provider = wrapper.Provider | ||
| return bifrostReq, nil | ||
|
|
||
| case *genai.GeminiChatRequest: | ||
| bifrostReq := actualReq.ConvertToBifrostRequest() | ||
| bifrostReq.Provider = wrapper.Provider | ||
| return bifrostReq, nil | ||
|
|
||
| default: | ||
| return nil, errors.New("unsupported request type") | ||
| } | ||
| } | ||
|
|
||
| responseConverter := func(resp *schemas.BifrostResponse) (interface{}, error) { | ||
| switch resp.ExtraFields.Provider { | ||
| case schemas.OpenAI, schemas.Azure: | ||
| return openai.DeriveOpenAIFromBifrostResponse(resp), nil | ||
| case schemas.Anthropic: | ||
| return anthropic.DeriveAnthropicFromBifrostResponse(resp), nil | ||
| case schemas.Vertex: | ||
| return genai.DeriveGenAIFromBifrostResponse(resp), nil | ||
| default: | ||
| return resp, nil | ||
| } | ||
| } | ||
|
|
||
| routes := []integrations.RouteConfig{} | ||
| for _, path := range paths { | ||
| routes = append(routes, integrations.RouteConfig{ | ||
| Path: "/litellm" + path, | ||
| Method: "POST", | ||
| GetRequestTypeInstance: getRequestTypeInstance, | ||
| RequestConverter: requestConverter, | ||
| ResponseConverter: responseConverter, | ||
| PreCallback: preHook, | ||
| }) | ||
| } | ||
|
|
||
| return &LiteLLMRouter{ | ||
| GenericRouter: integrations.NewGenericRouter(client, routes), | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.