Skip to content

Commit

Permalink
fix: add checking type of 'result' (#626) (#730)
Browse files Browse the repository at this point in the history
* fix: add checking type of 'result' (#626)

* (fix): add checking type of 'result' in '._add_result_to_memory()'
  method
* (fix): add checking that 'result' dict contains 'type' and 'value' the
  keys in '._add_result_to_memory()' method

* fix: checking type of 'result' (#626)

* (refactor): update 'SmartDataframe'
* (fix): remove an excessive validation in '_add_result_to_memory()'
  method due to duplicating of the functionality
* (chore): rename 'validation_ok' to 'result_is_valid'
* (fix): add pre-defining for 'result_is_valid'
* (refactor): simplify conditions checking
  • Loading branch information
nautics889 authored Nov 7, 2023
1 parent 3b6378c commit eea4a49
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions pandasai/smart_datalake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ def chat(self, query: str, output_type: Optional[str] = None):

self._memory.add(query, True)

result_is_valid = False

try:
output_type_helper = output_type_factory(output_type, logger=self.logger)
viz_lib_helper = viz_lib_type_factory(self._viz_lib, logger=self.logger)
Expand Down Expand Up @@ -464,29 +466,27 @@ def chat(self, query: str, output_type: Optional[str] = None):
self._retry_run_code, code, traceback_error
)

if result is not None:
if isinstance(result, dict):
validation_ok, validation_logs = output_type_helper.validate(result)
if not validation_ok:
self.logger.log(
"\n".join(validation_logs), level=logging.WARNING
)
self._query_exec_tracker.add_step(
{
"type": "Validating Output",
"success": False,
"message": "Output Validation Failed",
}
)
else:
self._query_exec_tracker.add_step(
{
"type": "Validating Output",
"success": True,
"message": "Output Validation Successful",
}
)
if isinstance(result, dict):
result_is_valid, validation_logs = output_type_helper.validate(result)
if result_is_valid:
self._query_exec_tracker.add_step(
{
"type": "Validating Output",
"success": True,
"message": "Output Validation Successful",
}
)
else:
self.logger.log("\n".join(validation_logs), level=logging.WARNING)
self._query_exec_tracker.add_step(
{
"type": "Validating Output",
"success": False,
"message": "Output Validation Failed",
}
)

if result is not None:
self.last_result = result
self.logger.log(f"Answer: {result}")

Expand All @@ -505,7 +505,13 @@ def chat(self, query: str, output_type: Optional[str] = None):
f"Executed in: {self._query_exec_tracker.get_execution_time()}s"
)

self._add_result_to_memory(result)
if result_is_valid:
self._add_result_to_memory(result)
else:
self.logger.log(
"The result will not be memorized since it has failed the "
"corresponding validation"
)

result = self._query_exec_tracker.execute_func(
self._response_parser.parse, result
Expand All @@ -524,9 +530,6 @@ def _add_result_to_memory(self, result: dict):
Args:
result (dict): The result to add to the memory
"""
if result is None:
return

if result["type"] in ["string", "number"]:
self._memory.add(result["value"], False)
elif result["type"] in ["dataframe", "plot"]:
Expand Down

0 comments on commit eea4a49

Please sign in to comment.