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

Code before:
        self.cursor.execute(
            ADD_CHAT_MESSAGE_QUERY,
            (
                app_id,
                memory_id,
                chat_memory.human_message.content,
                chat_memory.ai_message.content,
                created_at,
                metadata,
            ),
        )
        self.connection.commit()
        logging.info(f"Added chat memory to db with id: {memory_id}")
        return memory_id

    def delete_chat_history(self, app_id: str):
        DELETE_CHAT_HISTORY_QUERY = """
            DELETE FROM chat_memory WHERE app_id=?
        """
        self.cursor.execute(
            DELETE_CHAT_HISTORY_QUERY,
            (app_id,),
        )
        self.connection.commit()

    def get_recent_memories(self, app_id, n_memories=10) -> List[ECBaseChatMessage]:
        """
        Get the most recent n_memories number of memories
        for a given app_id.
        """

        QUERY = """
            SELECT * FROM chat_memory
            WHERE app_id=?
            ORDER BY created_at DESC
            LIMIT ?
        """
        self.cursor.execute(
            QUERY,
            (app_id, n_memories),
        )

        results = self.cursor.fetchall()
        memories = []
        for result in results:
            app_id, id, question, answer, timestamp, metadata = result
            metadata = self._deserialize_json(metadata=metadata)
            memory = ECBaseChatMessage()
            memory.add_user_message(question, metadata=metadata)
            memory.add_ai_message(answer, metadata=metadata)
            memories.append(memory)

Review comment (suggestion):
```suggestion
    def get_recent_memories(self, app_id, num_rounds=10) -> List[ECBaseChatMessage]:
```

Code after:
        self.connection.commit()
        logging.info(f"Added chat memory to db with id: {memory_id}")
        return memory_id

    def delete_chat_history(self, app_id: str):
        DELETE_CHAT_HISTORY_QUERY = """
            DELETE FROM chat_history WHERE app_id=?
        """
        self.cursor.execute(
            DELETE_CHAT_HISTORY_QUERY,
            (app_id,),
        )
        self.connection.commit()

    def get_recent_memories(self, app_id, num_rounds=10) -> List[ChatMessage]:
        """
        Get the most recent num_rounds rounds of conversations
        between human and AI, for a given app_id.
        """

        QUERY = """
            SELECT * FROM chat_history
            WHERE app_id=?
            ORDER BY created_at DESC
            LIMIT ?
        """
        self.cursor.execute(
            QUERY,
            (app_id, num_rounds),
        )

        results = self.cursor.fetchall()
        history = []
        for result in results:
            app_id, id, question, answer, metadata, timestamp = result
            metadata = self._deserialize_json(metadata=metadata)
            memory = ChatMessage()
            memory.add_user_message(question, metadata=metadata)
            memory.add_ai_message(answer, metadata=metadata)
            history.append(memory)
        return history

    def _serialize_json(self, metadata: Dict[str, Any]):
        return json.dumps(metadata)

    def _deserialize_json(self, metadata: str):
        return json.loads(metadata)

    def close_connection(self):
        self.connection.close()