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

Simple query on Python/Streamlit raises pandasai.exceptions.PandasAIApiCallError #1151

Closed Emad-Eldin-G closed 1 month ago

Emad-Eldin-G commented 1 month ago

System Info

MACOS Sonoma 14.4.1 Python Version 3.11.7 Pandasai Version 2.0.37

🐛 Describe the bug

Imports

from pandasai import Agent, SmartDataframe
import os
import streamlit as st
import pandas as pd

Code

def LLM_Analysis(pandas_csv):
     #Present the max, min, range, mean, and mode in a streamlit table with two columns
     st.markdown("> LLM Analysis")

     os.environ["PANDASAI_API_KEY"] = "MY_API_KEY"

     df = pd.DataFrame(pandas_csv)
     df = SmartDataframe(df, config={"verbose": True})

     df.chat("Give me a short summary of the dataset.")

This is a minimal version of the code, where a simple pandas csv read df is passed:

read_csv = pd.read_csv(uploaded_file)

Error Message

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/pipelines/chat/generate_chat_pipeline.py", line 307, in run
    output = (self.code_generation_pipeline | self.code_execution_pipeline).run(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/pipelines/pipeline.py", line 137, in run
    raise e
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/pipelines/pipeline.py", line 101, in run
    step_output = logic.execute(
                  ^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/pipelines/chat/code_generator.py", line 33, in execute
    code = pipeline_context.config.llm.generate_code(input, pipeline_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/llm/base.py", line 200, in generate_code
    response = self.call(instruction, context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/llm/bamboo_llm.py", line 18, in call
    response = self._session.post("/llm/chat", json=data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/helpers/request.py", line 37, in post
    return self.make_request("POST", path, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandasai/helpers/request.py", line 71, in make_request
    raise PandasAIApiCallError(data["message"])
pandasai.exceptions.PandasAIApiCallError: Unable to generate LLM response. 

Log file

2024-05-13 06:30:25 [INFO] Question: Give me a short summary of the dataset.
2024-05-13 06:30:25 [INFO] Running PandasAI with bamboo_llm LLM...
2024-05-13 06:30:25 [INFO] Prompt ID: 19bf8ed5-4015-40d1-8102-2f6d2169ac0c
2024-05-13 06:30:25 [INFO] Executing Pipeline: GenerateChatPipeline
2024-05-13 06:30:25 [INFO] Executing Step 0: ValidatePipelineInput
2024-05-13 06:30:25 [INFO] Executing Step 1: CacheLookup
2024-05-13 06:30:25 [INFO] Executing Step 2: PromptGeneration
2024-05-13 06:30:26 [INFO] Querying without using training data.
2024-05-13 06:30:26 [INFO] Querying without using training docs.
2024-05-13 06:30:26 [INFO] Using prompt: <dataframe>
dfs[0]:2371x8
Date,Salinity (ppt),DissolvedOxygen (mg/L),pH,SecchiDepth (m),WaterDepth (m),WaterTemp (C),AirTemp (C)
1991-05-20,,,6.5,0.53,0.53,7.5,15.2
2011-01-04,8.0,11.1,7.4,0.22,1.1,,30.4
,4.5,10.8,,,,24.5,-1.111111111
</dataframe>

Update this initial code:
```python
# TODO: import the required dependencies
import pandas as pd

# Write code here

# Declare result var: 
type (possible values "string", "number", "dataframe", "plot"). Examples: { "type": "string", "value": f"The highest salary is {highest_salary}." } or { "type": "number", "value": 125 } or { "type": "dataframe", "value": pd.DataFrame({...}) } or { "type": "plot", "value": "temp_chart.png" }

### QUERY
 Give me a short summary of the dataset.

Variable `dfs: list[pd.DataFrame]` is already declared.

At the end, declare "result" variable as a dictionary of type and value.

If you are asked to plot a chart, use "matplotlib" for charts, save as png.

Generate python code and return full updated code:
2024-05-13 06:30:26 [INFO] Executing Step 3: CodeGenerator
2024-05-13 06:30:27 [ERROR] Pipeline failed on step 3: Unable to generate LLM response.
Emad-Eldin-G commented 1 month ago

Imported the wrong OpenAI, fixed ✅

ssling0817 commented 1 month ago

May I know how you solve the issue? Which OpenAI is correct?

Emad-Eldin-G commented 1 month ago

Import the OpenAI instance from the pandas LLM library:

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

Then pass it in to your Smart DataFrame as a config param:

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

df = pd.DataFrame(self.__dataset)
pAI = SmartDataframe(df, config={"verbose": True, "llm": llm})

And Chat !

llm_analysis_response = pAI.chat("Your query here")

There are probably other ways to go around it, but this is how I've done it.