-
Notifications
You must be signed in to change notification settings - Fork 130
[SDCICD-1830] Add retry mechanism with fallback model for LLM analysis engines #3231
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
varunraokadaparthi
wants to merge
4
commits into
openshift:main
Choose a base branch
from
varunraokadaparthi:retry-mechanism
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
4 commits
Select commit
Hold shift + click to select a range
9b2d95d
[SDCICD-1830] Add retry mechanism with fallback model for LLM analysi…
varunraokadaparthi 5b4a897
[SDCICD-1830] Improve error naming and combine both model errors on e…
varunraokadaparthi 8217630
[SDCICD-1830] Fix gofumpt formatting
varunraokadaparthi 7dbc4df
[SDCICD-1830] Add retry mechanism with flat 30s delay, fallback model…
varunraokadaparthi 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package llm | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrNoResponseCandidates = errors.New("no response candidates from gemini") | ||
| ErrNoContentInResponse = errors.New("no content in gemini response") | ||
| ErrToolCallFailed = errors.New("failed to handle tool call") | ||
| ErrMaxIterations = errors.New("max iterations reached without final response") | ||
| ) |
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,123 @@ | ||
| package llm | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| const ( | ||
| primaryRetries = 2 | ||
| fallbackRetries = 0 | ||
| retryDelay = 30 * time.Second | ||
| ) | ||
|
|
||
| var ( | ||
| retryDelayOverride time.Duration | ||
|
|
||
| retryableStatusCodes = map[int]bool{ | ||
| 429: true, // Rate limit | ||
| 500: true, // Internal server error | ||
| 502: true, // Bad gateway | ||
| 503: true, // Service unavailable | ||
| } | ||
| ) | ||
|
|
||
| func AnalyzeWithRetry( | ||
| ctx context.Context, | ||
| logger logr.Logger, | ||
| primaryFn func() (*AnalysisResult, error), | ||
| fallbackFn func() (*AnalysisResult, error), | ||
| ) (*AnalysisResult, error) { | ||
| result, exhausted, primaryErr := retryLoop(ctx, logger, "primary", primaryFn, primaryRetries) | ||
| if primaryErr == nil { | ||
| return result, nil | ||
| } | ||
|
|
||
| if !exhausted { | ||
| return nil, primaryErr | ||
| } | ||
|
|
||
| logger.Info("switching to fallback model", "reason", "primary model retries exhausted") | ||
|
|
||
| result, _, fallbackErr := retryLoop(ctx, logger, "fallback", fallbackFn, fallbackRetries) | ||
| if fallbackErr != nil { | ||
| logger.Error(errors.Join(primaryErr, fallbackErr), "LLM analysis failed after all retries on both models") | ||
| return nil, fmt.Errorf("LLM analysis unavailable: both primary and fallback models failed after retries: %w", errors.Join(primaryErr, fallbackErr)) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func retryLoop(ctx context.Context, logger logr.Logger, modelName string, fn func() (*AnalysisResult, error), maxRetries int) (*AnalysisResult, bool, error) { | ||
| var lastErr error | ||
|
|
||
| for attempt := 0; attempt <= maxRetries; attempt++ { | ||
| result, err := fn() | ||
| if err == nil { | ||
| if attempt > 0 { | ||
| logger.Info("LLM analysis succeeded after retry", "model", modelName, "attempt", attempt) | ||
| } | ||
| return result, false, nil | ||
| } | ||
|
|
||
| lastErr = err | ||
|
|
||
| if !isRetryable(err) { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| if attempt < maxRetries { | ||
| backoff := retryDelay | ||
| if retryDelayOverride > 0 { | ||
| backoff = retryDelayOverride | ||
| } | ||
| logger.Info("retrying LLM analysis", "model", modelName, "attempt", attempt+1, "maxRetries", maxRetries, "backoff", backoff, "error", err.Error()) | ||
|
|
||
| timer := time.NewTimer(backoff) | ||
| select { | ||
| case <-ctx.Done(): | ||
| timer.Stop() | ||
| return nil, false, fmt.Errorf("retry canceled: %w (last LLM error: %v)", ctx.Err(), lastErr) | ||
| case <-timer.C: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil, true, lastErr | ||
| } | ||
|
|
||
| func isRetryable(err error) bool { | ||
| var apiErr genai.APIError | ||
| if errors.As(err, &apiErr) { | ||
| return retryableStatusCodes[apiErr.Code] | ||
| } | ||
|
|
||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return true | ||
| } | ||
|
|
||
| if errors.Is(err, context.Canceled) { | ||
| return false | ||
| } | ||
|
|
||
| var netErr net.Error | ||
| if errors.As(err, &netErr) && netErr.Timeout() { | ||
| return true | ||
| } | ||
|
|
||
| if errors.Is(err, ErrNoResponseCandidates) || errors.Is(err, ErrNoContentInResponse) { | ||
| return true | ||
| } | ||
|
|
||
| if errors.Is(err, ErrToolCallFailed) || errors.Is(err, ErrMaxIterations) { | ||
| return false | ||
| } | ||
|
|
||
| return false | ||
| } | ||
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.