Refactor code for CustomComponent class

- Refactored code to replace the deprecated is_valid property with the is_check_valid method.
- Added validation for the entrypoint function name and return type.
- Modified the error messages to provide more specific details.

This commit implements the necessary changes to refactor the CustomComponent class in the endpoints.py file. The is_valid property has been replaced with the is_check_valid method to check the validity of the custom component. Additionally, validation has been added to ensure the presence of a valid entrypoint function and a valid return type. If any of the validation checks fail, appropriate error messages are raised to provide detailed traceback information.
This commit is contained in:
gustavoschaedler 2023-07-06 01:02:34 +01:00
commit 11cece92b8
2 changed files with 20 additions and 13 deletions

View file

@ -100,9 +100,6 @@ async def custom_component(
raw_code: CustomComponentCode,
):
extractor = CustomComponent(code=raw_code.code)
if not extractor.is_valid:
print("ERROR")
# TODO: Raise error
extractor.is_check_valid()
return build_langchain_template_custom_component(extractor)

View file

@ -197,21 +197,32 @@ class CustomComponent(BaseModel):
)
functions = code.get("functions", [])
if build_function := next(
build_function = next(
(f for f in functions if f["name"] == self.function_entrypoint_name),
None,
):
# Check if the return type of the build function is valid
return build_function.get("return_type") in self.return_type_valid_list
else:
)
if not build_function:
raise HTTPException(
status_code=400,
detail={
"error": f"The class return [{str(build_function.get('return_type'))}] needs to be an item from this list. [{str(self.return_type_valid_list)}]",
"traceback": "",
"error": "Invalid entrypoint function name",
"traceback": f"There needs to be at least one entrypoint function named '{self.function_entrypoint_name}' and it needs to return one of the types from this list {str(self.return_type_valid_list)}.",
},
)
return_type = build_function.get("return_type")
if return_type not in self.return_type_valid_list:
raise HTTPException(
status_code=400,
detail={
"error": "Invalid entrypoint function return",
"traceback": f"The entrypoint function return '{return_type}' needs to be an item from this list {str(self.return_type_valid_list)}.",
},
)
return True
def get_function(self):
return validate.create_function(self.code, self.function_entrypoint_name)
@ -219,8 +230,7 @@ class CustomComponent(BaseModel):
def data(self):
return self.extract_class_info()
@property
def is_valid(self):
def is_check_valid(self):
return self._class_template_validation(self.data)
@property