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
11.69k stars 1.08k forks source link

Agent Method parameter save_charts acting like open_charts, and opens saved charts automatically #1175

Open Emad-Eldin-G opened 1 month ago

Emad-Eldin-G commented 1 month ago

System Info

Python 3.11.7 PandasAI 2.0.37 Pandas 1.5.3

πŸ› Describe the bug

 def test():
      os.environ["PANDASAI_API_KEY"] = os.environ.get("PANDASAI_API_KEY")
      llm = OpenAI(api_token="os.environ.get("OPENAI_API_KEY")")

      df = pd.DataFrame({
      "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", 
      "China"],
      "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 
      1607402389504, 1490967855104, 4380756541440, 14631844184064],
      "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12] })

       pAI = Agent(df, config={"verbose": True, "llm": llm, "save_charts": True, "enable_cache": True})

       llm_analysis_response = pAI.chat("Create a plot for GDP in relation to happiness")
       return llm_analysis_response

The above code should create the chart and save it to the default path (and it does) However, it opens the chart automatically, similar to what open_charts does, which causes problems when integrating pandasai in applications like streamlit for example.

dosubot[bot] commented 1 month ago

Hey @Emad-Eldin-G! I'm Dosu, your friendly bot here to help you solve bugs, answer questions, and become a contributor. I'm here to assist you while you wait for a human maintainer. Thanks for reporting the issue! I'll take a look at it and get back to you shortly.

Emad-Eldin-G commented 1 month ago

@jonbiemond, is there a way to save the charts without opening it automatically, save_charts calls plt.show(), acting like open_charts, which is confusing

jonbiemond commented 1 month ago

Have you tried setting open_charts to False?

Agent(df, config={"verbose": True, "llm": llm, "save_charts": True, "open_charts": False, "enable_cache": True})
dosubot[bot] commented 1 month ago

It looks like the open_charts parameter is not a valid configuration option in the PandasAI library. However, you can save charts without opening them by specifying a custom save path. Here’s an updated version of your code:

import os
import pandas as pd
from pandasai import Agent
from openai import OpenAI
from pandasai.helpers import path

def test():
    os.environ["PANDASAI_API_KEY"] = os.environ.get("PANDASAI_API_KEY")
    llm = OpenAI(api_token=os.environ.get("OPENAI_API_KEY"))

    df = pd.DataFrame({
        "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
        "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
        "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
    })

    try:
        user_defined_path = path.find_project_root()
    except ValueError:
        user_defined_path = os.getcwd()

    user_defined_path = os.path.join(user_defined_path, "exports", "charts")

    pAI = Agent(
        df,
        config={
            "verbose": True,
            "llm": llm,
            "save_charts": True,
            "save_charts_path": user_defined_path,
            "enable_cache": True,
        },
    )

    llm_analysis_response = pAI.chat("Create a plot for GDP in relation to happiness")
    return llm_analysis_response

test()

This code sets a custom path for saving charts, ensuring they are saved without being displayed automatically. This should work well with applications like Streamlit. Let me know if you have any further questions!

#### Details **Sources** - [Handling charts in different contexts](https://github.com/Sinaptik-AI/pandas-ai/discussions/277) - [The latest version of pandasAI is not saving the generated plot with save_charts = True attribute](https://github.com/Sinaptik-AI/pandas-ai/issues/404) - [Can we add feature where we can save the graphs instead of dispaling with matplotlib GUI ](https://github.com/Sinaptik-AI/pandas-ai/issues/146) - [pandasai/helpers/save_chart.py](https://github.com/Sinaptik-AI/pandas-ai/blob/main/pandasai/helpers/save_chart.py) - [examples/save_chart.py](https://github.com/Sinaptik-AI/pandas-ai/blob/main/examples/save_chart.py) - [tests/unit_tests/helpers/test_save_chart.py](https://github.com/Sinaptik-AI/pandas-ai/blob/main/tests/unit_tests/helpers/test_save_chart.py) - [pandasai/constants.py](https://github.com/Sinaptik-AI/pandas-ai/blob/main/pandasai/constants.py)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.

Emad-Eldin-G commented 1 month ago

The bot solution is completely incorrect, the image is till opened automatically (@dosu)

@jonbiemond your solution is correct, can I add this in the documentation somewhere? As it causes a real problem for streamlit applications and other apps that integrate pandasai.

jonbiemond commented 1 month ago

@Emad-Eldin-G Glad to hear that worked!

The open_charts parameter is listed in the Config section on the Getting Started page of the docs. Is there somewhere you think it might be also be helpful?

dosubot[bot] commented 1 month ago

Yes, you can add information to the documentation about setting open_charts to False to prevent charts from opening automatically when save_charts is set to True in the PandasAI library. The relevant section in the documentation already mentions the open_charts setting:

- `open_charts`: whether to open the chart during parsing of the response from the LLM. Defaults to `True`. You can completely disable displaying of charts by setting this option to `False`.

You can add an example to make it clearer:

import os
import pandas as pd
from pandasai import Agent
from openai import OpenAI

def test():
    os.environ["PANDASAI_API_KEY"] = os.environ.get("PANDASAI_API_KEY")
    llm = OpenAI(api_token=os.environ.get("OPENAI_API_KEY"))

    df = pd.DataFrame({
        "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
        "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
        "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
    })

    pAI = Agent(df, config={"verbose": True, "llm": llm, "save_charts": True, "open_charts": False, "enable_cache": True})

    llm_analysis_response = pAI.chat("Create a plot for GDP in relation to happiness")
    return llm_analysis_response

test()

Additionally, you can refer to the default configuration settings in the pandasai.json file for more context:

{
  "save_logs": true,
  "verbose": false,
  "enforce_privacy": false,
  "enable_cache": true,
  "use_error_correction_framework": true,
  "max_retries": 3,
  "open_charts": true,
  "save_charts": false,
  "save_charts_path": "exports/charts",
  "custom_whitelisted_dependencies": [],
  "llm": "BambooLLM",
  "llm_options": null
}

This shows the default values for various settings, including open_charts and save_charts_path.

By adding this information to the documentation, it will help others who face similar issues, especially when integrating PandasAI with Streamlit or other applications.

@gventuri, could you help with this?

#### Details **Sources** - [Handling charts in different contexts](https://github.com/Sinaptik-AI/pandas-ai/discussions/277) - [docs/getting-started.mdx](https://github.com/Sinaptik-AI/pandas-ai/blob/main/docs/getting-started.mdx) - [pandasai.json](https://github.com/Sinaptik-AI/pandas-ai/blob/main/pandasai.json)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.