Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

HOT FIX: Quick started error with custom python tool #401

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions aixplain/factories/file_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from aixplain.utils.file_utils import upload_data
from typing import Any, Dict, Text, Union, Optional, List


MB_1 = 1048576
MB_25 = 26214400
MB_50 = 52428800
Expand Down Expand Up @@ -94,10 +95,10 @@ def check_storage_type(cls, input_link: Any) -> StorageType:
if os.path.exists(input_link) is True and os.path.isfile(input_link) is True:
return StorageType.FILE
elif (
input_link.startswith("s3://")
or input_link.startswith("http://")
or input_link.startswith("https://")
or validators.url(input_link)
input_link.startswith("s3://") # noqa
or input_link.startswith("http://") # noqa
or input_link.startswith("https://") # noqa
or validators.url(input_link) # noqa
):
return StorageType.URL
else:
Expand Down
9 changes: 7 additions & 2 deletions aixplain/modules/agent/tool/custom_python_code_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from typing import Text, Union, Callable
from aixplain.modules.agent.tool import Tool
import logging


class CustomPythonCodeTool(Tool):
Expand All @@ -42,9 +43,13 @@ def to_dict(self):
}

def validate(self):
from aixplain.modules.model.utils import parse_code
from aixplain.modules.model.utils import parse_code_decorated

self.code, _, description, name = parse_code(self.code)
if not str(self.code).startswith("s3://"):
self.code, _, description, name = parse_code_decorated(self.code)
else:
logging.info("Utility Model Already Exists, skipping code validation")
return

# Set description from parsed code if not already set
if not self.description or self.description.strip() == "":
Expand Down
14 changes: 8 additions & 6 deletions aixplain/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ def upload_data(
bucket_name = re.findall(r"https://(.*?).s3.amazonaws.com", presigned_url)[0]
s3_link = f"s3://{bucket_name}/{path}"
return s3_link
except Exception as e:
except Exception:
if nattempts > 0:
return upload_data(file_name, content_type, content_encoding, nattempts - 1)
else:
raise Exception("File Uploading Error: Failure on Uploading to S3.")


def s3_to_csv(s3_url: Text, aws_credentials: Optional[Dict[Text, Text]] = {"AWS_ACCESS_KEY_ID": None, "AWS_SECRET_ACCESS_KEY": None}) -> Text:
def s3_to_csv(
s3_url: Text, aws_credentials: Optional[Dict[Text, Text]] = {"AWS_ACCESS_KEY_ID": None, "AWS_SECRET_ACCESS_KEY": None}
) -> Text:
"""Convert s3 url to a csv file and download the file in `download_path`

Args:
Expand All @@ -179,11 +181,11 @@ def s3_to_csv(s3_url: Text, aws_credentials: Optional[Dict[Text, Text]] = {"AWS_
aws_secret_access_key = aws_credentials.get("AWS_SECRET_ACCESS_KEY") or os.getenv("AWS_SECRET_ACCESS_KEY")
s3 = boto3.client("s3", aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
response = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
except NoCredentialsError as e:
except NoCredentialsError:
raise Exception(
"to use the s3 bucket option you need to set the right AWS credentials [AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY]"
)
except Exception as e:
except Exception:
raise Exception("the bucket you are trying to use does not exist")

try:
Expand Down Expand Up @@ -222,10 +224,10 @@ def s3_to_csv(s3_url: Text, aws_credentials: Optional[Dict[Text, Text]] = {"AWS_
main_file_name = Path(data[first_key][i]).stem
for val in data.values():
if Path(val[i]).stem != main_file_name:
raise Exception(f"all the files in different directories should have the same prefix")
raise Exception("all the files in different directories should have the same prefix")

elif prefix == "":
raise Exception(f"ERROR the files can't be at the root of the bucket ")
raise Exception("ERROR the files can't be at the root of the bucket ")
else:
data = {prefix: [f"s3://{bucket_name}/{file}" for file in files]}

Expand Down