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
#173 An implementation for Bedrock embedding models.
Now Semantic router will support generating embeddings using AWS Bedrock. It integrates the BedrockEncoder into the encoder modules and adds it to the AutoEncoder class. Additionally, it sets up unit tests for BedrockEncoder functionalities, error handling, and creates a Jupyter notebook documentation for demonstrating its usage. Boto3 has been added as a dependency for AWS operations related to Bedrock.
PR Type
enhancement, documentation
Description
Added BedrockEncoder class for embedding generation using AWS Bedrock.
Integrated BedrockEncoder into the encoder modules and added it to the AutoEncoder class.
Set up unit tests for BedrockEncoder functionalities and error handling.
Created a Jupyter notebook documentation for demonstrating the usage of BedrockEncoder.
Added boto3 as a dependency for AWS operations related to Bedrock.
Changes walkthrough 📝
Relevant files
Enhancement
__init__.py
Integrate BedrockEncoder into Encoder Modules
semantic_router/encoders/init.py
Imported BedrockEncoder in the module.
Added BedrockEncoder to the list of available encoders.
Added conditional initialization for BedrockEncoder in AutoEncoder class.
4, because the PR introduces a new encoder class with a significant amount of new functionality, including AWS integration and error handling. The complexity of the AWS client initialization and the embedding generation logic requires careful review to ensure security and functionality.
🧪 Relevant tests
Yes
⚡ Possible issues
Possible Bug: The chunk_strings function in bedrock.py uses a hardcoded MAX_WORDS value which might not be suitable for all types of documents. This could lead to incomplete or incorrect embeddings for longer texts.
Error Handling: The error handling in the __call__ method of BedrockEncoder might not catch all potential exceptions from the AWS SDK, leading to unhandled exceptions in some cases.
🔒 Security concerns
Sensitive information exposure: The code handles AWS credentials and session tokens, which are sensitive. While the PR retrieves these from environment variables or parameters, ensuring these are not logged or exposed through exceptions is crucial.
Code feedback:
relevant file
semantic_router/encoders/bedrock.py
suggestion
Consider parameterizing MAX_WORDS in the chunk_strings function or adjusting it based on the document length to handle longer texts more effectively. This change would make the function more flexible and capable of dealing with a variety of document sizes without risking data loss or inefficiency. [important]
Implement more comprehensive exception handling around the AWS SDK calls in the __call__ method to catch specific exceptions like network issues or API limits. This would improve the robustness of the encoder and provide clearer error messages for troubleshooting. [important]
Add a validation step to check the input_type in the __init__ method of BedrockEncoder. This ensures that only supported input types are used, which can prevent runtime errors and make the code safer and more predictable. [medium]
Consider using a more secure method to handle AWS credentials, such as AWS IAM roles for EC2 or other AWS services, which can avoid hardcoding or passing credentials directly and reduce the risk of credential leakage. [important]
Add error handling for JSON parsing in the __call__ method
The call method should handle potential JSON parsing errors when calling json.loads to prevent unhandled exceptions if the response is not in the expected format.
-response_body = json.loads(response.get("body").read())+try:+ response_body = json.loads(response.get("body").read())+except json.JSONDecodeError:+ raise ValueError("Failed to parse JSON response from Bedrock service.")
Suggestion importance[1-10]: 8
Why: This suggestion addresses a potential bug by adding error handling for JSON parsing, which is crucial for preventing runtime errors due to unexpected response formats. This improvement significantly enhances the robustness of the method.
8
Best practice
Use more specific exception handling for boto3 errors
The exception handling in the _initialize_client method could be more specific by catching boto3 related exceptions instead of a general Exception. This would make the error handling more precise and informative.
-except Exception as err:+except boto3.exceptions.BotoCoreError as err:
raise ValueError(
- f"The Bedrock client failed to initialize. Error: {err}"+ f"The Bedrock client failed to initialize due to a BotoCoreError. Error: {err}"
) from err
Suggestion importance[1-10]: 7
Why: This suggestion improves error handling by specifying the type of exceptions related to boto3, which enhances the clarity and maintainability of the error handling process.
7
Enhancement
Allow customization of the MAX_WORDS parameter in the chunk_strings method
The method chunk_strings uses a hardcoded MAX_WORDS value which limits flexibility. Consider making MAX_WORDS a parameter of the BedrockEncoder class or method to allow customization based on different use cases.
Why: Making MAX_WORDS customizable is a valid enhancement that increases the flexibility of the chunk_strings method. This change allows the method to be more adaptable to various text sizes and use cases.
6
Maintainability
Reorder imports alphabetically to improve code readability
It's recommended to maintain imports in alphabetical order for better readability and maintainability. The import of BedrockEncoder should be placed in the correct order.
Why: The suggestion correctly identifies a minor maintainability improvement by reordering imports alphabetically. However, the existing and improved code snippets are identical, indicating a mistake in the suggestion's presentation.
5
theanupllm
changed the title
feat/bedrock encoder
feat: Adding BedrockEncoder
May 14, 2024
❗ Current head 73cf6b8 differs from pull request most recent head a4b13a6. Consider uploading reports for the commit a4b13a6 to get more accurate results
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
#173 An implementation for Bedrock embedding models.
Now Semantic router will support generating embeddings using AWS Bedrock. It integrates the BedrockEncoder into the encoder modules and adds it to the AutoEncoder class. Additionally, it sets up unit tests for BedrockEncoder functionalities, error handling, and creates a Jupyter notebook documentation for demonstrating its usage. Boto3 has been added as a dependency for AWS operations related to Bedrock.
PR Type
enhancement, documentation
Description
BedrockEncoder
class for embedding generation using AWS Bedrock.BedrockEncoder
into the encoder modules and added it to theAutoEncoder
class.BedrockEncoder
functionalities and error handling.BedrockEncoder
.boto3
as a dependency for AWS operations related to Bedrock.Changes walkthrough 📝
__init__.py
Integrate BedrockEncoder into Encoder Modules
semantic_router/encoders/init.py
class.
bedrock.py
Implement BedrockEncoder with AWS Integration
semantic_router/encoders/bedrock.py
client setup, and embedding generation.
credentials and configuration.
schema.py
Add BEDROCK Encoder Type
semantic_router/schema.py
defaults.py
Set Default Bedrock Embedding Model
semantic_router/utils/defaults.py
test_bedrock.py
Add Unit Tests for BedrockEncoder
tests/unit/encoders/test_bedrock.py
generation, and error handling.
bedrock.ipynb
Documentation for BedrockEncoder Usage
docs/encoders/bedrock.ipynb
BedrockEncoder.
pyproject.toml
Add Boto3 Dependency for Bedrock Support
pyproject.toml