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

Code before:
        """
        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()
            result = self._lake.llm.call(prompt)
            self.logger.log(
                f"""Clarification Questions:  {result}
                """
            )

Review comment (suggestion):
I really like this feature, didn't thought about it. Let's only clear the memory of the `SmartDatalake` instead

Code after:
            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
        except Exception as exception:
            return (
                "Unfortunately, I was not able to explain, "
                "because of the following error:\n"
                f"\n{exception}\n"