You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains 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 update introduces a max_retries option for the OpenAIEncoder and AzureOpenAIEncoder classes. This option allows users to specify the maximum number of retry attempts for API calls in case of errors. By default, it is set to 3 retries. If no retries are needed, you can set max_retries to 0.
Introduced a max_retries option for both OpenAIEncoder and AzureOpenAIEncoder classes, allowing users to specify the maximum number of retry attempts for API calls.
Implemented exponential backoff retry logic for handling API call failures in both synchronous and asynchronous methods.
Enhanced error logging to provide more detailed information on exceptions.
Removed redundant error message handling to streamline code.
Changes walkthrough 📝
Relevant files
Enhancement
openai.py
Add retry logic and error handling to OpenAIEncoder
semantic_router/encoders/openai.py
Added max_retries attribute to OpenAIEncoder class with a default value of 3.
Implemented retry logic using exponential backoff for API calls.
Improved error logging and handling for API call failures.
ashraq1455
changed the title
feat: add max_retries option for OpenAI and AzureOpenAI encoders
feat: add max_retries to OpenAI and Azure encoders
Aug 18, 2024
Exception Handling The exception handling in the retry logic raises a generic ValueError with the message "OpenAI API call failed." This could be improved by providing more specific error types or more detailed error messages to help with debugging and error resolution.
Hardcoded Test Exception There is a hardcoded raise OpenAIError("Test") within the retry logic. This seems to be for testing purposes and should be removed or commented out for production code.
Exception Handling Similar to openai.py, the exception handling could be more specific than just raising a ValueError with a generic message. More specific exceptions or messages could aid in better error handling and debugging.
Hardcoded Test Exception The retry logic includes a raise OpenAIError("Test") which appears to be for testing. This should be removed or properly handled before moving to production.
-raise OpenAIError("Test")+# Removed test exception raise
Suggestion importance[1-10]: 9
Why: The hardcoded exception raise is likely for testing and should be removed or replaced with proper error handling to avoid unintended disruptions in production code.
9
Enhancement
Skip retry loop when max_retries is zero
Implement a mechanism to handle the case when max_retries is zero, to skip the retry loop entirely, optimizing performance when no retries are intended.
-sleep(2**j)+sleep(min(2**j, 120)) # Caps the sleep time to a maximum of 120 seconds
Suggestion importance[1-10]: 8
Why: Capping the sleep time during retries prevents excessive delays, which is beneficial for maintaining reasonable response times and system performance.
8
Possible issue
Validate max_retries to be a non-negative integer
The max_retries attribute should be validated to ensure it is a non-negative integer. This prevents potential runtime errors or logical bugs due to negative retry attempts.
-max_retries: int = 3+max_retries: int = max(0, int(value)) # Ensures non-negative integer
Suggestion importance[1-10]: 7
Why: Ensuring max_retries is a non-negative integer is a good practice to prevent logical errors and runtime exceptions. However, the suggested code does not show how value is defined or used, which could lead to confusion.
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.
User description
This update introduces a
max_retries
option for theOpenAIEncoder
andAzureOpenAIEncoder
classes. This option allows users to specify the maximum number of retry attempts for API calls in case of errors. By default, it is set to 3 retries. If no retries are needed, you can setmax_retries
to 0.Example:
PR Type
enhancement
Description
max_retries
option for bothOpenAIEncoder
andAzureOpenAIEncoder
classes, allowing users to specify the maximum number of retry attempts for API calls.Changes walkthrough 📝
openai.py
Add retry logic and error handling to OpenAIEncoder
semantic_router/encoders/openai.py
max_retries
attribute toOpenAIEncoder
class with a defaultvalue of 3.
zure.py
Add retry logic and error handling to AzureOpenAIEncoder
semantic_router/encoders/zure.py
max_retries
attribute toAzureOpenAIEncoder
class with a defaultvalue of 3.