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.71k stars 1.09k forks source link

No result returned error when using agent framework #1024

Open Marcus-M1999 opened 3 months ago

Marcus-M1999 commented 3 months ago

System Info

PandasAI version = "1.5.15" Python version = 3.9.0 langchain version = 0.1.9

🐛 Describe the bug

When testing the PandasAI_agent method from the pandasai library I will get the following error with select questions: 'Unfortunately, I was not able to answer your question, because of the following error:\n\nNo result returned\n'. This happens with all models that aren't GPT-4. (StarcoderPlus, Code Llama, Mitral 8x7b, etc.) Without the Pandas AI framework the models are able to generate code by having the DF passed into the prompt.

code:

import pandas as pd
from pandasai import Agent as PandasAI_Agent
from langchain_nvidia_ai_endpoints import ChatNVIDIA

os.environ["NVIDIA_API_KEY"] =#api key neccessary is not already set as env var

df2 = pd.DataFrame({
    "Quarter" : [ "Q2", "Q3", "Q4", "Q1", "Q2", "Q3", "Q4", "Q1", "Q2", "Q3" ]
    ,"Date" : ["31-Jul-21", "31-Oct-21", "31-Jan-22", "30-Apr-22", "31-Jul-22", "31-Oct-22", "31-Jan-23", "30-Apr-23", "31-Jul-23", "31-Oct-23" ] 
    ,"Revenue GAAP": [6543.0, 7873.0, 7603.0, 8218.0, 6704.0, 5931.0, 601.0, 10192.0, 18507.0, 20120.0]
})
#add prompt
template = "You are a data analyst and must answer the questions by using the given dataframe and extracting data from it. {query}"
data_retrieval_prompt = ChatPromptTemplate(
    messages=[ 
        HumanMessagePromptTemplate.from_template(template)  
    ],
    input_variables=["query"],
)

#create llm agent
llm= ChatNVIDIA(model="playground_mixtral_8x7b", temperature=0)

#add config to dictionary
agent_config= {
    'llm': llm,
    "enable_cache": False
}
#crete agent
agent_data_retrieval = PandasAI_Agent([df2], config=agent_config, memory_size=0)

sentence = "Show quarterly revenue over the past 5 quarters."
_input = data_retrieval_prompt.format_prompt(query = sentence, dataframe=df2).to_string()

pandasai.clear_cache()
resp = agent_data_retrieval.chat(_input)
print(resp)

Output: Unfortunately, I was not able to answer your question, because of the following error:\n\nNo result returned\n

gventuri commented 3 months ago

Hi @Marcus-M1999, this might be caused by the fact that the prompt has been optimized for GPT3.5 and google models. Every LLM might need some variation to perform at its best. We are working on different prompts based on the LLM (at least for the most common ones).

Marcus-M1999 commented 3 months ago

Thanks for the quick response! @gventuri

Marcus-M1999 commented 3 months ago

@gventuri are these the prompt templates that have been optimized for GPT3.5 and Google models?

gventuri commented 3 months ago

@Marcus-M1999 yes, that's correct!

ssling0817 commented 1 month ago

Could you clarify a bit that how could we avoid this error? Do we need to optimize our prompt? Thanks!