refactor(loading.py): remove unused imports and add validation for class_object parameters that are not in __fields__ attribute.

This commit is contained in:
Gabriel Almeida 2023-05-01 22:52:17 -03:00
commit 96ef8fd657

View file

@ -25,6 +25,7 @@ from langflow.interface.toolkits.base import toolkits_creator
from langflow.interface.types import get_type_list
from langflow.interface.utils import load_file_into_dict
from langflow.utils import util, validate
from pydantic import ValidationError
def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
@ -74,7 +75,16 @@ def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
elif base_type == "embeddings":
# ? Why remove model from params?
params.pop("model")
return class_object(**params)
# remove all params that are not in class_object.__fields__
try:
return class_object(**params)
except ValidationError:
params = {
key: value
for key, value in params.items()
if key in class_object.__fields__
}
return class_object(**params)
elif base_type == "vectorstores":
if len(params.get("documents", [])) == 0:
# Error when the pdf or other source was not correctly