Sinaptik-AI / pandas-ai

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.
https://pandas-ai.com
Other
12.82k stars 1.24k forks source link

Chart is shown despite setting open_charts config to False #1270

Open thomasblichfeldt opened 3 months ago

thomasblichfeldt commented 3 months ago

System Info

OS version: macOS Sonoma 14.0 python version: 3.11.3 pandasai version: 2.2.10

🐛 Describe the bug

I try to create charts using pandas-ai and saving them in a custom location without having the chart beings automatically shown.

My code looks like this:

async def query_postgresql_table(
        query:str,
        sample_dataframe:pd.DataFrame, 
        table_name:str):

    timestamp = str(datetime.now()).replace(" ","")
    save_chart_path = f"./temp_charts/{table_name}_{timestamp}"

    llm = AzureOpenAI(
        api_token=os.environ.get('AZURE_OPENAI_API_KEY'),
        azure_endpoint=os.environ.get('AZURE_OPENAI_ENDPOINT'),
        api_version=os.environ.get('AZURE_OPENAI_API_VERSION'),
        deployment_name=os.environ.get('AZURE_OPENAI_DEFAULT_MODEL')
    )

    postgres_connector = PostgreSQLConnector(
        config={
            "host": os.environ.get('POSTGRES_HOST'),
            "port": os.environ.get('POSTGRES_PORT'),
            "database": os.environ.get('POSTGRES_DATABASE_NAME'),
            "username": os.environ.get('POSTGRES_USER'),
            "password": os.environ.get('POSTGRES_PASSWORD'),
            "table": table_name,
            }
        )

    df = SmartDataframe(
        postgres_connector, 
        custom_head=sample_dataframe, 
        config={
            "llm": llm, 
            "enable_cache": False,
            "open_charts": False,
            "save_charts": True,
            "save_charts_path": save_chart_path,
            "verbose": False
        })

    result = df.chat(f"{query}")

    code = df.last_code_executed

    # If a dataframe was returned as a response, we convert it to markdown
    if isinstance(result, pd.DataFrame):
        result = result.to_markdown()

    return result, code
dosubot[bot] commented 3 months ago

Hey @thomasblichfeldt! I'm here to help you out with any issues or questions you have. Let's figure this out together!

To ensure that charts are not displayed while being saved to a specified path, you need to set both "open_charts" to False and "save_charts" to True in the configuration. Your current configuration seems correct, but there might be an issue with how the SmartDataframe class handles the open_charts setting internally.

Here is a revised version of your code with the correct configuration:

async def query_postgresql_table(
        query: str,
        sample_dataframe: pd.DataFrame, 
        table_name: str):

    timestamp = str(datetime.now()).replace(" ","")
    save_chart_path = f"./temp_charts/{table_name}_{timestamp}"

    llm = AzureOpenAI(
        api_token=os.environ.get('AZURE_OPENAI_API_KEY'),
        azure_endpoint=os.environ.get('AZURE_OPENAI_ENDPOINT'),
        api_version=os.environ.get('AZURE_OPENAI_API_VERSION'),
        deployment_name=os.environ.get('AZURE_OPENAI_DEFAULT_MODEL')
    )

    postgres_connector = PostgreSQLConnector(
        config={
            "host": os.environ.get('POSTGRES_HOST'),
            "port": os.environ.get('POSTGRES_PORT'),
            "database": os.environ.get('POSTGRES_DATABASE_NAME'),
            "username": os.environ.get('POSTGRES_USER'),
            "password": os.environ.get('POSTGRES_PASSWORD'),
            "table": table_name,
        }
    )

    df = SmartDataframe(
        postgres_connector, 
        custom_head=sample_dataframe, 
        config={
            "llm": llm, 
            "enable_cache": False,
            "open_charts": False,
            "save_charts": True,
            "save_charts_path": save_chart_path,
            "verbose": False
        })

    result = df.chat(f"{query}")

    code = df.last_code_executed

    # If a dataframe was returned as a response, we convert it to markdown
    if isinstance(result, pd.DataFrame):
        result = result.to_markdown()

    return result, code

If the charts are still being displayed, it might be due to a recent change in the pandas-ai codebase. The CodeCleaning class has been updated to include a method _replace_plot_png which replaces occurrences of plot.png with temp_chart.png in the code. Additionally, the get_code_to_run method has been modified to conditionally add chart saving functionality based on the configuration [1].

Ensure that you are using the latest version of pandas-ai and that your configuration is correctly set. If the issue persists, it might be worth checking the internal handling of the open_charts setting in the SmartDataframe class [2].

To continue talking to Dosu, mention @dosu.

thomasblichfeldt commented 3 months ago

So i have gotten further in the debugging.

Turns out the issue arises because the generated code contains a plt.show().

To circumvent this, i have created a fork and added this line:

https://github.com/thomasblichfeldt/pandas-ai-no-plot-showing/blob/5e5ab1df5b99cd86b1daacf79bffece00a817822/pandasai/pipelines/chat/code_cleaning.py#L149

So if anyone has the same issue, that can solve it.

gventuri commented 3 months ago

@thomasblichfeldt would you mind submitting a PR so that we can make the fix available to everyone?