🐛 fix(loading.py): add error handling for missing 'cursor' attribute in instantiate_memory function

The instantiate_memory function now includes error handling for a specific AttributeError that occurs when the object does not have a 'cursor' attribute. If this error occurs, an AttributeError is raised with a specific error message indicating a failure to build a connection to the database. This change improves the error handling and provides more informative error messages for debugging purposes.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-29 15:17:23 -03:00
commit 473bde940c

View file

@ -82,10 +82,25 @@ def instantiate_based_on_type(class_object, base_type, node_type, params):
return instantiate_llm(node_type, class_object, params)
elif base_type == "retrievers":
return instantiate_retriever(node_type, class_object, params)
elif base_type == "memory":
return instantiate_memory(node_type, class_object, params)
else:
return class_object(**params)
def instantiate_memory(node_type, class_object, params):
try:
return class_object(**params)
# I want to catch a specific attribute error that happens
# when the object does not have a cursor attribute
except AttributeError as exc:
if "object has no attribute 'cursor'" in str(exc):
raise AttributeError(
f"Failed to build connection to database. Please check your connection string and try again. Error: {exc}"
) from exc
raise exc
def instantiate_retriever(node_type, class_object, params):
if "retriever" in params and hasattr(params["retriever"], "as_retriever"):
params["retriever"] = params["retriever"].as_retriever()