-
Notifications
You must be signed in to change notification settings - Fork 830
feat: Add embedding support to providers #95
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
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4183d17
feat: Add embedding support to providers
50b002e
Merge branch 'main' into cjh-embedding-redux
2fe5e65
backout transport changes for now.
67fb522
add newUnsupportedOperationError helper
9375b44
move Embedding method to take *EmbeddingInput
38953e1
feat: comprehensive embedding provider improvements and optimizations
837fb0d
refactor: reorganize bifrost.go methods to follow idiomatic Go structure
522c7d5
Revert "refactor: reorganize bifrost.go methods to follow idiomatic G…
7147b0a
lighter touch refactoring of bifrost.go
connyay 4ff1cbf
simplify cohere requestbody + extraparam handling
989a238
simplify bedrock rawResponse
0b9034e
simplify bedrock rawResponse, again
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ type RequestType string | |
| const ( | ||
| TextCompletionRequest RequestType = "text_completion" | ||
| ChatCompletionRequest RequestType = "chat_completion" | ||
| EmbeddingRequest RequestType = "embedding" | ||
| ) | ||
|
|
||
| // ChannelMessage represents a message passed through the request channel. | ||
|
|
@@ -452,6 +453,18 @@ func (bifrost *Bifrost) requestWorker(provider schemas.Provider, queue chan Chan | |
| } else { | ||
| result, bifrostError = provider.ChatCompletion(req.Context, req.Model, key, *req.Input.ChatCompletionInput, req.Params) | ||
| } | ||
| } else if req.Type == EmbeddingRequest { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Pratham-Mishra04 as a code hygiene - I think nuking this if-else ladder is critical. We can probably use some factory pattern where type executor func (provider, message)
type MessageExecutors map[string]executor
const executors = MessageExecutors{<add pre-defined handlers for each message>} |
||
| if req.Input.EmbeddingInput == nil { | ||
| bifrostError = &schemas.BifrostError{ | ||
| IsBifrostError: false, | ||
| Error: schemas.ErrorField{ | ||
| Message: "input not provided for embedding request", | ||
| }, | ||
| } | ||
| break // Don't retry client errors | ||
| } else { | ||
| result, bifrostError = provider.Embedding(req.Context, req.Model, key, *req.Input.EmbeddingInput, req.Params) | ||
| } | ||
|
akshaydeo marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| bifrost.logger.Debug(fmt.Sprintf("Request for provider %s completed", provider.GetProviderKey())) | ||
|
|
@@ -788,6 +801,131 @@ func (bifrost *Bifrost) tryChatCompletion(req *schemas.BifrostRequest, ctx conte | |
| } | ||
| } | ||
|
|
||
| // EmbeddingRequest sends an embedding request to the specified provider. | ||
| // It handles plugin hooks, request validation, response processing, and fallback providers. | ||
| // If the primary provider fails, it will try each fallback provider in order until one succeeds. | ||
| func (bifrost *Bifrost) EmbeddingRequest(ctx context.Context, req *schemas.BifrostRequest) (*schemas.BifrostResponse, *schemas.BifrostError) { | ||
| if req == nil { | ||
| return nil, newBifrostErrorFromMsg("bifrost request cannot be nil") | ||
| } | ||
|
|
||
| if req.Provider == "" { | ||
| return nil, newBifrostErrorFromMsg("provider is required") | ||
| } | ||
|
|
||
| if req.Model == "" { | ||
| return nil, newBifrostErrorFromMsg("model is required") | ||
| } | ||
|
|
||
| // Try the primary provider first | ||
| primaryResult, primaryErr := bifrost.tryEmbedding(req, ctx) | ||
| if primaryErr == nil { | ||
| return primaryResult, nil | ||
| } | ||
|
|
||
| // If primary provider failed and we have fallbacks, try them in order | ||
| if len(req.Fallbacks) > 0 { | ||
| for _, fallback := range req.Fallbacks { | ||
| // Check if we have config for this fallback provider | ||
| _, err := bifrost.account.GetConfigForProvider(fallback.Provider) | ||
| if err != nil { | ||
| bifrost.logger.Warn(fmt.Sprintf("Config not found for provider %s, skipping fallback: %v", fallback.Provider, err)) | ||
| continue | ||
| } | ||
|
|
||
| // Create a new request with the fallback provider and model | ||
| fallbackReq := *req | ||
| fallbackReq.Provider = fallback.Provider | ||
| fallbackReq.Model = fallback.Model | ||
|
|
||
| // Try the fallback provider | ||
| result, fallbackErr := bifrost.tryEmbedding(&fallbackReq, ctx) | ||
| if fallbackErr == nil { | ||
| bifrost.logger.Info(fmt.Sprintf("Successfully used fallback provider %s with model %s", fallback.Provider, fallback.Model)) | ||
| return result, nil | ||
| } | ||
| if fallbackErr.Error.Type != nil && *fallbackErr.Error.Type == schemas.RequestCancelled { | ||
| return nil, fallbackErr | ||
| } | ||
|
|
||
| bifrost.logger.Warn(fmt.Sprintf("Fallback provider %s failed: %s", fallback.Provider, fallbackErr.Error.Message)) | ||
| } | ||
| } | ||
|
|
||
| // All providers failed, return the original error | ||
| return nil, primaryErr | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // tryEmbedding attempts an embedding request with a single provider. | ||
| // This is a helper function used by EmbeddingRequest to handle individual provider attempts. | ||
| func (bifrost *Bifrost) tryEmbedding(req *schemas.BifrostRequest, ctx context.Context) (*schemas.BifrostResponse, *schemas.BifrostError) { | ||
| queue, err := bifrost.getProviderQueue(req.Provider) | ||
| if err != nil { | ||
| return nil, newBifrostError(err) | ||
| } | ||
|
|
||
| pipeline := NewPluginPipeline(bifrost.plugins, bifrost.logger) | ||
| preReq, preResp, preCount := pipeline.RunPreHooks(&ctx, req) | ||
| if preResp != nil { | ||
| resp, bifrostErr := pipeline.RunPostHooks(&ctx, preResp, nil, preCount) | ||
| if bifrostErr != nil { | ||
| return nil, bifrostErr | ||
| } | ||
| return resp, nil | ||
| } | ||
| if preReq == nil { | ||
| return nil, newBifrostErrorFromMsg("bifrost request after plugin hooks cannot be nil") | ||
| } | ||
|
|
||
| msg := bifrost.getChannelMessage(*preReq, EmbeddingRequest) | ||
| msg.Context = ctx | ||
|
|
||
| select { | ||
| case queue <- *msg: | ||
| // Message was sent successfully | ||
| case <-ctx.Done(): | ||
| bifrost.releaseChannelMessage(msg) | ||
| return nil, newBifrostErrorFromMsg("request cancelled while waiting for queue space") | ||
| default: | ||
| if bifrost.dropExcessRequests { | ||
| bifrost.releaseChannelMessage(msg) | ||
| bifrost.logger.Warn("Request dropped: queue is full, please increase the queue size or set dropExcessRequests to false") | ||
| return nil, newBifrostErrorFromMsg("request dropped: queue is full") | ||
| } | ||
| if ctx == nil { | ||
| ctx = bifrost.backgroundCtx | ||
| } | ||
| select { | ||
| case queue <- *msg: | ||
| // Message was sent successfully | ||
| case <-ctx.Done(): | ||
| bifrost.releaseChannelMessage(msg) | ||
| return nil, newBifrostErrorFromMsg("request cancelled while waiting for queue space") | ||
| } | ||
| } | ||
|
|
||
| var result *schemas.BifrostResponse | ||
| var resp *schemas.BifrostResponse | ||
| select { | ||
| case result = <-msg.Response: | ||
| resp, bifrostErr := pipeline.RunPostHooks(&ctx, result, nil, len(bifrost.plugins)) | ||
| if bifrostErr != nil { | ||
| bifrost.releaseChannelMessage(msg) | ||
| return nil, bifrostErr | ||
| } | ||
| bifrost.releaseChannelMessage(msg) | ||
| return resp, nil | ||
| case bifrostErrVal := <-msg.Err: | ||
| bifrostErrPtr := &bifrostErrVal | ||
| resp, bifrostErrPtr = pipeline.RunPostHooks(&ctx, nil, bifrostErrPtr, len(bifrost.plugins)) | ||
| bifrost.releaseChannelMessage(msg) | ||
| if bifrostErrPtr != nil { | ||
| return nil, bifrostErrPtr | ||
| } | ||
| return resp, nil | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Cleanup gracefully stops all workers when triggered. | ||
| // It closes all request channels and waits for workers to exit. | ||
| func (bifrost *Bifrost) Cleanup() { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.