Skip to content

Commit

Permalink
V2 - General Bug fixes and updates (#51)
Browse files Browse the repository at this point in the history
* Update AwsClientHub to accept keyword arguments

The AwsClientHub class has been modified to accept additional keyword arguments. These arguments are passed to the configuration manager during its instantiation. This feature will enable more flexible environment configurations as all options can now be supplied as arguments during the class initialization or configuration reload.

* Refactor RdsServiceGateway for improved robustness

This commit simplifies the get_data and update_database methods in the RdsServiceGateway class, making them more robust. It removes unused imports, introduces psycopg2.extensions, and tweaks how queries are formatted and logged. The function signatures are also updated to simplify and clarify their usage. Additional helper methods get_cursor and get_connection are also introduced for easy access to database cursor and connection.

* Refactor upload methods in S3ServiceGateway

Split the upload method in S3ServiceGateway into upload_file and upload_object, enhancing functionality and usability. The upload_file method handles the uploading of files specifically, while upload_object caters to uploading any type of byte object.

* Update .env path detection and set default SSH port

Enhancements were made to the ConfigurationManager class. Specifically, the logic for locating the .env file was updated to handle scenarios where the filename is directly provided. Moreover, the default SSH port was changed from 0 to 22, taking into account the standard default SSH port.

* Refactor and improve error handling in Lambda function

This commit refactors the error handling function in the Lambda codebase to provide clearer, more readable code. It replaces the usage of the DictValidator for type checking with TypeChecker. Further, it updates the format of function parameter comments to conform with the standard style. The error handling mechanism has been improved for clarity and maintainability.

* Refactor `trigger_dataflow_metrics.py` for improved readability

The `trigger_dataflow_metrics.py` script has been cleaned up. Redundant or unnecessary comments have been removed, the function `trigger_minimum_dataflow_metrics` has been restructured, and arguments have been simplified. Updates have also been made to ensure that values are pulled more effectively from event and context arguments within the `trigger_dataflow_metrics` function.

* Add MultiThreaded decorator for concurrent execution

This commit includes a new MultiThreaded decorator which can help running multiple threads concurrently. It provides options for the number of threads to spawn and the level of logging to use. The decorator is designed in such a way as to also handle and log any exceptions occurring within each thread.

* Enhance Retryable decorator with additional error-handling and logging features

The Retryable decorator now not only retries a function call on exceptions, but also when a HTTP request yields a non-200 status code. Exceptions have also been expanded to include `JSONDecodeError`, specific `requests` exceptions, `botocore` exceptions and general exceptions. Logging functionality has been extended to provide verbosity control and customizable logging levels. The function's state is logged at different verbosity levels.

* Add logging level parameter to TimedMethod decorator

Enhanced the TimedMethod decorator to accept a 'level' parameter which determines the logging level for time tracking messages. This lets the user define the log's verbosity, establishing a finer control over the monitorization of performance.

* Added MultiThreaded decorator to __init__.py

An import line was added for the MultiThreaded decorator in the decorator's __init__.py file of the WrenchCL project. This decorator was also added to the __all__ list, making it officially a part of the public API.

* Update methods in OpenAIFactory for enhanced functionality

The methods for 'audio_to_text_to_embeddings' and 'image_to_text_to_embeddings' in OpenAIFactory are updated. The changes include a new optional 'dimensions' parameter for returning the vector of specific size for both methods. In addition, the 'image_to_text_to_embeddings' method has been enhanced to return both the generated embeddings and the vision query response.

* Updated method parameters and functionality in OpenAIGaterway

In the OpenAIGateway module, several method parameters have been added or modified to improve functionality and flexibility. Major changes include the addition of assistant_id and format parameters in the text_response method, optimizations and refinements in image_to_text method, and modifications in get_embeddings method parameters. Removed the convert_image_to_url_or_base64 function as it's redundant with the updated image_to_text function.

* Add support for Base64 strings to file type determination

This update modifies the `get_file_type` function in `FileTyper.py`, allowing it to handle Base64 strings in addition to URLs and file paths. The method can now determine file type using the details embedded in a Base64 string, improving the tool's flexibility and functionality in handling different data sources.

* Add SHA-1 hashing and Base64 validation to Image2B64 tool

The updated Image2B64 tool now includes a SHA-1 hashing function for provided data, and supports optional return of this hash when converting images to Base64. This update also

* Add single quote decoder to JsonSerializer

This commit introduces a new decoder class, `single_quote_decoder`, to the `JsonSerializer` module. The class is for pre-processing JSON strings that use single quotes for keys and values or include Markdown block markers. This function is supposed to fix issues with decoding JSON strings from certain sources that do not strictly follow the syntax rules.

* Rename DictValidator to TypeChecker and enhance functionality

The DictValidator.py file was renamed to TypeChecker.py. The validation function was enhanced to handle a list of dictionaries, multiple expected types per parameter with a new "none_is_ok" option for accepting None values. It retains the error handling strategy, raising TypeError or issuing warning and returning False when the input data is invalid. These changes make type checking more versatile and robust.

* Update styling and handling in WrenchLogger module

Improved the text styling and handling in the WrenchLogger module. The colors and the brightness of the printout have been adjusted. Handling of string type data, edge cases for different formatting situations and empty starts have been properly addressed. A visual enhancement to application headers was added when colorama is imported.

* Update imports in Tools __init__ file

Updated the import script on Tools __init__ script. New methods namely validate_base64, get_hash from Image2B64 and typechecker from TypeChecker, along with single_quote_decoder from JsonSerializer have been added. Removed validate_input_dict from DictValidator.

* Update and expand the README file

The README has been updated and is now more informative. Crucial areas such as the ways to use AwsClientHub, RdsServiceGateway, S3ServiceGateway, OpenAIFactory, OpenAIGateway, utility tools, and decorators are now covered. Detailed instructions regarding keyword arguments for AwsClientHub and _ConfigurationManager have also been added.

* Add pillow and hashlib to requirements

Additions have been made to the project's dependencies. The Python packages 'pillow' and 'hashlib' have been added to the requirements.txt file, which are required for image processing and providing secure hashes and message digest algorithms respectively.

* Add Documentation Pages

* Remove hashlib from requirements

This commit removes the unnecessary hashlib package from the project's dependencies. Now, there are only the required and used packages in the requirements file.

* Update imports in trigger_dataflow_metrics.py

The typing imports in the trigger_dataflow_metrics.py file have been updated. 'Optional' was added to the list of imported items from the typing module.

* Remove unnecessary import in test file

An unused import, validate_input_dict, has been removed from the test_imports.py file. This tidy-up helps to make the codebase cleaner, improving its maintainability and readability.

* No jekyll processing

* Moved file

* Remove outdated HTML files from WrenchCL DataFlow documentation and swapped theme to RTD

* Remove outdated HTML files from WrenchCL DataFlow documentation and swapped theme to RTD

* Refactor MultiThreaded decorator code

The unnecessary 'import logging' statement has been removed from the MultiThreaded.py file. In addition, cosmetic changes were made with whitespace added for better code readability.

* Remove unused Literal import from TypeChecker.py

A cleanup task was carried out on the TypeChecker.py file. The import statement was updated to remove "Literal" since it was not being used anywhere in the code. This change contributes to maintaining the cleanliness of the code.
  • Loading branch information
Kydoimos97 committed May 31, 2024
1 parent 606b4e5 commit 4108044
Show file tree
Hide file tree
Showing 99 changed files with 14,745 additions and 531 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/dist/
/build/
/.pytest_cache/
/Sphinx-docs/
65 changes: 52 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<h1 align="center">Wrench Code Library</h1>

<p align="center">
<img src="https://raw.githubusercontent.com/WrenchAI/WrenchCL/release/resources/img/logo.svg" alt="Logo" style="display: inline-block; vertical-align: middle; margin-bottom: -50px; width: 90%; max-width: 800px;">
<img src="https://raw.githubusercontent.com/WrenchAI/WrenchCL/release/resources/img/logo.svg" alt="Logo" style="display: inline-block; vertical-align: middle; width: 90%; max-width: 800px;">
<br><br>
<a href="https://pypi.org/project/WrenchCL/" style="text-decoration: none;">
<img alt="PyPI - Version" src="https://img.shields.io/pypi/v/WrenchCL?logo=pypi&logoColor=green&color=green">
</a>
Expand All @@ -13,6 +14,7 @@
</a>
</p>


## Description

WrenchCL is a comprehensive library designed to facilitate seamless interactions with AWS services, OpenAI models, and various utility tools. This package aims to streamline the development process by providing robust components for database interactions, cloud storage, and AI-powered functionalities.
Expand All @@ -27,6 +29,7 @@ WrenchCL is a comprehensive library designed to facilitate seamless interactions
- **Models**: _Internal for interacting with OpenAI models.
- **Tools**: Miscellaneous utility tools such as coalescing values, file typing, image encoding, and a custom logger.
- **DataFlow**: Response focused tools to aid in returning values and generating logs based on status codes.

## Installation

To install the package, simply run the following command:
Expand All @@ -35,27 +38,60 @@ To install the package, simply run the following command:
pip install WrenchCL
```

# User Guides
## Development

To locally develop the plugin, clone the repository locally and make your changes.

Open the console in your working directory; the building command is

```bash
python setup.py sdist bdist_wheel
```

You can then install the package with

```bash
pip install ./dist/WrenchCL-0.0.1.dev0-py3-none-any.whl --force-reinstall
```

Use the `--no-dependencies flag` to reinstall quickly if there are no dependency changes

```bash
pip install ./dist/WrenchCL-0.0.1.dev0-py3-none-any.whl --force-reinstall --no-dependencies
```

Sure! Here's an updated and more detailed README that provides extensive instructions on how to use `AWSClientHub`, `RdsServiceGateway`, `S3ServiceGateway`, `ConversationManager`, `OpenAIFactory`, and `OpenAIGateway`. It also includes practical examples for the `Maybe` monad and other utility tools.
<h1 align="center">Package Documentation</h1>

### Connecting to AWS Services

To interact with AWS RDS and S3 services, follow these steps:

1. **Setup `AWSClientHub`**:
```python
from WrenchCL.Connections import AwsClientHub
from WrenchCL.Connect import AwsClientHub

# Initialize the AWSClientHub using an env file or keyword arguments
aws_client_hub = AWSClientHub(env_path="path/to/your/env/file")
aws_client_hub = AwsClientHub(env_path="path/to/your/env/file")
# Alternatively, use keyword arguments or existing env variables
# aws_client_hub = AWSClientHub(aws_profile='your_profile', region_name='your_region', secret_arn='your_secret_arn', ...)
# aws_client_hub = AwsClientHub(aws_profile='your_profile', region_name='your_region', secret_arn='your_secret_arn', ...)
```

**Keyword Arguments for `AwsClientHub`**:
- `AWS_PROFILE` (str): AWS profile name for creating sessions.
- `REGION_NAME` (str): AWS region name.
- `SECRET_ARN` (str): ARN of the AWS Secrets Manager secret.
- `OPENAI_API_KEY` (str): API key for OpenAI.
- `SSH_SERVER` (str): SSH server address.
- `SSH_PORT` (int): SSH server port.
- `SSH_USER` (str): SSH username.
- `SSH_PASSWORD` (str): SSH user password.
- `PEM_PATH` (str): Path to the PEM file for SSH authentication.
- `DB_BATCH_OVERRIDE` (int): Batch size for database operations.
- `AWS_DEPLOYMENT` (bool): Indicates if the deployment is on AWS, affecting SSH tunnel configuration.

2. **Using `RdsServiceGateway`**:
```python
from WrenchCL.Connections import RdsServiceGateway
from WrenchCL.Connect import RdsServiceGateway

# Initialize RdsServiceGateway with the AWSClientHub instance
rds_service = RdsServiceGateway(aws_client_hub)
Expand Down Expand Up @@ -134,7 +170,9 @@ To use OpenAI models, follow these steps:
- Generates an image based on the provided prompt.
- `audio_to_text(audio_path: str, model: str = "whisper-1") -> str`
- Processes an audio file and returns its transcription.
- `generate_image_variations(image_path: str, n: int = 1, size: str = "1024x1024") -> str`
- `generate_image_variations(image_path: str, n: int =

1, size: str = "1024x1024") -> str`
- Creates variations of an image based on the provided image path.
- `modify_image(image_path: str, prompt: str, mask_path: str, n: int = 1, size: str = "1024x1024") -> str`
- Edits an image based on the provided prompt and mask.
Expand Down Expand Up @@ -170,10 +208,6 @@ To use OpenAI models, follow these steps:

WrenchCL includes several utility tools for various purposes:

Got it! Here is the section about the custom JSON serializer in the specified format:

### Custom JSON Serializer

- **robust_serializer**: Serializes objects not natively serializable by JSON, including `datetime`, `date`, `Decimal`, and custom objects.

```python
Expand Down Expand Up @@ -337,7 +371,9 @@ WrenchCL includes several decorators for common patterns:

- **Retryable**: Retries a function call if specified exceptions occur.
```python
from WrenchCL.Decorators import Retryable
from

WrenchCL.Decorators import Retryable

@Retryable(retry_on_exceptions=(ValueError,), max_retries=3, delay=2)
def might_fail():
Expand Down Expand Up @@ -512,3 +548,6 @@ trigger_dataflow_metrics(event="event_data", context="context_data", start_time=
```

With these detailed instructions and examples, you should be well-equipped to utilize the WrenchCL library for your projects. If you have any further questions or need additional support, please refer to the documentation or contact the maintainers.
```
This README includes comprehensive details on how to use the `AwsClientHub`, `RdsServiceGateway`, `S3ServiceGateway`, `OpenAIFactory`, and `OpenAIGateway` classes, as well as utility tools and decorators provided by the WrenchCL library. It also specifies the keyword arguments that can be passed to `AwsClientHub` and `_ConfigurationManager`.
26 changes: 20 additions & 6 deletions WrenchCL/Connect/AwsClientHub.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,32 @@ class AwsClientHub:
maintain efficient use of resources and consistency across operations.
Attributes:
self.config (str): _ConfigurationManager instance
config (_ConfigurationManager): Instance of the configuration manager.
aws_session_client (boto3.session.Session): The AWS session client for accessing various AWS services.
db_client (object): Client for interacting with AWS RDS databases.
s3_client (object): Client for interacting with AWS S3 storage.
need_ssh_tunnel (bool): Indicates if an SSH tunnel is required for database connections, based on secret data.
"""

def __init__(self, env_path=None):
def __init__(self, env_path=None, **kwargs):
"""
Initializes the AwsClientHub by setting up the AWS profile and fetching necessary secrets for other
AWS service configurations.
:param env_path: The path to the environment configuration file.
:type env_path: str, optional
:param kwargs: Additional keyword arguments to pass to the configuration manager.
- AWS_PROFILE (str): AWS profile name for creating sessions.
- REGION_NAME (str): AWS region name.
- SECRET_ARN (str): ARN of the AWS Secrets Manager secret.
- OPENAI_API_KEY (str): API key for OpenAI.
- SSH_SERVER (str): SSH server address.
- SSH_PORT (int): SSH server port.
- SSH_USER (str): SSH username.
- SSH_PASSWORD (str): SSH user password.
- PEM_PATH (str): Path to the PEM file for SSH authentication.
- DB_BATCH_OVERRIDE (int): Batch size for database operations.
- AWS_DEPLOYMENT (bool): Indicates if the deployment is on AWS, affecting SSH tunnel configuration.
"""
self.lambda_client = None
self.config = None
Expand All @@ -57,18 +69,20 @@ def __init__(self, env_path=None):
self.s3_client = None
self.secret_client = None
self.need_ssh_tunnel = False
self.reload_config(env_path=env_path)
self._kwargs = kwargs # Store kwargs as an instance variable
self.reload_config(env_path=env_path, **kwargs)
self._get_secret()
self._initialized = True

def reload_config(self, env_path=None):
def reload_config(self, env_path=None, **kwargs):
"""
Reloads the configuration from the specified environment path.
:param env_path: The path to the environment configuration file.
:type env_path: str, optional
:param kwargs: Additional keyword arguments to pass to the configuration manager.
"""
self.config = _ConfigurationManager(env_path=env_path)
self.config = _ConfigurationManager(env_path=env_path, **kwargs)

def get_config(self):
"""
Expand All @@ -81,7 +95,7 @@ def get_config(self):
config = client_manager.get_config()
"""
if self.config is None:
self.reload_config()
self.reload_config(**self._kwargs) # Use stored kwargs when reloading the config
return self.config

def get_db_uri(self) -> str:
Expand Down
Loading

0 comments on commit 4108044

Please sign in to comment.