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.73k stars 1.23k forks source link

exec() KeyError: '__import__' #1242

Closed ssling0817 closed 3 days ago

ssling0817 commented 3 months ago

System Info

pandasai 2.0.42

🐛 Describe the bug

I want to describe the table, but sometimes it has KeyError: 'import', but the code does not import anything. How to solve this issue? sdf1 = Agent(tables, config={"llm": ollama_llm, "enable_cache": False, "custom_whitelisted_dependencies": ["calendar", "dateutil"], "use_error_correction_framework": True})

Code running:

description = f'The table has {dfs[0].shape[0]} rows and {dfs[0].shape[1]} columns.\n\n'
description += 'Columns:\n'
for column in dfs[0].columns:
    description += f'- {column} ({dfs[0][column].dtype}): {dfs[0][column].describe()}\n'
result = {'type': 'string', 'value': description}

2024-06-19 19:21:58 [INFO] Executing Step 6: CodeExecution 2024-06-19 19:21:58 [ERROR] Failed with error: Traceback (most recent call last): File "C:\pandasai\pipelines\chat\code_execution.py", line 85, in execute result = self.execute_code(code_to_run, code_context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\pandasai\pipelines\chat\code_execution.py", line 171, in execute_code exec(code, environment) File "", line 4, in KeyError: 'import'

dosubot[bot] commented 3 months ago

To resolve the KeyError: '__import__' when trying to describe a table, ensure that the __import__ key is correctly set in the variables used within the DirectSQLPrompt class. The error might be occurring due to a missing or incorrect setup in the variables.

Here is a potential fix by ensuring that the default_import variable is correctly set:

def on_prompt_generation(self) -> None:
    default_import = f"import {pd.__name__} as pd"
    self.set_var("default_import", default_import)
    self.set_var("reasoning", SimpleReasoningPrompt())

Make sure that the default_import variable is correctly set and used in the template or wherever it is required. If the error persists, you might need to check other parts of the code where __import__ might be referenced or required [1].

Additionally, the issue might be related to the use of non-whitelisted libraries. Ensure that the libraries you are using are whitelisted or allowed in the execution environment. Check if the execution environment has restrictions on dynamic imports or specific security policies that prevent the use of __import__ [2].

If the problem continues, you might need to review the _is_jailbreak method in the code_cleaning.py file, which checks for dangerous built-ins, including __import__, and removes them to prevent malicious code execution [3].

To continue talking to Dosu, mention @dosu.