-
Notifications
You must be signed in to change notification settings - Fork 569
feat: Add OpenAI integration in HTTP transport #63
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-09-feat_openai_integration_added_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,71 @@ | ||
| package genai | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/fasthttp/router" | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/lib" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // GenAIRouter holds route registrations for genai endpoints. | ||
| type GenAIRouter struct { | ||
| client *bifrost.Bifrost | ||
| *integrations.GenericRouter | ||
| } | ||
|
|
||
| // NewGenAIRouter creates a new GenAIRouter with the given bifrost client. | ||
| func NewGenAIRouter(client *bifrost.Bifrost) *GenAIRouter { | ||
| return &GenAIRouter{client: client} | ||
| } | ||
| routes := []integrations.RouteConfig{ | ||
| { | ||
| Path: "/genai/v1beta/models/{model}", | ||
| Method: "POST", | ||
| RequestType: &GeminiChatRequest{}, | ||
| RequestConverter: func(req interface{}) *schemas.BifrostRequest { | ||
| if geminiReq, ok := req.(*GeminiChatRequest); ok { | ||
| return geminiReq.ConvertToBifrostRequest() | ||
| } | ||
| return nil | ||
| }, | ||
| ResponseFunc: func(resp *schemas.BifrostResponse) interface{} { | ||
| return DeriveGenAIFromBifrostResponse(resp) | ||
| }, | ||
| PreCallback: extractAndSetModelFromURL, | ||
| }, | ||
| } | ||
|
|
||
| // RegisterRoutes registers all genai routes on the given router. | ||
| func (g *GenAIRouter) RegisterRoutes(r *router.Router) { | ||
| r.POST("/genai/v1beta/models/{model}", g.handleChatCompletion) | ||
| return &GenAIRouter{ | ||
| GenericRouter: integrations.NewGenericRouter(client, routes), | ||
| } | ||
| } | ||
|
|
||
| // handleChatCompletion handles POST /genai/v1beta/models/{model} | ||
| func (g *GenAIRouter) handleChatCompletion(ctx *fasthttp.RequestCtx) { | ||
| // extractAndSetModelFromURL extracts model from URL and sets it in the request | ||
| func extractAndSetModelFromURL(ctx *fasthttp.RequestCtx, req interface{}) error { | ||
| model := ctx.UserValue("model") | ||
| if model == nil { | ||
| ctx.SetStatusCode(fasthttp.StatusBadRequest) | ||
| ctx.SetBodyString("Model parameter is required") | ||
| return | ||
| return fmt.Errorf("model parameter is required") | ||
| } | ||
|
|
||
| modelStr := model.(string) | ||
| modelStr = modelStr[:len(modelStr)-len(":generateContent")] | ||
| // Remove :generateContent suffix if present | ||
| modelStr = strings.TrimSuffix(modelStr, ":generateContent") | ||
| // Remove trailing colon if present | ||
| if len(modelStr) > 0 && modelStr[len(modelStr)-1] == ':' { | ||
| modelStr = modelStr[:len(modelStr)-1] | ||
| } | ||
|
|
||
| var req GeminiChatRequest | ||
| if err := json.Unmarshal(ctx.PostBody(), &req); err != nil { | ||
| ctx.SetStatusCode(fasthttp.StatusBadRequest) | ||
| json.NewEncoder(ctx).Encode(err) | ||
| return | ||
| // Add google/ prefix for Bifrost if not already present | ||
| processedModel := modelStr | ||
| if !strings.HasPrefix(modelStr, "google/") { | ||
| processedModel = "google/" + modelStr | ||
| } | ||
|
|
||
| bifrostReq := req.ConvertToBifrostRequest("google/" + modelStr) | ||
|
|
||
| bifrostCtx := lib.ConvertToBifrostContext(ctx) | ||
|
|
||
| result, err := g.client.ChatCompletionRequest(*bifrostCtx, bifrostReq) | ||
| if err != nil { | ||
| ctx.SetStatusCode(fasthttp.StatusInternalServerError) | ||
| json.NewEncoder(ctx).Encode(err) | ||
| return | ||
| // Set the model in the request | ||
| if geminiReq, ok := req.(*GeminiChatRequest); ok { | ||
| geminiReq.Model = processedModel | ||
| return nil | ||
| } | ||
|
|
||
| genAIResponse := DeriveGenAIFromBifrostResponse(result) | ||
| ctx.SetStatusCode(fasthttp.StatusOK) | ||
| ctx.SetContentType("application/json") | ||
| json.NewEncoder(ctx).Encode(genAIResponse) | ||
| return fmt.Errorf("invalid request type for GenAI") | ||
| } | ||
|
Pratham-Mishra04 marked this conversation as resolved.
|
||
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,37 @@ | ||
| package openai | ||
|
|
||
| import ( | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations" | ||
| ) | ||
|
|
||
| // OpenAIRouter holds route registrations for OpenAI endpoints. | ||
| // It supports standard chat completions and image-enabled vision capabilities. | ||
| type OpenAIRouter struct { | ||
| *integrations.GenericRouter | ||
| } | ||
|
|
||
| // NewOpenAIRouter creates a new OpenAIRouter with the given bifrost client. | ||
| func NewOpenAIRouter(client *bifrost.Bifrost) *OpenAIRouter { | ||
| routes := []integrations.RouteConfig{ | ||
| { | ||
| Path: "/openai/v1/chat/completions", | ||
| Method: "POST", | ||
| RequestType: &OpenAIChatRequest{}, | ||
| RequestConverter: func(req interface{}) *schemas.BifrostRequest { | ||
| if openaiReq, ok := req.(*OpenAIChatRequest); ok { | ||
| return openaiReq.ConvertToBifrostRequest() | ||
| } | ||
| return nil | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Pratham-Mishra04 marked this conversation as resolved.
|
||
| ResponseFunc: func(resp *schemas.BifrostResponse) interface{} { | ||
| return DeriveOpenAIFromBifrostResponse(resp) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| return &OpenAIRouter{ | ||
| GenericRouter: integrations.NewGenericRouter(client, routes), | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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.