Repository: mem0ai/mem0
File: embedchain/llm/base.py (Python)

Code before:
        self.online = False
        self.history: Any = None

    def get_llm_model_answer(self):
        """
        Usually implemented by child class
        """
        raise NotImplementedError

    def set_history(self, history: Any):
        """
        Provide your own history.
        Especially interesting for the query method, which does not internally manage conversation history.

        :param history: History to set
        :type history: Any
        """
        self.history = history

    def update_history(self, app_id: str):
        """Update class history attribute with history in memory (for chat method)"""
        chat_history = self.memory.get_recent_memories(app_id=app_id, n_memories=10)
        if chat_history:
            self.set_history([str(history) for history in chat_history])

    def add_history(self, app_id: str, question: str, answer: str, metadata: Dict[str, Any] = {}):
        chat_memory = ECBaseChatMessage()
        chat_memory.add_user_message(question, metadata=metadata)
        chat_memory.add_ai_message(answer, metadata=metadata)
        self.memory.add(app_id=app_id, chat_memory=chat_memory)
        self.update_history(app_id=app_id)

    def generate_prompt(self, input_query: str, contexts: List[str], **kwargs: Dict[str, Any]) -> str:
        """
        Generates a prompt based on the given query and context, ready to be
        passed to an LLM

        :param input_query: The query to use.
        :type input_query: str
        :param contexts: List of similar documents to the query used as context.
        :type contexts: List[str]
        :return: The prompt
        :rtype: str
        """
        context_string = (" | ").join(contexts)
        web_search_result = kwargs.get("web_search_result", "")
        if web_search_result:
            context_string = self._append_search_and_context(context_string, web_search_result)

        template_contains_history = self.config._validate_template_history(self.config.template)
        if template_contains_history:

Review comment (suggestion):
```suggestion
    def add_history(self, app_id: str, question: str, answer: str, metadata: Dict[str, Any] = None):
```

Defaulting to empty dict is never a good idea. 

Code after:
        self.online = False
        self.history: Any = None

    def get_llm_model_answer(self):
        """
        Usually implemented by child class
        """
        raise NotImplementedError

    def set_history(self, history: Any):
        """
        Provide your own history.
        Especially interesting for the query method, which does not internally manage conversation history.

        :param history: History to set
        :type history: Any
        """
        self.history = history

    def update_history(self, app_id: str):
        """Update class history attribute with history in memory (for chat method)"""
        chat_history = self.memory.get_recent_memories(app_id=app_id, num_rounds=10)
        if chat_history:
            self.set_history([str(history) for history in chat_history])

    def add_history(self, app_id: str, question: str, answer: str, metadata: Optional[Dict[str, Any]] = None):
        chat_message = ChatMessage()
        chat_message.add_user_message(question, metadata=metadata)
        chat_message.add_ai_message(answer, metadata=metadata)
        self.memory.add(app_id=app_id, chat_message=chat_message)
        self.update_history(app_id=app_id)

    def generate_prompt(self, input_query: str, contexts: List[str], **kwargs: Dict[str, Any]) -> str:
        """
        Generates a prompt based on the given query and context, ready to be
        passed to an LLM

        :param input_query: The query to use.
        :type input_query: str
        :param contexts: List of similar documents to the query used as context.
        :type contexts: List[str]
        :return: The prompt
        :rtype: str
        """
        context_string = (" | ").join(contexts)
        web_search_result = kwargs.get("web_search_result", "")
        if web_search_result:
            context_string = self._append_search_and_context(context_string, web_search_result)

        template_contains_history = self.config._validate_template_history(self.config.template)
        if template_contains_history: