Repository: gventuri/pandas-ai
File: pandasai/agent/__init__.py (Python)

Code before:
        self.logger = self._lake.logger
        # For the conversation multiple the memory size by 2
        self._memory = Memory(memory_size * 2)

    def _get_conversation(self):
        """
        Get Conversation from history

        """
        return "\n".join(
            [
                f"{'Question' if message['is_user'] else 'Answer'}: "
                f"{message['message']}"
                for i, message in enumerate(self._memory.all())
            ]
        )

    def chat(self, query: str, output_type: Optional[str] = None):
        """
        Simulate a chat interaction with the assistant on Dataframe.
        """
        try:
            self._memory.add(query, True)
            conversation = self._get_conversation()
            result = self._lake.chat(
                query, output_type=output_type, start_conversation=conversation
            )
            self._memory.add(result, False)
            return result
        except Exception as exception:
            return (
                "Unfortunately, I was not able to get your answers, "
                "because of the following error:\n"
                f"\n{exception}\n"
            )

    def _get_clarification_prompt(self) -> Prompt:
        """
        Create a clarification prompt with relevant variables.
        """
        prompt = ClarificationQuestionPrompt()
        prompt.set_var("dfs", self._lake.dfs)
        prompt.set_var("conversation", self._get_conversation())
        return prompt

    def clarification_questions(self) -> ClarificationResponse:
        """
        Generate clarification questions based on the data
        """
        try:
            prompt = self._get_clarification_prompt()

Review comment (refactor):
If we use the memory from the `SmartDataframe`, we don't need to pass the start_conversation, I suggest we remove this.

Code after:
    def chat(self, query: str, output_type: Optional[str] = None):
        """
        Simulate a chat interaction with the assistant on Dataframe.
        """
        try:
            result = self._lake.chat(query, output_type=output_type)
            return result
        except Exception as exception:
            return (
                "Unfortunately, I was not able to get your answers, "
                "because of the following error:\n"
                f"\n{exception}\n"
            )

    def clarification_questions(self) -> List[str]:
        """
        Generate clarification questions based on the data
        """
        prompt = ClarificationQuestionPrompt(
            self._lake.dfs, self._lake._memory.get_conversation()
        )

        result = self._lake.llm.call(prompt)
        self._logger.log(
            f"""Clarification Questions:  {result}
            """
        )
        questions: list[str] = json.loads(result)
        return questions[:3]

    def start_new_conversation(self):
        """
        Clears the previous conversation
        """
        self._lake._memory.clear()

    def explain(self) -> str:
        """
        Returns the explanation of the code how it reached to the solution
        """
        try:
            prompt = ExplainPrompt(
                self._lake._memory.get_conversation(),
                self._lake.last_code_executed,
            )
            response = self._lake.llm.call(prompt)
            self._logger.log(
                f"""Explaination:  {response}
                """
            )
            return response