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.

Home Page:https://pandas-ai.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'PipelineContext' object has no attribute 'prompt_id' - New release(v2.0.23) introduced

gDanzel opened this issue Β· comments

System Info

OS version: win11
Python version: 3.10
The current version of pandasai being used: v2.0.23

πŸ› Describe the bug


from time import sleep

import pandas as pd
from pandasai.llm import OpenAI
from pandasai.llm.google_gemini import GoogleGemini
from pandasai import Agent, SmartDataframe

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],
})


llm = OpenAI(api_token="token")

# sdf = SmartDataframe(df, config={"llm": llm})
# print(sdf.table_description)

dfa = Agent([df], config={"llm": llm, "verbose": False, "save_charts": True}, memory_size=10)

# response = dfa.chat("Return the top 5 countries by GDP")
# print(response)
#
response = dfa.chat("Plot the histogram of countries showing for each the gpd")
print(response)

Error trace below:

Traceback (most recent call last):
  File "C:\Users\Danzel\PycharmProjects\Tools\venv\Lib\site-packages\pandasai\pipelines\chat\generate_chat_pipeline.py", line 283, in run
    output = (self.code_generation_pipeline | self.code_execution_pipeline).run(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Danzel\PycharmProjects\Tools\venv\Lib\site-packages\pandasai\pipelines\pipeline.py", line 137, in run
    raise e
  File "C:\Users\Danzel\PycharmProjects\Tools\venv\Lib\site-packages\pandasai\pipelines\pipeline.py", line 101, in run
    step_output = logic.execute(
                  ^^^^^^^^^^^^^^
  File "C:\Users\Danzel\PycharmProjects\Tools\venv\Lib\site-packages\pandasai\pipelines\chat\code_cleaning.py", line 87, in execute
    code_to_run = self.get_code_to_run(input, context)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Danzel\PycharmProjects\Tools\venv\Lib\site-packages\pandasai\pipelines\chat\code_cleaning.py", line 117, in get_code_to_run
    file_name=str(context.prompt_id),
                  ^^^^^^^^^^^^^^^^^
AttributeError: 'PipelineContext' object has no attribute 'prompt_id'
Unfortunately, I was not able to answer your question, because of the following error:

'PipelineContext' object has no attribute 'prompt_id'

Happened when set config "save_charts": True

dfa = Agent([df], config={"llm": llm, "verbose": False, "save_charts": True}, memory_size=10)

@gDanzel I was able to replicate this issue. Yes, currently when we are setting 'save_charts'=True, in that case, it results in the above mentioned error.

As a temporary workaround in the code_cleaning file you can update the file_name variable that is causing this issue.
(Code_cleaning.py -> class CodeCleaning -> get_code_to_run -> file_name)

                Instead of having file_name=str(context.prompt_id), use the below file_name
                file_name =str(context.intermediate_values['last_prompt_id']),

@gventuri I looked into code_cleaning module and found that when CodeExecutionContext class is defined, prompt_id is uniquely set. Late we are inheriting it during get_code_to_run method as context. If I try to look through the prompt_id attribute of context, it shows there is no variable by that name.

Also here is a sample list of what's it's showing inside context object

dict_items([('dfs', [<pandasai.connectors.pandas.PandasConnector object at 0x000001C5158239D0>]), ('memory', <pandasai.helpers.memory.Memory object at 0x000001C515823A90>), ('skills_manager', <pandasai.helpers.skills_manager.SkillsManager object at 0x000001C515823C10>), ('cache', <pandasai.helpers.cache.Cache object at 0x000001C515823C70>), ('config', Config(save_logs=True, verbose=False, enforce_privacy=False, enable_cache=True, use_error_correction_framework=True, open_charts=True, save_charts=True, save_charts_path='exports/charts', custom_whitelisted_dependencies=[], max_retries=3, lazy_load_connector=True, response_parser=None, llm=<pandasai.llm.openai.OpenAI object at 0x000001C5785C98D0>, data_viz_library='', log_server=None, direct_sql=False, dataframe_serializer=<DataframeSerializerType.YML: 2>)), ('intermediate_values', {'output_type': None, 'last_prompt_id': UUID('be98cea5-4905-4dce-b018-363163a2fee8'), 'found_in_cache': True}), ('vectorstore', None), ('_initial_values', None)])

Can you guide me through why exactly are not getting the prompt_id parameter for context ? Or what can be workaround to fix the prompt_id parameter.

@dudesparsh thanks for the update, will try the workaroud temp.

@gDanzel This issue this PR will solve this issue

#1069

@ArslanSaleem , thanks for notice!

@gDanzel This issue this PR will solve this issue

#1069