-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Figure object output type #1333
Labels
enhancement
New feature or request
Comments
@Blubbaa May I ask how did you modify the |
@at-eez-jedi yes surely. I have modified
I also inserted this part in
And def validate_value(self, expected_type: str) -> bool:
if not expected_type:
return True
elif expected_type == "number":
return isinstance(self, (int, float))
elif expected_type == "string":
return isinstance(self, str)
elif expected_type == "dataframe":
return isinstance(self, (pd.DataFrame, pd.Series))
elif expected_type == "plot":
if not isinstance(self, (str, dict)):
return False
if isinstance(self, dict):
return True
path_to_plot_pattern = r"^(\/[\w.-]+)+(/[\w.-]+)*$|^[^\s/]+(/[\w.-]+)*$"
return bool(re.match(path_to_plot_pattern, self))
elif expected_type == "figure":
return "plotly.graph_objs._figure.Figure" in repr(type(self)) or "matplotlib.figure.Figure" in repr(type(self))
@staticmethod
def validate_result(result: dict) -> bool:
if not isinstance(result, dict) or "type" not in result:
raise InvalidOutputValueMismatch(
"Result must be in the format of dictionary of type and value"
)
if not result["type"]:
return False
elif result["type"] == "number":
return isinstance(result["value"], (int, float, np.int64))
elif result["type"] == "string":
return isinstance(result["value"], str)
elif result["type"] == "dataframe":
return isinstance(result["value"], (pd.DataFrame, pd.Series))
elif result["type"] == "plot":
if "plotly" in repr(type(result["value"])):
return True
if not isinstance(result["value"], (str, dict)):
return False
if isinstance(result["value"], dict) or (
isinstance(result["value"], str)
and "data:image/png;base64" in result["value"]
):
return True
path_to_plot_pattern = r"^(\/[\w.-]+)+(/[\w.-]+)*$|^[^\s/]+(/[\w.-]+)*$"
return bool(re.match(path_to_plot_pattern, result["value"]))
elif result["type"] == "figure":
return "plotly.graph_objs._figure.Figure" in repr(type(result["value"])) or "matplotlib.figure.Figure" in repr(type(result["value"])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🚀 The feature
I would love to have the ability of setting the ouput type to a plotly or matplotlib figure, instead of saving plots to PNG and returning filepaths, where the usefulness is quite limited.
Motivation, pitch
I recently started using pandasai for building custom data analysis apps and I like it quite a lot so far. I was wondering why it is limited to the four output datatypes and even more why the (cumbersome) way of saving images to disk and returning a filepath has been chosen. Maybe it has a security related reason or it is due to the client-server architecture of pandasai? Instead returning objects (like already implemented for the dataframe) would open much more potential, especially regarding plots and figures.
I have already tinkered with modifying the
output_type_template.tmpl
andoutput_validator.py
in order to make pandasai return figure objects. However I do not really know about potential problems/implications of this and thus am proposing this here as a feature request, since my "hacky" implementation is probably not how it should be implemented.Here you can see the resulting prompt used and the generated code, which works fine for now in a standalone app.
Prompt used:
Resulting Code:
Alternatives
I know its also possible to convert plotly figures from/to json. So maybe this could be another option to return (or potentially also save) the figure as json instead.
Additional context
Final Result in Chatbot App:
The text was updated successfully, but these errors were encountered: