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.53k stars 1.21k forks source link

SemanticAgent : 'str' object has no attribute 'to_json' #1328

Open ddtjco opened 3 weeks ago

ddtjco commented 3 weeks ago

System Info

Windows OS, Python 3.11.1, PandasAI 2.2.14

🐛 Describe the bug

I'm attempting to test the SemanticAgent option but I'm getting the following error when querying:

**Unfortunately, I was not able to answer your question, because of the following error:

'str' object has no attribute 'to_json'**

I asked the agent "How many items are for Customer XYZ?" and it returned that error.

This is how I have the agent configured: analyst = SemanticAgent(df,config = {"llm":sqlqueries.bamboo_llm})

dosubot[bot] commented 3 weeks ago

Hey @ddtjco! I'm here to help you with any bugs, questions, or contributions. Let's tackle this issue together!

The error "'str' object has no attribute 'to_json'" occurs because the context.memory object is expected to have a to_json method, but it appears to be a string in your configuration.

To resolve this, ensure that context.memory is an instance of the Memory class, which includes the to_json method. Here is the relevant part of the SemanticAgent class:

class SemanticAgent(BaseAgent):
    def __init__(self, ...):
        ...
        self.context = PipelineContext(
            dfs=self.dfs,
            config=self.config,
            memory=Memory(memory_size, agent_info=description),
            vectorstore=self._vectorstore,
            initial_values={"df_schema": self._schema},
        )
        ...

The Memory class should have the following to_json method:

class Memory:
    def to_json(self):
        messages = []
        for message in self.all():
            if message["is_user"]:
                messages.append({"role": "user", "message": message["message"]})
            else:
                messages.append({"role": "assistant", "message": message["message"]})
        return messages

Ensure that your context.memory is correctly initialized as an instance of the Memory class, and this should resolve the error [1][2].

To continue talking to Dosu, mention @dosu.