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

when i note "direct_sql": True, error:Not an executable object: "\nSELECT \n ELECTRY_NAME,\n ...... #1183

Open colorwlof opened 1 month ago

colorwlof commented 1 month ago

System Info

2.0.43

🐛 Describe the bug

code:

elect = MySQLConnector(
    config={
        "host": "180.101.226.110",
        "port": 4414,
        "database": "jssp-server",
        "username": "jssp",
        "password": "aaaaa",
        "table": "M_Y_ENERGYELECTRY"
    }, field_descriptions=prompt_config.ENERGYELECTRY
)

agent = Agent(elect, config={"llm":llm,"save_logs": False, "open_charts": False,"memory_size":0,
                             "enable_cache": False, "save_charts": True, "direct_sql": True,"dataframe_serializer":4,
                             "data_viz_library": "plotly","lazy_load_connector":False,"max_retries":1,"use_error_correction_framework":False,"use_error_correction":False})

chat = agent.chat(user_question)
print(chat)

erro: Not an executable object: "\nSELECT \n ELECTRY_NAME,\n METER_READING_READING\nFROM \n M_Y_ENERGYELECTRY\nWHERE \n BUILDING_NAME LIKE '%12号楼%'\nORDER BY \n METER_READING_READING DESC\nLIMIT 5;\n"

debug:

2024-05-29 13:57:31,470 - logger.py[line:75] - INFO: 
Code running:

result = {} sql_query = """ SELECT ELECTRY_NAME, METER_READING_READING FROM M_Y_ENERGYELECTRY WHERE BUILDING_NAME LIKE '%12号楼%' ORDER BY METER_READING_READING DESC LIMIT 5; """ top_meters_df = execute_sql_query(sql_query) if not top_meters_df.empty: result = {'type': 'dataframe', 'value': top_meters_df} else: result = {'type': 'string', 'value': 'No data found for Building 12 meters.'}

2024-05-29 13:57:31,471 - logger.py[line:75] - INFO: Executing Step 6: CodeExecution
{'M_Y_ENERGYELECTRY': 'M_Y_ENERGYELECTRY', '"M_Y_ENERGYELECTRY"': 'M_Y_ENERGYELECTRY'}
['M_Y_ENERGYELECTRY']
2024-05-29 13:57:31,478 - logger.py[line:79] - ERROR: Failed with error: Traceback (most recent call last):
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\sqlalchemy\engine\base.py", line 1414, in execute
    meth = statement._execute_on_connection
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_execute_on_connection'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 85, in execute
    result = self.execute_code(code_to_run, code_context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 171, in execute_code
    exec(code, environment)
  File "<string>", line 14, in <module>
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\connectors\sql.py", line 443, in execute_direct_sql_query
    return pd.read_sql(sql_query, self._connection)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 590, in read_sql
    return pandas_sql.read_query(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 1560, in read_query
    result = self.execute(*args)
             ^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 1405, in execute
    return self.connectable.execution_options().execute(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\sqlalchemy\engine\base.py", line 1416, in execute
    raise exc.ObjectNotExecutableError(statement) from err
sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: "\nSELECT \n    ELECTRY_NAME,\n    METER_READING_READING\nFROM \n    M_Y_ENERGYELECTRY\nWHERE \n    BUILDING_NAME LIKE '%12号楼%'\nORDER BY \n    METER_READING_READING DESC\nLIMIT 5;\n"

2024-05-29 13:57:31,478 - logger.py[line:79] - ERROR: Pipeline failed on step 6: Not an executable object: "\nSELECT \n    ELECTRY_NAME,\n    METER_READING_READING\nFROM \n    M_Y_ENERGYELECTRY\nWHERE \n    BUILDING_NAME LIKE '%12号楼%'\nORDER BY \n    METER_READING_READING DESC\nLIMIT 5;\n"
Traceback (most recent call last):
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\sqlalchemy\engine\base.py", line 1414, in execute
    meth = statement._execute_on_connection
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_execute_on_connection'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\generate_chat_pipeline.py", line 307, in run
    output = (self.code_generation_pipeline | self.code_execution_pipeline).run(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\pipeline.py", line 137, in run
    raise e
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\pipeline.py", line 101, in run
    step_output = logic.execute(
                  ^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 113, in execute
    raise e
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 85, in execute
    result = self.execute_code(code_to_run, code_context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 171, in execute_code
    exec(code, environment)
  File "<string>", line 14, in <module>
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandasai\connectors\sql.py", line 443, in execute_direct_sql_query
    return pd.read_sql(sql_query, self._connection)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 590, in read_sql
    return pandas_sql.read_query(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 1560, in read_query
    result = self.execute(*args)
             ^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\pandas\io\sql.py", line 1405, in execute
    return self.connectable.execution_options().execute(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Anaconda3\envs\pytorch\Lib\site-packages\sqlalchemy\engine\base.py", line 1416, in execute
    raise exc.ObjectNotExecutableError(statement) from err
sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: "\nSELECT \n    ELECTRY_NAME,\n    METER_READING_READING\nFROM \n    M_Y_ENERGYELECTRY\nWHERE \n    BUILDING_NAME LIKE '%12号楼%'\nORDER BY \n    METER_READING_READING DESC\nLIMIT 5;\n"
Unfortunately, I was not able to answer your question, because of the following error:

Not an executable object: "\nSELECT \n    ELECTRY_NAME,\n    METER_READING_READING\nFROM \n    M_Y_ENERGYELECTRY\nWHERE \n    BUILDING_NAME LIKE '%12号楼%'\nORDER BY \n    METER_READING_READING DESC\nLIMIT 5;\n"
dosubot[bot] commented 1 month ago

Hey there, @colorwlof! I'm here to help you out with any issues you're facing. Just give me a nudge if you need assistance with bug fixes, questions, or becoming a contributor.

The error Not an executable object occurs because the SQL query generated or executed is not valid or executable. This can happen if the query syntax is incorrect or if the query does not conform to the expected SQL standards.

In your case, the SQL query seems to be correctly formatted. However, the issue might be related to how the query is being passed or executed. Ensure that the query passed to the agent.chat method is a valid SQL query and that it uses authorized tables.

Here is an example of how to correctly pass a valid SQL query:

response = agent.chat("SELECT ELECTRY_NAME, METER_READING_READING FROM M_Y_ENERGYELECTRY WHERE BUILDING_NAME LIKE '%12号楼%' ORDER BY METER_READING_READING DESC LIMIT 5;")
print(response)

Additionally, make sure that the execute_direct_sql_query method in the SQLConnector class is used correctly. This method checks if the SQL query is safe before executing it:

def execute_direct_sql_query(self, sql_query):
    if not self._is_sql_query_safe(sql_query):
        raise MaliciousQueryError("Malicious query is generated in code")

    return pd.read_sql(sql_query, self._connection)

Ensure that your SQL query does not contain any potentially malicious keywords such as INSERT, UPDATE, DELETE, DROP, EXEC, ALTER, or CREATE. If any of these keywords are present, a MaliciousQueryError will be raised [1][2].

By ensuring the query is valid and safe, you should be able to avoid the Not an executable object error.

#### Details **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.

colorwlof commented 1 month ago

I have fixed it, sql_query=text(sql_query) , in sql.py , line 441